<template>
  <!--查看业务数据-->
  <dialog-edit :visible.sync="dialogVisible"
               :title="$t('traceMgr.viewBusinessData')"
               :editModel="editModel"
               :form="form"
               width="70%"
               :appendToBody="true"
               :autoClose="true"
               @close="closeDialogVisible"
               :footerBtnShow="false">
    <el-form ref="form" :api="api" :model="queryParas" style="margin-bottom: 30px" :rules="rules">
      <el-row>
        <!--产品-->
        <el-col :span="7">
          <el-form-item :label="$t('traceMgr.product')" prop="productId" :label-width="'71px'">
            <!--                        <select-product-name   v-model="productId"></select-product-name>-->
            <select-dictionary :dictTypeCode="'ZD0087'" @selectChanged="selectChanged"
                               v-model="queryParas.productId"></select-dictionary>
          </el-form-item>
        </el-col>
        <!--核心企业-->
        <el-col :span="7">
          <el-form-item :label="$t('traceMgr.coreEnterprise')" prop="coreEnterpriseId" :label-width="'100px'">
            <select-core-enterprise @selectChanged="selectqiye"
                                    v-model="queryParas.coreEnterpriseId"></select-core-enterprise>
          </el-form-item>
        </el-col>
        <!--计划管理人/再保理银行-->
        <el-col :span="10">
          <el-form-item :label="$t('traceMgr.financialInstitutions')" prop="planOrBankId" :label-width="'230px'">
            <select-financial-institutions v-model="queryParas.planOrBankId"></select-financial-institutions>
          </el-form-item>
        </el-col>
        <!--期数-->
        <el-col :span="7">
          <el-form-item :label="$t('traceMgr.periodsCascade')" prop="specialPlanPeriods" :label-width="'71px'">
            <select-flag :options="options" :disabled="disabled" v-model="queryParas.specialPlanPeriods"></select-flag>
          </el-form-item>
        </el-col>
        <!--提单日期-->
        <el-col :span="7">
          <el-form-item :label="$t('traceMgr.billOfLadingDate')" prop="factoringApplyTime" :label-width="'100px'">
            <div class="inline-block">
              <el-date-picker
                v-model="queryParas.factoringApplyTime"
                type="date"
                value-format="yyyy-MM-dd"
                :placeholder="$t('common.point')"
                :picker-options="pickerOptions"
                style="width: 300px">
              </el-date-picker>

            </div>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <auth-button type="primary" size="medium" :label="$t('common.search')" :loading="showLoading" @click="showQuery('form')"
                       style="margin-left: 50px"></auth-button>
          <auth-button type="primary" size="medium" :label="$t('common.reset')" :loading="showLoading" @click="resetQuery"
                       style="margin-left: 10px"></auth-button>
        </el-col>
      </el-row>
    <template v-if="query">
      <el-row>
        <data-table v-if="query" :menuCode="menuId"
                    v-loading="loading2"
                    :element-loading-text="elementLoadingText"
                    element-loading-spinner="el-icon-loading"
                    element-loading-background="rgba(255, 255, 255, 1)"
                    :api="api"
                    :loadCount.sync="loadCount"
                    :menuFlag="false"
                    :handleFlag.sync="handleFlag"
                    :sourceData.sync="sourceData"
                    :columns="columns"
                    :queryParas="queryParas"
                    :operateBtnLayout="''"
                    :showPage='false'
        >
        </data-table>
      </el-row>
      <el-row style="margin-top: 10px;">
        <el-col :span="1" class="leftText">
          <span>{{$t('common.discourse')}}</span>
        </el-col>
        <el-col :span="23">
          <speech-craft-input v-model="content" :defaultSpeechFlag="false"
                              @defaultSpeech="setDefaultSpeech"></speech-craft-input>
        </el-col>
      </el-row>
    </template>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="button" @click="closeDialogVisible()" size="medium">{{$t('common.close')}}</el-button>
    </div>
  </dialog-edit>
</template>
<script>
import BaseEditPage from '@/pages/baseEditPage'
import {
  DataTable,
  DialogEdit,
  SelectProductName,
  SelectCoreEnterprise,
  SelectFinancialInstitutions,
  SelectPeriodsCascade,
  authButton,
  speechCraftInput,
  SelectDictionary
} from 'newepoch-components'
import tracemgrApi from '@/api/tracemgrApi'
import SelectFlag from '@/components/tracemgr/common/SelectFlag'

export default {
  name: 'businessDataStatisticsDialog',
  extends: new BaseEditPage(''),
  props: {
    editModel: {
      type: String
    },
    orderType: {
      type: String
    },
    importQueryParas: {
      type: Object
    },
    form: {
      type: Object
    }
  },
  components: {
    DataTable,
    DialogEdit,
    SelectProductName,
    SelectCoreEnterprise,
    SelectFinancialInstitutions,
    SelectPeriodsCascade,
    authButton,
    speechCraftInput,
    tracemgrApi,
    SelectFlag,
    SelectDictionary
  },
  computed: {
    dialogVisible: {
      get: function () {
        return this.visible
      },
      set: function (value) {
        this.$emit('update:visible', value)
      }
    },
    api () {
      return tracemgrApi['businessDataStatisticsDialog']
    }
  },
  data () {
    return {
      // 点击查询按钮的次数
      clickQuery: 0,
      // 第一次点击查询按钮后的话术内容
      firstConent: '',
      disabled: false,
      loadCount: 0,
      handleFlag: true,
      loading2: false,
      elementLoadingText: this.$i18n.messages[this.$i18n.locale].traceMgr.loading,
      productNum: '22',
      content: this.$i18n.messages[this.$i18n.locale].traceMgr.businessDataStatisticsDialogConent,
      options: [{ label: 1, value: 1 }, { label: 2, value: 2 }, { label: 3, value: 3 }, {
        label: 4,
        value: 4
      }, { label: 5, value: 5 }, { label: 6, value: 6 },
      { label: 7, value: 7 }, { label: 8, value: 8 }, { label: 9, value: 9 }, { label: 10, value: 10 }, {
        label: 11,
        value: 11
      }, { label: 12, value: 12 },
      { label: 13, value: 13 }, { label: 14, value: 14 }, { label: 15, value: 15 }, {
        label: 16,
        value: 16
      }, { label: 17, value: 17 }, { label: 18, value: 18 },
      { label: 19, value: 19 }, { label: 20, value: 20 }, { label: 21, value: 21 }, {
        label: 22,
        value: 22
      }, { label: 23, value: 23 }, { label: 24, value: 24 },
      { label: 25, value: 25 }, { label: 26, value: 26 }, { label: 27, value: 27 }, {
        label: 28,
        value: 28
      }, { label: 29, value: 29 }, { label: 30, value: 30 },
      { label: 31, value: 31 }, { label: 32, value: 32 }, { label: 33, value: 33 }, {
        label: 34,
        value: 34
      }, { label: 35, value: 35 }, { label: 36, value: 36 },
      { label: 37, value: 37 }, { label: 38, value: 38 }, { label: 39, value: 39 }, {
        label: 40,
        value: 40
      }, { label: 41, value: 41 }, { label: 42, value: 42 },
      { label: 43, value: 43 }, { label: 44, value: 44 }, { label: 45, value: 45 }, {
        label: 46,
        value: 46
      }, { label: 47, value: 47 }, { label: 48, value: 48 },
      { label: 49, value: 49 }, { label: 50, value: 50 }],
      queryParas: {
        productId: '',
        coreEnterpriseId: '',
        coreEnterpriseName: '',
        planOrBankId: '',
        planOrBankName: '',
        specialPlanPeriods: '',
        factoringApplyTime: '',
        productName: ''
      },
      rules: {
        productId: [
          { required: true, message: this.$i18n.messages[this.$i18n.locale].traceMgr.pleaseSelectTheProduct, trigger: 'change' }
        ],
        coreEnterpriseId: [
          { required: true, message: this.$i18n.messages[this.$i18n.locale].traceMgr.pleaseSelectTheCoreEnterpriseId, trigger: 'change' }
        ],
        planOrBankId: [
          { required: true, message: this.$i18n.messages[this.$i18n.locale].traceMgr.pleaseSelectThePlanOrBankId, trigger: 'change' }
        ],
        specialPlanPeriods: [
          { required: true, message: this.$i18n.messages[this.$i18n.locale].traceMgr.pleaseSelectTheSpecialPlanPeriods, trigger: 'change' }
        ],
        factoringApplyTime: [
          { required: true, message: this.$i18n.messages[this.$i18n.locale].traceMgr.pleaseSelectTheFactoringApplyTime, trigger: 'change' }
        ]
      },
      initFlag: false,
      query: false,
      showLoading: false,
      needComplete: false,
      productId: '',
      coreEnterpriseId: '',
      planOrBankId: '',
      specialPlanPeriods: '',
      sourceData: [],
      name: '',
      nameId: '',
      qiye: '',
      qiyeId: '',
      period: '',
      // 表格合并参数
      spanArr: [],
      sIdx: 0,
      position: 0,
      sameRowArr: [],
      columns: [
        {
          prop: 'coreAreaName',
          align: 'center',
          label: '',
          child: [// 业务分部
            {
              prop: 'orgName',
              align: 'center',
              label: this.$i18n.messages[this.$i18n.locale].traceMgr.coreAreaName
            },
            // 区域
            {
              prop: 'coreAreaName',
              align: 'center',
              label: this.$i18n.messages[this.$i18n.locale].traceMgr.region
            },
            // 笔数
            {
              prop: 'orderNum',
              align: 'center',
              label: this.$i18n.messages[this.$i18n.locale].traceMgr.num
            },
            // 宝丽金额
            {
              prop: 'factoringMoney',
              align: 'center',
              label: this.$i18n.messages[this.$i18n.locale].traceMgr.factoringAmount
            }
          ]
        }

      ],
      factoringApplyTime: '',
      pickerOptions: {
        disabledDate (time) {
          return time.getTime() > Date.now() - 8.64e6
        }
      }
    }
  },
  watch: {
    dialogVisible (value, oldVal) {
      if (value) {
        this.queryParas.coreEnterpriseId = this.importQueryParas.coreEnterpriseId
        this.queryParas.productId = this.importQueryParas.productId
        this.queryParas.planOrBankId = this.importQueryParas.planOrBankId
        this.queryParas.specialPlanPeriods = this.importQueryParas.specialPlanPeriods
        this.queryParas.factoringApplyTime = this.importQueryParas.factoringApplyTime
        this.name = this.importQueryParas.name
        this.nameId = this.importQueryParas.nameId
        this.qiye = this.importQueryParas.qiye
        this.qiyeId = this.importQueryParas.qiyeId
        this.queryParas.orderType = this.orderType
        this.queryParas.productName = this.importQueryParas.productName

        // 关闭父组件
        this.$emit('closeParent')

        this.showQuery('form')
      }
    }
  },
  methods: {
    reset () {
      this.queryParas = {
        productId: '',
        coreEnterpriseId: '',
        coreEnterpriseName: '',
        planOrBankId: '',
        planOrBankName: '',
        specialPlanPeriods: '',
        factoringApplyTime: ''
      }
      this.$refs['form'].resetFields()
    },
    setDefaultSpeech () {
      this.resetSpeechCraft()
    },
    resetSpeechCraft () {
      // let text = '各位领导好，以下是XX年X月X日（日期）万科ABS（ 核心企业+金融机构）提单情况汇总：\n' +
      //     '共提单：XX笔（提单总笔数），总金额：XX亿（提单总金额）\n' +
      //     '详情如下表：' // this.$i18n.messages[this.$i18n.locale].traceMgr.contentInit
      this.content = this.firstConent
    },
    selectChanged (v, a, b) {
      this.name = b.dictName
      this.nameId = v
      if (v === '1') {
        this.rules.factoringApplyTime[0].required = false
        this.rules.specialPlanPeriods[0].required = true
        this.disabled = false
      }
      if (v === '2') {
        this.rules.factoringApplyTime[0].required = true
        this.rules.specialPlanPeriods[0].required = false
        this.disabled = true
        this.queryParas.specialPlanPeriods = ''
      }
    },
    selectqiye (v, a, b) {
      this.qiye = b.enterpriseName
      this.qiyeId = v
    },
    showQuery (formName) {
      this.clickQuery++
      if (this.queryParas.productId === '1') {
        this.rules.factoringApplyTime[0].required = false
        this.rules.specialPlanPeriods[0].required = true
        this.queryParas.factoringApplyTime = ''
      }
      if (this.queryParas.productId === '2') {
        this.rules.factoringApplyTime[0].required = true
        this.rules.specialPlanPeriods[0].required = false
      }
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // this.showLoading = true
          this.query = true
          this.sourceData = []
          // 每次查询都清空这个，不然合并就会少合并，或者多合并的错乱
          this.spanArr = []
          this.loading2 = true
          let $this = this
          tracemgrApi['businessDataStatisticsDialog'].getBusinessData({
            'coreEnterpriseId': this.queryParas.coreEnterpriseId,
            'productId': this.queryParas.productId,
            'planOrBankId': this.queryParas.planOrBankId,
            'specialPlanPeriods': this.queryParas.specialPlanPeriods,
            'factoringApplyTime': this.queryParas.factoringApplyTime,
            'orderType': this.orderType,
            'productName': this.queryParas.productName
          }).then(res => {
            // content: '各位领导好，以下是XX年X月X日（日期）万科ABS（ 核心企业+金融机构）提单情况汇总：\n' +
            // '共提单：XX笔（提单总笔数），总金额：XX亿（提单总金额）\n' +
            // '详情如下表：',
            this.initContent = JSON.parse(JSON.stringify(this.content))
            this.sourceData = res.data
            // let len = res.data.length - 1 + ''
            var end = res.data[res.data.length - 1]
            if ($this.nameId === '1') {
              this.columns[0].label = this.name + '-' + this.qiye + '-' + this.queryParas.specialPlanPeriods + this.$i18n.messages[this.$i18n.locale].traceMgr.caseSummary + end.orderNum + '' + this.$i18n.messages[this.$i18n.locale].traceMgr.stock
              this.content = this.$i18n.messages[this.$i18n.locale].traceMgr.businessDataStatisticsDialogConent1 + this.queryParas.specialPlanPeriods +
                this.$i18n.messages[this.$i18n.locale].traceMgr.expect + this.qiye + this.name + this.$i18n.messages[this.$i18n.locale].traceMgr.businessDataStatisticsDialogConent2 +
                this.$i18n.messages[this.$i18n.locale].traceMgr.businessDataStatisticsDialogConent3 + end.orderNum +
                this.$i18n.messages[this.$i18n.locale].traceMgr.businessDataStatisticsDialogConent4 + end.factoringMoney + '\n' +
                this.$i18n.messages[this.$i18n.locale].traceMgr.businessDataStatisticsDialogConent5
              this.firstConent = this.content
              if (this.clickQuery === 1) {
                this.firstConent = this.$i18n.messages[this.$i18n.locale].traceMgr.businessDataStatisticsDialogConent1 + this.queryParas.specialPlanPeriods +
                  this.$i18n.messages[this.$i18n.locale].traceMgr.expect + this.qiye + this.name + this.$i18n.messages[this.$i18n.locale].traceMgr.businessDataStatisticsDialogConent2 +
                  this.$i18n.messages[this.$i18n.locale].traceMgr.businessDataStatisticsDialogConent3 + end.orderNum +
                  this.$i18n.messages[this.$i18n.locale].traceMgr.businessDataStatisticsDialogConent4 + end.factoringMoney + '\n' +
                  this.$i18n.messages[this.$i18n.locale].traceMgr.businessDataStatisticsDialogConent5
              }
            }
            if ($this.nameId === '2') {
              this.columns[0].label = this.name + '-' + this.qiye + '-' + this.queryParas.factoringApplyTime + this.$i18n.messages[this.$i18n.locale].traceMgr.caseSummary + end.orderNum + '' + this.$i18n.messages[this.$i18n.locale].traceMgr.stock
              this.content = this.$i18n.messages[this.$i18n.locale].traceMgr.businessDataStatisticsDialogConent1 + '：' + this.queryParas.factoringApplyTime +
                this.qiye + this.name + this.$i18n.messages[this.$i18n.locale].traceMgr.businessDataStatisticsDialogConent2 +
                this.$i18n.messages[this.$i18n.locale].traceMgr.businessDataStatisticsDialogConent3 + end.orderNum +
                this.$i18n.messages[this.$i18n.locale].traceMgr.businessDataStatisticsDialogConent4 + end.factoringMoney + '\n' +
                this.$i18n.messages[this.$i18n.locale].traceMgr.businessDataStatisticsDialogConent5
              this.firstConent = this.content
              if (this.clickQuery === 1) {
                this.firstConent = this.$i18n.messages[this.$i18n.locale].traceMgr.businessDataStatisticsDialogConent1 + this.queryParas.specialPlanPeriods +
                  this.$i18n.messages[this.$i18n.locale].traceMgr.expect + this.qiye + this.name + this.$i18n.messages[this.$i18n.locale].traceMgr.businessDataStatisticsDialogConent2 +
                  this.$i18n.messages[this.$i18n.locale].traceMgr.businessDataStatisticsDialogConent3 + end.orderNum +
                  this.$i18n.messages[this.$i18n.locale].traceMgr.businessDataStatisticsDialogConent4 + end.factoringMoney + '\n' +
                  this.$i18n.messages[this.$i18n.locale].traceMgr.businessDataStatisticsDialogConent5
              }
              this.firstConent = this.$i18n.messages[this.$i18n.locale].traceMgr.businessDataStatisticsDialogConent1 + '：' + this.queryParas.factoringApplyTime +
                this.qiye + this.name + this.$i18n.messages[this.$i18n.locale].traceMgr.businessDataStatisticsDialogConent2 +
                this.$i18n.messages[this.$i18n.locale].traceMgr.businessDataStatisticsDialogConent3 + end.orderNum +
                this.$i18n.messages[this.$i18n.locale].traceMgr.businessDataStatisticsDialogConent4 + end.factoringMoney + '\n' +
                this.$i18n.messages[this.$i18n.locale].traceMgr.businessDataStatisticsDialogConent5
            }
            this.loading2 = false
            // this.showLoading = false
          })
        }
      })
    },
    resetQuery () {
      this.query = false
      this.queryParas.productId = ''
      this.queryParas.coreEnterpriseId = ''
      this.queryParas.planOrBankId = ''
      this.queryParas.specialPlanPeriods = ''
      this.queryParas.factoringApplyTime = ''
      this.sourceData = []
      this.$refs['form'].resetFields()
      for (let key in this.importQueryParas) {
        this.importQueryParas[key] = ''
      }
      for (let key in this.queryParas) {
        this.queryParas[key] = ''
      }
    },
    closeDialogVisible () {
      for (let key in this.queryParas) {
        this.queryParas[key] = ''
      }
      this.dialogVisible = false
      this.query = false
      this.sourceData = []
      this.clickQuery = 0
      this.firstConent = ''
      this.$refs['form'].resetFields()
      this.rules.factoringApplyTime[0].required = true
      this.rules.specialPlanPeriods[0].required = true
      this.content = this.$i18n.messages[this.$i18n.locale].traceMgr.businessDataStatisticsDialogConent
      this.disabled = false
    },
    dialogClose: function () {
      for (let key in this.queryParas) {
        this.queryParas[key] = ''
      }
      this.$refs['form'].resetFields()
      this.dialogVisible = false
      this.sourceData = []
      this.rules.factoringApplyTime[0].required = true
      this.rules.specialPlanPeriods[0].required = true
      this.content = this.$i18n.messages[this.$i18n.locale].traceMgr.businessDataStatisticsDialogConent
      this.disabled = false
    }
  }
}
</script>
