<!--
    上传文件的弹窗
-->

<template>
  <div v-if="dialog.addDevice">
    <el-dialog
      :title="title"
      :center="isCenter"
      :visible.sync="dialog.addDevice"
      :width="width"
      :fullscreen="isFull"
    >
      <div id="addDevice">
        <div v-show="!isCenter">
          <my-cascader ref="cascader" @change="changeCader"></my-cascader>
        </div>
        <div v-show="isCenter">
          <div>
            <div class="hanson-bar">
              <span
                style="width:20px;float:left;display:block;height:20px;cursor: pointer;"
                @click="changeAssetsFalg"
              >
                <i class="iconfont icon-xiaolian"></i>
              </span>
              <span>当前选择的设备族：{{deviceType.facility}}</span>
              <!-- 增加 -->
              <div style="width:360px;float:right;overflow:hidden;">
                <span>增加</span>
                <el-input v-model="addNum" style="width:50px;" placeholder="增加个数" size="small"></el-input>
                <span>个{{deviceType.facility}}</span>
                <el-button size="small" @click="addMain">增加</el-button>
              </div>
              <!-- switch -->
              <div
                style="width:200px;color:gray;float:right;padding-top:7px;font-size:10px;"
              >是否隐藏自动填充信息点:
                <el-tooltip :content="'' + (isWatch ? '隐藏' : '不隐藏')" placement="top">
                  <el-switch
                    @change="changeWatch"
                    v-model="isWatch"
                    active-color="#13ce66"
                    inactive-color="gray"
                  ></el-switch>
                </el-tooltip>
              </div>
            </div>
            <div v-show="main && main.length" id="myHandson" ref="myHandson"></div>
          </div>
        </div>
      </div>
      <div v-show="!isCenter" slot="footer" class="dialog-footer">
        <el-button>取 消</el-button>
        <el-button type="primary" @click="step(1)">下 一 步</el-button>
      </div>
      <span v-show="isCenter" slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addDevice">创建设备</el-button>
      </span>
    </el-dialog>
    <qrcode :dialog="myDialog" :addBody="true" ref="qrcode"></qrcode>
    <firm
      :mess="{deviceId : deviceType.assetType}"
      ref="firm"
      @changeFirm="firmChange"
      :dialog="myDialog"
    ></firm>
    <supply-dialog @change="supplyChange" :id="id" ref="supply" :dialog="myDialog"></supply-dialog>
    <supplier-dialog ref="supplier" @changeSupplier="supplierChange" :dialog="myDialog"></supplier-dialog>
    <guarantee-dialog ref="guarantee" :id="id" @change="guaranteeChange" :dialog="myDialog"></guarantee-dialog>
    <upload-files-dialog
      ref="upload"
      @changeFile="fileChange"
      :keysArr="filesArr"
      :dialog="myDialog"
    ></upload-files-dialog>
    <upload-img-dialog @changeFile="imgChange" :keysArr="imgsArr" :dialog="myDialog"></upload-img-dialog>
    <maintainer-dialog @changeMaintainer="changeMaintainer" ref="maintainer" :dialog="myDialog"></maintainer-dialog>
    <insurer-dialog @changeInsurer="changeInsurer" ref="insurer" :dialog="myDialog"></insurer-dialog>
    <pic-dialog :dialog="myDialog" :keysArr="picsArr" @change="changePics"></pic-dialog>
    <system-type
      type="check"
      :infos="''"
      :list="systemList"
      @change="setSystem"
      :graphyId="graphyId"
      :dialog="dialog"
    ></system-type>
  </div>
</template>
<script>

import myCascader from "@/components/cascader";
import hansonTable from "@/components/dialogHanson/addDevice";
import { getTableHeader, getSpaceFloor, getEquipmentFamily, BatchCreateSome, createAssetsList, createSystem, EqToAssets, createRelation } from "@/api/request";
import tools from "@/utils/scan/tools";
import showTools from "@/utils/handsontable/notShow"
import text from "@/utils/handsontable/mainText"

import qrcode from "@/components/ledger/lib/qrcode";
import firm from "@/components/dialogs/list/firm";
import supplyDialog from "@/components/dialogs/list/supplyDialog";
import supplierDialog from "@/components/dialogs/list/supplierDialog";
import maintainerDialog from "@/components/dialogs/list/maintainerDialog";
import insurerDialog from "@/components/dialogs/list/insurerDialog";
import guaranteeDialog from "@/components/dialogs/list/guaranteeDialog";
import uploadFilesDialog from "@/components/dialogs/list/filesDialog";
import uploadImgDialog from "@/components/dialogs/list/uploadImgDialog";
import picDialog from "@/components/dialogs/list/picDialog"
import systemType from "@/components/dialogs/list/systemType"


//下拉插件
import "@/assets/js/chosen.jquery.min";
import "@/assets/js/handsontable-chosen-editor";

import uuid from "@/utils/uuid";
import {mapGetters, mapActions} from "vuex";
let table = function (num) {
  let main = []
  for (let i = 0; i < num; i++) {
    main.push({ Checked: 1 })
  }
  return main
}
export default {
  components: {
    myCascader,
    hansonTable,
    qrcode, //二维码页面
    firm, //
    supplyDialog,
    supplierDialog,
    guaranteeDialog,
    uploadFilesDialog,
    maintainerDialog,
    insurerDialog,
    uploadImgDialog,
    picDialog,
    systemType
  },
  props: {
    dialog: {
      type: Object,
      default: function () {
        return {
          addDevice: false
        };
      }
    },
    graphyId: {
      type: String
    },
    assetType: {
      type: Array,
      default: function () {
        return []
      }
    }
  },
  computed: {
    ...mapGetters("layout", ["projectId", "secret", "userId"]),
  },
  data() {
    return {
      width: "30%",
      isFull: false,//dialog是否为全屏
      title: "",
      isCenter: false,
      deviceType: {},
      main: table(1),
      mess: {},
      headers: null,
      page: {
        size: 50,
        sizes: [10, 30, 50, 100, 150, 200],
        total: 400,
        currentPage: 1
      },
      myDialog: {
        qrcode: false, //二维码弹窗
        firm: false, //厂商弹窗
        supply: false, //选择供应合同
        supplier: false, //供应商选择
        guarantee: false, //选择保单
        maintainer: false, //选择维修商
        insurer: false, //选择保险公司
        uploadFiles: false,//上传文件
        uploadImgs: false,//上传单个图片
        pic: false, //多个图片
        systemType: false,
      },
      addNum: 1,//增加个数
      row: null,//被修改的row
      filesArr: [],//保存临时的文件key
      messKey: null,
      imgsArr: [],//临时保存的图片key数组
      picsArr: [],//临时设备图片keys数组
      param: {
        ProjId: "",
        secret: ""
      },
      id: 0,
      systemList: null,
      isWatch: true,
      EquipmentList: [],//资产族
      floorFalg: false,
    };
  },
  created() {
    this.param.ProjId = this.projectId
    this.param.secret = this.secret
    this.getFloorData()
    this.getEquipmentFamilyList()
  },
  mounted() { },
  methods: {
    changeAssetsFalg() {
      if (this.floorFalg) {
        this.floorFalg = false
      } else {
        this.$confirm('<p>维护设备所在建筑楼层后，对后续数据影响较大，如业务空间中的所在关系or其他?暂未梳理明白……</p><p>后续要修改设备所属建筑楼层，只能通过模型中的待建模清单操作</p>', '提示', {
          dangerouslyUseHTMLString: true,
          confirmButtonText: '就要维护设备所属建筑楼层',
          cancelButtonText: '暂时不搞了'
        }).then(_ => {
          this.floorFalg = true
          this.getData()
        }).catch(_ => {
          this.$message("取消")
        })
      }
    },
    //获取资产族
    getEquipmentFamilyList() {
      getEquipmentFamily(res => {
        this.EquipmentList = res.Content
        console.log(this.EquipmentList, "EquipmentList")
      })
    },
    //设置默认选中数据
    setValue() {
      this.$nextTick(_ => {
        this.$refs.cascader.setValue(this.assetType)
      })
    },
    //获取楼层数据
    getFloorData() {
      getSpaceFloor(this.param).then(res => {
        if (res.data.Result == 'success') {
          let data = this.changeArr(res.data.Content), floorData = []
          data.map(item => {
            if (!!item.children) {
              item.children.unshift({
                value: '',
                label: "未明确楼层"
              })
            }
            return item
          })
          data.map(item => {
            if (!!item.children && item.children.length) {
              item.children.map(child => {
                floorData.push({
                  Code: item.value + "-" + child.value,
                  Name: item.label + "-" + child.label
                })
              })
            }
          })
          this.floorData = floorData
          console.log(this.floorData)
        } else {
          this.$message.error(res.data.ResultMsg)
        }
      }).catch(() => {
        this.$message.error("请求出错")
      })
    },
    //将数组转换成optiosn格式
    changeArr(arr) {
      return arr.map(item => {
        if (item.floors && item.floors.length) {
          return {
            value: item.id,
            label: item.infos.BuildLocalName,
            children: item.floors.map(i => {
              return {
                value: i.id,
                label: i.infos.FloorLocalName || "未知",
              }
            })
          }
        } else {
          return {
            value: item.id,
            label: item.infos.BuildLocalName,
            children: null,
            isChilren: 1,
          }
        }
      })
    },
    //增加个数
    addMain() {
      for (let i = 0; i < this.addNum; i++) {
        this.main.push({
          Checked: 1
        })
      }
      if (!!this.hot) {
        this.hot.destroy()
      }
      this.initHot()
    },

    //下一步
    step(val) {
      if (!!val) {
        if (!!!this.deviceType.code) {
          this.$message("请选择设备类别")
          return
        }
        this.isFull = true
        this.isCenter = true
        this.title = "添加设备"
        this.getData()
      } else {
        this.isFull = false
        this.isCenter = false
      }
    },
    //修改设备类型
    changeCader(val) {
      this.deviceType = val
      this.EquipmentList.map(item => {
        if (!!item.content && item.content.length) {
          item.content.map(child => {
            if (child.code == this.deviceType.code) {
              this.deviceType.assetType = item.code
              this.deviceType.assetName = item.name
            }
          })
        }
      })
    },
    //获取header的mess
    getHeader(mess) {
      this.mess = mess;
    },

    //获取表头
    getData() {
      this.main = table(1)
      getTableHeader({
        code: this.deviceType.code,
        ProjId: this.projectId
      }).then(res => {
        this.headers = res.data.Content;
        if (!!this.hot) {
          this.hot.destroy();
          this.hot = null;
        }
        let data = tools.getItem(this.deviceType.code)
        if (!!data) {
          this.main = data
        }
        this.initHot();
      });
    },

    //添加设备
    addDevice() {
      let data = []
      let copyData = tools.copyArr(this.main)
      let falg = false
      copyData.map(item => {
        if (!!item.infos || !!item.relation) {
          if (item.infos.EquipLocalName) {
            item.equipment_category = this.deviceType.code
            data.push(item)
          } else {
            falg = true
          }
        }
      })
      if (falg) {
        this.$message.info("存在设备的本地名称为空，请检查")
        return
      }
      data = data.map(item => {
        if (!!item.infos) {
          for (let key in item.infos) {
            item.infos[key] = [{ "value": item.infos[key] != "" ? item.infos[key] : null }]
          }
        }
        if (!!item.flowBuild) {
          item.building_id = item.flowBuild.split("-")[0]
          item.floor_id = item.flowBuild.split("-")[1] || null
          //不存在floor字段去除floor
          if (!item.floor_id) {
            delete item.floor_id
          }
          delete item.flowBuild
        }
        return item
      })
      if (data.length) {
        this.createJson(data)
      } else {
        this.$message.info("信息点不能为空")
      }
    },

    //创建设备接口
    async createJson(data) {
      let param = {
        data: { criterias: data },
        ProjId: this.projectId,
        secret: this.secret
      }
      param.data.criterias.map(item => {
        if (!!item.infos.DPSupplierID && !!item.infos.DPSupplierID[0].value) {
          item.infos.DPSupplierID[0].value = item.infos.DPSupplierID[0].value.split("-")[0]
        }
        if (!!item.infos.DPManufacturerID && !!item.infos.DPManufacturerID[0].value) {
          item.infos.DPManufacturerID[0].value = item.infos.DPManufacturerID[0].value.split("-")[0]
        }
        if (!!item.infos.DPInsurerID && !!item.infos.DPInsurerID[0].value) {
          item.infos.DPInsurerID[0].value = item.infos.DPInsurerID[0].value.split("-")[0]
        }
        if (!!item.infos.DPMaintainerID && !!item.infos.DPMaintainerID[0].value) {
          item.infos.DPMaintainerID[0].value = item.infos.DPMaintainerID[0].value.split("-")[0]
        }
        if (!!item.infos.DPSpecificationID && !!item.infos.DPSpecificationID[0].value) {
          item.infos.DPSpecificationID[0].value = item.infos.DPSpecificationID[0].value.split("-")[0]
        }
      })
      BatchCreateSome(param, res => {
        console.log(res)
        this.createPost(param.data.criterias, res.Content)
        this.dialog.addDevice = false
        console.log(this.deviceType)
        this.$emit("close", this.deviceType)
        tools.removeItem(this.deviceType.code)
      })
    },

    //c创建请求
    async createPost(param, data) {
      console.log(param, data)
      let pushParam = {
        data: { criterias: [] },
        ProjId: this.projectId,
        secret: this.secret
      }
      let list = []
      data.map((item, index) => {
        //linkSystem存在且长度不为0
        if (!!param[index].linkSystem && param[index].linkSystem.length) {
          param[index].linkSystem.map(child => {
            let list = {
              "from_id": child.id, //必填，object id
              "to_id": item.id, //必填，object id
              "graph_id": this.graphyId,                     //必填，图实例id
              "rel_type": "1",
            }
            pushParam.data.criterias.push(list)
          })
        }

        if (!!param[index].Checked) {
          param[index].id = item.id
          list.push(param[index])
        }
      })
      await this.EqCreateAs(list)
      await this.relationCreate(pushParam)
    },

    //根据设备id创建资产
    async EqCreateAs(list) {
      let param = {
        FmList: [],
        ProjId: this.projectId,
        UserId: this.userId
      }
      list.map(item => {
        param.FmList.push({
          EquipmentId: item.id,
          FmId: "Pe" + uuid(32, 16),
          FmName: item.EquipLocalName || "",
          Family: this.deviceType.assetType,
          FamilyName: this.deviceType.assetName,
          ProjId: this.projectId,
          Inofs: {
            DPManufacturerID: item.infos.DPManufacturerID ? item.infos.DPManufacturerID[0].value : null,
            DPSpecificationID: item.infos.DPSpecificationID ? item.infos.DPSpecificationID[0].value : null,
            DPSupplierID: item.infos.DPSupplierID ? item.infos.DPSupplierID[0].value : null,
            DPMaintainerID: item.infos.DPMaintainerID ? item.infos.DPMaintainerID[0].value : null,
            DPInsurerID: item.infos.DPInsurerID ? item.infos.DPInsurerID[0].value : null,
          }
        })
      })
      await createAssetsList(param, res => {
        return true
      })
    },

    //创建关系
    async relationCreate(param) {
      await createRelation(param).then(res => {
        if (res.data.Result == "success") {
          return true
        } else {
          this.$message.error("请求失败：" + res.data.ResultMsg)
        }
      }).catch(_ => {
        this.$message.error("请求失败")
      })
    },

    changeWatch() {
      this.getData()
    },

    /**
     * 表头文案处理函数
     * @param list header数组数据
     *
     * @return 处理好的文案
     */
    delHeader(list) {
      let arr = tools.copyArr(list)
      // 如果不是只读状态，添加四大厂商选择
      if (!this.onlyRead) {
        arr = showTools.arrToArr(arr)
      }
      let readArr = ["A1", "A2", "B1", "C5", "D1", "D2", "X", "L", "L1", "L2", "F1", "F2", "M"]
      readArr.push("Own")
      let data = showTools.changeHeader(arr, readArr, false, this.isWatch, true)
      data.unshift("同时创建资产", "所属系统实例");
      if (this.floorFalg) {
        data.splice(1, 0, "所属建筑楼层")
      }
      return data;
    },

    /**
     * 表头数据处理函数
     * @param list header数组数据
     *
     * @return 处理好的数据格式
     */
    getType(list) {
      let arr = tools.copyArr(list)
      if (!this.onlyRead) {
        arr = showTools.arrToArr(arr)
      }
      let data = showTools.showTypes(arr, this.onlyRead, this.isWatch, true)
      data.unshift(
        {
          type: "checkbox",
          checkedTemplate: 1,
          uncheckedTemplate: 0,
          data: "Checked",
          label: {
            position: "after",
          }
        },
        {
          data: "linkSystem",
          renderer: text.systemList,
          readOnly: true
        }
      );
      if (this.floorFalg) {
        data.splice(1, 0, {
          data: "flowBuild",
          renderer: tools.customDropdownRenderer,
          editor: "chosen",
          chosenOptions: {
            // multiple: true,//多选
            data: this.floorData
          }
        })
      }
      return data;
    },
    //初始化插件
    initHot() {
      var container = document.getElementById("myHandson");
      let winHeight = document.documentElement.clientHeight;
      console.log(this.delHeader(this.headers), this.getType(this.headers))
      this.hot = new Handsontable(container, {
        data: this.main,
        colHeaders: this.delHeader(this.headers), //表头文案
        columns: this.getType(this.headers), //数据显示格式
        filters: true,
        maxRows: this.main.length,
        rowHeaders: true,
        height: winHeight - 100 - 50 - 60,
        columnSorting: true, //添加排序
        sortIndicator: true, //添加排序
        renderAllRows: true,
        autoColumnSize: true,
        language: "zh-CN",
        manualColumnResize: true,
        manualColumnMove: true,
        dropdownMenu: [
          "filter_by_condition",
          "filter_by_value",
          "filter_action_bar"
        ],
        contextMenu: this.onlyRead ? false :{
          items: {
            remove_row: {
              name: "删除该业务空间"
            }
          }
        },
        filters: true,
        afterFilter: this.trimmedRows,
        // 事件
        afterChange: this.tdChange, //修改后
        afterFilter: this.trimmedRows, //排序前
        beforeRemoveRow: this.romoveFm, //右键删除
        afterOnCellMouseDown: this.eventClick //鼠标点击
        // afterOnCellMouseUp: this.eventClick
      });
      let pro = document.getElementById("hot-display-license-info");
      if (!!pro) {
        pro.parentNode.removeChild(pro);
      }
      this.isLoading = false;
    },

    tdChange() {
      tools.setItem(this.deviceType.code, this.main)
    },

    //表格中的点击
    eventClick(el, rowArr) {
      console.log(el, rowArr)
      //点击的是表头
      if (rowArr.row < 0) {
        return;
      }
      let filter = this.filtersArr;
      //被筛选过后的数组
      let trimmedArr = this.trimmedRows();
      //是否启用了排序
      let isSort = this.hot.getPlugin("columnSorting").isSorted();
      if (trimmedArr.length && isSort) {
        let sortArr = this.myHotArr.getPlugin("columnSorting").rowsMapper
          .__arrayMap;
        let infos = this.main[trimmedArr[sortArr[rowArr.row]]];
        this.getInfors(infos, sortArr[rowArr.row]);
      } else if (isSort) {
        //排序后的数组
        let sortArr = this.hot.getPlugin("columnSorting").rowsMapper.__arrayMap;
        let infos = this.main[sortArr[rowArr.row]];
        this.getInfors(infos, sortArr[rowArr.row]);
      } else if (trimmedArr.length) {
        let infos = this.main[trimmedArr[rowArr.row]];
        this.getInfors(infos, trimmedArr[rowArr.row]);
      } else {
        let infos = this.main[rowArr.row];
        this.getInfors(infos, rowArr);
      }
    },

    getInfors(infos, row) {
      //其他的开始判断
      let val = this.hot.colToProp(row.col);

      if (val == "linkSystem") {
        this.systemList = this.main[row.row].linkSystem || []
        this.dialog.systemType = true
      }

      //   if (val == "infos.Brand" || val == "infos.Specification") {
      //     this.myDialog.firm = true;
      //   }
      if (val == "infos.DPManufacturerID") {
        this.myDialog.firm = true;
      }

      if (val == "infos.SupplierContractID") {
        let falg = null
        if (!!this.main[row.row].infos) {
          if (!!this.main[row.row].infos.DPSupplierID) {
            falg = this.main[row.row].infos.DPSupplierID.split("-")[0]
          }
        }
        if (!!falg) {
          this.id = falg
          this.myDialog.supply = true;
        } else {
          this.$message("请先选择供应商")
        }
      }

      if (val == "infos.InsuranceNum") {
        //选择保单
        let falg = null
        if (!!this.main[row.row].infos) {
          if (!!this.main[row.row].infos.DPInsurerID) {
            falg = this.main[row.row].infos.DPInsurerID.split("-")[0]
          }
        }
        if (!!falg) {
          this.id = falg
          this.myDialog.guarantee = true;
        } else {
          this.$message("请先选择保险商")
        }
      }

      if (val == "infos.InsuranceFile" || val == "infos.Archive") {
        this.filesArr = this.main[row.row].infos ? (this.main[row.row].infos[val.split(".")[1]] ? [this.main[row.row].infos[val.split(".")[1]]] : []) : []
        this.myDialog.uploadFiles = true
      }

      if (
        val == "infos.InstallPic" ||
        val == "infos.InstallDrawing" ||
        val == "infos.Nameplate" ||
        val == "infos.Drawing"
      ) {
        if (val == "infos.Nameplate") {
          this.imgsArr = this.main[row.row].infos ? (this.main[row.row].infos[val.split(".")[1]] ? this.main[row.row].infos[val.split(".")[1]] : []) : []
        } else {
          this.imgsArr = this.main[row.row].infos ? (this.main[row.row].infos[val.split(".")[1]] ? this.main[row.row].infos[val.split(".")[1]] : []) : []
        }
        this.myDialog.uploadImgs = true
      }

      if (val == "infos.Pic") {
        // console.log(this.main[row.row].infos[val.split(".")[1]], "this.main[row.row].infos[val.split")
        this.picsArr = this.main[row.row].infos ? (this.main[row.row].infos[val.split(".")[1]] ? this.main[row.row].infos[val.split(".")[1]] : []) : []
        this.myDialog.pic = true
      }

      if (
        val == "infos.DPSupplierID"
      ) {
        if (!!this.main[row.row].Checked) {
          this.myDialog.supplier = true;
        } else {
          this.$message("该信息点需勾选同步创建资产方可编辑")
        }
      }

      if (val == "infos.DPMaintainerID"
      ) {
        if (!!this.main[row.row].Checked) {
          this.myDialog.maintainer = true;
        } else {
          this.$message("该信息点需勾选同步创建资产方可编辑")
        }
      }

      if (
        val == "infos.DPInsurerID"

      ) {
        if (!!this.main[row.row].Checked) {
          this.myDialog.insurer = true;
        } else {

          this.$message("该信息点需勾选同步创建资产方可编辑")
        }
      }
      this.row = row.row
      this.messKey = val
      console.log(val, row);
    },

    //获取被筛选掉的行号
    trimmedRows() {
      // var plugin = hot.getPlugin('trimRows').trimmedRows;//获取被筛选掉的行号
      var plugin = this.hot.getPlugin("trimRows").trimmedRows;
      let dataLength = this.main.length;
      let dataArr = new Array();
      for (let i = 0; i < dataLength; i++) {
        dataArr.push(i);
      }
      if (plugin.length <= 0) {
        dataArr = undefined;
      } else {
        dataArr = this.array_diff(dataArr, plugin);
      }
      return dataArr || [];
      // var DataArray = new Array();

      // for (var i = 0; i < plugin.length; i++) {
      //     // 通过行号获取数据
      //     DataArray.push(this.hot.getSourceDataAtRow(plugin[i]));
      // }
    },

    //判断是否为空行
    isEmptyRow(instance, row) {
      var rowData = instance.countRows();
      for (var i = 0, ilen = rowData.length; i < ilen; i++) {
        if (rowData[i] !== null) {
          return false;
        }
      }
      return true;
    },
    //选择型号修改
    firmChange(data) {
      for (let key in data) {
        // this.utilToKey(key, "brand", data, "Brand")
        // this.utilToKey(key, "name", data, "Specification")
        // this.utilToKey(key, "venderName", data, "Manufacturer")
        if (key == "venderId") {
          data[key] = data[key] + "-" + data.name + "/" + data.brand
          this.utilToKey(key, "venderId", data, "DPManufacturerID")
        }
        this.utilToKey(key, "specificationId", data, "DPSpecificationID")
      }
      tools.setItem(this.deviceType.code, this.main)
    },

    //供应商合同
    supplyChange(data) {
      let changeData = { id: data }
      this.utilToKey("id", "id", changeData, "SupplierContractID")
      tools.setItem(this.deviceType.code, this.main)
    },

    //保险合同
    guaranteeChange(data) {
      console.log("保险合同发生改变", data)
      for (let key in data) {
        this.utilToKey(key, "insuranceNo", data, "InsuranceNum")
        this.utilToKey(key, "contractFile", data, "InsuranceFile")
      }
      tools.setItem(this.deviceType.code, this.main)
    },

    //如果选择供应商之后
    supplierChange(data) {
      console.log("供应商", data)
      for (let key in data) {
        // this.utilToKey(key, "name", data, "Supplier")
        // this.utilToKey(key, "email", data, "SupplierEmail")
        // this.utilToKey(key, "website", data, "SupplierWeb")
        // this.utilToKey(key, "phone", data, "SupplierPhone")
        // this.utilToKey(key, "fox", data, "SupplierFax")
        // this.utilToKey(key, "man", data, "SupplierContactor")
        if (key == "venderId") {
          data[key] = data[key] + "-" + data.name
          console.log(data)
          this.utilToKey(key, "venderId", data, "DPSupplierID")
        }
      }
      tools.setItem(this.deviceType.code, this.main)
    },

    //保险商变更
    changeInsurer(data) {
      for (let key in data) {
        // this.utilToKey(key, "name", data, "Insurer")
        // this.utilToKey(key, "email", data, "InsurerEmail")
        // this.utilToKey(key, "website", data, "InsurerWeb")
        // this.utilToKey(key, "phone", data, "InsurerFax")
        // this.utilToKey(key, "man", data, "InsurerContactor")
        if (key == "venderId") {
          data[key] = data[key] + "-" + data.name
          this.utilToKey(key, "venderId", data, "DPInsurerID")
        }
      }
      tools.setItem(this.deviceType.code, this.main)
    },

    //维修商变更
    changeMaintainer(data) {
      console.log(data, "data")
      for (let key in data) {
        // this.utilToKey(key, "name", data, "Maintainer")
        // this.utilToKey(key, "email", data, "MaintainerEmail")
        // this.utilToKey(key, "website", data, "MaintainerWeb")
        // this.utilToKey(key, "phone", data, "MaintainerPhone")
        // this.utilToKey(key, "fox", data, "MaintainerFax")
        // this.utilToKey(key, "man", data, "MaintainerContactor")
        if (key == "venderId") {
          data[key] = data[key] + "-" + data.name
          this.utilToKey(key, "venderId", data, "DPMaintainerID")
        }
      }
      tools.setItem(this.deviceType.code, this.main)
    },

    utilToKey(key, name, data, messName) {
      if (key == name) {
        this.setDataToMain(data[key], messName, this.row)
      }
    },

    //上传文件弹窗触发事件
    fileChange(keys) {
      this.setDataToMain(keys, this.messKey.split(".")[1], this.row)
      tools.setItem(this.deviceType.code, this.main)
    },

    //上传图片弹窗触发事件
    imgChange(keys) {
      console.log(keys, "keys", this.messKey.split("."), this.row)
      this.setDataToMain(keys, this.messKey.split(".")[1], this.row)
      tools.setItem(this.deviceType.code, this.main)
    },

    //设备图片弹窗改变事件
    changePics(keys) {
      this.setDataToMain(keys, this.messKey.split(".")[1], this.row)
      tools.setItem(this.deviceType.code, this.main)
    },

    //系统实例弹窗确定
    setSystem(data) {
      this.main[this.row].linkSystem = data
      tools.setItem(this.deviceType.code, this.main)
    },

    //判断是否有值，有值赋值
    setDataToMain(data, key, row) {
      if (!!data && data != '--') {
        if (!!this.main[row].infos) {
          //铭牌照片特殊处理
          this.main[row].infos[key] = data
        } else {
          this.main[row].infos = {}
          this.main[row].infos[key] = data
        }
      } else {
        this.main[row].infos[key] = ''
      }
    }
  },
  watch: {
    dialog: {
      deep: true,
      handler: function () {
        if (this.dialog.addDevice) {
          this.setValue()
        }
      }
    }
  }
};
</script>
<style lang="less" scoped>
#addDevice {
    overflow: hidden;
    // height: 300px;
    // overflow-x: hidden;
    // overflow-y:auto;

}
</style>
