<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      style="with"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item :label="$t('产品名称')" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
         
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('单据状态')" prop="prototypeMakeStatus">
        <el-select
          v-model="queryParams.prototypeMakeStatus"
          clearable
         
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >{{ $t('common.search') }}</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >{{ $t('common.reset') }}</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
       <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mes:pro:prototypeMake:add']"
          >{{ $t('common.add') }}</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mes:pro:prototypeMake:edit']"
          >{{ $t('common.edit') }}</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mes:pro:prototypeMake:remove']"
          >{{ $t('common.del') }}</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mes:pro:prototypeMake:export']"
          >{{ $t('common.export') }}</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="prototypeMakeList"
      row-key="prototypeMakeId"
      default-expand-all
    >
      <el-table-column :label="$t('制作单编码')" prop="prototypeMakeCode">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleView(scope.row)"
            v-hasPermi="['mes:pro:prototypeMake:query']"
            >{{ scope.row.prototypeMakeCode }}</el-button
          >
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('产品名称')"

        align="center"
        prop="itemName"
        :show-overflow-tooltip="true"
      />
      <el-table-column :label="$t('工艺路线名称')" align="center" prop="routeName">
      </el-table-column>
      <el-table-column
        :label="$t('计划完成日期')"
        align="center"
        prop="plannedFinishDate"

      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.plannedFinishDate, "{y}-{m}-{d}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('版面需求时间')"
        align="center"
        prop="plannedLayoutDate"

      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.plannedLayoutDate, "{y}-{m}-{d}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('common.status')" align="center" prop="prototypeMakeStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.mes_pm_status"
            :value="scope.row.prototypeMakeStatus"
          />
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('common.option')"
        width="200px"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if="scope.row.prototypeMakeStatus == '0'"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mes:pro:prototypeMake:edit']"
            >{{ $t('common.edit') }}</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-check"
            v-if="scope.row.prototypeMakeStatus == '0'"
            @click="handleFinish(scope.row)"
            v-hasPermi="['mes:pro:prototypeMake:update']"
            >{{$t('完成')}}</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes:pro:prototypeMake:remove']"
            >{{ $t('common.del') }}</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if="scope.row.prototypeRequestStatus == '1'"
            @click="generate(scope.row)"
            v-hasPermi="['mes:pro:prototypeMake:edit']"
            >{{$t('打样')}}</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>


import {
  listPrototypeMake,
  getPrototypeMake,
  delPrototypeMake,
  addPrototypeMake,
  updatePrototypeMake,
  dofinish,
} from "@/api/mes/pro/prototypeMake";
import ItemSelect from "@/components/itemSelect/single.vue";
import ClientSelect from "@/components/clientSelect/single.vue";
import VendorSelect from "@/components/vendorSelect/single.vue";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "PrototypeMake",
  dicts: ["mes_pm_status", "mes_prototypeMake_type"],
  components: {
    Treeselect,
    ItemSelect,
    ClientSelect,
    VendorSelect,
  },
  data() {
    return {
      //自动生成编码
      autoGenFlag: false,
      optType: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 非单个禁用
      single: true,
      // 总条数
      total: 0,
      // 非多个禁用
      multiple: true,
      // 生产工单表格数据
      prototypeMakeList: [],
      // 生产工单树选项
      prototypeMakeOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemName: null, // 可以查简称与英文名
        prototypeMakeStatus: null,
      },
      options: [
        { value: "0", label: this.$t('待检查') },
        { value: "1", label: this.$t('待打样') },
        { value: "2", label: this.$t('打样中') },
        { value: "3", label: this.$t('待质检') },
        { value: "4", label: this.$t('待确认') },
        { value: "5", label: this.$t('已完成') },
      ],

      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // prototypeMakeCode: [
        //   { required: true, message: "工单编码不能为空", trigger: "blur" }
        // ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询生产工单列表 */
    getList() {
      this.loading = true;
      listPrototypeMake(this.queryParams).then((response) => {
        this.prototypeMakeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        prototypeMakeId: null,
        prototypeMakeCode: null,
        prototypeRequestId: null,
        routeId: null,
        routeName: null,
        itemId: null,
        itemName: null,
        screenItem: null,
        pibanItem: null,
        diecuttingItem: null,
        standardColor: null,
        module: null,
        colNum: null,
        rowNum: null,
        buttstrapNum: null,
        coldoubleCutspacing: null,
        rowdoubleCutspacing: null,
        sendworkMultiple: null,
        printingPaperlenth: null,
        printPaperwidth: null,
        frontColorId: null,
        frontColorName: null,
        oppositeColorId: null,
        oppositeColorName: null,
        ngReason: null,
        remark: null,
        prototypeMakeStatus: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      // 跳转到申请单界面
      this.$router.push("/mes/pro/prototypeMake/apply");
      return;
    },
    // 查询明细按钮操作
    handleView(row) {
      this.reset();
      const prototypeMakeId = row.prototypeMakeId || this.ids;
      this.$router.push(
        "/mes/pro/prototypeMake/info?prototypeMakeId=" + prototypeMakeId
      );
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const prototypeMakeId = row.prototypeMakeId || this.ids;
      this.$router.push(
        "/mes/pro/prototypeMake/edit?prototypeMakeId=" + prototypeMakeId
      );
      // getPrototypeMake(prototypeMakeId).then((response) => {
      //   // this.form = response.data;
      //   // this.open = true;
      //   // this.title = "修改生产工单";
      //   // this.optType="edit";
      //   // 跳转修改
      // });
    },
    // /** 提交按钮 */
    // submitForm() {
    //   this.$refs["form"].validate(valid => {
    //     if (valid) {
    //       if (this.form.prototypeMakeId != null) {
    //         updateprototypeMake(this.form).then(response => {
    //           this.$modal.msgSuccess(this.$t('common.edit_success'));
    //           //this.open = false;
    //           this.$refs["bomlist"].getList();
    //           this.getList();
    //         });
    //       } else {
    //         addprototypeMake(this.form).then(response => {
    //           this.$modal.msgSuccess(this.$t('common.add_success'));
    //           //this.open = false;
    //           this.form.prototypeMakeId = response.data;
    //           this.getList();
    //         });
    //       }
    //     }
    //   });
    // },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm(
          this.$t('是否确认删除编号为"') + row.prototypeMakeId + this.$t('"的打样制作单？')
        )
        .then(function () {
          return delPrototypeMake(row.prototypeMakeId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess(this.$t('common.del_success'));
        })
        .catch(() => {});
    },

    handleSelectProduct() {
      this.$refs.itemSelect.showFlag = true;
    },
    handleSelectClient() {
      this.$refs.clientSelect.showFlag = true;
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "mes/pro/prototypeMake/export",
        {
          ...this.queryParams,
        },
        this.$t('打样申请单_{0}.xlsx', [new Date().getTime()])
      );
    },
    // 审批
    handleFinish(row) {
      row.prototypeMakeStatus = 1;
      this.$modal
        .confirm(this.$t('确认通过样品制作单？一旦确认，此工单将无法继续修改'))
        .then(function () {
          return dofinish(row); //完成申请单审批
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess(this.$t('审批成功'));
        })
        .catch(() => {});
    },
  },
  // 生成制作单
  generate(row) {
    // 跳转到制作单界面
    const prototypeRequestId = row.prototypeRequestId || this.ids;
    this.$router.push(
      "/mes/pro/prototypeMake/apply?prototypeRequestId=" + prototypeRequestId
    );
    return;
  },
};
</script>
