<!--未开票清单-->
<template>
  <div class="uninvoiced">
    <up-down-layout :header="$t('uninvoiced.head')" :handleFlag.sync="handleFlag" :footerFlag.sync="footerFlag">
      <search-form
        @handleFilter="handleFilter"
        @clearParam="clearParam"
        :menuCode="menuId"
        searchPerms="searchPerms"
        :formValue.sync="queryParas"
        :componentList="componentList"
        resetPerms="resetPerms"
        @selectChanged="selectChanged"
        slot="query">
      </search-form>
      <template slot="table">
        <data-table
          :headTitle="$t('uninvoiced.headCenter')"
          :operateBtnLayout="''"
          :queryParas="queryParas"
          :selectedRows.sync="selectedRows"
          :columns="columns"
          :sourceData="showsourceData"
          :api="api"
          :menuFlag="true"
          :showSelection="true"
          :selectTable="selectTable"
          @getTableData="getTableData"
          v-bind="elHeightObj"
          :loadCount="loadCount">
          <template slot="headButton">
            <div class="floatRight">
              <!--开电子票-->
              <auth-button class="iconfont iconycxiazai" :size="'medium'" :label="$t('uninvoiced.openInvoice')"
                           :menuCode="menuId" perms="btnopenInvoice" :openPerms="true"
                           @click="handleClick" :type="'primary'"></auth-button>
              <!--下载发票-->
              <btn-export :btnSize="'medium'"
                          :menuCode="menuId" perms="btnDownloadInvoice" :openPerms="true"
                          :templateCode="'shourangsql002'" :queryParas="queryParas"></btn-export>
            </div>
          </template>
        </data-table>
      </template>
    </up-down-layout>
    <uninvoiced-edit :visible.sync="editVisible" :form="formItem" :editModel="editModel" ref="edit" @closeEdit="closeEdit"></uninvoiced-edit>
    <uninvoiced-result :visible.sync="resultVisible" :form="formItem" editModel="V"></uninvoiced-result>
  </div>
</template>

<script>
import { upDownLayout, searchForm, DataTable, btnExport,
  SelectCoreEnterprise, SelectProductName,
  authButton, SelectFinancialInstitutions } from 'newepoch-components'
import BasePage from '@/pages/basePage'
import uninvoiceApi from '@/api/uninvoiceApi'
import * as format from '@/filters/index'
import UninvoicedEdit from '@/pages/shourang/uninvoiced/uninvoicedEdit'
import uninvoicedResult from '@/pages/shourang/uninvoiced/uninvoicedResult'
import mixin from '@/pages/mixin'
import { regExp } from '@/validator/index'
import componentsApi from '@/api/componentsApi'

export default {
  extends: new BasePage(),
  mixins: [mixin],
  name: 'uninvoiced',
  components: {
    UninvoicedEdit,
    upDownLayout,
    searchForm,
    DataTable,
    btnExport,
    authButton,
    uninvoicedResult
  },
  computed: {
    api () {
      return uninvoiceApi.assetmgruninvoice
    }
  },
  mounted () {},
  data () {
    const labelWidth = '160px'
    return {
      queryParas: {},
      resultVisible: false,
      selectedRows: [],
      formItem: {},
      editVisible: false,
      footerFlag: true,
      formValue: {},
      sourceData: [],
      showsourceData: [],
      componentList: [
        // 产品名称
        {
          label: this.$i18n.messages[this.$i18n.locale].uninvoiced.productName,
          value: 'projectId',
          name: 'projectId',
          componentName: SelectProductName,
          labelWidth: labelWidth
        },
        // 核心企业
        {
          label: this.$i18n.messages[this.$i18n.locale].uninvoiced.coreEnterprises,
          value: 'coreEnterpriseId',
          componentName: SelectCoreEnterprise,
          labelWidth: labelWidth
        },
        // 计划管理人/再保理银行
        {
          label: this.$i18n.messages[this.$i18n.locale].uninvoiced.refactorBank,
          value: 'planningManagerId',
          componentName: SelectFinancialInstitutions,
          labelWidth: labelWidth
        },
        // 供应商名称
        {
          label: this.$i18n.messages[this.$i18n.locale].uninvoiced.supplierName,
          value: 'creditor',
          componentName: 'el-input',
          clearable: true,
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput,
          labelWidth: labelWidth
        },
        // 付款确认书编号
        {
          label: this.$i18n.messages[this.$i18n.locale].uninvoiced.paymentConfNum,
          value: 'assetCode',
          componentName: 'el-input',
          clearable: true,
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput,
          labelWidth: labelWidth
        },
        // 期数
        {
          label: this.$i18n.messages[this.$i18n.locale].uninvoiced.periodsNum,
          value: 'specialPlanPeriods',
          componentName: 'el-input',
          maxlength: '3',
          disabled: false,
          clearable: true,
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput,
          labelWidth: labelWidth
        },
        // 计息日期(开始)
        {
          label: this.$i18n.messages[this.$i18n.locale].uninvoiced.BearingDateStart,
          value: 'factoringStartDate',
          componentName: 'el-date-picker',
          labelWidth: labelWidth,
          format: 'timestamp'
        },
        // 计息日期(结束)
        {
          label: this.$i18n.messages[this.$i18n.locale].uninvoiced.BearingDateEnd,
          value: 'factoringExpirationDate',
          componentName: 'el-date-picker',
          labelWidth: labelWidth,
          format: 'timestamp'
        },
        // 是否允许开电子票
        {
          label: this.$i18n.messages[this.$i18n.locale].uninvoiced.whetherInvoice,
          value: 'ifsElectronicInvoice',
          componentName: 'SelectDictionary',
          labelWidth: labelWidth,
          dictTypeCode: 'ZD0003'
        }
      ],
      columns: [
        // 核心企业
        {
          prop: 'coreEnterpriseName',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].uninvoiced.coreEnterprises,
          width: '180px'
        },
        // 期数
        {
          prop: 'specialPlanPeriods',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].uninvoiced.periodsNum,
          width: '60px',
          format: (row) => {
            if (row.specialPlanPeriods === 0) {
              return ''
            } else {
              return row.specialPlanPeriods
            }
          }
        },
        // 计划管理人/再保理银行
        {
          prop: 'planningManagerName',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].uninvoiced.refactorBank,
          width: '180px'
        },
        // 付款确认书编号
        {
          prop: 'assetCode',
          align: 'left',
          sortable: true,
          label: this.$i18n.messages[this.$i18n.locale].uninvoiced.paymentConfNum,
          width: '150px'
        },
        // 供应商名称
        {
          prop: 'creditor',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].uninvoiced.supplierName,
          width: '220px'
        },
        // 供应商税号
        {
          prop: 'creditorTaxNumber',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].uninvoiced.supplierTaxNum,
          width: '180px'
        },
        // 银行账号
        {
          prop: 'bankNo',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].uninvoiced.bankAccount,
          width: '180px'
        },
        // 地址电话
        {
          prop: 'tel',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].uninvoiced.addressTelephone,
          width: '180px'
        },
        // 保理起息日期
        {
          prop: 'factoringStartDate',
          align: 'center',
          sortable: true,
          label: this.$i18n.messages[this.$i18n.locale].uninvoiced.factorStartDate,
          width: '130px',
          format: (row) => {
            return format.formatDateTpcModify(row.factoringStartDate)
          }
        },
        // 保理到期日期
        {
          prop: 'factoringExpirationDate',
          align: 'center',
          sortable: true,
          label: this.$i18n.messages[this.$i18n.locale].uninvoiced.factorEndDate,
          width: '130px',
          format: (row) => {
            return format.formatDateTpcModify(row.factoringExpirationDate)
          }
        },
        // 开票金额
        {
          prop: 'invoiceAmount',
          align: 'right',
          sortable: true,
          label: this.$i18n.messages[this.$i18n.locale].uninvoiced.invoiceMoney,
          width: '120px',
          format: (row) => {
            if (row.invoiceAmount === 0) {
              return ''
            }
            return format.thousandFilter(row.invoiceAmount)
          }
        },
        // 待开票金额
        {
          prop: 'unInvoicedAmount',
          align: 'right',
          label: this.$i18n.messages[this.$i18n.locale].uninvoiced.DinvoiceMoney,
          width: '120px',
          format: (row) => {
            if (row.unInvoicedAmount === 0) {
              return ''
            }
            return format.thousandFilter(row.unInvoicedAmount)
          }
        },
        // 资料是否完整
        {
          prop: 'dataCompleteFlag',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].uninvoiced.dataComplete,
          width: '120px',
          format: (row) => {
            return format.formatDict(row.dataCompleteFlag, 'ZD0003')
          }
        },
        // 是否允许开电子票
        {
          prop: 'ifsElectronicInvoice',
          align: 'center',
          sortable: true,
          label: this.$i18n.messages[this.$i18n.locale].uninvoiced.whetherInvoice,
          width: '160px',
          format: (row) => {
            return format.formatDict(row.ifsElectronicInvoice, 'ZD0003')
            // if (!row.unInvoicedAmount || isNaN(Number(row.unInvoicedAmount)) || Number(row.unInvoicedAmount) <= 0) {
            //   return '否'
            // } else {
            //   return format.formatDict(row.ifsElectronicInvoice, 'ZD0003')
            // }
          }
        }
      ]
    }
  },
  methods: {
    clearParam () {
      this.queryParas = {
        pageIndex: 1,
        pageSize: 10
      }
      this.selectChangeLocal('1')
    },
    selectChanged (a) {
      if (a.hasOwnProperty('projectIdInfo')) {
        if (a.projectIdInfo && a.projectIdInfo.keyId) {
          this.getTypeById(a.projectIdInfo.keyId)
        } else {
          this.selectChangeLocal('1')
        }
      }
    },
    // 改变选择信息、日期
    selectChangeLocal (isAbs) {
      this.componentList[5].disabled = isAbs !== '1'
      if (this.componentList[5].disabled) {
        this.componentList[5].placeholder = ' '
      } else {
        this.componentList[5].placeholder = this.$i18n.messages[this.$i18n.locale].common.pleaseInput
      }
    },
    // 根据ID获取产品类别
    getTypeById (projectId) {
      componentsApi.productenterprise.getclassify({ projectId: projectId }).then(res => {
        var a = res.data ? res.data.productType : '2'
        this.selectChangeLocal(a)
      })
    },
    handleFilter () {
      if (!this.UtilJs.isEmpty(this.queryParas.specialPlanPeriods) && !regExp.number.test(this.queryParas.specialPlanPeriods)) {
        return this.warning(this.$i18n.messages[this.$i18n.locale].expressDataList.specialPlanPeriodsCheck)
      }
      this.loadCount = this.loadCount + 1
    },
    closeEdit (data) {
      this.formItem = Object.assign({}, { data: data })
      this.resultVisible = true
      this.specialPlanPeriods()
    },
    getTableData (data) {
      this.sourceData = []
      this.showsourceData = []
      this.sourceData.push(...data)
      this.showsourceData.push(...data)
      // this.sourceData.forEach((it, index) => {
      //   it.invoiceOutInfoList.forEach((it1, index1) => {
      //     it1.payCode = it.payCode
      //     let item = {
      //       coreEnterpriseName: it.coreEnterpriseName,
      //       planningManagerName: it.planningManagerName,
      //       accountsReceivable: it.accountsReceivable,
      //       invoiceAmount: it.invoiceAmount,
      //       specialPlanPeriods: it.specialPlanPeriods,
      //       unInvoicedAmount: it.unInvoicedAmount
      //     }
      //     item = Object.assign(item, it1)
      //     item = Object.assign(item, { value_index: index, cvalue_index: index1 })
      //     this.showsourceData.push(item)
      //   })
      // })
    },
    // 禁选条件
    selectTable (row, index) {
      return !(!row.unInvoicedAmount || isNaN(Number(row.unInvoicedAmount)) || Number(row.unInvoicedAmount) <= 0)
    },
    handleClick () {
      let keyIds = []
      this.selectedRows.forEach(it => {
        keyIds.push(it)
      })
      if (keyIds.length > 0) {
        this.formItem = Object.assign({}, { keyIds: keyIds })
        this.editModel = 'E'
        this.editVisible = true
        this.$refs['edit'].parentDialogVisible()
      } else {
        return this.$message({
          type: 'warning',
          message: this.$i18n.messages[this.$i18n.locale].uninvoiced.opendata
        })
      }
    }
  }
}
</script>

<style scoped lang="scss">
  @import '~@/styles/shourang/uninvoiced/uninvoicedList.scss';
</style>
