<template>
  <div>
    <div class="divborder newSearchDiv">
      <div class="filter">
        <label>{{ $t(1132) }}：</label>
        <Cascader
          :data="datalevel"
          :value="leveldata"
          trigger="click"
          change-on-select
          class="searchSelect"
          @on-change="changelevel"
        ></Cascader>
      </div>
      <div class="filter">
        <label>{{ $t(800191) }}：</label>
        <Select
          v-model="filterObj.typeMode"
          label-in-value
          clearable
          class="searchSelect"
        >
          <Option
            v-for="(value, key) in initPageData.defendTypeList"
            :value="key"
            :key="key"
            >{{ value }}</Option
          >
        </Select>
      </div>
      <div class="filter">
        <label>{{ $t(950330) }}：</label>
        <Select
          v-if="filterObj.typeMode === '2'"
          v-model="filterObj.severity_level"
          label-in-value
          clearable
          class="searchSelect"
        >
          <Option
            v-for="(item, key) in initPageData.datalist2"
            :value="item.value"
            :key="key"
            >{{ item.name }}</Option
          >
        </Select>
        <Select
          v-else-if="filterObj.typeMode === '3'"
          v-model="filterObj.severity_level"
          label-in-value
          clearable
          class="searchSelect"
        >
          <Option
            v-for="(item, key) in initPageData.datalist3"
            :value="item.value"
            :key="key"
            >{{ item.name }}</Option
          >
        </Select>
        <Select
          v-else
          v-model="filterObj.severity_level"
          label-in-value
          clearable
          class="searchSelect"
        >
          <Option
            v-for="(item, key) in initPageData.datalist1"
            :value="item.value"
            :key="key"
            >{{ item.name }}</Option
          >
        </Select>
      </div>
      <div class="filter">
        <label>{{ $t(900908) }}：</label>
        <DatePicker
          type="datetimerange"
          v-model="timeRange"
          placeholder=""
          class="searchInput MaxWidth"
        ></DatePicker>
      </div>
      <div class="searchBtn">
        <Button
          type="primary"
          class="twoWord intervalRight"
          icon="ios-search"
          @click="search(1, pagesize, 0)"
          >{{ $t("1001") }}</Button
        >
        <Button
          type="primary"
          class="fourWord"
          icon="md-arrow-round-up"
          @click="search(1, pagesize, 1)"
          >{{ $t("1007") }}</Button
        >
      </div>
      <!-- <div class="conditionblock">
        <div class="conditionborder" @click="changecondition">
          <div class="box">
            <div class="skew">
              <div class="skew-main">
                <div v-show="morecondition">
                  <span
                    ><span v-text="$t('950400')">更多条件</span
                    ><Icon type="ios-arrow-down"
                  /></span>
                </div>
                <div v-show="!morecondition">
                  <span
                    ><span v-text="$t('950401')">收起条件</span
                    ><Icon type="ios-arrow-up"
                  /></span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div> -->
    </div>

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

      <div class="resultwrap" v-show="tableShow == true">
        <Table
          style="margin-top: 10px; width: 100%"
          @on-selection-change="changeMultipleSelection"
          :data="tableData3"
          :columns="tableColumns3"
        ></Table>
      </div>
    </div>
  </div>
</template>
<script>
import axios from "@/libs/api.request";
import FileUpload from "_c/file-upload/file-upload.vue";
import JsPDF from "jspdf";

export default {
  name: "tpm-defend-list",
  components: {
    FileUpload,
  },
  data() {
    const validate_add_leveldata = (rule, value, callback) => {
      // 正整数
      if (this.add_leveldata.length > 0) {
        callback();
      } else {
        return callback(new Error(this.$t(930124)));
      }
    };
    const validate_add_machine = (rule, value, callback) => {
      // 正整数
      if (this.machinedata.length > 0) {
        callback();
      } else {
        return callback(new Error(this.$t(100150)));
      }
    };
    const validateTypeMode = (rule, value, callback) => {
      // 正整数
      if (/^[1-9]\d*$/.test(value)) {
        callback();
      } else {
        return callback(new Error(this.$t(930117)));
      }
    };
    const validateValueType = (rule, value, callback) => {
      // 正整数
      if (/^[1-9]\d*$/.test(value)) {
        callback();
      } else {
        return callback(new Error(this.$t(930118)));
      }
    };
    const validateSeverityLevel = (rule, value, callback) => {
      // 正整数
      if (/^[1-9]\d*$/.test(value)) {
        callback();
      } else {
        return callback(new Error(this.$t(930119)));
      }
    };
    const validateUpLimit = (rule, value, callback) => {
      // 上限
      if (this.addObj.value_type === "2") {
        if (/^[1-9]\d*$/.test(value)) {
          if (value < this.addObj.lower_limit) {
            return callback(new Error(this.$t(920101)));
          }
          callback();
        } else {
          return callback(new Error(this.$t(930120)));
        }
      } else {
        callback();
      }
    };
    const validateLowerLimit = (rule, value, callback) => {
      // 下限
      if (this.addObj.value_type === "2") {
        if (/^[1-9]\d*$/.test(value)) {
          callback();
        } else {
          return callback(new Error(this.$t(930121)));
        }
      } else {
        callback();
      }
    };
    const validateUnit = (rule, value, callback) => {
      // 下限
      if (this.addObj.value_type === "2") {
        if (value != "") {
          callback();
        } else {
          return callback(new Error(this.$t(100106)));
        }
      } else {
        callback();
      }
    };
    const validateStandardTime = (rule, value, callback) => {
      // 正整数
      if (/^[1-9]\d*$/.test(value)) {
        callback();
      } else {
        return callback(new Error(this.$t(930122)));
      }
    };
    const validateInstructionCycle = (rule, value, callback) => {
      // 正整数
      if (/^[1-9]\d*$/.test(value) || value === undefined) {
        callback();
      } else {
        return callback(new Error(this.$t(200156)));
      }
    };
    const validateDateType = (rule, value, callback) => {
      // 正整数
      if (/^[1-9]\d*$/.test(value)) {
        callback();
      } else {
        return callback(new Error(this.$t(930123)));
      }
    };
    const validateFirstInstructionTime = (rule, value, callback) => {
      // 正整数
      if (value != "") {
        callback();
      } else {
        return callback(new Error(this.$t(930080)));
      }
    };

    return {
      action:
        this.axios.publicPath +
        "tpm/Upload/Submit?templateName=LevelPlanListSkf",
      transferx: true, // 优化下拉框
      leveldata: [], // 产线层级插件所需数据
      datalevel: [], // 产线层级选中数据
      levelid: 0, // 产线层级选中的section_id
      add_leveldata: [], // 添加产线层级value
      machinedata: [], // 设备层级value
      datamachine: [], // 设备层级
      add_machine: 0, // 添加模态框的设备层级id
      ids: [], // 选择数据的id集合
      morecondition: false,
      filterObj: {
        typeMode: 0,
        valueType: 0,
        import: 0,
        instructionCycle: 0,
        dateRang: 0,
        department: 0,
        user: 0,
      },
      timeRange: "",
      initPageData: {
        departmentList: [],
        userList: [],
        defendTypeList: {
          1: "AM",
          2: "PM",
          3: "CMP",
        },
        projectTypeList: {
          1: this.$t(1118),
          2: this.$t(1119),
        },
        datalist1: [
          {
            value: 1,
            name: this.$t(950331),
          },
          {
            value: 2,
            name: this.$t(950332),
          },
          {
            value: 3,
            name: this.$t(950333),
          },
          {
            value: 4,
            name: this.$t(950335),
          },
          {
            value: 5,
            name: this.$t(950334),
          },
          {
            value: 6,
            name: this.$t(950336),
          },
        ],
        datalist2: [
          {
            value: 10,
            name: this.$t(930077),
          },
          {
            value: 11,
            name: this.$t(800074),
          },
          {
            value: 12,
            name: this.$t(930078),
          },
          {
            value: 13,
            name: this.$t(930079),
          },
        ],
        datalist3: [
          {
            value: 10,
            name: this.$t(930077),
          },
          {
            value: 11,
            name: this.$t(800074),
          },
          {
            value: 12,
            name: this.$t(930078),
          },
          {
            value: 13,
            name: this.$t(930079),
          },
          {
            value: 14,
            name: this.$t(930129),
          },
        ],
        levelList: {
          1: this.$t(950331),
          2: this.$t(950332),
          3: this.$t(950333),
          4: this.$t(950334),
          5: this.$t(950335),
          6: this.$t(950336),
        },
        levelList2: {
          10: this.$t(930077),
          11: this.$t(800074),
          12: this.$t(930078),
          13: this.$t(930079),
        },
        instructionCycleList: {
          1: this.$t(1101),
          7: this.$t(1136),
          14: this.$t(1137),
          30: this.$t(1138),
          60: this.$t(1139),
          90: this.$t(1140),
          180: this.$t(1141),
          365: this.$t(1142),
        },
        dateRangList: {
          1: this.$t(200151),
          2: this.$t(200152),
          3: this.$t(200153),
        },
        failmodeList: {},
      },
      treeContents: [],
      sectionname: "",
      map: {
        value: "machineid",
        label: "name",
        children: "children",
        expand: true,
        selected: false,
      },
      addModal: false, // 添加Modal的开关
      modal1: false,
      loading: true,
      modalTitle: "",
      optType: "add", // add,edit
      addObj: {
        id: 0,
        sectionId: 0, // 设备id 当选中产线时值为0
        planid: 0,
        typemode: 0, // 维护类型
        content: "", // 项目内容
        value_type: 0, // 项目类型
        severity_level: 0, // 项目分类
        unit: "", // 单位
        up_limit: 0, // 上限
        lower_limit: 0, // 下限
        failure_mode: "", // 对应失效模式
        standard_time: 0, // 计划用时
        instruction_cycle: 0, // 执行周期

        datetype: 0, // 执行类型
        datetime1: "", // 执行时间范围
        degree: 0, // 执行次数

        first_instruction_time: "", // 开始执行时间

        work: [], // 指导文件
        remark: "", // 备注
        maintain_tool: "", // 维护工具
        maintain_sparepart: "", // 维护备件
      },
      workUrl: "", // 指导文件预览
      selectedObj: {}, // 选中对象容器
      multipleSelectedObj: [],
      total: 0, // 刀位列表total
      page: 1, // 刀位列表当前页数
      pagesize: 10, // 刀位列表pagesize

      tableData1: [], // 刀位列表数据容器
      tableColumns1: [
        {
          title: "全选",
          align: "center",
          width: 50,
          type: "selection",
        },
        {
          // 列表字段属性集合
          type: "index2",
          title: this.$t(1008),
          key: "xuhao",
          render: (h, params) => {
            return h(
              "span",
              params.index + (this.page - 1) * this.pagesize + 1
            );
          },
          width: 60,
        },
        {
          title: this.$t(200038),
          key: "stas",
          width: 180,
        },
        {
          title: this.$t(200064),
          key: "instruction_time_str",
          width: 150,
        },
        {
          title: this.$t("200058"),
          key: "content",
          width: 250,
        },
        {
          title: this.$t(800191),
          align: "center",
          key: "mode",
        },
        {
          title: this.$t(950330),
          align: "center",
          key: "severity_level",
        },
        {
          title: this.$t(800192) + "(min)",
          align: "center",
          key: "standard_time",
          //   width: 120
        },
        {
          title: this.$t(1015),
          align: "center",
          children: [
            {
              title: this.$t(1118),
              width: 100,
              align: "center",
              key: "valuetype",
            },
            {
              title: this.$t(200059),
              align: "center",
              children: [
                {
                  title: this.$t(200017),
                  align: "center",
                  key: "unit",
                  width: 100,
                },
                {
                  title: this.$t(200059),
                  align: "center",
                  key: "up_limit",
                  width: 100,
                },
                {
                  title: this.$t(200060),
                  align: "center",
                  key: "lower_limit",
                  width: 100,
                },
              ],
            },
          ],
        },
      ],
      tableShow: false,
      tableData3: [], // 结果分析列表
      tableColumns3: [
        {
          // 列表字段属性集合
          type: "index2",
          title: this.$t(1008),
          key: "xuhao",
          render: (h, params) => {
            return h(
              "span",
              params.index + (this.page - 1) * this.pagesize + 1
            );
          },
          width: 90,
        },
        {
          title: this.$t(200053),
          key: "name",
          width: 120,
        },
        {
          title: this.$t(200072),
          key: "actualtime",
          width: 120,
        },
        {
          title: this.$t(900328),
          key: "time",
          width: 120,
        },
        {
          title: this.$t(800081),
          align: "center",
          key: "value",
          width: 120,
        },
        {
          title: this.$t(200054),
          align: "center",
          key: "state_name",
          width: 120,
        },
        {
          title: this.$t(900700),
          align: "center",
          key: "record_code",
          width: 300,
        },
        {
          title: this.$t(900337),
          key: "work_order",
          width: 120,
        },
        {
          title: this.$t("1030"),
          key: "keep_remark",
          width: 120,
        },
        {
          title: this.$t(90000035),
          key: "corepair",
          width: 220,
        },
        {
          title: this.$t(90000036),
          key: "act_standard_time",
          width: 140,
        },
        {
          title: this.$t("900292"), // 图片展示
          align: "center",
          key: "pic",
          width: 120,

          render: (h, params) => {
            if (params.row.imagesurl_arr.length > 0) {
              return h(
                "Poptip",
                {
                  props: {
                    trigger: "hover",
                    title: this.$t("1045"), // 图片
                    placement: "bottom",
                    transfer: true,
                    left: "30%",
                    width: "60px",
                  },
                },
                [
                  h("Tag", this.$t("1045")),
                  h(
                    "div",
                    {
                      slot: "content",
                    },
                    [
                      h(
                        "Carousel",
                        {
                          attrs: {
                            // src:params.row.piclist,
                            // style: 'border-radius: 2px;z-index:9999',
                            autoplay: false,
                            slot: "content",
                            style:
                              "border-radius: 2px;z-index:9999;width:400px;height:400px;",
                          },
                        },
                        params.row.imagesurl_arr.map((item) => {
                          if (item) {
                            return h("CarouselItem", {}, [
                              h("div", {}, [
                                h(
                                  "img",
                                  {
                                    attrs: {
                                      src: item,
                                      style: "width:100%;height:400px;",
                                    },
                                  },
                                  []
                                ),
                              ]),
                            ]);
                          }
                        })
                      ),
                    ]
                  ),
                ]
              );
            } else {
              return h(
                "Poptip",
                {
                  props: {
                    trigger: "hover",
                    title: this.$t("1045"), // 图片
                    placement: "bottom",
                    transfer: true,
                    left: "30%",
                  },
                },
                [
                  h("Tag", this.$t("1045")),
                  h(
                    "div",
                    {
                      slot: "content",
                    },
                    []
                  ),
                ]
              );
            }
          },
        },
        {
          title: this.$t(90000037),
          key: "pshow",
          width: 120,
        },
      ],
      addModalRule:
        this.optType == "add"
          ? {
              add_leveldata: [
                {
                  validator: validate_add_leveldata,
                  required: true,
                  trigger: "blur",
                },
              ],
              add_machine: [
                {
                  validator: validate_add_machine,
                  required: true,
                  trigger: "blur",
                },
              ],
              typemode: [
                {
                  validator: validateTypeMode,
                  required: true,
                  trigger: "blur",
                },
              ],
              value_type: [
                {
                  validator: validateValueType,
                  required: true,
                  trigger: "blur",
                },
              ],
              severity_level: [
                {
                  validator: validateSeverityLevel,
                  required: true,
                  trigger: "blur",
                },
              ],
              unit: [
                {
                  validator: validateUnit,
                  required: true,
                  trigger: "blur",
                },
              ],
              up_limit: [
                {
                  validator: validateUpLimit,
                  required: true,
                  trigger: "blur",
                },
              ],
              lower_limit: [
                {
                  validator: validateLowerLimit,
                  required: true,
                  trigger: "blur",
                },
              ],
              standard_time: [
                {
                  validator: validateStandardTime,
                  required: true,
                  trigger: "blur",
                },
              ],
              instruction_cycle: [
                {
                  validator: validateInstructionCycle,
                  required: true,
                  trigger: "blur",
                },
              ],
              datetype: [
                {
                  validator: validateDateType,
                  required: true,
                  trigger: "blur",
                },
              ],
              content: [
                {
                  trigger: "blur",
                  required: true,
                  message: this.$t(90000038),
                },
              ],
              failure_mode: [
                {
                  trigger: "blur",
                  required: true,
                  message: this.$t(90000039),
                },
              ],
              first_instruction_time: [
                {
                  validator: validateFirstInstructionTime,
                  trigger: "blur",
                  required: true,
                },
              ],
              maintain_tool: [
                {
                  trigger: "blur",
                  required: true,
                  message: this.$t(90000040),
                },
              ],
              maintain_sparepart: [
                {
                  trigger: "blur",
                  required: true,
                  message: this.$t(90000041),
                },
              ],
            }
          : {
              typemode: [
                {
                  validator: validateTypeMode,
                  required: true,
                  trigger: "blur",
                },
              ],
              value_type: [
                {
                  validator: validateValueType,
                  required: true,
                  trigger: "blur",
                },
              ],
              severity_level: [
                {
                  validator: validateSeverityLevel,
                  required: true,
                  trigger: "blur",
                },
              ],
              unit: [
                {
                  validator: validateUnit,
                  required: true,
                  trigger: "blur",
                },
              ],
              up_limit: [
                {
                  validator: validateUpLimit,
                  required: true,
                  trigger: "blur",
                },
              ],
              lower_limit: [
                {
                  validator: validateLowerLimit,
                  required: true,
                  trigger: "blur",
                },
              ],
              standard_time: [
                {
                  validator: validateStandardTime,
                  required: true,
                  trigger: "blur",
                },
              ],
              instruction_cycle: [
                {
                  validator: validateInstructionCycle,
                  required: true,
                  trigger: "blur",
                },
              ],
              datetype: [
                {
                  validator: validateDateType,
                  required: true,
                  trigger: "blur",
                },
              ],
              content: [
                {
                  trigger: "blur",
                  required: true,
                  message: this.$t(90000038),
                },
              ],
              failure_mode: [
                {
                  trigger: "blur",
                  required: true,
                  message: this.$t(90000039),
                },
              ],
              first_instruction_time: [
                {
                  validator: validateFirstInstructionTime,
                  trigger: "blur",
                  required: true,
                },
              ],
              maintain_tool: [
                {
                  trigger: "blur",
                  required: true,
                  message: this.$t(90000040),
                },
              ],
              maintain_sparepart: [
                {
                  trigger: "blur",
                  required: true,
                  message: this.$t(90000041),
                },
              ],
            },

      setExecutorModel: false,
      setExecutorObj: {
        department: 0,
        userList: [],
        users: [],
      },
      changeExecutorModel: false,
      changeExecutorObj: {
        department: 0,
        userList: [],
        user: 0,
        planTimes: [],
        pTimeId: 0,
        planId: 0,
        instruction_time: "",
      },
      tableColumns2: [
        {
          title: this.$t("200053"),
          key: "name",
          width: 100,
        },
        {
          title: this.$t("200064"),
          key: "instruction_time_str",
        },
      ],

      headers: {
        token: localStorage.getItem("token"),
        timestamp: localStorage.getItem("timestamp"),
        nonce: localStorage.getItem("nonce"),
        sign: localStorage.getItem("sign"),
      },
    }; // end return data
  },
  // created ************************************************************************************
  created() {
    this.defaultPageData();
    this.search(1, 10, 0); // 初始化列表
    // this.initAddModal()
    // 格式化表格样式
    this.tableColumns1 = this.$time.initTableTitle(this.tableColumns1);
    this.tableColumns2 = this.$time.initTableTitle(this.tableColumns2);
    this.tableColumns3 = this.$time.initTableTitle(this.tableColumns3);
  },
  // methods ************************************************************************************
  methods: {
    defaultPageData() {
      this.initlevel(); // 初始化产线层级
      this.initDepartmentList();
      // this.inittree()
    },
    handlelevelchange(value) {
      // 添加框 生产线  变化函数
      this.add_leveldata = value;
      this.machinedata = [];
      this.initmachine();
    },
    initmachine() {
      // 根据生产线变化得到设备data
      var params = {
        section: this.add_leveldata[this.add_leveldata.length - 1],
      };
      axios
        .request({
          url: "/config/MachineAttribution/GetAttributionDataBySection",
          params,
          method: "get",
        })
        .then((ret) => {
          if (ret.data.ret === 1) {
            const arr = [];
            this.datamachine = ret.data.data;
            if (ret.data.data.length > 0) {
              arr.push(ret.data.data[0].machine);
              this.machinedata = arr;
            }
          } else {
            this.$Message.error(this.$t(res.data.msg));
          }
        });
    },
    handlemachinechange(value, selectedData) {
      // 设备变化函数
      // this.tableData1 = []
      this.machinedata = value;
      this.add_machine = value[value.length - 1];
      // 加载失效模式
      this.inifailemode();
    },
    inifailemode() {
      var params = {
        machineid: this.add_machine,
      };
      axios
        .request({
          url: "/tpm/FaultType/GetFaultTypeListByMachineId",
          params,
          method: "get",
        })
        .then((ret) => {
          if (ret.data.ret === 1) {
            this.initPageData.failmodeList = ret.data.data;
          } else {
            this.$Message.error(this.$t(res.data.msg));
          }
        });
    },
    initDepartmentList() {
      axios
        .request({
          url: "/config/ProjectSection/GetSections",
          method: "get",
        })
        .then((res) => {
          if (res.data.data != undefined && res.data.data.length > 0) {
            this.initPageData.departmentList = res.data.data;
          } else {
            this.initPageData.departmentList = [];
          }
        });
    },
    changeDepartment(item, opt) {
      if (item === undefined) {
        return false;
      }
      if (parseInt(item.value) > 0) {
        let params = {
          sectionid: item.value,
        };
        axios
          .request({
            url: "/config/ProjectUser/getUserListsBySectionId",
            params,
            method: "get",
          })
          .then((res) => {
            if (res.data.data != undefined && res.data.data.length > 0) {
              if (opt === "setExecutor") {
                this.setExecutorObj.userList = res.data.data;
              } else if (opt === "changeExecutor") {
                this.changeExecutorObj.userList = res.data.data;
              } else {
                this.initPageData.userList = res.data.data;
              }
            } else {
              if (opt === "setExecutor") {
                this.setExecutorObj.userList = [];
              } else if (opt === "changeExecutor") {
                this.changeExecutorObj.userList = [];
              } else {
                this.initPageData.userList = [];
              }
            }
            this.filterObj.user = 0;
            this.setExecutorObj.users = [];
            this.changeExecutorObj.user = 0;
          });
      }
    },
    changeMultipleSelection(val) {
      this.multipleSelectedObj = val;
      var arr = [];
      val.forEach((item) => {
        arr.push(item.id);
      });
      this.ids = arr;
    },
    initlevel() {
      // 初始化产线层级
      axios
        .request({
          url: "config/ProjectLevelSection/GetSectionTree",
          method: "get",
        })
        .then((res) => {
          const arr = [];
          if (res.data.data.length > 0) {
            this.datalevel = res.data.data;
            arr.push(res.data.data[0].value);
            this.leveldata = arr;
          }
        });
    },
    changelevel(data) {
      // 切换产线层级，触发事件
      // 请求数据
      this.levelid = data[data.length - 1];
    },
    changePage(value) {
      // 换页事件
      this.page = value;
      this.tableData1 = this.search(this.page, this.pagesize, 0);
    },
    handleRowChange(currentRow, oldCurrentRow) {
      // 选中一条数据，触发事件
      this.selectedObj = currentRow;
      this.tableShow = true;
      // 获取该条维修结果内容
      var data = {
        id: this.selectedObj.id,
        start_time: this.timeRange[0],
        end_time: this.timeRange[1],
      };
      axios
        .request({
          url: "/tpm/PlanResult/GetlistPlanOneResultItem",
          data,
          method: "post",
        })
        .then((res) => {
          const arr = [];
          if (res.data.data.length > 0) {
            this.mockTableData3(res.data.data);
          } else {
            this.tableData3 = [];
          }
        });
    },
    handlePageSize(value) {
      // 切换分页页数，触发事件
      this.pagesize = value;
      this.tableData1 = this.search(this.page, this.pagesize, 0);
    },
    search(page, pagesize, toexcel) {
      // 点击查询，触发事件
      var data = {
        mode: this.filterObj.typeMode,
        start_time: this.timeRange[0],
        end_time: this.timeRange[1],
        sectionid: this.levelid,
        severity_level: this.filterObj.severity_level,
        page: page,
        pagesize: pagesize,
        toexcel: toexcel,
      };
      axios
        .request({
          url: "/tpm/PlanResult/GetlistPlanResultItem",
          data,
          method: "post",
        })
        .then((res) => {
          if (res.data.ret === 1) {
            if (toexcel === 0) {
              this.total = res.data.total;
              this.mockTableData1(res.data.data);
              this.page = res.data.page;
              this.tableData3 = [];
            } else {
              window.location.href = axios.publicPath + "" + res.data.data;
            }
          } else {
            this.$Message.error(this.$t(res.data.msg));
            this.tableData1 = [];
            this.tableData3 = [];
          }
        });
    },
    mockTableData1(tablelist) {
      // 转换数据，并赋值给列表数据容器tableData1
      let data = [];
      tablelist.forEach((item) => {
        if (item) {
          data.push({
            id: item.id,
            mode: item.mode === 1 ? "AM" : item.mode === 2 ? "PM" : "CMP",
            planid: item.planid,
            content: item.content,
            stas: item.machinetitle,
            valuetype: parseInt(item.value_type) === 1 ? "√" : "",
            unit: item.unit,
            up_limit: item.up_limit,
            lower_limit: item.lower_limit,
            severity_level:
              item.mode === 1
                ? this.initPageData.levelList[item.severity_level]
                : this.initPageData.levelList2[item.severity_level],
            work_instruction_name: item.work_instruction_name,
            failure_mode: item.failure_mode,
            standard_time: item.standard_time,
            date_rang:
              parseInt(item.date_rang) === 1
                ? this.$t("200151")
                : parseInt(item.date_rang) === 2
                ? item.date_rang_str + this.$t("200162")
                : this.$t("200162") + item.date_rang_str + this.$t("200163"),
            instruction_cycle1: this.initPageData.instructionCycleList[
              parseInt(item.instruction_cycle)
            ],
            remark: item.remark,
            name: item.name,
            first_instruction_time: item.first_instruction_time,
            workUrl: axios.publicPath + item.work_instruction_url,
            instruction_time_str: item.instruction_time_str,
          });
        }
      });
      this.tableData1 = data;
    },
    mockTableData3(tablelist) {
      let data = [];
      var imagesurl_arr = [];
      var imagesurl = [];
      tablelist.forEach((item) => {
        if (item) {
          imagesurl = [];
          if (item.imgurl == "") {
            imagesurl = [];
          } else {
            imagesurl = item.imgurl.split(",");
          }
          // imagesurl_arr.push(imagesurl);
          data.push({
            id: item.id,
            planid: item.planid,
            name: item.name,
            time: item.time,
            act_standard_time: item.act_standard_time,
            record_code: item.record_code,
            value: item.value,
            corepair: item.corepair,
            pshow: imagesurl.length + this.$t("800129"),
            imagesurl_arr: imagesurl,
            work_order: item.work_order,
            state_name: item.state_name,
            keep_remark: item.keep_remark,
            actualtime: item.actualtime,
          });
        }
      });
      this.tableData3 = data;
    },
    //* *****************************************************************************************
    // ok() {
    //   this.modal1 = false;
    // },
    // outFileUploadChild(files) {
    //   // 接受文件上传的数据
    //   this.addObj.work = files;
    // },
    downpdf() {
      var target = document.querySelector("#demo");
      html2canvas(target, {
        allowTaint: true,
        dpi: 172,
        background: "#fff",
      }).then(function (canvas) {
        var contentWidth = canvas.width;
        var contentHeight = canvas.height;

        // 一页pdf显示html页面生成的canvas高度;
        var pageHeight = (contentWidth / 592.28) * 841.89;
        // 未生成pdf的html页面高度
        var leftHeight = contentHeight;
        // pdf页面偏移
        var position = 0;
        // html页面生成的canvas在pdf中图片的宽高（a4纸的尺寸[595.28,841.89]）
        var imgWidth = 595.28;
        var imgHeight = (592.28 / contentWidth) * contentHeight;

        var pageData = canvas.toDataURL("image/jpeg", 1.0);
        var pdf = new JsPDF("", "pt", "a4");

        // 有两个高度需要区分，一个是html页面的实际高度，和生成pdf的页面高度(841.89)
        // 当内容未超过pdf一页显示的范围，无需分页
        if (leftHeight < pageHeight) {
          pdf.addImage(pageData, "JPEG", 0, 0, imgWidth, imgHeight);
        } else {
          while (leftHeight > 0) {
            pdf.addImage(pageData, "JPEG", 0, position, imgWidth, imgHeight);
            leftHeight -= pageHeight;
            position -= 841.89;
            // 避免添加空白页
            if (leftHeight > 0) {
              pdf.addPage();
            }
          }
        }
        pdf.save("content.pdf");
      });
    },

    handleChangeExecutorRowChange(row) {
      this.changeExecutorObj.department = row.sectionid;
      this.changeExecutorObj.instruction_time = row.instruction_time_str;
      this.changeExecutorObj.pTimeId = row.id;
      this.changeExecutorObj.planId = row.planid;
      axios
        .request({
          url:
            "/config/ProjectUser/getUserListsBySectionId?sectionid=" +
            row.sectionid,
          method: "get",
        })
        .then((res) => {
          if (res.data.data != undefined && res.data.data.length > 0) {
            this.changeExecutorObj.userList = res.data.data;
            this.changeExecutorObj.user = row.mid;
          } else {
            this.changeExecutorObj.userList = [];
            this.changeExecutorObj.user = 0;
          }
        });
    },

    changeDate(dateA) {
      var dateee = new Date(dateA).toJSON();
      var date = new Date(+new Date(dateee) + 8 * 3600 * 1000)
        .toLocaleDateString()
        .replace(/T/g, " ")
        .replace(/\.[\d]{3}Z/, "")
        .replace(/\//g, "-");
      return date;
    },
    clearLoading() {
      this.loading = false;
      this.$nextTick(() => {
        this.loading = true;
      });
    },
    changecondition() {
      this.morecondition = !this.morecondition;
    },
    changetypemode() {
      if (this.filterObj.typemode === "2" || this.filterObj.typemode === "3") {
        this.initPageData.levelList = {
          10: this.$t(930077),
          11: this.$t(800074),
          12: this.$t(930078),
          13: this.$t(930079),
        };
      } else {
        this.initPageData.levelList = {
          1: this.$t(950331),
          2: this.$t(950332),
          3: this.$t(950333),
          4: this.$t(950334),
          5: this.$t(950335),
          6: this.$t(950336),
        };
      }
    },
  },
};
</script>
<style lang="less">
.treeSelect {
  span {
    background: #2d8cf1;
    color: #fff;
    margin: 0;
    padding: 0 4px;
    border-radius: 3px;
    cursor: pointer;
    vertical-align: top;
  }
}
</style>
