// 不显示的信息点数组
import text from "@/utils/handsontable/mainText"
import tools from "@/utils/scan/tools"
import fieldData from "@/utils/handsontable/fillterField"
import dataTypeMap from "./dataTypeMap"

//在编辑模式下并且勾选隐藏自动填充的信息点不显示的字段
let noShowArr = [
  "specification",
  "serviceLife"
]
//在编辑模式下不显示的字段
let cantShow = [
  "EquipQRCode",
  "EquipID",
  "EquipName",
  "BIMLocation",
  "Brand",
  "Specification",
  "Manufacturer",
  "Supplier",
  "SupplierContactor",
  "SupplierPhone",
  "SupplierEmail",
  "SupplierWeb",
  "SupplierFax",
  "Maintainer",
  "MaintainerContactor",
  "MaintainerPhone",
  "MaintainerEmail",
  "MaintainerWeb",
  "Principal",
  "MaintainerFax",
  "Insurer",
  "InsurerContactor",
  "InsurerPhone",
  "InsurerEmail",
  "InsurerWeb",
  "InsurerFax",
  "InsuranceFile",
  "EquipID",
  "EquipName",
  "SpaceCount",
  "SpaceList",
  "TenantID",
  "ShaftID"
]

//台账编辑页面不显示的厂商信息
let firmArr = [
  "brand", //品牌
  "specification", //设备型号
  "manufacturer", //生产厂家
  "supplier", //供应商单位名称
  "supplierContactor", //供应商联系人
  "supplierPhone", //供应商联系电话
  "supplierEmail", //供应商电子邮件
  "supplierWeb", //供应商网址
  "supplierFax", //供应商传真
  "maintainer", //维修商单位名称
  "maintainerContactor", //维修商联系人
  "maintainerPhone", //维修商联系电话
  "maintainerEmail", //维修商电子邮件
  "maintainerWeb", //维修商网址
  "maintainerFax", //维修商传真
  "insurer", //保险公司名称
  "insurerContactor", //保险公司联系人
  "insurerPhone", //保险公司联系电话
  'insurerEmail', //保险公司电子邮件
  "insurerWeb", //保险公司网址
  "insurerFax", //保险公司传真
  // "dpManufacturerId",//品牌型号Id
  // "dpSupplierId",//供应商信息Id
  // "dpMaintainerId",//维修商信息Id
  // "dpInsurerId"//保险公司信息Id
]

// 将映射表枚举值转为数组
const mapToArr = () => {
  const arr = []
  for (let key in dataTypeMap) {
    arr.push(dataTypeMap[key])
  }
  return arr;
}
const showTools = {
  inputModeArr: mapToArr(),
  cantShow: (code) => {
    let falg = true
    cantShow.map(item => {
      if (item == code) {
        falg = false
      }
    })
    return falg
  },
  fixedNoShowField: (noShowList, code) => {
    let flag = true
    noShowList.map(item => {
      if (item == code) {
        flag = false
      }
    })
    return flag
  },
  isShow: (code) => {
    let falg = true
    noShowArr.map(item => {
      if (item == code) {
        falg = false
      }
    })
    return falg
  },
  arrToArr(arr, showType) {
    let first = 0,
      scound = 0,
      three = 0,
      four = 0
    arr.find(function (value, index, arr) {
      let flag = (showType != "Visible" || value.visible)
      if (
        ((value.code == "brand" && flag) ||
          (value.code == "specification" && flag) ||
          (value.code == "manufacturer" && flag)) && !first
      ) {
        first = index
      }
      if (
        ((value.code == "supplier" && flag) ||
          (value.code == "supplierContactor" && flag) ||
          (value.code == "supplierEmail" && flag) ||
          (value.code == "supplierWeb" && flag) ||
          (value.code == "supplierFax" && flag) ||
          (value.code == "supplierPhone" && flag)) && !scound
      ) {
        scound = index
      }
      if (
        ((value.code == "maintainer" && flag) ||
          (value.code == "maintainerContactor" && flag) ||
          (value.code == "maintainerPhone" && flag) ||
          (value.code == "maintainerEmail" && flag) ||
          (value.code == "maintainerWeb" && flag) ||
          (value.code == "maintainerFax" && flag)) && !three
      ) {
        three = index
      }
      if (
        ((value.code == "insurer" && flag) ||
          (value.code == "insurerContactor" && flag) ||
          (value.code == "insurerPhone" && flag) ||
          (value.code == "insurerEmail" && flag) ||
          (value.code == "insurerWeb" && flag) ||
          (value.code == "insurerFax" && flag)) && !four
      ) {
        four = index
      }
    })
    let numList = [first, scound, three, four]
    numList.reverse().map((item, index) => {
      if (item) {
        if (index == 0) {
          arr.splice(item, 0, {
            category: 'STATIC',
            dataType: "STRING",
            firstName: "基本信息",
            aliasCode: "dpInsurerId",
            aliasName: "保险公司信息",
            name: "保险公司信息",
            code: "dpInsurerId",
            origCode: "dpInsurerId",
            origDataType: "Str",
            path: "dpInsurerId",
            priority: "S",
            editable: true,
            visible: true
          })
        } else if (index == 1) {
          arr.splice(item, 0, {
            category: 'STATIC',
            dataType: "STRING",
            firstName: "基本信息",
            aliasCode: "dpMaintainerId",
            aliasName: "维修商信息",
            name: "维修商信息",
            code: "dpMaintainerId",
            origCode: "dpMaintainerId",
            origDataType: "Str",
            path: "dpMaintainerId",
            priority: "S",
            editable: true,
            visible: true
          })
        } else if (index == 2) {
          arr.splice(item, 0, {
            category: 'STATIC',
            dataType: "STRING",
            firstName: "基本信息",
            aliasCode: "dpSupplierId",
            aliasName: "供应商信息",
            name: "供应商信息",
            code: "dpSupplierId",
            origCode: "dpSupplierId",
            origDataType: "Str",
            path: "dpSupplierId",
            priority: "S",
            editable: true,
            visible: true
          })
        } else if (index == 3) {
          arr.splice(item, 0, {
            category: 'STATIC',
            dataType: "STRING",
            firstName: "基本信息",
            aliasCode: "dpManufacturerId",
            aliasName: "生产厂家/品牌/型号",
            name: "生产厂家/品牌/型号",
            code: "dpManufacturerId",
            origCode: "dpManufacturerId",
            origDataType: "Str",
            path: "dpManufacturerId",
            priority: "S",
            editable: true,
            visible: true
          })
        }
      }
    })
    return arr
  },
  /**
   *
   * @param {*} arr 原始表头数组
   * @param {*} type 对应表格的模块类型
   * @param {*} onlyRead 是否只读
   * @param {*} showType 查看类型(全部;只看要采集的信息;隐藏部分信息)
   * @param {*} taizhang 是否是台账类型(默认为false)
   *
   */
  headerTextFilter(arr, type, onlyRead, showType, taizhang = false) {
    // if (taizhang && !onlyRead) {
    //   arr = this.arrToArr(arr, showType)
    // }
    let data = arr.map(item => {
      // if (type == "property" && item.firstTag == "RunParam") { //资产台账不显示运行参数
      //     return undefined
      // }
      if (onlyRead) { //只读模式
        if (!this.filterUnshow(onlyRead, type, item.code)) { //过滤固定不显示字段
          return undefined
        }
        if (showType == "Visible" && item.visible) { //只看要采集的信息
          return this.isUnit(item)
        } else if (showType == "all") { //显示全部
          return this.isUnit(item)
        } else {
          return undefined
        }
      } else { //编辑模式
        // if (taizhang && !this.filterFirm(item.code)) {
        //   return undefined
        // }
        if (!this.filterUnshow(onlyRead, type, item.code)) { //过滤固定不显示字段
          return undefined
        }
        if (showType == "Visible" && item.visible) { //只看要采集的信息
          return this.isUnit(item)
        } else if (showType == "all") { //显示全部
          return this.isUnit(item)
        } else if (showType == "partInfo" && item.visible && this.isShow(item.code)) { //隐藏部分厂家信息(***需维护不显示列表***)
          // if (item.FirstName == "控制参数" || item.FirstName == "设定参数" || item.FirstName == "运行参数") {
          if (item.firstTag == "techParam") { //技术参数
            return undefined
          } else {
            return this.isUnit(item)
          }
        } else {
          return undefined
        }

      }
    }).filter(d => d)
    return data
  },
  headerTypeFilter(arr, type, onlyRead, showType, taizhang = false) {
    // if (taizhang && !onlyRead) {
    //   arr = this.arrToArr(arr, showType)
    // }
    text.readOnly = onlyRead;
    let data = arr.map(item => {
      // if (type == "property" && item.firstTag == "runParam") { //资产台账不显示运行参数
      //     return undefined
      // }
      if (onlyRead) { //只读模式
        if (!this.filterUnshow(onlyRead, type, item.code)) { //过滤固定不显示字段
          return undefined
        }
        if (showType == "Visible" && item.visible) { //只看要采集的信息
          return this.textFormat(item, onlyRead)
        } else if (showType == "all") { //显示全部
          return this.textFormat(item, onlyRead)
        } else {
          return undefined
        }
      } else { //编辑模式
        // if (taizhang && !this.filterFirm(item.code)) {
        //   return undefined
        // }
        if (!this.filterUnshow(onlyRead, type, item.code)) { //过滤固定不显示字段
          return undefined
        }
        if (showType == "Visible" && item.visible) { //只看要采集的信息
          return this.inputModeFilter(item, onlyRead)
        } else if (showType == "all") { //显示全部
          return this.inputModeFilter(item, onlyRead)
        } else if (showType == "partInfo" && item.visible && this.isShow(item.code)) { //隐藏部分厂家信息(***需维护不显示列表***)
          // if (item.FirstName == "控制参数" || item.FirstName == "设定参数" || item.FirstName == "运行参数") {
          if (item.firstTag == "techParam") { //技术参数
            return undefined
          } else {
            return this.inputModeFilter(item, onlyRead)
          }
        } else {
          return undefined
        }
      }
    }).filter(d => d)
    return data
  },
  isUnit(item) {
    if (!item.unit || item.unit == "") {
      return item.name;
    } else {
      return item.name + "(" + item.unit + ")";
    }
  },
  textFormat(item, onlyRead) {
    if (
      // item.code == "EquipQRCode" ||
      // item.code == "RoomQRCode"
      item.code == "defaultQRCode"
    ) {
      return {
        data: item.path,
        renderer: text.lookQRCode,
        readOnly: onlyRead
      }
    }

    //图片类型
    if (
      item.code == "installPic" ||
      item.code == "nameplate" ||
      item.code == "systemChart" ||
      item.code == "pic"
    ) {
      return {
        data: item.path,
        renderer: text.picType,
        readOnly: onlyRead
      }
    }
    //文件类型
    if (item.code == "insuranceFile" ||
      item.code == "archive" ||
      item.code == "checkReport" ||
      item.code == "maintainManual" ||
      item.code == "approachingAcceptance" ||
      item.code == "acceptanceReport" ||
      item.code == "operationManual" ||
      item.code == "originalCertificate" ||
      item.code == "testReport" ||
      item.code == "productCertification" ||
      item.code == "installInstruction" ||
      item.code == "supplierContract" ||
      item.code == "drawing" ||
      item.code == "installDrawing" ||
      item.code == "designIDGraph" ||
      item.code == "fanCharacterCurve"
    ) {
      return {
        data: item.path,
        renderer: text.fileType,
        readOnly: onlyRead
      }
    }
    if (item.category == 'STATIC') {
      if ((item.dataType == dataTypeMap.ENUM && !item.multiple) || (item.dataType == dataTypeMap.BOOLEAN && !item.multiple)) { //单选输入(单数)
        return {
          data: item.path,
          renderer: tools.customDropdownRenderer,
          editor: "chosen",
          readOnly: onlyRead,
          chosenOptions: {
            data: item.dataSource ? tools.formatDataSource(item.dataSource) : ""
          }
        };
      } else if ((item.dataType == dataTypeMap.MENUM) || (item.dataType == dataTypeMap.ENUM && item.multiple) || (item.dataType == dataTypeMap.BOOLEAN && item.multiple)) { //多选输入（多选枚举，单选枚举复数）
        return {
          data: item.path,
          renderer: tools.multipleDropdownRenderer,
          readOnly: onlyRead,
          chosenOptions: item.dataSource ? tools.formatDataSource(item.dataSource) : ""
        };
      } else if (item.dataType == dataTypeMap.DOUBLE || item.dataType == dataTypeMap.INTEGER) { // 数字输入
        return {
          data: item.path,
          type: "numeric",
          readOnly: onlyRead,
          numericFormat: {
            pattern: "0,0.00"
          }
        };
      } else if (item.dataType == dataTypeMap.DATETIME) { // 日期输入
        return {
          data: item.path,
          type: "date",
          dateFormat: "YYYYMMDD",
          readOnly: onlyRead,
          correctFormat: true
        };
      } else if (item.dataType == dataTypeMap.STRING) { // 文本输入
        return {
          data: item.path,
          readOnly: onlyRead
        }
      } else { //(未支持的输入方式)暂不支持编辑
        return {
          data: item.path,
          readOnly: onlyRead
        }
      }
    } else { // 动态信息点
      return {
        data: item.path,
        readOnly: onlyRead
      }
    }
  },
  inputModeFilter(item, onlyRead) {
    if (
      // item.code == "EquipQRCode" ||
      // item.code == "RoomQRCode"
      item.code == "defaultQRCode"
    ) {
      return {
        data: item.path,
        renderer: text.lookQRCode,
        readOnly: onlyRead
      }
    }

    //图片类型
    if (
      item.code == "installPic" ||
      item.code == "nameplate" ||
      item.code == "systemChart" ||
      item.code == "pic"
    ) {
      return {
        data: item.path,
        renderer: text.picType,
        readOnly: onlyRead
      }
    }

    //文件类型
    if (item.code == "insuranceFile" ||
      item.code == "archive" ||
      item.code == "checkReport" ||
      item.code == "maintainManual" ||
      item.code == "approachingAcceptance" ||
      item.code == "acceptanceReport" ||
      item.code == "operationManual" ||
      item.code == "originalCertificate" ||
      item.code == "testReport" ||
      item.code == "productCertification" ||
      item.code == "installInstruction" ||
      item.code == "supplierContract" ||
      item.code == "drawing" ||
      item.code == "installDrawing" ||
      item.code == "designIDGraph" ||
      item.code == "fanCharacterCurve"
    ) {
      return {
        data: item.path,
        renderer: text.fileType,
        readOnly: onlyRead
      }
    }

    //四大厂商
    // if (
    //   item.code == "dpSupplierId" ||
    //   item.code == "dpMaintainerId" ||
    //   item.code == "dpInsurerId" ||
    //   item.code == "dpManufacturerId"
    // ) {
    //   return {
    //     data: item.path,
    //     renderer: text.idType,
    //     readOnly: onlyRead
    //   }
    // }
    if (!item.category) {
      console.log('新版字典无输入类型', item)
    }
    if (item.category == 'STATIC') {
      if ((item.dataType == dataTypeMap.ENUM && !item.multiple) || (item.dataType == dataTypeMap.BOOLEAN && !item.multiple)) { //单选输入(单数)
        return {
          data: item.path,
          renderer: tools.customDropdownRenderer,
          editor: "chosen",
          readOnly: onlyRead,
          chosenOptions: {
            data: item.dataSource ? tools.formatDataSource(item.dataSource) : ""
          }
        };
      } else if ((item.dataType == dataTypeMap.MENUM) || (item.dataType == dataTypeMap.ENUM && item.multiple) || (item.dataType == dataTypeMap.BOOLEAN && item.multiple)) { //多选输入（多选枚举，单选枚举复数）
        return {
          data: item.path,
          renderer: tools.multipleDropdownRenderer,
          readOnly: onlyRead,
          chosenOptions: item.dataSource ? tools.formatDataSource(item.dataSource) : ""
        };
      } else if (item.dataType == dataTypeMap.DOUBLE || item.dataType == dataTypeMap.INTEGER) { // 数字输入
        return {
          data: item.path,
          type: "numeric",
          readOnly: onlyRead,
          numericFormat: {
            pattern: "0,0.00"
          }
        };
      } else if (item.dataType == dataTypeMap.DATETIME) { // 日期输入
        return {
          data: item.path,
          type: "date",
          dateFormat: item.formater || "YYYYMMDD",
          readOnly: onlyRead,
          correctFormat: true
        };
      } else if (item.dataType == dataTypeMap.STRING) { // 文本输入
        return {
          data: item.path,
          readOnly: onlyRead
        }
      } else { // (未支持的输入方式)暂不支持编辑
        return {
          data: item.path,
          readOnly: onlyRead
        }
      }
    } else { // 动态信息点
      return {
        data: item.path,
        readOnly: onlyRead
      }
    }
  },
  filterUnshow: (onlyRead, type, code) => {
    let falg = true,
      data = fieldData[type]
    if (onlyRead) {
      data.onlyRead.map(item => {
        if (item == code) {
          falg = false
        }
      })
      return falg
    } else {
      data.edit.map(item => {
        if (item == code) {
          falg = false
        }
      })
      return falg
    }
  },
  filterFirm: (code) => {
    let falg = true
    firmArr.map(item => {
      if (item == code) {
        falg = false
      }
    })
    return falg
  }
}

export default showTools
