<!---->
<template>
  <div>
    <div class="headTitleStyle">
      <span class="fontWeightBold">{{$t('ladingNoticeHandle.productInfo')}}</span>
      <div class="rightStyle">
        <auth-button icon="iconfont iconlvli11" type="primary" :label="$t('ladingNoticeHandle.dialogHead')"
                     :menuCode="menuCode" perms="handleDetail" :openPerms="true"
                     @click="detailsPopup"></auth-button>
      </div>
    </div>
    <el-divider class="elDivider"></el-divider>
    <!-- 基础信息 -->
<!--    <dym-form :componentList="componentList" :formValue="item"></dym-form>-->
    <el-row :gutter="10">
      <el-col  :xs="it.cols[0]" :sm="it.cols[1]" :md="it.cols[2]" :lg="it.cols[3]" v-for="(it, ind) in componentList" :key="ind">
        <el-form-item v-if="(it.showFlag === undefined  || it.showFlag === true)" :label="it.label"
                      :label-width="it.labelWidth" :prop="it.prop" :show-message="it.showMessage">
          <el-tooltip class="item" effect="dark" :content="item[it.value].toString()" placement="top" :disabled="it.tooltipDisabled">
            <component class="dymForm" :is="it.componentName" :dictTypeCode="it.dictTypeCode" :disabled="it.disabled"
                       v-model.trim="item[it.value]"
                       :placeholder="editModel === 'V' ? '' : it.placeholder" :clearable="it.clearable">
            </component>
          </el-tooltip>
        </el-form-item>
      </el-col>
    </el-row>
    <el-collapse v-model="activeNames" class="Blod">
      <el-collapse-item :title="$t('ladingNoticeHandle.areaContactsInfo')" name="1">
        <!-- 区域表格 -->
        <handle-info-tab-regin :localData.sync="areaContactsInfoRemoteData"
                               :menuCode="menuCode"
                               @showItem="showItem(arguments, '0')" @getList="getEnterpriseArea()"
                               ref="areaContactsInfo"></handle-info-tab-regin>
      </el-collapse-item>
      <el-collapse-item :title="$t('ladingNoticeHandle.projectContactsInfo')" name="2" v-if="projectContactsInfo.length !== 0">
        <div v-for="(item, index) in projectContactsInfo" :key="item.enterpriseId">
          <handle-info-tab-company :localData.sync="item"
                                   :menuCode="menuCode"
                                   @showItem="showItem(arguments, '1', index)" @getList="getCompyByAreaId()"
                                   :ref="'projectContactsInfo' + index">
          </handle-info-tab-company>
          <el-divider v-if="index !== (projectContactsInfo.length - 1)"></el-divider>
        </div>
      </el-collapse-item>
    </el-collapse>
    <handle-detail :menuCode="menuCode" :visible.sync="popupVisible" editModel="S" :form="formItem"></handle-detail>
  </div>
</template>

<script>
import { DataTable, authButton, DialogEdit, SelectDictionary, dymForm } from 'newepoch-components'
import handleInfoTabRegin from '@/pages/shouji/ladingHandle/handleInfoTabRegin'
import handleInfoTabCompany from '@/pages/shouji/ladingHandle/handleInfoTabCompany'
import handleDetail from '@/pages/shouji/ladingHandle/handleDetail'
import ladingNoticeManageApi from '@/api/shouji/ladingNoticeManageApi'
import * as format from '@/filters/index'
export default {
  name: 'ladingHandleEdit',
  components: {
    DialogEdit,
    authButton,
    DataTable,
    dymForm,
    SelectDictionary,
    handleInfoTabRegin,
    handleInfoTabCompany,
    handleDetail
  },
  props: {
    menuCode: {
      type: String,
      default: null
    },
    item: {
      type: Object,
      default: function () {
        return {}
      }
    },
    form: {
      type: Object,
      default: function () {
        return {}
      }
    },
    areaContactsInfoRemoteData: {
      type: Object,
      default: function () {
        return {}
      }
    },
    projectInfo: {
      type: Object,
      default: function () {
        return {}
      }
    },
    index: {
      type: Number,
      default: function () {
        return null
      }
    }
  },
  data () {
    return {
      // newFormValue: [], // 一栏页面传过来的数据
      activeNames: ['1', '2'],
      formItem: {},
      editModel: '',
      popupVisible: false, // 履历详情页面
      contactItem: {},
      enterpriseType: '60',
      getCompyByAreaIdData: {},
      talkSkill: {},
      componentList: [
        // 产品名称
        {
          value: 'projectName',
          label: this.$i18n.messages[this.$i18n.locale].ladingNoticeHandle.productName,
          clearable: true,
          prop: 'projectName',
          componentName: 'el-input',
          cols: [8, 8, 8, 8],
          labelWidth: '156px',
          tooltipDisabled: false,
          disabled: true
        },
        // 期数
        {
          value: 'specialPlanPeriods',
          label: this.$i18n.messages[this.$i18n.locale].ladingNoticeHandle.specialPlanPeriods,
          clearable: true,
          prop: 'specialPlanPeriods',
          componentName: 'el-input',
          labelWidth: '160px',
          cols: [8, 8, 8, 8],
          tooltipDisabled: true,
          disabled: true
        },
        // 核心企业
        {
          value: 'coreEnterpriseName',
          label: this.$i18n.messages[this.$i18n.locale].ladingNoticeHandle.coreEnterpriseName,
          clearable: true,
          prop: 'coreEnterpriseName',
          componentName: 'el-input',
          labelWidth: '160px',
          cols: [8, 8, 8, 8],
          tooltipDisabled: false,
          disabled: true
        },
        // 计划管理人/再保理银行
        {
          value: 'planningManagerName',
          label: this.$i18n.messages[this.$i18n.locale].ladingNoticeHandle.planningManagerName,
          clearable: true,
          prop: 'planningManagerName',
          labelWidth: '160px',
          componentName: 'el-input',
          cols: [8, 8, 8, 8],
          tooltipDisabled: false,
          disabled: true
        },
        // 提单类型
        {
          value: 'ladingType',
          label: this.$i18n.messages[this.$i18n.locale].ladingNoticeHandle.ladingType,
          clearable: true,
          prop: 'ladingType',
          labelWidth: '160px',
          name: 'ladingType',
          componentName: 'SelectDictionary',
          dictTypeCode: 'ZD0100',
          cols: [8, 8, 8, 8],
          tooltipDisabled: true,
          disabled: true
        },
        // 原始权益人
        {
          value: 'initialOwnersName',
          label: this.$i18n.messages[this.$i18n.locale].ladingNoticeHandle.initialOwnersName,
          clearable: true,
          prop: 'initialOwnersName',
          labelWidth: '160px',
          componentName: 'el-input',
          cols: [8, 8, 8, 8],
          tooltipDisabled: false,
          disabled: true
        },
        // 资产服务机构
        {
          value: 'assetServiceName',
          label: this.$i18n.messages[this.$i18n.locale].ladingNoticeHandle.assetServiceId,
          clearable: true,
          prop: 'assetServiceName',
          labelWidth: '160px',
          componentName: 'el-input',
          cols: [8, 8, 8, 8],
          tooltipDisabled: false,
          disabled: true
        }
      ]
    }
  },
  computed: {
    api () {
      return ladingNoticeManageApi.ladingNoticeManageListApi
    },
    projectContactsInfo: {
      get () {
        return this.projectInfo.data
      },
      set (val) {
        this.$emit('update:projectInfo', { data: val })
      }
    }
  },
  activated () {
    this.$nextTick(() => {
      this.getInit()
    })
    this.$emit('changPage', this.index)
  },
  methods: {
    // 时间判断
    compareTime (time) {
      if (time) {
        time = format.formatDate(time)
        if (time && time.toString().indexOf('1970') < 0 && time.toString().indexOf('1900') < 0) {
          return time
        } else {
          return ''
        }
      } else {
        return ''
      }
    },
    // 区域联系人
    getEnterpriseArea () {
      var coreArea = this.item.coreArea
      var coreAreaName = this.item.coreAreaName
      this.areaContactsInfoRemoteData.expressNumber = coreAreaName
      this.areaContactsInfoRemoteData.keyId = coreArea
      this.areaContactsInfoRemoteData.enterpriseId = this.form.coreEnterpriseId
      this.areaContactsInfoRemoteData.enterpriseName = this.form.coreEnterpriseName
      this.getAssetcollectemployee({ areaId: coreArea })
      this.getCompyByAreaId({ areaKid: coreArea })
      this.getCompyByAreaIdData = { areaKid: coreArea }
      let temp = this.talkSkill.filter(it => it.contactType === '0')
      if (temp.length > 0) {
        temp[0].planLadingBeginAmount = temp[0].planLadingBeginAmount.toString()// === '0' ? '' : temp[0].planLadingBeginAmount
        temp[0].planLadingEndAmount = temp[0].planLadingEndAmount.toString()// === '0' ? '' : temp[0].planLadingEndAmount
        this.$set(this.areaContactsInfoRemoteData.locallocalData, 'ladingMoney', [temp[0].planLadingBeginAmount, temp[0].planLadingEndAmount])
        this.$set(this.areaContactsInfoRemoteData.locallocalData, 'ladingTime', [this.compareTime(temp[0].planLadingBeginTime), this.compareTime(temp[0].planLadingEndTime)])
        this.$set(this.areaContactsInfoRemoteData.locallocalData, 'ifLading', temp[0].ifsLading)
        this.$set(this.areaContactsInfoRemoteData, 'speechCraft', temp[0].talkWord)
        this.$set(this.areaContactsInfoRemoteData.locallocalData, 'keyId', temp[0].keyId)
      }
      // let params = { areaCommissionerId: this.$store.state.user.user.userId, coreCompanyId: this.form.coreEnterpriseId }
      // ladingNoticeManageApi.callentErpriseInfoApi.getcallentErpriseArea(params).then(res => {
      //   if (res.data && res.data.length >= 1) {
      //     this.areaContactsInfoRemoteData.expressNumber = res.data[0].areaName
      //     this.areaContactsInfoRemoteData.keyId = res.data[0].keyId
      //     this.areaContactsInfoRemoteData.enterpriseId = this.form.coreEnterpriseId
      //     this.areaContactsInfoRemoteData.enterpriseName = this.form.coreEnterpriseName
      //     this.getAssetcollectemployee({ areaId: res.data[0].keyId })
      //     this.getCompyByAreaId({ areaKid: res.data[0].keyId })
      //     this.getCompyByAreaIdData = { areaKid: res.data[0].keyId }
      //     let temp = this.talkSkill.filter(it => it.contactType === '0')
      //     if (temp.length > 0) {
      //       this.$set(this.areaContactsInfoRemoteData.locallocalData, 'ladingMoney', [temp[0].planLadingBeginAmount, temp[0].planLadingEndAmount])
      //       this.$set(this.areaContactsInfoRemoteData.locallocalData, 'ladingTime', [temp[0].planLadingBeginTime, temp[0].planLadingEndTime])
      //       this.$set(this.areaContactsInfoRemoteData.locallocalData, 'ifLading', temp[0].ifsLading)
      //       this.$set(this.areaContactsInfoRemoteData, 'speechCraft', temp[0].talkWord)
      //       this.$set(this.areaContactsInfoRemoteData.locallocalData, 'keyId', temp[0].keyId)
      //     }
      //   }
      // })
    },
    // 根据区域ID获取联系人
    getAssetcollectemployee (params) {
      ladingNoticeManageApi.callentErpriseInfoApi.getAssetcollectemployee(params).then(res => {
        this.areaContactsInfoRemoteData.localRemeteDatasourceData = []
        res.data.forEach(it => {
          // let { contactLevel, employeeDTO: { employeeName, telephone, weixin }, enterpriseAccountDTO: { account } } = it
          // /* { contactLevel, employeeName, telephone, weixin, isCreate: account ? '1' : '0', account }*/
          if (it.employeeDTO && it.employeeDTO.forbiddenStatus && it.employeeDTO.forbiddenStatus !== '1') {
            this.areaContactsInfoRemoteData.localRemeteDatasourceData.push(it)
          }
        })
      })
    },
    // 根据区域查询项目公司
    getCompyByAreaId (data = this.getCompyByAreaIdData) {
      if (Object.keys(data).length === 0) {
        this.getEnterpriseArea()
        return
      }
      this.projectContactsInfo = []
      ladingNoticeManageApi.fircrmApi.getCompyByAreaId(data).then(res => {
        if (res && res.data) {
          let temp = this.talkSkill.filter(it => it.contactType === '1')
          res.data.forEach(it => {
            let tempItem = temp.filter(it1 => it1.enterpriseId === it.companyId)
            let itemB = {
              expressNumber: it.companyName,
              keyId: '',
              coreArea: this.item.coreArea,
              coreAreaName: this.item.coreAreaName,
              enterpriseId: it.companyId,
              enterpriseName: it.companyName,
              coreEnterpriseId: this.form.coreEnterpriseId,
              coreEnterpriseName: this.form.coreEnterpriseName,
              localRemeteDatasourceData: [],
              locallocalData: {}
            }
            if (tempItem.length > 0) {
              tempItem[0].planLadingBeginAmount = tempItem[0].planLadingBeginAmount.toString()// === '0' ? '' : tempItem[0].planLadingBeginAmount
              tempItem[0].planLadingEndAmount = tempItem[0].planLadingEndAmount.toString()// === '0' ? '' : tempItem[0].planLadingEndAmount
              this.$set(itemB.locallocalData, 'ladingMoney', [tempItem[0].planLadingBeginAmount, tempItem[0].planLadingEndAmount])
              this.$set(itemB.locallocalData, 'ladingTime', [this.compareTime(tempItem[0].planLadingBeginTime), this.compareTime(tempItem[0].planLadingEndTime)])
              this.$set(itemB.locallocalData, 'ifLading', tempItem[0].ifsLading)
              this.$set(itemB, 'speechCraft', tempItem[0].talkWord)
              this.$set(itemB, 'keyId', tempItem[0].keyId)
            }
            this.projectContactsInfo.push(itemB)
            this.getCompanyEmployee({ companyId: it.companyId })
          })
        }
      })
    },
    getCompanyEmployee (params) {
      ladingNoticeManageApi.callentErpriseInfoApi.getCompanyEmployee(params).then(res => {
        if (res && res.data) {
          this.projectContactsInfo.forEach(it => {
            if (it.enterpriseId === params.companyId) {
              it.localRemeteDatasourceData = []
              res.data.forEach(it1 => {
                // let { contactLevel, employeeDTO: { employeeName, telephone, weixin }, enterpriseAccountDTO: { account } } = it
                // /* { contactLevel, employeeName, telephone, weixin, isCreate: account ? '1' : '0', account }*/
                if (it1.employeeDTO && it1.employeeDTO.forbiddenStatus && it1.employeeDTO.forbiddenStatus !== '1') {
                  it.localRemeteDatasourceData.push(it1)
                }
              })
            }
          })
        }
      })
    },
    // 获取话术相关信息
    getInit () {
      this.api.getHandleHandle(this.form.handleId).then(res => {
        if (res) {
          this.talkSkill = res.data
          this.getEnterpriseArea()
        }
      })
    },
    // 显示履历
    detailsPopup () {
      this.formItem = this.form
      this.popupVisible = true
    },
    // 查看联系人
    showItem (data, index, idx1) {
      if (index === '0') {
        this.enterpriseType = '60'
        let temp = this.areaContactsInfoRemoteData.localRemeteDatasourceData.filter(it => {
          return it.enterpriseAccountDTO && it.enterpriseAccountDTO.accountId === data[0] && it.employeeId === data[1]
        })
        this.contactItem = temp[0].enterpriseAccountDTO
      } else {
        this.enterpriseType = '150'
        let temp = this.projectContactsInfo[idx1].localRemeteDatasourceData.filter(it => {
          return it.enterpriseAccountDTO && it.enterpriseAccountDTO.accountId === data[0] && it.employeeId === data[1]
        })
        this.contactItem = temp[0].enterpriseAccountDTO
      }
      this.$emit('showItem', this.enterpriseType, this.contactItem)
    }
  }
}
</script>

<style scoped lang="scss">
  @import '~@/styles/sj/ladingHandle/ladingHandleEdit.scss';
</style>
