<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="产品类别名称" prop="itemTypeName">
        <el-input
          v-model="queryParams.itemTypeName"
          placeholder="请输入产品类别名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工序名称" prop="processName">
        <el-input
          v-model="queryParams.processName"
          placeholder="请输入工序名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="换型关键字" prop="ruleCode">
        <el-input
          v-model="queryParams.ruleCode"
          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="info" plain icon="el-icon-upload2" size="mini" v-hasPermi="['pro:scheduleSetupRule:import']" @click="handleImport">导入</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" width="120" prop="ruleCode" />
      <el-table-column label="产品类别名称" align="center" width="120" prop="itemTypeName" />
      <el-table-column label="工序名称" align="center" width="120" prop="processName" />
      <el-table-column label="刀模版具" align="center" prop="tool" />
      <!-- <el-table-column label="模" align="center" prop="mould" />
      <el-table-column label="版" align="center" prop="plate" /> -->
      <el-table-column label="材料" align="center" prop="materialRecoil" />
      <el-table-column label="印张长度" align="center" prop="sheetLength" />
      <el-table-column label="印张宽度" align="center" prop="sheetWidth" />
      <el-table-column label="产品规格" align="center" prop="productStandard" />
      <el-table-column label="正反颜色" align="center" prop="proConColor" />
      <el-table-column label="Inlay" align="center" prop="inlay" />
      <el-table-column label="工艺路线" align="center" prop="routeCode" />
      <el-table-column label="产品材质宽度" width="120" align="center" prop="productWidth" />
      <el-table-column label="操作" width="200" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['pro:scheduleSetupRule:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['pro:scheduleSetupRule: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"
    />

    <!-- 添加或修改排产换型对照信息对话框 -->
    <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>
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <!-- <el-checkbox v-model="upload.updateSupport" />
            是否更新已经存在的物料产品数据 -->
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size: 12px; vertical-align: baseline"
            @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listScheduleSetupRule, getScheduleSetupRule, delScheduleSetupRule, addScheduleSetupRule, updateScheduleSetupRule } from "@/api/mes/pro/scheduleSetupRule";
import BrandSelect from "@/components/itemType/single.vue";
import ProcessSelect from "@/components/process/taskSelectSingle.vue";
import { getToken } from "@/utils/auth";
import { genCode } from "@/api/system/autocode/rule";

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,
        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,      },
      // 表单参数
      form: {},
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        // updateSupport: false,
        updateSupport: true,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/pro/scheduleSetupRule/importData",
      },
      // 表单校验
      rules: {
        ruleCode: [
          { required: true, message: '请输入换型关键字', trigger: 'blur' },
        ],
        itemTypeName: [
          { required: true, message: '请输入产品类别名称', trigger: ['blur', 'change'] },
        ],
        processName: [
          { required: true, message: '请输入工序名称', trigger: ['blur', 'change'] },
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询排产换型对照信息列表 */
    getList() {
      this.loading = true;
      listScheduleSetupRule(this.queryParams).then(response => {
        this.scheduleSetupRuleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    gCode() {
      genCode("SCHEDULE_SETUP_RULE_CODE").then((response) => {
        this.form.ruleCode = response;
      });
    },
    // 取消按钮
    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.gCode();
      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(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pro/scheduleSetupRule/export', {
        ...this.queryParams
      }, `scheduleSetupRule_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download(
        "pro/scheduleSetupRule/importTemplate",
        {},
        `scheduleSetupRule_${new Date().getTime()}.xlsx`
      );
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      const msg = response.msg ? response.msg : "导入失败";
      this.$alert(
        "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
          msg +
        "</div>",
        "导入结果",
        { dangerouslyUseHTMLString: true }
      );
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
  }
};
</script>
