import { getCommercialEngineeringDetail, getPositionProject } from '@/api/frock'
import { getDictList, getSalesmanListService } from '@/api/common'
import { getDealerListV3 } from '@/api/basic_data/dealer'
import { getFirstPartyCustomerManagementList } from '@/api/basic_data/partya'
import { mapGetters } from 'vuex'
export default {
  props: {
    detailId: {
      type: String,
      default: ''
    },
    module: {
      type: String,
      default: 'add'
    }
  },
  data() {
    return {
      resetData: {},
      formData: {
        address: '',
        adminCompanyId: '',
        adminUserId: '',
        adminWebsitId: '',
        applyUpdateBy: '',
        applyUpdateCompanyId: '',
        applyUpdateCompanyName: '',
        applyUpdateName: '',
        areaId: '',
        area: '',
        cityId: '',
        city: '',
        commonFollowProject: '',
        confirmBy: '',
        confirmName: '',
        confirmTime: '',
        customerAddress: '',
        customerId: '',
        customerLinkMobile: '',
        customerLinkName: '',
        customerName: '',
        customerNumber: '',
        del: null,
        drawUpload: '',
        enginLinkMobile: '',
        enginLinkName: '',
        examineNote: '',
        tradeName: '',
        tradeParentName: '',
        extent: '',
        files: [],
        followCustomer: '',
        followCustomerMobile: '',
        homeProjectNameArea: '',
        homeProjectNameArea2: '',
        homeProjectNameArea3: '',
        homeProjectNameNumber3: '',
        homeProjectNameNumber: '',
        homeProjectNameRadio: '',
        homeProjectNameSeat: '',
        id: '',
        isApplyUpdate: null,
        isSpan: null,
        items: [],
        lat: '',
        lnt: '',
        loginStatus: 'OK',
        machineType: '',
        note: '',
        orderStatus: '',
        orderType: 'WORK',
        otherInfo: '',
        partyA: '',
        partyAId: '',
        positionAddress: '',
        preDeviceAmount: null,
        preSignDate: '',
        projectArea: '',
        projectCategory: 'COMMON',
        projectMent: '',
        projectName: '',
        projectNo: '',
        provinceId: '',
        province: '',
        refCustomerId: '',
        remark: '',
        salesCompanyName: '韶关弘格贸易有限公司',
        serviceId: '',
        serviceName: '',
        status: 'ING',
        streetId: '',
        street: '',
        submitBy: '',
        submitName: '',
        submitTime: '',
        successLoginProject: '',
        successRate: 'FIRST',
        tradeId: '',
        tradeParentId: '',
        type: 'DRAW',
        useExtent: '',
        fileList: [],
        serviceNumber2: '',
        serviceName2: '',
        customerNumber2: '',
        customerName2: '',
        joinAddress: '',
        successServiceNumber: '',
        successServiceName: '',
        successCustomerNumber: '',
        successCustomerName: '',
      },
      commonData: {
        dict: {
          TRADE_LOGIN_CATEGORY: [], // 项目类别
          TRADE_LOGIN_TYPE: [], // 类型
          SUCCESS_RATE: [], // 成功机率
          FOLLOW_STATUS: [], // 工程跟进状态
          LOGIN_MACHINE_TYPE: [], // 机组类型
          TRADE_EXAMINE_NOTE: [], // 审核备注
          TRADE_PROJECT_AREA: [] // 项目所在区域
        },
        salesmanList: [],
        customerList: [],
        PartyAList: [],
        vicinityMarkers: []
      }
    }
  },
  computed: {
    ...mapGetters(['isTradeExaminer'])
  },
  created() {
    this.resetData = {
      ...this.formData
    }
    this.getDictList()
    this.getSalesmanListService()
    this.getFirstPartyCustomerManagementList()
    if (this.module === 'add') {
      this.getDealerListV3()

    }
    if (this.detailId) {
      this.getCommercialEngineeringDetail()
    }
  },
  methods: {
    getDictList() {
      Object.keys(this.commonData.dict).forEach(async k => {
        const { data } = await getDictList({ sysDictEnum: k })
        this.commonData.dict[k] = data.map(l => {
          return {
            label: l.dictValue,
            value: l.dictCode
          }
        })
      })
    },
    getSalesmanListService() {
      getSalesmanListService({
        pageNum: 1,
        pageSize: -1,
        isCustomer: 0,
        status: true
      }).then(res => {
        this.commonData.salesmanList = res.data.records.map(k => {
          return {
            label: k.nickName,
            value: k.adminUserId,
            userName: k.userName
          }
        })
      })
    },
    getFirstPartyCustomerManagementList() {
      // getFirstPartyCustomerManagementList({ pageNum: 1, pageSize: -1 }).then(res => {
      //   this.commonData.PartyAList = res.data.records.map(k => {
      //     return {
      //       label: k.name,
      //       value: k.id
      //     }
      //   })
      // })
    },
    getDealerListV3() {
      getDealerListV3({
        pageNum: 1,
        pageSize: 100
      }).then(res => {
        this.commonData.customerList = res.data.records.map(k => {
          return {
            number: k.number,
            label: k.name,
            value: k.id
          }
        })
      })
    },
    getCommercialEngineeringDetail() {
      getCommercialEngineeringDetail({ id: this.detailId }).then(res => {
        this.formData = {
          ...this.formData,
          ...res.data,
          fileList: []
        }
        if (res.data.drawUpload) {
          const name = res.data.drawUpload.substring(0, res.data.drawUpload.lastIndexOf(".")).replace('uploadfile/', '')
          const fileName =res.data.drawUpload.replace('uploadfile/', '')
          this.formData.fileList = [
            {
              name,
              fileName,
              status: 'success',
              url: res.data.drawUpload,
            }
          ]
        }
      })
    },
    getPositionProject() {
      getPositionProject({ id: this.detailId }).then(res => {
        this.commonData.vicinityMarkers = res.data
      })
    },
    onReset() {
      this.formData = {
        ...this.resetData,
        id: this.detailId
      }
    },
    // 校验机型数量
    verificationModel(){
      if (!this.formData.items.length) {
         this.$errorMsg('机型明细不能为空')
         return false
      }
      console.log(this.formData.items)
      const bol =  this.formData.items.every(k=>!['', null, undefined,0, '0'].includes(k.qty))
      if (!bol) {
          this.$errorMsg('机型信息数量不能为空并且不能小于1')
      }
      return  bol
    }
  }
}
