<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="productionSolutionName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入版本名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产用途" prop="usageName">
        <el-select
          v-model="queryParams.usageName"
          clearable
          placeholder="请输入生产用途"
        >
          <el-option
            v-for="item in usageNames"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="单据状态" prop="status">
        <el-select
          v-model="queryParams.status"
          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>
        <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-table
      v-loading="loading"
      :data="productionSolutionList"
      row-key="productionSolutionId"
      default-expand-all
    >
      <el-table-column label="生产版本编码" prop="productionSolutionCode">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleView(scope.row)"
            v-hasPermi="['pro:productionSolutionCode:query']"
            >{{ scope.row.productionSolutionCode }}</el-button
          >
        </template>
      </el-table-column>
      <el-table-column
        label="产品版本名称"
        align="center"
        prop="productionSolutionName"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="生产用途" align="center" prop="usageName" />
      <el-table-column label="状态" align="center" width="100" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.production_solution_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <!-- <el-table-column label="备注" align="center" prop="usageName"  width="180"/> -->

      <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.status == 'PREPARE'"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['pro:productionSolution:edit']"
            >确认</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 {
  //   listPrototypeRequest,
  //   getPrototypeRequest,
  //   delPrototypeRequest,
  //   addPrototypeRequest,
  //   updatePrototypeRequest,
  //   dofinish,
  listProductionSolution,
  getProductionSolution,
  addProductionSolution,
  updateProductionSolution,
  delProductionSolution,
} from "@/api/mes/pro/productionSolution";
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: "PrototypeRequest",
  dicts: [
    "mes_pr_status",
    "mes_prototypeRequest_sourcetype",
    "mes_prototypeRequest_type",
    "production_solution_status",
  ],
  components: {
    Treeselect,
    ItemSelect,
    ClientSelect,
    VendorSelect,
  },
  data() {
    return {
      //自动生成编码
      autoGenFlag: false,
      optType: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 非单个禁用
      single: true,
      // 总条数
      total: 0,
      // 非多个禁用
      multiple: true,
      // 生产工单表格数据
      prototypeRequestList: [],
      // 生产工单树选项
      prototypeRequestOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productionSolutionId: null, // 可以查简称与英文名
        productionSolutionCode: null,
        productionSolutionName: null,
        statsu: "PREPARE",
        itemId: null,
        routeId: null,
        usageId: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      },
      options: [
        { value: "PREPARE", label: "待确认" },
        { value: "FINISH", label: "已完成" },
      ],
      usageNames: [
        { value: "打样", label: "打样" },
        { value: "量产", label: "量产" },
        { value: "销售", label: "销售" },
        { value: "特定", label: "特定" },
      ],

      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // prototypeRequestCode: [
        //   { required: true, message: "工单编码不能为空", trigger: "blur" }
        // ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询生产工单列表 */
    getList() {
      this.loading = true;
      listProductionSolution(this.queryParams).then((response) => {
        this.productionSolutionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        productionSolutionId: null,
        productionSolutionCode: null,
        productionSolutionName: null,
        itemId: null,
        routeId: null,
        usageId: 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/proofing/apply");
      return;
      //   this.reset();
      //   this.getTreeselect();
      //   if (row != null && row.prototypeRequestId) {
      //     this.form.parentId = row.prototypeRequestId;
      //     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 productionSolutionId = row.productionSolutionId || this.ids;
      this.$router.push(
        "/mes/pro/productionSolution/info?productionSolutionId=" +
          productionSolutionId
      );
      // getprototypeRequest(prototypeRequestId).then((response) => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "查看工单信息";
      //   this.optType = "view";
      // });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const productionSolutionId = row.productionSolutionId || this.ids;
      this.$router.push(
        "/mes/pro/productionSolution/edit?productionSolutionId=" +
          productionSolutionId
      );
      // getPrototypeRequest(prototypeRequestId).then((response) => {
      //   // this.form = response.data;
      //   // this.open = true;
      //   // this.title = "修改生产工单";
      //   // this.optType="edit";
      //   // 跳转修改
      // });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除编号为"' + row.prototypeRequestId + '"的打样单？')
        .then(function () {
          return delPrototypeRequest(row.prototypeRequestId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },

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

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