<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="产品名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单据状态" prop="productionRequestStatus">
        <el-select
          v-model="queryParams.productionRequestStatus"
          clearable
          placeholder="请选择单据状态"
        >
          <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 label="样品名称" prop="abbreviation">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入样品名称"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
      <!-- <el-date-picker
          clearable
          v-model="queryParams.abbreviation"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择申请日期"
        >
        </el-date-picker>-->
      <!-- </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</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:productionRequest: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:productionRequest: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:productionRequest:remove']"
          >删除</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:productionRequest:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="productionRequestList"
      row-key="productionRequestId"
      default-expand-all
    >
      <el-table-column
        label="申请单编码"
        width="120"
        prop="productionRequestCode"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleView(scope.row)"
            v-hasPermi="['mes:pro:productionRequest:query']"
            >{{ scope.row.productionRequestCode }}</el-button
          >
        </template>
      </el-table-column>
      <el-table-column
        label="工艺路线"
        width="200"
        align="center"
        prop="routeName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="产品名称"
        width="200"
        align="center"
        prop="itemName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="制造工厂"
        width="200"
        align="center"
        hidden="hidden"
        prop="factoryName"
        :show-overflow-tooltip="true"
      />
      <!-- <el-table-column
        label="仓库"
        align="center"
        prop="warehouse"
        :show-overflow-tooltip="true"
      /> -->
      <!-- <el-table-column
        label="尺码组"
        align="center"
        prop="sizeGroup"
      /> -->
      <el-table-column
        label="是否要鞋图"
        align="center"
        prop="isNeedShoeImg"
        width="100"
        :formatter="yesNoFmt"
      />
      <el-table-column
        label="是否附样品"
        align="center"
        width="100"
        prop="isNeedPrototype"
        :formatter="yesNoFmt"
      />
      <el-table-column
        label="唯一码产品"
        align="center"
        width="100"
        prop="isUniqueCode"
        :formatter="yesNoFmt"
      />
      <el-table-column
        label="不考虑型体配色"
        align="center"
        width="130"
        prop="isConsiderColor"
        :formatter="yesNoFmt"
      />
      <el-table-column
        label="是否禁用"
        align="center"
        width="80"
        prop="isDisabled"
        :formatter="yesNoFmt"
      />

      />
      <el-table-column
        label="申请单状态"
        align="center"
        width="100"
        prop="prototypeRequestStatus"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.mes_productionreq_status"
            :value="scope.row.productionRequestStatus"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="200px"
        align="center"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if="scope.row.productionRequestStatus == '0'"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mes:pro:productionRequest:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-check"
            v-if="scope.row.productionRequestStatus == '0'"
            @click="handleFinish(scope.row)"
            v-hasPermi="['mes:pro:productionRequest:update']"
            >通过</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if="scope.row.productionRequestStatus == '1'"
            @click="generate(scope.row)"
            v-hasPermi="['mes:pro:productionRequest:edit']"
            >生成制作单</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes:pro:productionRequest:remove']"
            >删除</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 {
  listProductionRequest,
  getProductionRequest,
  delProductionRequest,
  addProductionRequest,
  updateProductionRequest,
  dofinish,
} from "@/api/mes/pro/productionRequest";
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: "ProductionRequest",
  dicts: [
    "sys_yes_no",
    "mes_productionreq_status",
    "mes_productionRequest_sourcetype",
    "mes_productionRequest_type",
  ],
  components: {
    Treeselect,
    ItemSelect,
    ClientSelect,
    VendorSelect,
  },
  data() {
    return {
      //自动生成编码
      autoGenFlag: false,
      optType: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 非单个禁用
      single: true,
      // 总条数
      total: 0,
      // 非多个禁用
      multiple: true,
      // 生产工单表格数据
      productionRequestList: [],
      // 生产工单树选项
      productionRequestOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemName: null, // 可以查简称与英文名
        productionRequestStatus: null,
      },
      options: [
        { value: 0, label: "审批中" },
        { value: 1, label: "待制作" },
        { value: 2, label: "量产中" },
        { value: 3, label: "待质检" },
        { value: 4, label: "待确认" },
        { value: 5, label: "已完成" },
      ],

      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // productionRequestCode: [
        //   { required: true, message: "工单编码不能为空", trigger: "blur" }
        // ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询生产工单列表 */
    getList() {
      this.loading = true;
      listProductionRequest(this.queryParams).then((response) => {
        this.productionRequestList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        productionRequestId: null,
        productionRequestCode: null,
        itemId: null,
        itemName: null,
        routeId: null,
        routeName: null,
        abbreviation: null,
        enName: null,
        num: null,
        type: null,
        modality: null,
        surfaceTreatment: null,
        ngReason: null,
        plannedFinishDate: null,
        plannedLayoutDate: null,
        productionRequestStatus: "0",
        testStandard: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      // 跳转到申请单界面
      this.$router.push("/mes/pro/productiorequest/apply");
      return;
      //   this.reset();
      //   this.getTreeselect();
      //   if (row != null && row.productionRequestId) {
      //     this.form.parentId = row.productionRequestId;
      //     this.form.orderSource = row.orderSource;
      //     this.form.sourceCode = row.sourceCode;
      //     this.form.clientId = row.clientId;
      //     this.form.clientCode = row.clientCode;
      //     this.form.clientName = row.clientName;
      //   } else {
      //     this.form.parentId = 0;
      //   }
      //   this.open = true;
    },
    // 查询明细按钮操作
    handleView(row) {
      this.reset();
      const productionRequestId = row.productionRequestId || this.ids;
      this.$router.push(
        "/mes/pro/productiorequest/info?productionRequestId=" +
          productionRequestId
      );
      // getproductionRequest(productionRequestId).then((response) => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "查看工单信息";
      //   this.optType = "view";
      // });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const productionRequestId = row.productionRequestId || this.ids;
      this.$router.push(
        "/mes/pro/productiorequest/edit?productionRequestId=" +
          productionRequestId
      );
      // getProductionRequest(productionRequestId).then((response) => {
      //   // this.form = response.data;
      //   // this.open = true;
      //   // this.title = "修改生产工单";
      //   // this.optType="edit";
      //   // 跳转修改
      // });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm(
          '是否确认删除编号为"' + row.productionRequestId + '"的量产单？'
        )
        .then(function () {
          return delProductionRequest(row.productionRequestId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },

    handleSelectProduct() {
      this.$refs.itemSelect.showFlag = true;
    },
    handleSelectClient() {
      this.$refs.clientSelect.showFlag = true;
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "mes/pro/productionRequest/export",
        {
          ...this.queryParams,
        },
        `量产申请单_${new Date().getTime()}.xlsx`
      );
    },

    // 审批
    handleFinish(row) {
      row.productionRequestStatus = 1;
      this.$modal
        .confirm("确认通过样品申请单？一旦完成，此工单将无法继续修改")
        .then(function () {
          return dofinish(row); //完成申请单审批
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("审批成功");
        })
        .catch(() => {});
    },
    yesNoFmt(row, column, cellValue) {
      if (cellValue == 1) {
        return "是";
      } else {
        return "否";
      }
    },
    // 生成制作单
    generate(row) {
      // 跳转到制作单界面
      const prototypeRequestId = row.productionRequestId || this.ids;
      this.$router.push(
        "/mes/pro/productionMake/apply?productionRequestId=" +
          prototypeRequestId
      );
      return;
    },
  },
};
</script>
