<template>
  <div>
    <dialog-edit :visible.sync="dialogVisible" :editModel="editModel"
                 :title="$t('ladingNoticeHandle.Handle')" width="1240px" :autoSave="false" :footerBtnShow="false"
                 :api="api" :form="form" :appendToBody="true">
      <el-tabs v-model="activeName" :lazy="true">
        <el-tab-pane v-for="(item, index) in newFormValue" :label="`${item.coreAreaName}`" :name="item.handleId" :key="item.handleId">
          <keep-alive>
            <lading-handle-edit :item="item" :form="item"
                                :ref="'fromEdit' + index"
                                @showItem="showItem"
                                :menuCode="menuCode"
                                :visible.sync="showDetail" :editModel="editModel" :index="index" @changPage="changPage"
                                :areaContactsInfoRemoteData.sync="localData.areaContactsInfoRemoteData[index]"
                                :projectInfo.sync="localData.projectContactsInfo[index]"
                                v-if="activeName === item.handleId"></lading-handle-edit>
          </keep-alive>
        </el-tab-pane>
      </el-tabs>
      <template slot="footer">
        <auth-button size="" type="primary" :label="$t('common.save')" :menuCode="menuCode"
                     perms="handleSave" :openPerms="true" @click="onSaved" :loading="loading"></auth-button>
        <auth-button size="" :label="$t('common.cancel')" :menuCode="menuCode"
                     perms="handleCancel" :openPerms="true" @click="dialogVisible=false"></auth-button>
      </template>
    </dialog-edit>
    <!--查看联系人详情-->
    <contact-info-edit :visible.sync="showContact" :enterpriseType="enterpriseType"
                       :form="contactItem" :editModel="contactModel" :regMsg="regMsgList"
                       :employeeShow="true"></contact-info-edit>
  </div>
</template>

<script>
import BasePage from '@/pages/baseEditPage'
import { DialogEdit, authButton } from 'newepoch-components'
import ladingHandleEdit from '@/pages/shouji/ladingHandle/ladingHandleEdit'
import ladingNoticeManageApi from '@/api/shouji/ladingNoticeManageApi'
import ContactInfoEdit from '@/pages/enterpriseInfoManage/supplierInfoManage/contactInfoEdit/contactInfoEdit.vue'
export default {
  extends: new BasePage(),
  name: 'handleInfoTabEdit',
  components: {
    DialogEdit,
    authButton,
    ladingHandleEdit,
    ContactInfoEdit
  },
  props: {
    menuCode: {
      type: String,
      default: null
    }
  },
  data () {
    return {
      loading: false,
      showContact: false,
      enterpriseType: '60',
      contactModel: 'V',
      contactItem: {},
      regMsgList: this.$i18n.messages[this.$i18n.locale].regMsg,
      showDetail: true,
      item: {},
      activeName: '',
      localData: {
        areaContactsInfoRemoteData: [],
        projectContactsInfo: []
      },
      newFormValue: [],
      areaContactsInfoRemoteData: {
        localRemeteDatasourceData: [],
        locallocalData: {}
      }, // 区域数据
      projectContactsInfo: {
        data: [{
          localRemeteDatasourceData: [],
          locallocalData: {}
        }]
      },
      pageIndex: 0
    }
  },
  computed: {
    api () {
      return ladingNoticeManageApi.ladingNoticeManageListApi
    }
  },
  methods: {
    changPage (index) {
      this.pageIndex = index
    },
    // 保存前处理日期
    riqiHandle () {
      this.localData.areaContactsInfoRemoteData.forEach((areaContactsInfoRemoteData, index) => {
        if (index === this.pageIndex) {
          if (this.UtilJs.isEmpty(areaContactsInfoRemoteData.locallocalData.ladingTime) || areaContactsInfoRemoteData.locallocalData.ladingTime[0].length === 0 || areaContactsInfoRemoteData.locallocalData.ladingTime[1].length === 0) {
            areaContactsInfoRemoteData.locallocalData.ladingTime = null
          }
        }
      })
      this.localData.projectContactsInfo.forEach((it, idx) => {
        if (idx === this.pageIndex) {
          let projectContactsInfo = it.data
          projectContactsInfo.forEach(it => {
            if (this.UtilJs.isEmpty(it.locallocalData.ladingTime) || it.locallocalData.ladingTime[0].length === 0 || it.locallocalData.ladingTime[1].length === 0) {
              it.locallocalData.ladingTime = null
            }
          })
        }
      })
    },
    // 保存
    onSaved () {
      this.riqiHandle()
      let flag = false
      if (this.localData.projectContactsInfo[this.pageIndex].length === 0) {
        return this.warning('项目公司为空，请到参与机构维护')
      }
      this.$refs['fromEdit' + this.pageIndex].forEach(it1 => {
        if (it1) {
          let temp = Object.keys(it1.$refs)
          temp.forEach(it2 => {
            let temp = {}
            if (typeof it1.$refs[it2].$refs === 'object') {
              temp = it1.$refs[it2].$refs.check
            } else {
              if (it1.$refs[it2][0]) {
                temp = it1.$refs[it2][0].$refs.check
              }
            }
            if (Object.keys(temp).length > 0) {
              temp.validate((valid) => {
                if (!valid) flag = true
              })
            }
          })
        }
      })
      if (flag) {
        return this.error(this.$i18n.messages[this.$i18n.locale].common.formrulesMessage)
      }
      if (this.localData.areaContactsInfoRemoteData && this.localData.areaContactsInfoRemoteData.length === 0) {
        return this.error(this.$i18n.messages[this.$i18n.locale].common.nodatanosave)
      }
      let item = {}
      if (this.editModel === 'A') {
        item.assetcollectLadingId = this.form.data[this.pageIndex].keyId
        item.coreAreaCommissionId = this.form.data[this.pageIndex].coreAreaAttache
        item.coreAreaCommissionName = this.form.data[this.pageIndex].coreAreaAttacheName
        item.coreAreaId = this.form.data[this.pageIndex].coreArea
        item.coreAreaName = this.form.data[this.pageIndex].coreAreaName
        item.keyId = this.form.data[this.pageIndex].handleId
      } else {
        item.assetcollectLadingId = this.form.keyId
        item.coreAreaCommissionId = this.form.coreAreaAttache
        item.coreAreaCommissionName = this.form.coreAreaAttacheName
        item.coreAreaId = this.form.coreArea
        item.coreAreaName = this.form.coreAreaName
        item.keyId = this.form.handleId
      }
      let $this = this
      this.localData.areaContactsInfoRemoteData.forEach((areaContactsInfoRemoteData, index) => {
        if (index === $this.pageIndex) {
          let handleContractInfoDtoList = []
          if (areaContactsInfoRemoteData.keyId) {
            let planLadingBeginAmount = ['', '']
            if (areaContactsInfoRemoteData.locallocalData.ladingMoney) {
              planLadingBeginAmount[0] = areaContactsInfoRemoteData.locallocalData.ladingMoney[0].toString()
              planLadingBeginAmount[0] = planLadingBeginAmount[0].length === 0 ? '0' : planLadingBeginAmount[0]
              planLadingBeginAmount[1] = areaContactsInfoRemoteData.locallocalData.ladingMoney[1].toString()
              planLadingBeginAmount[1] = planLadingBeginAmount[1].length === 0 ? '0' : planLadingBeginAmount[1]
            } else {
              planLadingBeginAmount[0] = '0'
              planLadingBeginAmount[1] = '0'
            }
            let planLadingBeginTime = ['', '']
            if (areaContactsInfoRemoteData.locallocalData.ladingTime) {
              planLadingBeginTime[0] = areaContactsInfoRemoteData.locallocalData.ladingTime[0]
              planLadingBeginTime[1] = areaContactsInfoRemoteData.locallocalData.ladingTime[1]
            } else {
              planLadingBeginTime[0] = '1900-01-01'
              planLadingBeginTime[1] = '1900-01-01'
            }
            handleContractInfoDtoList.push({
              // assetcollectLadingHandleId: this.form.keyId,
              contactType: '0', // 联系人类型(0:区域联系人 / 1:项目公司联系人)
              ifsLading: areaContactsInfoRemoteData.locallocalData.ifLading,
              planLadingBeginAmount: planLadingBeginAmount[0],
              planLadingEndAmount: planLadingBeginAmount[1],
              keyId: areaContactsInfoRemoteData.locallocalData.keyId ? areaContactsInfoRemoteData.locallocalData.keyId : '',
              planLadingBeginTime: planLadingBeginTime[0],
              planLadingEndTime: planLadingBeginTime[1],
              talkWord: areaContactsInfoRemoteData.speechCraft
            })
          }
          item.handleContractInfoDtoList = handleContractInfoDtoList
        }
      })
      let handleContractInfoDtoList = []
      this.localData.projectContactsInfo.forEach((it, idx) => {
        if (idx === $this.pageIndex) {
          let projectContactsInfo = it.data
          projectContactsInfo.forEach(it => {
            let planLadingBeginAmount = ['', '']
            if (it.locallocalData.ladingMoney) {
              planLadingBeginAmount[0] = it.locallocalData.ladingMoney[0]
              planLadingBeginAmount[0] = planLadingBeginAmount[0].length === 0 ? '0' : planLadingBeginAmount[0]
              planLadingBeginAmount[1] = it.locallocalData.ladingMoney[1]
              planLadingBeginAmount[1] = planLadingBeginAmount[1].length === 0 ? '0' : planLadingBeginAmount[1]
            } else {
              planLadingBeginAmount[0] = '0'
              planLadingBeginAmount[1] = '0'
            }
            let planLadingBeginTime = ['', '']
            if (it.locallocalData.ladingTime) {
              planLadingBeginTime[0] = it.locallocalData.ladingTime[0]
              planLadingBeginTime[1] = it.locallocalData.ladingTime[1]
            } else {
              planLadingBeginTime[0] = '1900-01-01'
              planLadingBeginTime[1] = '1900-01-01'
            }
            handleContractInfoDtoList.push({
              // assetcollectLadingHandleId: this.form.handleId,
              contactType: '1', // 联系人类型(0:区域联系人 / 1:项目公司联系人)
              enterpriseId: it.enterpriseId,
              enterpriseName: it.expressNumber,
              ifsLading: it.locallocalData.ifLading,
              keyId: it.keyId ? it.keyId : '',
              planLadingBeginAmount: planLadingBeginAmount[0],
              planLadingEndAmount: planLadingBeginAmount[1],
              planLadingBeginTime: planLadingBeginTime[0],
              planLadingEndTime: planLadingBeginTime[1],
              talkWord: it.speechCraft
            })
          })
          item.handleContractInfoDtoList.push(...handleContractInfoDtoList)
        }
      })
      this.getSave(item)
    },
    getSave (data) {
      let $this = this
      this.loading = true
      this.api.getSave(data).then(res => {
        if (res && res.data) {
          $this.success($this.$i18n.messages[$this.$i18n.locale].common.saveSuccess)
          if ($this.editModel !== 'A') {
            $this.dialogVisible = false
          }
        } else {
          this.loading = false
        }
      }).finally(res => {
        this.loading = false
      })
    },
    showItem (data, index) {
      this.enterpriseType = data
      this.contactItem = index
      this.contactItem.tagCode = data
      this.showContact = true
    }
  },
  watch: {
    dialogVisible: {
      deep: true,
      immediate: true,
      handler (now, old) {
        if (now) {
          this.loading = false
          this.$nextTick(() => {
            if (this.editModel === 'A') {
              this.newFormValue = this.form.data
            } else {
              this.newFormValue.push(this.form)
            }
            this.activeName = this.newFormValue[0].handleId
            this.newFormValue.forEach(it => {
              this.localData.areaContactsInfoRemoteData.push(this.areaContactsInfoRemoteData)
              this.localData.projectContactsInfo.push(this.projectContactsInfo)
            })
          })
        } else {
          this.newFormValue = []
          this.areaContactsInfoRemoteData = {
            localRemeteDatasourceData: [],
            locallocalData: {}
          }
          this.projectContactsInfo = {
            data: [{
              localRemeteDatasourceData: [],
              locallocalData: {}
            }]
          }
          this.localData.areaContactsInfoRemoteData = []
          this.localData.projectContactsInfo = []
        }
      }
    }
  }
}
</script>

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