<template>
  <div class="testdiv">
    <div class="divborder newSearchDiv">
      <div class="filter">
        <label>{{ $t("1031") }}：</label>
        <Select
          v-model="filterObj.machineFType"
          @on-change="machineFTypeChange($event)"
          label-in-value
          clearable
          class="searchSelect"
        >
          <Option
            v-for="item in initPageData.machineFTypeList"
            :value="item.id"
            :key="item.id"
            >{{ item.title }}</Option
          >
        </Select>
      </div>
      <div class="filter">
        <label> {{ $t("1032") }}：</label>
        <Select
          v-model="filterObj.machineCType"
          @on-change="machineCTypeChange($event)"
          label-in-value
          clearable
          class="searchSelect"
        >
          <Option
            v-for="item in initPageData.machineCTypeList"
            :value="item.id"
            :key="item.id"
            >{{ item.title }}</Option
          >
        </Select>
      </div>
      <div class="filter">
        <label> {{ $t("1590027") }}：</label>
        <Select
          v-model="filterObj.codetype"
          label-in-value
          clearable
          class="searchSelect"
          :disabled="filterObj.machineCType > 0 ? false : true"
        >
          <Option :value="1" v-text="$t('200022')" :label="$t('200022')"
            >失效模式</Option
          >
          <Option :value="2" v-text="$t('1590018')" :label="$t('1590018')"
            >失效原因</Option
          >
          <Option :value="3" v-text="$t('1950472')" :label="$t('1950472')"
            >修复行动</Option
          >
        </Select>
        <!-- <Select
          v-model="Trouble_code2"
          label-in-value
          :disabled="filterObj.codetype> 0?false:true"
          style="width:200px;margin-left:10px;"
        >
          <Option
            v-for="(item, key) in Trouble_codelist"
            :value="item.id"
            :key="key"
          >{{item.faulttype}}</Option>
        </Select> -->
      </div>
      <div class="searchBtn">
        <Button
          type="primary"
          class="twoWord"
          icon="ios-search"
          @click="search(true)"
          >{{ $t("1001") }}</Button
        >
        <Button
          type="success"
          class="twoWord"
          icon="md-add-circle"
          @click="add()"
          >{{ $t(1002) }}</Button
        >
        <Button
          type="warning"
          class="twoWord"
          icon="ios-create-outline"
          @click="edit()"
          >{{ $t(1003) }}</Button
        >
        <Button
          type="error"
          class="twoWord intervalRight"
          icon="md-close-circle"
          @click="del()"
          >{{ $t(1004) }}</Button
        >
        <Button
          type="primary"
          class="fourWord"
          icon="md-download"
          @click="downModel()"
          >{{ $t(800049) }}</Button
        >

        <Upload
          style="display: inline-block"
          ref="upload"
          :action="action"
          name="excel-file"
          :headers="headers"
          :show-upload-list="false"
          :on-format-error="handleFormatError"
          :on-success="handleSuccess"
          :on-error="handleError"
          :format="['xlsx']"
        >
          <Button class="fourWord" icon="md-cloud-upload" type="primary">{{
            $t("upload-excel")
          }}</Button>
        </Upload>
      </div>
    </div>

    <div class="divborder newTableDiv">
      <Table
        class="tableClass"
        highlight-row
        :data="tableData1"
        :columns="tableColumns1"
        @on-selection-change="handleSelection"
        @on-current-change="handleRowChange1"
      ></Table>
      <div class="pageDiv">
        <div class="pageDirection">
          <Page
            :total="total1"
            :current="page1"
            @on-change="changePage1"
            show-elevator
            show-sizer
            show-total
            @on-page-size-change="handlePageSize1"
            :page-size-opts="[10, 20, 30, 40, 100]"
          ></Page>
        </div>
      </div>
    </div>

    <!-- <div class="mt20">
      <div class="clear" style="clear: both;">
        <span class="unstandard" v-text="$t(200007)">未定义失效模式工单</span>
        <Button class="ml20" type="primary" @click="search2(1,pagesize3)" v-text="$t(1001)">查询</Button>
      </div>
      <div class="clear"></div>
      <div class="filter">
        <Row type="flex" justify="space-between" class="code-row-bg">
          <Col span="6">
            <label
              style="padding-left: 8px;text-align: left;height: 30px; line-height: 30px;"
              v-text="$t(1035)"
            >时间范围:</label>
          </Col>
          <Col span="18">
            <DatePicker
              type="datetimerange"
              @on-change="timechange"
              :value="filterObj.time"
              format="yyyy-MM-dd hh:mm:ss"
              placement="bottom-end"
              split-panels
              :placeholder="$t(1143)"
              style="width: 300px"
            ></DatePicker>
          </Col>
        </Row>
      </div>
    </div>
    <div class="divborder">
      <Table
        style="margin-top:10px"
        ref="table3"
        width="100%"
        @on-current-change="handleRowChange3"
        highlight-row
        :data="tableData3"
        :columns="tableColumns3"
        @on-selection-change="selectionChange"
      ></Table>
      <div style="margin: 10px;overflow: hidden">
        <div style="float: right;">
          <Page
            :total="total3"
            :current="page3"
            @on-change="changePage3"
            show-elevator
            show-sizer
            show-total
            @on-page-size-change="handlePageSize3"
            :page-size-opts="[10,20,30,40,100]"
          ></Page>
        </div>
        <div class="fl">
          <Button type="primary" @click="defineFaultInfo()" v-text="$t(1111)">定义故障信息</Button>
        </div>
      </div>
    </div> -->

    <Modal
      class="newModalClass"
      :title="modalTitle"
      v-model="addModal"
      @on-ok="handleSubmit('addObj')"
      :loading="loading"
      :styles="{ width: '400px' }"
    >
      <div class="modal_body">
        <Form
          ref="addObj"
          :model="addObj"
          label-position="left"
          :label-width="100"
          :rules="addModalRule"
        >
          <FormItem :label="$t(800210)">
            <Select v-model="addObj.tab" class="searchSelect">
              <Option
                v-for="(item, index) in typeList"
                :value="index"
                :key="index"
                >{{ item }}</Option
              >
            </Select>
          </FormItem>
          <FormItem :label="$t(900572)" prop="fault">
            <Input class="searchInput" type="text" v-model="addObj.fault" />
          </FormItem>
          <FormItem :label="$t(100044)" prop="des">
            <Input class="searchInput" type="text" v-model="addObj.des" />
          </FormItem>
        </Form>
      </div>
    </Modal>

    <Modal
      class="newModalClass"
      :title="$t(1111)"
      v-model="defineFaultInfoModal"
      @on-ok="DefineFaultInfoSubmit('defineFaultInfoObj')"
      :styles="{ width: '600px' }"
    >
      <div class="modal_body">
        <Form
          ref="defineFaultInfoObj"
          :model="defineFaultInfoObj"
          label-position="left"
          :label-width="80"
          :rules="defineFaultInfoModalRule"
        >
          <FormItem :label="$t(200005)" prop="type">
            <Select
              v-model="defineFaultInfoObj.type"
              @on-change="typeChange($event)"
              label-in-value
              clearable
              class="searchSelect"
            >
              <Option
                v-for="(value, key) in defineFaultInfoObj.typeList"
                :value="key"
                :key="key"
                >{{ value }}</Option
              >
            </Select>
          </FormItem>
          <FormItem :label="$t(900572)" prop="fault">
            <Select
              v-model="defineFaultInfoObj.fault"
              @on-change="faultChange($event)"
              label-in-value
              clearable
              class="searchSelect"
            >
              <Option
                v-for="item in defineFaultInfoObj.faultList"
                :tag="item.des"
                :value="item.id"
                :key="item.id"
                >{{ item.title }}</Option
              >
            </Select>
          </FormItem>
          <FormItem :label="$t(100044)" class="pl10">
            <!-- <input class="publicinput" type="text" v-model="defineFaultInfoObj.des"> -->
            <label>{{ defineFaultInfoObj.des }}</label>
          </FormItem>
        </Form>
      </div>
    </Modal>
  </div>
</template>
<script>
import axios from "@/libs/api.request";

export default {
  name: "tpm-setting",
  components: {},
  data() {
    const validateFault = (rule, value, callback) => {
      if (value != "") {
        callback();
      } else {
        return callback(new Error(this.$t(1590023)));
      }
    };
    const validateDes = (rule, value, callback) => {
      callback();
      // if (value != "") {
      //   callback()
      // } else {
      //   return callback(new Error(this.$t(1590024)))
      // }
    };

    const validateType = (rule, value, callback) => {
      // 正整数
      if (/^[1-9]\d*$/.test(value)) {
        callback();
      } else {
        return callback(new Error(this.$t(100233)));
      }
    };

    const validateFault2 = (rule, value, callback) => {
      // 正整数
      if (/^[1-9]\d*$/.test(value)) {
        callback();
      } else {
        return callback(new Error(this.$t(1590025)));
      }
    };
    return {
      action: axios.publicPath + "tpm/Upload/Submit?templateName=FaultType",
      headers: {
        token: localStorage.getItem("token"),
        timestamp: localStorage.getItem("timestamp"),
        nonce: localStorage.getItem("nonce"),
        sign: localStorage.getItem("sign"),
      },

      datamt: [], // 插件所需数据
      mtdata: [], // 选中数据
      filterObj: {
        machineFType: 0, // 父级设备类型
        machineCType: 0, // 子级设备类型
        machineFTypeName: "",
        machineCTypeName: "",
        codetype: 0,
        time: [], // '2017-11-01 12:00:00', '2020-02-19 12:00:00'
      },
      // Trouble_code2: "",
      // Trouble_codelist: [],
      initPageData: {
        machineFTypeList: [], // 父级设备类型 下拉菜单
        machineCTypeList: [], // 子级设备类型 下拉菜单
      },

      total1: 0,
      page1: 1,
      pagesize1: 10, // 列表pagesize
      tableData1: [],
      tableColumns1: [
        {
          type: "selection",
          width: 60,
          align: "center",
        },
        {
          title: this.$t(1008),
          render: (h, params) => {
            return h(
              "div",
              params.index + 1 + (this.page1 - 1) * this.pagesize1
            );
          },
          width: 80,
          align: "center",
        },
        {
          title: this.$t(100342),
          key: "machineFTypeName",
        },
        {
          title: this.$t(100343),
          key: "machineCTypeName",
        },
        {
          title: this.$t(1590027),
          key: "fault_type_name",
        },
        {
          title: this.$t(900572),
          key: "title", // ele.repairtime+分钟
        },
        {
          title: this.$t(100044),
          key: "des", //
        },
      ],

      total3: 0,
      page3: 1,
      pagesize3: 10, // 列表pagesize
      selection: [], // 多选容器
      tableData3: [],
      tableColumns3: [
        {
          // 列表字段属性集合
          type: "index2",
          title: this.$t(1008),
          key: "xuhao",
          render: (h, params) => {
            return h(
              "span",
              params.index + (this.page3 - 1) * this.pagesize3 + 1
            );
          },
          width: 80,
        },
        {
          title: this.$t(800041),
          key: "remark",
        },
        {
          title: this.$t(200008),
          key: "fault_description",
        },
        {
          title: this.$t(200009),
          key: "repair_process",
        },
        {
          title: this.$t(200010),
          key: "real_faultid_desc", // ele.repairtime+分钟
        },
        {
          title: this.$t(200011),
          key: "sparepart", // ele.repairtime+分钟
        },
        {
          title: this.$t(200012),
          key: "reporttime", // ele.repairtime+分钟
        },
        {
          title: this.$t(800207),
          key: "stoptime", // ele.repairtime+分钟
          width: 180,
        },
        {
          title: this.$t(200023),
          key: "add_repairmid_name", // ele.repairtime+分钟
          width: 100,
        },
        {
          type: "selection",
          align: "center",
        },
      ],

      selectedObj1: null, // 选中对象容器
      selectedObj3: null, // 选中对象容器

      addModal: false, // 添加Modal的开关
      loading: true,
      modalTitle: "",
      optType: "add", // add,edit
      // tab: 1,
      addObj: {
        tab: 0,
        fault: "", // mTypeVal 故障类型文本框
        des: "", // title 失效模式
        status: 1,
      },
      typeList: [this.$t(200022), this.$t(1590018), this.$t(1590019)],
      defineFaultInfoModal: false,
      defineFaultInfoObj: {
        type: 1,
        fault: "", // mTypeVal 故障类型文本框
        typeList: {
          1: this.$t(200022),
          2: this.$t(1590018),
          3: this.$t(1590019),
        }, // listid 故障类型下拉列表
        faultList: [],
        des: "", // title 失效模式
      },
      addModalRule: {
        fault: [
          {
            required: true,
            validator: validateFault,
            trigger: "blur",
          },
        ],
        des: [
          {
            trigger: "blur",
          },
        ],
      },
      defineFaultInfoModalRule: {
        fault: [
          {
            required: true,
            validator: validateFault2,
            trigger: "blur",
          },
        ],
        type: [
          {
            required: true,
            validator: validateType,
            trigger: "blur",
          },
        ],
      },
      idArr: [],
    };
  },
  mounted() {
    this.initMachineFTypeList();
    this.initMT();
  },
  created() {
    // 格式化表格样式
    this.tableColumns1 = this.$time.initTableTitle(this.tableColumns1);
    this.tableColumns3 = this.$time.initTableTitle(this.tableColumns3);
  },
  methods: {
    initMT() {
      axios
        .request({
          url: "/config/MachineType/GetMachineTypeTree",
          method: "get",
        })
        .then((res) => {
          this.datamt = res.data.data;
          this.mtdata = [];
        });
    },
    // machineTypeChange(data) {
    //   this.mtdata = data
    //   this.filterObj.machineFType=data.length>=1?data[0]:0
    //   this.filterObj.machineCType=data.length>=2?data[1]:0
    // },
    initMachineFTypeList() {
      axios
        .request({
          url: "/config/MachineType/GetEditDcategory",
          method: "get",
        })
        .then((res) => {
          if (res.data.data != undefined && res.data.data.length > 0) {
            this.initPageData.machineFTypeList = res.data.data;
          } else {
            this.initPageData.machineFTypeList = [];
          }
        });
    },
    machineFTypeChange(data) {
      if (!data) {
        this.initPageData.machineCTypeList = [];
        this.filterObj.machineCType = 0;
        return false;
      }
      this.initPageData.machineCTypeList = [];
      this.filterObj.machineCType = 0;
      let params = {
        id: data === undefined ? 0 : data.value,
      };
      this.filterObj.machineFTypeName = data.label;
      axios
        .request({
          url: "/config/MachineType/GetLastMachineType",
          params,
          method: "get",
        })
        .then((res) => {
          if (res.data.data != undefined && res.data.data.length > 0) {
            this.initPageData.machineCTypeList = res.data.data;
          } else {
            this.initPageData.machineCTypeList = [];
          }
        });
    },
    Trouble_change() {
      var params = {
        type: this.filterObj.codetype,
      };
      this.axios
        .request({
          url: "/tpm/FaultType/GetFaultTypeListByType",
          params,
          method: "get",
        })
        .then((res) => {
          if (res.data.ret == 1) {
            this.Trouble_codelist = res.data.data;
          } else {
            this.$Message.error(this.$t(res.data.msg));
          }
        });
    },
    machineCTypeChange(data) {
      if (data && data != undefined) {
        this.filterObj.machineCTypeName = data.label;
      }
    },
    timechange(value) {
      this.filterObj.time = value;
    },
    handleRowChange1(currentRow, oldCurrentRow) {
      // 选中一条数据，触发事件
      this.selectedObj1 = currentRow;
    },
    changePage1(value) {
      this.page1 = value;
      this.search();
    },
    handleSelection(ids) {
      let idArr = [];
      if (ids && Array.isArray(ids) && ids.length) {
        ids.forEach((v) => {
          idArr.push(v.id);
        });
      }
      this.idArr = idArr;
    },
    handlePageSize1(value) {
      // 切换分页页数，触发事件
      this.pagesize1 = value;
      this.search();
    },

    search(isInit) {
      if (this.filterObj.machineCType === 0) {
        this.$Message.error(this.$t("9000280"));
        return;
      }
      if (isInit) {
        this.page1 = 1;
        this.pagesize1 = 10;
      }
      axios
        .request({
          url:
            "/tpm/FaultType/GetFaultList?typeid=" +
            (this.filterObj.machineCType === 0
              ? this.filterObj.machineFType
              : this.filterObj.machineCType) +
            "&faulttype=" +
            this.filterObj.codetype +
            "&page=" +
            this.page1 +
            "&pagesize=" +
            this.pagesize1,
          method: "get",
        })
        .then((res) => {
          if (res.data.ret === 1) {
            let data = [];
            res.data.data.forEach((item) => {
              var fault_type_name = "";
              if (item.fault_type === 1) {
                fault_type_name = this.$t("200022");
              } else if (item.fault_type === 2) {
                fault_type_name = this.$t("1590018");
              } else if (item.fault_type === 3) {
                fault_type_name = this.$t("1950472");
              }
              if (item) {
                data.push({
                  machineFTypeName: this.filterObj.machineFTypeName,
                  machineCTypeName: this.filterObj.machineCTypeName,
                  title: item.title,
                  type: item.fault_type,
                  fault_type_name: fault_type_name,
                  des: item.description,
                  id: item.id,
                });
              }
            });
            this.tableData1 = data;
            this.total1 = res.data.total;
          }
        });
    },

    changePage3(value) {
      // 换页事件
      this.page3 = value;
      this.search2(this.page3, this.pagesize3);
    },
    handleRowChange3(currentRow, oldCurrentRow) {
      // 选中一条数据，触发事件
      this.selectedObj3 = currentRow;
    },
    handlePageSize3(value) {
      // 切换分页页数，触发事件
      this.pagesize3 = value;
      this.search2(this.page3, this.pagesize3);
    },
    search2(page, pagesize) {
      this.getUnstandardRepairInfo(page, pagesize);
    },
    getUnstandardRepairInfo(page, pagesize) {
      if (this.filterObj.machineCType === 0) {
        this.$Message.error(this.$t("9000280"));
        return false;
      }
      // 获取非标维修列表
      let params = {
        machineType:
          this.filterObj.machineCType === 0
            ? this.filterObj.machineFType
            : this.filterObj.machineCType,
        starttime: this.filterObj.time[0],
        endtime: this.filterObj.time[1],
        page: page,
        pagesize: pagesize,
      };
      axios
        .request({
          url: "/tpm/Repair/getUnstandardRepairInfo",
          params,
          method: "get",
        })
        .then((res) => {
          // 测试用数据，尽量不要删，不好找
          // res = {
          //   data: {
          //     "total": 36,
          //     "page": 1,
          //     "pagesize": 10,
          //     "ret": 1,
          //     "msg": 1,
          //     "data": [{
          //       "id": 478,
          //       "remark": "A车刹车电机晃动厉害",
          //       "fault_description": "天桥纠偏刹车棍电机一直晃动，至今没有合理的解决方案",
          //       "repair_process": "一直晃动",
          //       "real_faultid_desc": "设计缺陷",
          //       "sparepartid": "",
          //       "createtime": 1547209927,
          //       "stoptime": 237,
          //       "nickname": "张南南",
          //       "reporttime": "2019-01-11 20:32",
          //       "add_repairmid_name": "",
          //       "sparepart": "充电电锤(1401772)*1",
          //       "checktime": 1547224195
          //     }, {
          //       "id": 483,
          //       "remark": "B  车天桥刹车数为   0    实际瓦纸刹车太大，导致断纸多次\n请尽快维修",
          //       "fault_description": "B车下层天桥刹车轮驱动马达脱落",
          //       "repair_process": "马达脱落",
          //       "real_faultid_desc": "厂家设计缺陷，更换不到一年脱落，",
          //       "sparepartid": "",
          //       "createtime": 1547241344,
          //       "stoptime": 6715,
          //       "nickname": "张南南",
          //       "reporttime": "2019-01-12 05:15",
          //       "add_repairmid_name": "张南南",
          //       "sparepart": "",
          //       "checktime": 1547644256
          //     }]
          //   }
          // }
          if (res.data.ret === 1) {
            let data = [];
            if (res.data.data != 1) {
              res.data.data.forEach((item) => {
                if (item) {
                  data.push({
                    remark: item.remark,
                    fault_description: item.fault_description,
                    repair_process: item.repair_process,
                    real_faultid_desc: item.real_faultid_desc,
                    sparepart: item.sparepart,
                    reporttime: item.reporttime,
                    stoptime: item.stoptime,
                    add_repairmid_name: item.add_repairmid_name,
                    id: item.id,
                  });
                }
              });
            }
            this.total3 = res.data.total;
            this.tableData3 = data;
          }
        });
    },
    add() {
      if (this.filterObj.machineCType === 0) {
        this.$Message.error(this.$t("9000280"));
        return false;
      }
      this.optType = "add";
      this.addModal = true;
      this.modalTitle = "添加";
      this.initAddModal(); // 初始化
    },
    edit() {
      if (this.selectedObj1 === null && this.selectedObj2 == null) {
        this.$Message.error(this.$t("1043"));
        return false;
      }
      this.tab = this.selectedObj1 != null ? 0 : 1;
      this.optType = "edit";
      this.addModal = true;
      this.modalTitle = this.$t("1003");
      this.initEditModal(); // 初始化
    },
    initAddModal() {
      this.tab = "1"; // 默认添加失效模式
      this.$refs.addObj.resetFields();
    },
    initEditModal() {
      this.addObj.fault = this.selectedObj1.title;
      this.addObj.des = this.selectedObj1.des;
      this.tab = this.selectedObj1.type.toString();
    },
    del() {
      if (!this.idArr.length) {
        this.$Message.error(this.$t("1043"));
        return;
      }
      this.$Modal.confirm({
        title: this.$t("1018"),
        width: 300,
        closable: true,
        okText: this.$t("1004"),
        onOk: () => {
          let data = {
            ids: this.idArr,
          };
          axios
            .request({
              url: "/tpm/FaultType/DeleteFaultTypeByIds",
              data,
              method: "post",
            })
            .then((res) => {
              if (res.data.ret == 1) {
                this.$Message.success(this.$t("1058"));
                this.page1 = 1;
                this.search();
                this.idArr = [];
              } else {
                this.$Message.error(this.$t(res.data.msg));
              }
            });
        },
      });
    },
    faultChangeAdd(type, def) {
      let params = {
        machinetypeid: this.filterObj.machineCType,
      };
      let temp = [];
      axios
        .request({
          url: "/tpm/FaultType/GetFaultTypeList",
          params,
          method: "get",
        })
        .then((res) => {
          if (res.data.ret === 1 && res.data.data != undefined) {
            res.data.data.forEach((item) => {
              temp.push({
                title: item.faulttype,
                id: item.faulttype,
              });
            });
          }
          this.addObj.faultList = temp;
        });
    },
    handleSubmit(name) {
      if (this.optType === "add") {
        this.addHandle(name);
      } else {
        this.editHandle(name);
      }
    },
    addHandle(name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          // valid
          let data = {
            title: this.addObj.fault,
            typeid: this.filterObj.machineCType,
            fault_type: this.addObj.tab + 1,
            description: this.addObj.des,
          };
          axios
            .request({
              url: "/tpm/FaultType/AddFaultType",
              data,
              method: "post",
            })
            .then((res) => {
              if (res.data.ret === 1) {
                this.$Message.success(this.$t("1055"));
                this.addModal = false;
                this.page1 = 1;
                this.search();
              } else {
                this.$Message.error(this.$t(res.data.msg));
                this.clearLoading();
                return false;
              }
            });
        } else {
          this.clearLoading();
          return false;
        }
      });
    },
    editHandle(name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          // valid
          let data = {
            title: this.addObj.fault,
            typeid: this.filterObj.machineCType,
            fault_type: this.addObj.tab + 1,
            description: this.addObj.des,
            id: this.selectedObj1.id,
          };
          axios
            .request({
              url: "/tpm/FaultType/EditFaultType",
              data,
              method: "post",
            })
            .then((res) => {
              if (res.data.ret === 1) {
                this.$Message.success(this.$t("1060"));
                this.addModal = false;
                this.page1 = 1;
                this.search();
              } else {
                this.$Message.error(this.$t(res.data.msg));
                this.clearLoading();
              }
            });
        } else {
          this.clearLoading();
          return false;
        }
      });
    },
    selectionChange(selection) {
      this.selection = selection;
    },
    defineFaultInfo() {
      if (this.selection.length === 0) {
        this.$Message.error(this.$t("1043"));
        return false;
      }
      this.defineFaultInfoModal = true;
      // this.initDefineFaultInfoModal()
      this.typeChange({
        value: 1,
      });
    },
    typeChange(row) {
      let params = {
        type: row === undefined ? 1 : row.value,
        // machinetypeid: (this.filterObj.machineCType === 0 ? this.filterObj.machineFType : this.filterObj.machineCType)
      };
      let temp = [];
      axios
        .request({
          url: "/tpm/FaultType/GetFaultTypeListByType",
          params,
          method: "get",
        })
        .then((res) => {
          if (res.data.ret === 1 && res.data.data != undefined) {
            res.data.data.forEach((item) => {
              temp.push({
                title: item.faulttype,
                id: item.id,
                des: item.description,
              });
            });
            this.defineFaultInfoObj.faultList = temp;
          } else {
            this.defineFaultInfoObj.faultList = [];
          }
        });
    },
    faultChange(row) {
      this.defineFaultInfoObj.des = row != undefined ? row.tag : "";
    },

    DefineFaultInfoSubmit(name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          // valid
          let ids = [];
          if (this.selection.length > 0) {
            this.selection.forEach((item) => {
              ids.push(item.id);
            });
          }
          let params = {
            id: ids.toString(), // 失效模式id
            fault_id: this.defineFaultInfoObj.fault, // 失效模式内容
          };
          axios
            .request({
              url: "/tpm/Repair/batchDefineRepairFault",
              params,
              method: "get",
            })
            .then((res) => {
              if (res.data.ret === 1) {
                this.$Message.success(this.$t("8000000"));
                this.addModal = false;
                this.page1 = 1;
                this.search();
              } else {
                this.$Message.error(this.$t("8000001"));
                this.clearLoading();
              }
            });
        } else {
          this.clearLoading();
        }
      });
    },

    changeTab(e) {
      // 初始化数据
      if (this.optType === "add") {
        this.addObj.fault = "";
        this.addObj.des = "";
      }
    },
    clearLoading() {
      this.loading = false;
      this.$nextTick(() => {
        this.loading = true;
      });
    },
    changeDate(dateA) {
      var dateee = new Date(dateA).toJSON();
      var date = new Date(+new Date(dateee) + 8 * 3600 * 1000)
        .toISOString()
        .replace(/T/g, " ")
        .replace(/\.[\d]{3}Z/, "");
      return date;
    },
    importFromExcel() {
      const file = this.$refs.fileInt.files[0];
      const data = new FormData();
      data.append("file", file);
      axios
        .request({
          url: "/tpm/Upload/Submit?templateName=FaultType",
          data,
          method: "post",
        })
        .then((res) => {
          if (res.data.ret === 1) {
            this.page1 = 1;
            this.search();
            this.$Message.success(this.$t("100378"));
          } else {
            this.$Message.error(this.$t(res.data.msg));
          }
        })
        .catch((err) => {
          alert(err.data.msg);
        });
    },
    downModel() {
      axios
        .request({
          url: "/config/download/GetTemplate?name=FaultType",
          method: "get",
        })
        .then((res) => {
          if (res.data.ret === 1) {
            window.location.href = axios.publicPath + "" + res.data.data;
          } else {
            this.$Message.error(this.$t(res.data.msg));
          }
        });
    },
    handleFormatError(file) {
      this.$Message.error(this.$t("950396") + file.name + this.$t("950397"));
    },
    handleSuccess(res, file) {
      if (res.ret === 1) {
        this.page1 = 1;
        this.search();
        this.$Message.success(this.$t("950398"));
      } else {
        if ((res.msg + "").indexOf(",") != -1) {
          inittip(res.msg);
        } else {
          this.$Message.error(this.$t(res.msg));
        }
      }
    },
    handleError() {
      this.$Message.error(this.$t("950399")); // 数据导入失败！
    },
  },
};
</script>
<style lang="less" scoped>
.unstandard {
  border-radius: 5px;
  padding: 6px;
  width: 150px;
  text-align: center;
  margin-bottom: 15px;
  border: 1px solid #ddd;
  float: left;
}
</style>
