/*
* 打包/框架计划共用混入
*/
import { filterDictText } from '@/assets/js/utils' // 常用方法

export const lineItemBackAdd = {
  data() {
    return {
      flowStatus: this.$route.query.flowStatus, // true 工作流进入驳回编辑
      /* --------------字典----------------*/
      tenderTypeList: [], // 公开/邀请
      supplierAttributeList: [], // 供应商属性
      supplierCategoryList: [], // 供应商分类
      reviewMethodList: [], // 评审办法
      tenderMethodList: [], // 采购方式
      // collectionTypeList: [], // 是否集采
      taxRateList: [], // 税率
      purchaseUnitList: [], // 计量单位
      priorityOrderByList: [], // 是否优先
      dictionaryType: ['tenderType', 'supplierAttribute', 'supplierCategory', 'reviewMethod', 'tenderMethod',
        'taxRate', 'purchaseUnit', 'priorityOrderBy', 'ifComboType'], // 字典请求参数
      ifComboTypeList: [], // 是否联合体
      /* ------------------------------*/
      // principalList: [], // 上传公开附件数据
      // 公开附件
      lineItemFileType: {
        bizId: '',
        fileType: 'open_tender_project_file'
      },
      // interiorFileList: [], // 内部附件数据
      // 内部附件
      lnteriorFileType: {
        bizId: '',
        fileType: 'inside_tender_plan_file' // 原fileType：inside_tender_project_file
      },
      // 联合体协议
      invitationComboFileType: {
        bizId: '',
        fileType: 'invitation_combo_file_type'
      }
    }
  },
  computed: {
    // 开启选择供应商条件
    supplierIsShow() {
      let flag = false
      if (this.formData.tenderType === '01') {
        flag = true
      }
      return flag
    },
    // 判断选择供应商是否单选
    single() {
      let flag = false
      if (this.formData.tenderMethod === '12') {
        flag = true
      }
      return flag
    }
  },
  methods: {
    filterDictText,
    // 供应商属性全选
    attrCheckAllChange(val) {
      /**
         * chedAll：全选状态 allData：绑定的数组 oAllData：字典数组 isIn：复选框初始化样式控制
         */
      const attrs = this.formData.supplierAttribute // 绑定的数组
      const oAllData = this.supplierAttributeList // 字典数组
      const { allData, isIn } = this.checkedAll({ chedAll: val, allData: attrs, oAllData, isIn: this.attributeIsIn })
      this.formData.supplierAttribute = allData // 绑定的数组重新赋值
      this.attributeIsIn = isIn // 复选框初始化样式重新赋值
    },
    // 供应商属性反选
    attrCheckedCitiesChange(value) {
      /**
         * chedAll：全选状态 allData：绑定的数组 oAllData：字典数组 isIn：复选框初始化样式控制
         */
      const oAllData = this.supplierAttributeList
      const { chedAll, isIn } = this.handleChedAll({ chedAll: this.attributedCheckAll, allData: value, oAllData, isIn: this.attributeIsIn })
      this.attributedCheckAll = chedAll // 供应商属性全选重新赋值
      this.attributeIsIn = isIn // 复选框初始化样式重新赋值
    },
    // 供应商分类全选
    handleCheckAllChange(val) {
      /**
         * chedAll：全选状态 allData：绑定的数组 oAllData：字典数组 isIn：复选框初始化样式控制
         */
      const attrs = this.formData.supplierCategory // 绑定的数组
      const oAllData = this.supplierCategoryList // 字典数组
      const { allData, isIn } = this.checkedAll({ chedAll: val, allData: attrs, oAllData, isIn: this.isIndeterminate })
      this.formData.supplierCategory = allData // 绑定的数组重新赋值
      this.isIndeterminate = isIn // 复选框初始化样式控制重新赋值
    },
    // 供应商分类反选
    handleCheckedCitiesChange(value) {
      /**
         * chedAll：全选状态 allData：绑定的数组 oAllData：字典数组 isIn：复选框初始化样式控制
         */
      const oAllData = this.supplierCategoryList
      const { chedAll, isIn } = this.handleChedAll({ chedAll: this.categoryCheckAll, allData: value, oAllData, isIn: this.isIndeterminate })
      this.categoryCheckAll = chedAll // 供应商分类全选状态重新赋值
      this.isIndeterminate = isIn // 复选框初始化样式控制重新赋值
    },
    // 全选
    /**
       * chedAll：全选状态 allData：绑定的数组 oAllData：字典数组 isIn：复选框初始化样式控制
       */
    checkedAll({ chedAll = false, allData = [], oAllData = [], isIn = false } = {}) {
      const keys = oAllData.map(item => item.typecode)
      allData = chedAll ? keys : []
      isIn = false
      return {
        allData,
        isIn
      }
    },
    // 反选
    /**
         * chedAll：全选状态 allData：绑定的数组 oAllData：字典数组 isIn：复选框初始化样式控制
         */
    handleChedAll({ chedAll = false, allData = [], oAllData = [], isIn = false } = {}) {
      const checkedCount = allData.length
      chedAll = checkedCount === oAllData.length
      isIn = checkedCount > 0 && checkedCount < oAllData.length
      return {
        chedAll,
        isIn
      }
    },
    /* ===========================选择供应商================*/
    // 校验是否选择供应商
    supplierVerify() {
      let flag = true
      let msg = '请选择供应商！'
      const tenderMethod = this.formData.tenderMethod
      // 00 公开招标 11 询价 12 单一来源 13 竞争性谈判 14 竞价
      if (this.supplierIsShow) {
        if (this.supplierIsShow && this.supplierList.length <= 0) {
          flag = false
        } else if (tenderMethod === '12' && this.supplierList.length > 1) {
          flag = false
          msg = '单一来源只能选择一家供应商！'
        } else if (tenderMethod === '13' && this.supplierList.length < 2) {
          flag = false
          msg = '请选择2家及以上供应商！'
        } else if (tenderMethod === '00' && this.supplierList.length < 2) {
          flag = false
          msg = '请选择2家及以上供应商！'
        } else if (tenderMethod === '11' && this.supplierList.length < 3) {
          flag = false
          msg = '请选择3家及以上供应商！'
        } else {
          flag = true
        }
      }
      return {
        flag,
        msg
      }
    }
  }
}
