<template>
  <div class="app-container">
    <el-form :model="queryParams" @submit.native.prevent ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item :label="$t('工单编号')" prop="workorderCode">
        <el-input
          v-model="queryParams.workorderCode"
         
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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="['pro:scheduleSetupRule: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="['pro:scheduleSetupRule: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="['pro:scheduleSetupRule: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="['pro:scheduleSetupRule: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="scheduleSetupRuleList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column :label="$t('工单编号')" align="center" prop="workorderCode" />
      <el-table-column :label="$t('销售单号')" align="center" prop="saleCode" />
      <el-table-column :label="$t('物料名称')" align="center" prop="itemName" />
      <el-table-column :label="$t('开始时间')" align="center" prop="startTime" />
      <el-table-column :label="$t('完成时间')" align="center" prop="endTime" />
      <el-table-column :label="$t('common.option')" 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']"
          >{{$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"
    />

    <!-- 添加或修改排产换型对照信息对话框 -->
    <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="$t('换型关键字')" prop="ruleCode">
              <el-input v-model="form.ruleCode" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="产品类别ID" prop="itemTypeId">
              <el-input disabled v-model="form.itemTypeId" />
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item :label="$t('产品类别名称')" prop="itemTypeName">
              <el-input v-model="form.itemTypeName" >
              <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" />
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item :label="$t('工序名称')" prop="processName">
              <el-input v-model="form.processName" >
                <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="$t('刀模版具')" 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="$t('材料')" 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="$t('印张长度')" 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="$t('印张宽度')" 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="$t('产品规格')" 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="$t('正反颜色')" 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="$t('工艺路线')" 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="$t('产品材质宽度')" 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">{{ $t('common.confirm') }}</el-button>
        <el-button @click="cancel">{{ $t('common.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";


export default {
  name: "ScheduleSetupRule",
  components: {BrandSelect,ProcessSelect},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      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: this.$t('请输入换型关键字'), trigger: 'blur' },
        ],
        itemTypeName: [
          { required: true, message: this.$t('请输入产品类别名称'), trigger: ['blur', 'change'] },
        ],
        processName: [
          { required: true, message: this.$t('请输入工序名称'), trigger: ['blur', 'change'] },
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询排产换型对照信息列表 */
    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 = this.$t('添加排产换型对照信息');
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const ruleId = row.ruleId || this.ids
      getScheduleSetupRule(ruleId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = this.$t('修改排产换型对照信息');
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.ruleId != null) {
            updateScheduleSetupRule(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('common.edit_success'));
              this.open = false;
              this.getList();
            });
          } else {
            addScheduleSetupRule(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('common.add_success'));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ruleIds = row.ruleId || this.ids;
      this.$modal.confirm(this.$t('common.confirm_del_data')).then(function() {
        return delScheduleSetupRule(ruleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(this.$t('common.del_success'));
      }).catch(() => {});
    },
    /** 完成按钮操作 */
    handleFinish(row) {
      this.$modal.confirm(this.$t('是否确认完成？')).then(function() {
        return finishApi(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(this.$t('完成成功'));
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pro/scheduleSetupRule/export', {
        ...this.queryParams
      }, `scheduleSetupRule_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
