<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="线体编码" prop="lineCode">
        <el-input
          v-model="queryParams.lineCode"
          placeholder="请输入线体编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="线体名称" prop="lineName">
        <el-input
          v-model="queryParams.lineName"
          placeholder="请输入线体名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标准用人" prop="useMan">
        <el-input
          v-model="queryParams.useMan"
          type="number"
          placeholder="请输入标准用人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标准效率(%)" prop="efficiency">
        <el-input
          v-model="queryParams.efficiency"
          type="number"
          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="handleAddLineBody"
          v-hasPermi="['mes:mesLine: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:mesLine: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:mesLine: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:mesLine:export']"
          >导出
        </el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="mesLineList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" align="center" />
      <el-table-column label="线体编码" align="center" prop="lineCode" />
      <el-table-column label="线体名称" align="center" prop="lineName" />
      <el-table-column label="标准用人" align="center" prop="useMan" />
      <el-table-column label="标准效率(%)" align="center" prop="efficiency" />
      <el-table-column label="检验工具" align="center" prop="attr1" />
      <el-table-column
        label="操作"
        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="['mes:mesLine:edit']"
            >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes:mesLine: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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="标准用人" prop="useMan">
          <el-input v-model="form.useMan" type="number" placeholder="请输入标准用人" />
        </el-form-item>
        <el-form-item label="标准效率(%)" prop="efficiency">
          <el-input v-model="form.efficiency" type="number" placeholder="请输入标准效率" />
        </el-form-item>
        <el-form-item label="检验工具" prop="attr1">
          <el-input v-model="form.attr1" placeholder="请输入检验工具" />
        </el-form-item>
        <el-form-item
            v-for="(item, index) in processItems"
            :label="'第' + (index+1) +'道工艺'"
            :key="item.key"
            :prop="'processItems.'+index+'.value'"
            :rules="{required: true, message: '请输入工艺', trigger: 'blur'}"
          >
            <el-input v-model="item.value" style="width: 70%;" placeholder="请输入工艺"></el-input>
            <el-button @click.prevent="removeProcessItem(item)" style="margin-left:30px">删除</el-button>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="addProcessItem">新增工艺</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <SelectMesLine ref="selectMesLine" @onSelected="onSelectLineBody">
    </SelectMesLine>
  </div>
</template>

<script>
import {
  listMesLine,
  getMesLine,
  delMesLine,
  addMesLine,
  updateMesLine,
} from "@/api/mes/mesLine";
import SelectMesLine from "./selectMesLine";

export default {
  name: "MesLine",
  components: {SelectMesLine,},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 线体管理表格数据
      mesLineList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        lineCode: null,
        lineName: null,
        useMan: null,
        efficiency: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
        factoryCode: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        useMan: [
          { required: true, message: "标准用人不能为空", trigger: "blur" },
        ],
        efficiency: [
          { required: true, message: "标准效率不能为空", trigger: "blur" },
        ],
      },
      processItems:[{
        value: '',
        key: Date.now()
      }],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询线体管理列表 */
    getList() {
      this.loading = true;
      listMesLine(this.queryParams).then((response) => {
        this.mesLineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        lineCode: null,
        lineName: null,
        useMan: null,
        efficiency: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        factoryCode: null,
        delFlag: null,
        processList: [],
        processItems: [{
          value: '',
          key: Date.now()
        }],
      };
      this.processItems=[{
        value: '',
        key: Date.now()
      }],
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加线体管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getMesLine(id).then((response) => {
        this.form = response.data;
        if (response.data.processList !== null) {
          this.processItems = response.data.processList.map(item => {
            return {
              value: item,
              key: Date.now(),
            };
          });
          this.form.processItems = this.processItems;
        }
        this.open = true;
        this.title = "修改线体管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            this.form.processList = this.form.processItems.map(item => item.value);
            updateMesLine(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMesLine(this.form).then((response) => {
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除线体编号为"' + ids + '"的数据项？')
        .then(function () {
          return delMesLine(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "mes/mesLine/export",
        {
          ...this.queryParams,
        },
        `mesLine_${new Date().getTime()}.xlsx`
      );
    },
    handleAddLineBody() {
      this.$refs.selectMesLine.showFlag = true;
    },
    onSelectLineBody(obj) {
      console.log(obj);
      this.form.lineCode = obj.code;
      this.form.lineName = obj.name;
      addMesLine(this.form).then((response) => {
        if(response.code == 200) {
          this.$modal.msgSuccess("添加成功");
        }else {
          this.$modal.msgError(response.msg);
        }
        this.getList();
      });
    },
    //添加工艺项
    addProcessItem() {
      this.processItems.push({
        value: '',
        key: Date.now()
      });
      this.form.processItems = this.processItems;
    },
    //移除工艺项
    removeProcessItem(item) {
      var index = this.processItems.indexOf(item)
      if (index !== -1) {
        this.processItems.splice(index, 1)
      }
      this.form.processItems = this.processItems;
    },
  },
};
</script>
