<template>
  <div>
    <DialogEdit :autoClose="true" @close="close" :footerBtnShow="false" :form="form" :title="$t('traceMgr.addSupplierContact')" :visible.sync="dialogVisible" class="el-dialog--center" :close-on-click-modal="false" :editModel="editModel">
      <order-list :assetCodes="assetCode" :orderListResetData="orderListResetData" :checkDialogClose="checkDialogClose" :dataReset="dataReset" @func="getMsgOrder" ></order-list>
      <contact-list :dataReset="dataReset" :supplierId="supplierId" :contactResetData="contactResetData" :checkDialogClose="checkDialogClose" @func="getMsgContacts" ref="mychild"></contact-list>

      <template >
        <div :key="key" v-for="(item,key) in msgContacts">
          <span v-show="doCreateWords" class="marginBottom2 title">{{$t('common.contact')}}：{{item.employeeName}}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{$t('traceMgr.phoneNumber')}}：{{item.telephone}}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{$t('traceMgr.loginInfo')}}：{{item.account}}</span>
        </div>
      </template>
      <el-row v-show="doCreateWords" class="word_wrap">
        <el-col :span="1" class="text-center">
          <span>{{$t('traceMgr.wordsTechnique')}}</span>
        </el-col>
        <el-col :span="12">
          <speech-craft-input v-model="content"  :defaultSpeechFlag="false" @defaultSpeech="setDefaultSpeech"></speech-craft-input>
        </el-col>
      </el-row>

      <div slot="footer" class="dialog-footer">
        <auth-button size="medium" type="primary" :label="$t('traceMgr.createWords')" @click="createWords"></auth-button>
        <auth-button size="medium" type="primary" :label="$t('traceMgr.checkAdd')" @click="checkAdd"></auth-button>
        <auth-button size="medium" type="primary" :label="$t('traceMgr.accountAdd')" @click="addAccount"></auth-button>
        <el-button size="medium" @click="dialogVisible=false">{{$t('common.close')}}</el-button>
      </div>

    </DialogEdit>
    <DialogEdit :footerBtnShow="false" :form="form" editModel="E" :visible.sync="doAdd" :title="$t('common.tip')" width="35%" :close-on-click-modal="false" class="el-dialog--center">
      <p class="text-center">
        {{$t('traceMgr.areaAddConcat')}}
        {{area}}
        {{$t('traceMgr.areaAddConcatText')}}
        {{name}}
        {{$t('traceMgr.areaAddConcatTextTwo')}}
      </p>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" size="medium" :loading="loading" @click="taskSure">{{$t('traceMgr.sure')}}</el-button>
        <el-button type="button" size="medium" @click="closeSure()">{{$t('traceMgr.pageClose')}}</el-button>
      </div>
    </DialogEdit>
    <contact-maintain-edit :visible.sync="showContact" :menuId="menuId" @createSuccess="createSuccess" :form="item" :editModel="editModel" :regMsg="regMsg"></contact-maintain-edit>
  </div>
</template>

<script>
import orderList from './orderList'
import contactList from './contactList'
import tracemgrApi from '@/api/tracemgrApi'
import { DataTable, authButton, DialogEdit, speechCraftInput } from 'newepoch-components'
import mixin from '@/pages/mixin'
import BasePage from '@/pages/basePage'
import contactMaintainEdit from '@/pages/enterpriseInfoManage/supplierInfoManage/contactMaintainEdit'
export default {
  extends: new BasePage(),
  mixins: [mixin],
  components: {
    orderList,
    DataTable,
    authButton,
    DialogEdit,
    contactList,
    tracemgrApi,
    contactMaintainEdit,
    speechCraftInput
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    form: {
      type: Object
    },
    handler: {
      type: Function
    }
  },
  computed: {
    dialogVisible: {
      get: function () {
        return this.visible
      },
      set: function (val) {
        this.$emit('update:visible', val)
      }
    },
    api () {
      return tracemgrApi['taskDialogs']
    }
  },
  watch: {
    visible: function (newValue, oldValue) {
      let $this = this
      this.$nextTick(() => {
        if (newValue) {
          $this.supplierId = $this.form.param[0].creditorId
          $this.form.param.forEach(item => {
            $this.assetCode.push(item.assetCode)
          })
          $this.dataReset = true
          tracemgrApi['orderTrackApi'].getPhone().then(info => {
            $this.userInfo = info.data
          }, res => {
            $this.error(this.$i18n.messages[this.$i18n.locale].traceMgr.getUserInfoFial)
          })
        } else {
          $this.assetCode = []
          $this.dataReset = false
        }
      })
    }
  },
  data () {
    return {
      showContact: false,
      loading: false,
      assetCode: [],
      userInfo: {},
      dataReset: false,
      orderListResetData: {
        check: false,
        dataList: []
      },
      contactResetData: {
        check: false,
        dataList: []
      },
      contactListResetData: '',
      doAdd: false,
      doCreateWords: false,
      name: '',
      content: '',
      supplierId: '',
      area: '',
      contacts: '',
      account: '',
      // 联系人组件传回的值
      msgContacts: [],
      // 联系人组件传回的值
      msgOrder: []
    }
  },
  methods: {
    createWords () {
      if (this.doCreateWords) {
        this.doCreateWords = false
        return
      }
      //  校验订单信息
      if (this.msgOrder.length < 1) {
        this.error(this.$i18n.messages[this.$i18n.locale].traceMgr.selectOrder)
        return
      }
      //  校验联系人信息
      if (this.msgContacts.length !== 1) {
        this.error(this.$i18n.messages[this.$i18n.locale].traceMgr.selectRowOnes)
        return
      }
      this.resetSpeechCraft()
      this.doCreateWords = !this.doCreateWords
    },
    checkAdd () {
      if (this.msgOrder.length < 1) {
        this.error(this.$i18n.messages[this.$i18n.locale].traceMgr.selectOrder)
        return
      }
      if (this.msgContacts.length < 1) {
        this.error(this.$i18n.messages[this.$i18n.locale].traceMgr.selectContacts)
        return
      }
      if (this.msgContacts.length > 1) {
        this.error(this.$i18n.messages[this.$i18n.locale].traceMgr.contactList)
        return
      }
      if (this.msgContacts[0].account === '' || this.msgContacts[0].account === undefined) {
        this.error(this.$i18n.messages[this.$i18n.locale].traceMgr.contactAccountEmpty)
        return
      }
      let district = ''
      let $this = this
      this.msgOrder.forEach(function (val, index) {
        if ($this.msgOrder.length > 1) {
          if (index === 0) {
            district += val.coreAreaName
          } else {
            if (district.indexOf(val.coreAreaName) === -1) {
              district += ',' + val.coreAreaName
            }
          }
        } else {
          district = val.coreAreaName
        }
      })
      this.area = district
      this.name = this.msgContacts[0].employeeName
      this.doAdd = !this.doAdd
    },
    addAccount () {
      if (this.msgOrder.length !== 1) {
        this.error(this.$i18n.messages[this.$i18n.locale].traceMgr.selectRowOne)
        return
      }
      this.editModel = 'C'
      this.item = { tagCode: '140', enterpriseId: this.msgOrder[0].creditorId, loading: false }
      this.showContact = true
    },
    close () {
      this.doCreateWords = false
      this.dialogVisible = false

      // this.closeAddSupplierContact()
    },
    getMsgContacts (data) {
      this.msgContacts = data
    },
    getMsgOrder (data) {
      this.msgOrder = data
    },
    closeSure () {
      this.doAdd = false
    },
    taskSure () {
      this.loading = true
      let $this = this
      //  重置
      $this.orderListResetData.check = false
      $this.contactResetData.check = false
      $this.orderListResetData.dataList = []
      $this.contactResetData.dataList = []
      let accountAreaRelRequestDTO = []
      let updateByAssetInfoParam = []
      let paramJosn = ''
      $this.msgOrder.forEach(function (val, index) {
        let data = {}
        let param = {}
        data.enterpriseId = $this.msgContacts[0].enterpriseId
        data.coreEnterpriseId = val.coreEnterpriseId
        data.coreEnterpriseName = val.coreEnterpriseName
        data.areaId = val.coreArea
        data.areaName = val.coreAreaName
        data.employeeId = $this.msgContacts[0].employeeId
        data.employeeName = $this.msgContacts[0].employeeName
        data.employeeAccountType = $this.msgContacts[0].employeeAccountType
        data.accountId = $this.msgContacts[0].accountId
        param.assetInfoId = val.assetInfoId
        accountAreaRelRequestDTO.push(data)
        updateByAssetInfoParam.push(param)
        $this.form.param.forEach(item => {
          if (item.assetCode === val.assetCode) {
            paramJosn += item.taskId += ','
          }
        })
      })
      tracemgrApi['taskDialogs'].save({ accountAreaRelRequestDTO: accountAreaRelRequestDTO, updateByAssetInfoParam: updateByAssetInfoParam, taskId: paramJosn }).then(res => {
        if (res.data) {
          $this.msgOrder.forEach(item => {
            $this.orderListResetData['dataList'].push(item.assetInfoId)
          })
          $this.msgContacts.forEach(item => {
            $this.contactResetData['dataList'].push(item.employeeId)
          })
          $this.orderListResetData['check'] = true
          $this.contactResetData['check'] = true
          $this.loading = false
          $this.doCreateWords = false
          $this.doAdd = false
        } else {
          $this.error(this.$i18n.messages[this.$i18n.locale].traceMgr.noticeFailure)
          $this.loading = false
        }
      }, res => {
        $this.error(this.$i18n.messages[this.$i18n.locale].traceMgr.noticeFailure)
        $this.loading = false
      })
    },
    // 新增成功调查询
    createSuccess () {
      this.refreshList()
    },
    checkDialogClose () {
      let $this = this
      $this.dialogVisible = false
      $this.handler()
    },
    // 调用联系人组件刷新列表方法
    refreshList () {
      this.$refs.mychild.handleFilter()
    },
    resetSpeechCraft () {
      let text = this.$i18n.messages[this.$i18n.locale].traceMgr.businessProcessTaskHolleMyIsxx
      // 拼接 【核心企业】
      let coreEnterpriseName = ''
      if (this.msgOrder.length > 1) {
        this.msgOrder.forEach(item => {
          if (item.coreEnterpriseName) {
            coreEnterpriseName = item.coreEnterpriseName
          }
        })
      }
      text = text + coreEnterpriseName
      text += this.$i18n.messages[this.$i18n.locale].traceMgr.businessProcessTaskintroduceOneselfTo
      //  保理专员
      text = text + this.userInfo.nickName + '。'
      text += this.$i18n.messages[this.$i18n.locale].traceMgr.businessProcessOne
      //  保理金额
      let factoringMoneys = []
      //  合同名称
      let basicContractNames = []
      this.msgOrder.forEach(item => {
        factoringMoneys.push(item.factoringMoney)
        basicContractNames.push(item.basicContractName)
      })
      factoringMoneys.forEach((item, key) => {
        text = text + this.$i18n.messages[this.$i18n.locale].traceMgr.two + basicContractNames[key] + this.$i18n.messages[this.$i18n.locale].traceMgr.one + factoringMoneys[key] + '\n'
      })
      text = text + this.$i18n.messages[this.$i18n.locale].traceMgr.there
      text = text + this.$i18n.messages[this.$i18n.locale].traceMgr.website
      text = text + this.$i18n.messages[this.$i18n.locale].traceMgr.four
      text = text + this.userInfo.nickName
      text = text + this.$i18n.messages[this.$i18n.locale].traceMgr.five
      text = text + this.$i18n.messages[this.$i18n.locale].traceMgr.six
      text = text + this.userInfo.mobile
      this.content = text
    },
    setDefaultSpeech () {
      this.resetSpeechCraft()
    }
  }
}
</script>
