<template>
  <!-- 设备产能维护 -->
  <el-container>
    <el-header style="width: 100%">
      <el-form ref="requestForm" :model="request" label-width="80px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="跨位:">
              <el-select v-model="request.kw" placeholder="跨位" clearable>
                <el-option
                  v-for="item in kwOptions"
                  :key="item.bm"
                  :value="item.bm"
                  :label="item.bmsm"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
<!--            <el-form-item label="设备名称:">-->
<!--              <el-input-->
<!--                v-model="request.sbmc"-->
<!--                placeholder="设备名称"-->
<!--                clearable-->
<!--              />-->
<!--            </el-form-item>-->
          </el-col>
          <el-col :span="16" style="text-align: right">
            <el-button type="success" @click="toolbarButtonClickEvent('insert')"
              >添加</el-button
            >
            <el-button type="primary" @click="initList()">查询</el-button>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24" style="text-align: right">
            <el-button type="danger" @click="toolbarButtonClickEvent('delete')"
              >删除</el-button
            >
            <el-button type="primary" @click="toolbarButtonClickEvent('save')"
              >保存</el-button
            >
          </el-col>
        </el-row>
      </el-form>
    </el-header>
    <el-container style="padding-top: 10px">
      <el-container>
        <el-main>
          <div class="mytable">
            <vxe-grid
              id="id"
              ref="tableRef"
              border
              resizable
              keep-source
              :height="height"
              :auto-resize="true"
              :align="'center'"
              :columns="tableColumn"
              :data="list"
              :custom-config="{ storage: true }"
              :edit-rules="validRules"
              :edit-config="{
                trigger: 'click',
                mode: 'row',
                showStatus: true,
              }"
              highlight-hover-row
              @checkbox-all="selectAllEvent"
              @checkbox-change="selectChangeEvent"
            >
            </vxe-grid>
          </div>
        </el-main>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
import {
  getList,
  emSbjbbZM,
  getBMMCBM,
  save,
  update,
  delete1,
} from "@/api/sjzx/sbcnWH";
export default {
  name: "sbcnWH",
  data() {
    return {
      request: {
        kw: "",
        sbmc: "",
      },
      list: [],
      height: "500px",
      tableColumn: [
        { type: "checkbox", width: "50", align: "center" },
        { type: "seq", width: "50", title: "序号", align: "center" },
        {
          field: "gx",
          title: "工序",
          align: "center",
          minWidth: "70",
          editRender: {
            name: "$select",
            options: [],
            props: { clearable: true },
          },
          filters: [
            {
              data: {
                checks: [],
                sVal: "",
                sMenu: "",
                fType1: "",
                fVal1: "",
                fMode: "and",
                fType2: "",
                fVal2: "",
              },
            },
          ],
          filterRender: { name: "FilterCombination" },
        },
        { field: 'kw', title: '跨位', width: 120,
          editRender: {
            name: "$select",
            options: [],
            props: { clearable: true },
          },
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination', },
        },
        { field: '', title: '白班',
          children:[
            { field: 'zxcnBb', title: '最小产能', width: 120,editRender: { name: "$input"},
              filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
              filterRender: { name: 'FilterCombination', },
            },
            { field: 'zdcnBb', title: '最大产能', width: 120,editRender: { name: "$input"},
              filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
              filterRender: { name: 'FilterCombination', },
            },
          ],
        },
        { field: '', title: '夜班',
          children:[
            { field: 'zxcnYb', title: '最小产能', width: 120,editRender: { name: "$input"},
              filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
              filterRender: { name: 'FilterCombination', },
            },
            { field: 'zdcnYb', title: '最大产能', width: 120,editRender: { name: "$input"},
              filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
              filterRender: { name: 'FilterCombination', },
            },
          ],
        },
        { field: 'cndw', title: '产能单位', width: 120,
          editRender: {
            name: "$select",
            options: [],
            props: { clearable: true },
          },
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination', },
        },
        { field: 'cnhj', title: '合计', width: 120,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination', },
        },
        { field: '', title: '划线白班',
          children:[
            { field: 'zxcnBb1', title: '最小产能', width: 120,editRender: { name: "$input"},
              filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
              filterRender: { name: 'FilterCombination', },
            },
            { field: 'zdcnBb1', title: '最大产能', width: 120,editRender: { name: "$input"},
              filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
              filterRender: { name: 'FilterCombination', },
            },
          ],
        },
        { field: '', title: '划线夜班',
          children:[
            { field: 'zxcnYb1', title: '最小产能', width: 120,editRender: { name: "$input"},
              filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
              filterRender: { name: 'FilterCombination', },
            },
            { field: 'zdcnYb1', title: '最大产能', width: 120,editRender: { name: "$input"},
              filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
              filterRender: { name: 'FilterCombination', },
            },
          ],
        },
        { field: 'cnhj1', title: '合计', width: 120,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination', },
        },
        { field: '', title: '坡口白班',
          children:[
            { field: 'zxcnBb2', title: '最小产能', width: 120,editRender: { name: "$input"},
              filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
              filterRender: { name: 'FilterCombination', },
            },
            { field: 'zdcnBb2', title: '最大产能', width: 120,editRender: { name: "$input"},
              filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
              filterRender: { name: 'FilterCombination', },
            },
          ],
        },
        { field: '', title: '坡口夜班',
          children:[
            { field: 'zxcnYb2', title: '最小产能', width: 120,editRender: { name: "$input"},
              filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
              filterRender: { name: 'FilterCombination', },
            },
            { field: 'zdcnYb2', title: '最大产能', width: 120,editRender: { name: "$input"},
              filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
              filterRender: { name: 'FilterCombination', },
            },
          ],
        },
       { field: 'cndw2', title: '产能单位', width: 120,
          editRender: {
            name: "$select",
            options: [],
            props: { clearable: true },
          },
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination', },
        },
        { field: 'cnhj2', title: '合计', width: 120,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination', },
        },
        { field: '', title: '打磨白班',
          children:[
            { field: 'zxcnBb3', title: '最小产能', width: 120,editRender: { name: "$input"},
              filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
              filterRender: { name: 'FilterCombination', },
            },
            { field: 'zdcnBb3', title: '最大产能', width: 120,editRender: { name: "$input"},
              filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
              filterRender: { name: 'FilterCombination', },
            },
          ],
        },
        { field: '', title: '打磨夜班',
          children:[
            { field: 'zxcnYb3', title: '最小产能', width: 120,editRender: { name: "$input"},
              filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
              filterRender: { name: 'FilterCombination', },
            },
            { field: 'zdcnYb3', title: '最大产能', width: 120,editRender: { name: "$input"},
              filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
              filterRender: { name: 'FilterCombination', },
            },
          ],
        },
        { field: 'cndw3', title: '产能单位', width: 120,
          editRender: {
            name: "$select",
            options: [],
            props: { clearable: true },
          },
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination', },
        },
        { field: 'cnhj3', title: '合计', width: 120,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination', },
        },
        { field: '', title: '曲加工白班',
          children:[
            { field: 'zxcnBb4', title: '最小产能', width: 120,editRender: { name: "$input"},
              filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
              filterRender: { name: 'FilterCombination', },
            },
            { field: 'zdcnBb4', title: '最大产能', width: 120,editRender: { name: "$input"},
              filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
              filterRender: { name: 'FilterCombination', },
            },
          ],
        },
        { field: '', title: '曲加工夜班',
          children:[
            { field: 'zxcnYb4', title: '最小产能', width: 120,editRender: { name: "$input"},
              filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
              filterRender: { name: 'FilterCombination', },
            },
            { field: 'zdcnYb4', title: '最大产能', width: 120,editRender: { name: "$input"},
              filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
              filterRender: { name: 'FilterCombination', },
            },
          ],
        },
        { field: 'cndw4', title: '产能单位', width: 120,
          editRender: {
            name: "$select",
            options: [],
            props: { clearable: true },
          },
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination', },
        },
        { field: 'cnhj4', title: '合计', width: 120,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination', },
        },
      ],
      validRules: {
        // sbbh: [{ required: true, message: "设备编号不可为空" }],
      },
      kwOptions: [],
      departmentOptions: [],
      options: [],
      sbmcOptions: [],
      checkedList: [],
    };
  },
  created() {
    window.addEventListener("resize", this.getHeight);
    this.getHeight();
    this.emSbjbbZM("QGKW");
    this.emSbjbbZM("bmbm");
    this.getBMMCBM();
    this.initList();
    this.initgxList();
    this.initdwList();
  },
  methods: {
    //工序 xn20231110
    initgxList(){
      const dict=window.localStorage.getItem( 'PCGX');
      const gxList =JSON.parse(dict)
        this.$nextTick(() => {
          const tableRef = this.$refs.tableRef;
          const gx = tableRef.getColumnByField("gx");
          const testList = [];
          for (let i = 0; i < gxList.length; i++) {
            const temp = {
              label: gxList[i].bmsm,
              value: gxList[i].bm,
            };
            testList.push(temp);
          }
          gx.editRender.options = testList;
        });

    },
    initdwList(){
      const dict=window.localStorage.getItem( 'JLDW');
      const dwList =JSON.parse(dict)
        this.$nextTick(() => {
          const tableRef = this.$refs.tableRef;
          const cndw = tableRef.getColumnByField("cndw");
          // const cndw1 = tableRef.getColumnByField("cndw1");
          const cndw2 = tableRef.getColumnByField("cndw2");
          const cndw3 = tableRef.getColumnByField("cndw3");
          const cndw4 = tableRef.getColumnByField("cndw4");
          const testList = [];
          for (let i = 0; i < dwList.length; i++) {
            const temp = {
              label: dwList[i].bmsm,
              value: dwList[i].bm,
            };
            testList.push(temp);
          }
          cndw.editRender.options = testList;
          // cndw1.editRender.options = testList;
          cndw2.editRender.options = testList;
          cndw3.editRender.options = testList;
          cndw4.editRender.options = testList;
        });

    },
    // 部门对应设备名称和编号
    getBMMCBM() {
      getBMMCBM().then((res) => {
        if (res.success) {
          this.options = res.data;
        }
      });
    },
    // 获取列表接口
    initList() {
      getList(this.request).then((res) => {
        this.list = res.data;
        this.checkedList = [];
        if (this.list.length > 0) {
          this.list.forEach((item) => {
            item.sbmcDisabled = true;
          });
        }
      });
    },
    // 设备信息转码接口
    emSbjbbZM(params) {
      emSbjbbZM({
        bmlb: params,
      }).then((res) => {
        const tableRef = this.$refs.tableRef;
        if (params == "bmbm") {
          // 所属部门
          this.departmentOptions = res.data;
          const sbztText = tableRef.getColumnByField("szcs");
          const testList = [];
          for (let i = 0; i < this.departmentOptions.length; i++) {
            const temp = {
              label: this.departmentOptions[i].bmsm,
              value: this.departmentOptions[i].bm,
            };
            testList.push(temp);
          }
          sbztText.editRender.options = testList;
        } else if (params == "QGKW") {
          // 跨位
          this.kwOptions = res.data;
          const sbztText = tableRef.getColumnByField("kw");
          const testList = [];
          for (let i = 0; i < this.kwOptions.length; i++) {
            const temp = {
              label: this.kwOptions[i].bmsm,
              value: this.kwOptions[i].bm,
            };
            testList.push(temp);
          }
          sbztText.editRender.options = testList;
        }
      });
    },

    // 监听选择所属部门
    changeSzcs(row, id) {
      console.log(row, id);
      if (row.row.szcs) {
        row.row.sbmcDisabled = false;
        row.row.sbmc = "";
        row.row.sbbh = "";
        this.sbmcOptions = [];
        const testList = [];
        console.log(this.options, "======");
        for (let i = 0; i < this.options[id.value].length; i++) {
          const temp = {
            label: this.options[id.value][i].sbmc,
            value: this.options[id.value][i].sbbh,
          };
          testList.push(temp);
        }
        this.sbmcOptions = testList;
      } else {
        row.row.sbmcDisabled = true;
        row.row.sbmc = "";
        row.row.sbbh = "";
        this.sbmcOptions = [];
      }
    },
    // 监听选择设备名称
    changeSbmc(event, row) {
      if (row.sbmc) {
        row.sbbh = event.value;
        let arr = this.sbmcOptions.filter((item, index) => {
          if (item.value == event.value) {
            return item;
          }
        });
        row.sbmc = arr[0].label;
      } else {
        row.sbbh = "";
      }
    },
    // 全选
    selectAllEvent() {
      const records = this.$refs.tableRef.getCheckboxRecords();
      this.checkedList = records;
    },
    // 单选
    selectChangeEvent() {
      const records = this.$refs.tableRef.getCheckboxRecords();
      this.checkedList = records;
    },
    // 操作
    async toolbarButtonClickEvent(code) {
      const { insertRecords, removeRecords, updateRecords } =
        this.$refs.tableRef.getRecordset();
      switch (code) {
        case "insert":
          this.$refs.tableRef.insertAt({ sbmcDisabled: true }, -1);
          break;
        case "delete":
          if (this.checkedList.length > 0) {
            this.$refs.tableRef.removeCheckboxRow();
          } else {
            this.$message.warning("请选择数据！");
          }
          break;
        case "save":
          const valid = this.$refs.tableRef;
          if (valid) {
            const errMap = await valid.validate(true);
            if (errMap) {
              return false;
            } else {
              // 新增
              if (insertRecords.length !== 0) {
                save(insertRecords).then((res) => {
                  if (res.success) {
                    this.list = this.list.concat(insertRecords);
                    const xTable = this.$refs.tableRef;
                    xTable.loadData(this.initList());
                    this.$message({
                      message: "保存成功！",
                      type: "success",
                    });
                  }
                });
              }

              //  修改
              if (updateRecords.length !== 0) {
                update(updateRecords).then((res) => {
                  if (res.success) {
                    const xTable = this.$refs.tableRef;
                    xTable.loadData(this.initList());
                    this.$message({ message: "保存成功！", type: "success" });
                  }
                });
              }
            }
          }
          // 删除
          if (removeRecords.length !== 0) {
            delete1(removeRecords).then((res) => {
              if (res.success) {
                for (let i = 0; i < this.list.length; i++) {
                  if (removeRecords.indexOf(this.list[i]) !== -1) {
                    this.list.splice(i, 1);
                    i--;
                  }
                }
                const xTable = this.$refs.tableRef;
                xTable.loadData(this.initList());
                this.$message({ message: "保存成功！", type: "success" });
              }
            });
          }

          if (removeRecords.length !== 0) {
            this.$message(
              `新增 ${insertRecords.length} 条，删除 ${removeRecords.length} 条，更新 ${updateRecords.length} 条`
            );
          } else {
            const valid = this.$refs.tableRef;
            if (valid) {
              const errMap = await valid.validate(true);
              if (errMap) {
                return false;
              } else {
                this.$message(
                  `新增 ${insertRecords.length} 条，删除 ${removeRecords.length} 条，更新 ${updateRecords.length} 条`
                );
              }
            }
          }
          break;
      }
    },
    getHeight() {
      this.height = window.innerHeight - 160;
    },
  },
};
</script>
<style lang="scss" scoped>
.el-header {
  margin: 0;
  padding: 5px 0;
  height: auto;
}
.el-container {
  padding: 0;
  margin: 0;
}
.el-main {
  padding: 0;
}
.el-col {
  padding: 0;
}
.el-aside {
  background: #fff;
  padding: 0;
}
.el-container {
  padding: 0;
}
.el-row {
  margin-bottom: 5px;
}
.el-row:last-child {
  margin-bottom: 0;
}
.tableStyles {
  background: #0a76a4;
}
.my-dropdown {
  width: 450px;
  height: 250px;
  background-color: #fff;
  border: 1px solid #dcdfe6;
  box-shadow: 0 0 6px 2px rgba(0, 0, 0, 0.1);
  font-size: 12px;
}
::v-deep .mytable .vxe-input {
  display: flex !important;
}
::v-deep .el-form-item {
  margin-bottom: 0px !important;
}
::v-deep .el-input {
  width: auto !important;
}
</style>
