<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="workorderCode">
        <el-input
          v-model="queryParams.workorderCode"
          placeholder="请输入工单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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="['pro:scheduleSetupRule: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="['pro:scheduleSetupRule: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="['pro:scheduleSetupRule: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="['pro:scheduleSetupRule:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="scheduleSetupRuleList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="工单编号" align="center" prop="workorderCode" />
      <el-table-column label="销售单号" align="center" prop="saleCode" />
      <el-table-column label="物料名称" align="center" prop="itemName" />
      <el-table-column label="开始时间" align="center" prop="startTime" />
      <el-table-column label="排版方式" align="center" prop="dtaType"
        :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <div>{{ scope.row.dtaType ? dtaOptions.find(v => v.value === scope.row.dtaType).name : ''}}</div>
        </template>
      </el-table-column>
      <el-table-column label="完成时间" align="center" prop="endTime" />
      <el-table-column label="操作" width="200" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="!scope.row.endTime"
            size="mini"
            type="text"
            @click="handleFinish(scope.row)"
            v-hasPermi="['pro:dta:finish']"
          >完成</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"
    />

    <!-- 添加或修改排产换型对照信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="960px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="换型关键字" prop="ruleCode">
              <el-input v-model="form.ruleCode" placeholder="请输入换型关键字" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="产品类别ID" prop="itemTypeId">
              <el-input disabled v-model="form.itemTypeId" placeholder="请输入产品类别ID" />
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="产品类别名称" prop="itemTypeName">
              <el-input v-model="form.itemTypeName" placeholder="请输入产品类别名称" >
              <el-button
                  slot="append"
                  icon="el-icon-search"
                  @click="handleBrandSelect"
                ></el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12"> -->
            <!-- <el-form-item label="工序ID" prop="processId">
              <el-input disabled v-model="form.processId" placeholder="请输入工序ID" />
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="工序名称" prop="processName">
              <el-input v-model="form.processName" placeholder="请输入工序名称" >
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  @click="handleProcess"
                ></el-button>
              </el-input>

            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="刀模版具" prop="tool">
              <el-input-number v-model="form.tool" :min="0" :step="1" step-strictly></el-input-number>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="模" prop="mould">
              <el-input-number v-model="form.mould" :min="0" :step="1" step-strictly></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="版" prop="plate">
              <el-input-number v-model="form.plate" :min="0" :step="1" step-strictly></el-input-number>
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="材料" prop="materialRecoil">
              <el-input-number v-model="form.materialRecoil" :min="0" :step="1" step-strictly></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="印张长度" prop="sheetLength">
              <el-input-number v-model="form.sheetLength" :min="0" :step="1" step-strictly></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="印张宽度" prop="sheetWidth">
              <el-input-number v-model="form.sheetWidth" :min="0" :step="1" step-strictly></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品规格" prop="productStandard">
              <el-input-number v-model="form.productStandard" :min="0" :step="1" step-strictly></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="正反颜色" prop="proConColor">
              <el-input-number v-model="form.proConColor" :min="0" :step="1" step-strictly></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="Inlay" prop="inlay">
              <el-input-number v-model="form.inlay" :min="0" :step="1" step-strictly></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工艺路线" prop="routeCode">
              <el-input-number v-model="form.routeCode" :min="0" :step="1" step-strictly></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品材质宽度" prop="productWidth">
              <el-input-number v-model="form.productWidth" :min="0" :step="1" step-strictly></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <BrandSelect
      ref="brSelect"
      v-if="open"
      @onSelected="onBrandSelected"
    ></BrandSelect>

    <ProcessSelect
      ref="brSelectpro"
      v-if="open"
      @onSelected="onBrandProcess"
    ></ProcessSelect>
  </div>
</template>

<script>
import { getDtaList, finishApi } from "@/api/mes/pro/dta";
import BrandSelect from "@/components/itemType/single.vue";
import ProcessSelect from "@/components/process/taskSelectSingle.vue";
import {
  getItmeDtaTypeEnum
} from "@/api/mes/md/mdItem";

export default {
  name: "ScheduleSetupRule",
  components: {BrandSelect,ProcessSelect},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      //dta列表
      dtaOptions: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 排产换型对照信息表格数据
      scheduleSetupRuleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workorderCode: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        ruleCode: [
          { required: true, message: '请输入换型关键字', trigger: 'blur' },
        ],
        itemTypeName: [
          { required: true, message: '请输入产品类别名称', trigger: ['blur', 'change'] },
        ],
        processName: [
          { required: true, message: '请输入工序名称', trigger: ['blur', 'change'] },
        ],
      }
    };
  },
  created() {
    this.getDtas()
    this.getList();
  },
  methods: {
    getDtas() {
      this.dtaOptions = []
      getItmeDtaTypeEnum().then((response) => {
        Object.keys(response.data).forEach(v => {
          this.dtaOptions.push({
            name: response.data[v],
            value: v
          })
        })
      });
    },
    /** 查询排产换型对照信息列表 */
    getList() {
      this.loading = true;
      getDtaList(this.queryParams).then(response => {
        this.scheduleSetupRuleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ruleId: null,        ruleCode: null,        itemTypeId: null,        itemTypeName: null,        processId: null,        processName: null,        tool: null,        mould: null,        plate: null,        materialRecoil: null,        sheetLength: null,        sheetWidth: null,        productStandard: null,        proConColor: null,        inlay: null,        routeCode: null,        productWidth: null,        createBy: null,        createTime: null      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handleBrandSelect(){
      this.$refs.brSelect.showFlag = true;
    },
    onBrandSelected(row){
     if (Object.values(row).length > 0) {
        this.form.itemTypeId = row.itemTypeId;
        this.form.itemTypeName = row.itemTypeName;
      }
    },
    handleProcess(){
      this.$refs.brSelectpro.showFlag = true;
    },
    onBrandProcess(row){
     if (Object.values(row).length > 0) {
         this.form.processId = row.processId;
        this.form.processName = row.processName;

      }
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.ruleId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加排产换型对照信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const ruleId = row.ruleId || this.ids
      getScheduleSetupRule(ruleId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改排产换型对照信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.ruleId != null) {
            updateScheduleSetupRule(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addScheduleSetupRule(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ruleIds = row.ruleId || this.ids;
      this.$modal.confirm('是否确认删除排产换型对照信息编号为"' + ruleIds + '"的数据项？').then(function() {
        return delScheduleSetupRule(ruleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 完成按钮操作 */
    handleFinish(row) {
      this.$modal.confirm('是否确认完成？').then(function() {
        return finishApi(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("完成成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pro/scheduleSetupRule/export', {
        ...this.queryParams
      }, `scheduleSetupRule_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
