<template>
  <!-- 设备维修计划维护 -->
  <el-container>
    <el-header style="width: 100%">
      <el-row>
        <el-col :span="18">
          <el-col :span="4">
            <el-input v-model="request.sbbh" placeholder="设备编号" clearable />
          </el-col>
        </el-col>
        <el-col :span="6" 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="18">
          <el-col :span="4">
            <el-date-picker
              v-model="request.beginTime"
              value-format="yyyy/MM/dd"
              type="date"
              placeholder="计划开始时间"
            />
          </el-col>
          <el-col :span="4">
            <el-date-picker
              v-model="request.endTime"
              type="date"
              value-format="yyyy/MM/dd"
              placeholder="计划结束时间"
            />
          </el-col>
        </el-col>
        <el-col :span="6" 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-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"
              :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"
            >
              <template #sbbh_edit="{ row }">
                <vxe-input
                  v-model="row.sbbh"
                  :disabled="row.sbbhDisabled"
                  placeholder=""
                ></vxe-input>
              </template>
              <template #fzr_edit="{ row }">
                <vxe-input
                  v-model="row.fzr"
                  :disabled="fzrDisabled"
                  placeholder=""
                ></vxe-input>
              </template>
              <template #kssjJh_edit="{ row }">
                <vxe-input
                  v-model="row.kssjJh"
                  type="datetime"
                  placeholder=""
                  transfer
                ></vxe-input>
              </template>
              <template #jssjJh_edit="{ row }">
                <vxe-input
                  v-model="row.jssjJh"
                  type="datetime"
                  placeholder=""
                  transfer
                ></vxe-input>
              </template>
              <template #kssjSj_edit="{ row }">
                <vxe-input
                  v-model="row.kssjSj"
                  type="datetime"
                  placeholder=""
                  transfer
                ></vxe-input>
              </template>
              <template #jssjSj_edit="{ row }">
                <vxe-input
                  v-model="row.jssjSj"
                  type="datetime"
                  placeholder=""
                  transfer
                ></vxe-input>
              </template>
            </vxe-grid>
          </div>
        </el-main>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
import { getList, save, update, delete1 } from "@/api/sjzx/sbwxjhWH";
export default {
  name: "sbwxjhWH",
  data() {
    return {
      request: {
        sbbh: "",
        beginTime: "",
        endTime: "",
      },
      list: [],
      height: "500px",
      tableColumn: [
        { type: "checkbox", width: 50, fixed: "left" },
        { type: "seq", width: 60, title: "序号", fixed: "left" },
        {
          field: "sbbh",
          title: "设备编号",
          editRender: { name: "input" },
          slots: { edit: "sbbh_edit" },
          fixed: "left",
        },
        {
          field: "sbmc",
          title: "设备名称",
          editRender: { name: "input" },
        },

        {
          field: "sbxh",
          title: "设备型号",
          editRender: { name: "input" },
        },

        {
          field: "sbgg",
          title: "设备规格",
          editRender: { name: "input" },
        },
        {
          field: "szcs",
          title: "所属部门",
          editRender: { name: "input" },
        },
        {
          field: "wxnr",
          title: "维修内容",
          editRender: { name: "input" },
        },
        {
          field: "kssjJh",
          title: "计划开始时间",
          editRender: { name: "input" },
          slots: { edit: "kssjJh_edit" },
        },
        {
          field: "jssjJh",
          title: "计划结束时间",
          editRender: { name: "input" },
          slots: { edit: "jssjJh_edit" },
        },
        {
          field: "kssjSj",
          title: "实际开始时间",
          editRender: { name: "input" },
          slots: { edit: "kssjSj_edit" },
        },
        {
          field: "jssjSj",
          title: "实际结束时间",
          editRender: { name: "input" },
          slots: { edit: "jssjSj_edit" },
        },
        {
          field: "zt",
          title: "状态",
          editRender: {
            name: "$select",
            options: [],
            props: { clearable: true },
          },
        },

        {
          field: "fzr",
          title: "负责人",
          editRender: { name: "input" },
          slots: { edit: "fzr_edit" },
        },
        {
          field: "bz",
          title: "备注",
          editRender: { name: "input" },
        },
      ],
      validRules: {
        sbbh: [{ required: true, message: "设备编号不可为空" }],
      },
      checkedList: [],
      sbbhDisabled: true,
      fzrDisabled: true,
    };
  },
  created() {
    window.addEventListener("resize", this.getHeight);
    this.getHeight();
    this.initList();
  },
  methods: {
    // 获取列表接口
    initList() {
      getList(this.request).then((res) => {
        this.list = res.data;
        this.checkedList = [];
        if (this.list.length > 0) {
          this.list.forEach((item) => {
            item.sbbhDisabled = this.sbbhDisabled;
            item.fzrDisabled = this.fzrDisabled;
          });
        }
      });
    },
    // 全选
    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(
            {
              sbbhDisabled: false,
              fzrDisabled: false,
            },
            -1
          );
          break;
        case "delete":
          if (this.checkedList.length > 0) {
            this.$confirm("是否确认删除选中的数据?", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
            })
              .then(() => {
                this.$refs.tableRef.removeCheckboxRow();
              })
              .catch(() => {});
          } 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) {
                this.$confirm("是否确认保存选中的数据?", "提示", {
                  confirmButtonText: "确定",
                  cancelButtonText: "取消",
                  type: "warning",
                })
                  .then(() => {
                    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",
                        });
                      }
                    });
                  })
                  .catch(() => {});
              }

              //  修改
              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;
  width: 100%;
}
.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 .el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 100% !important;
}
::v-deep .mytable .vxe-input {
  display: flex !important;
}
</style>
