<template>
  <!-- 再保理二次资料生成 -->
  <div>
    <up-down-layout :handleFlag.sync="handleFlag">
      <!-- 搜索栏 -->
      <search-form :formValue.sync="queryParas" searchPerms="search" @handleFilter="handleFilter" @clearParam="clearParam"
        slot="query" resetPerms="reset" :componentList="componentList"></search-form>
      <!-- 表格列表 -->
      <template slot="table">
        <data-table ref="dataTable" :api="api" :deleteApi="api" v-bind="elHeightObj" selectable="false" :handleFlag.sync="handleFlag" :menuFlag="false"
          :queryParas="queryParas" :sourceData.sync="sourceData" :loadCount.sync="loadCount" :columns="columns"
          :showSelection="false" :otherHeight="'240'" operateBtnLayout="" :IsRight="false" :pageAll="true" :operatWidth="'200'" @getTableData="getTableData">
          <el-table-column align="center" min-width="180px" :label="$t('common.operate')" fixed="right">
            <template slot-scope="scope">
              <!-- 编辑 -->
              <auth-button class="iconCircle" icon="iconfont iconyinhangbaojialuru" :title="$t('common.bankImport')" @click="checkInfo(scope.row.assetPackageCode)"></auth-button>
              <!-- 下载 -->
              <!-- <btn-batch-download v-if="scope.row.fileList && scope.row.fileList.length > 0 && computeFile(scope.row.fileList)" :fileID="scope.row.fileList" :title="$t('common.downLoad')"></btn-batch-download> -->
              <auth-button :disabled="!(scope.row.fileList && scope.row.fileList.length > 0 && computeFile(scope.row.fileList))" class="iconCircle" icon="iconfont iconxiazai" :title="$t('common.downLoad')" @click="operateItem(scope.row,1)"></auth-button>
              <!-- 重新生成 -->
              <auth-button class="iconCircle" icon="iconfont iconzhongxinshengcheng" :title="$t('common.remake')" @click="operateItem(scope.row.keyId,0)"></auth-button>
            </template>
          </el-table-column>
        </data-table>
      </template>
    </up-down-layout>
    <!-- 银行录入 -->
    <bank-price-input-pop :visible.sync="isAdd" editModel="E" :form="form" :assetPackageCode="assetPackageCode"></bank-price-input-pop>
  </div>
</template>
<script>
import {
  upDownLayout,
  searchForm,
  DataTable,
  authButton,
  btnDownload,
  SelectDictionary,
  BaseSelect,
  SelectReFactoringBank
} from 'newepoch-components'
import bankPriceInputPop from './bankPriceInputPop' // 银行录入
import fileApi from '@/api/fileApi.js'
import {
  downFile
} from '@/utils/devUtils'
import SelectProductName from '@/components/element/assetpkgmgr/SelectProductName'
// import SelectRefactorPropertyState from '@/components/element/assetpkgmgr/SelectRefactorPropertyState'
import bankPrice from '@/api/assetpkgmgr/bankPriceApi.js'
import * as format from '@/filters/index'
import BasePage from '@/pages/basePage'
import mixin from '@/pages/mixin'

export default {
  extends: new BasePage(),
  mixins: [mixin],
  name: 'bankPriceIndex',
  components: {
    searchForm,
    upDownLayout,
    DataTable,
    btnDownload,
    authButton,
    bankPriceInputPop
  },
  data () {
    let $this = this
    return {
      handleFlag: true,
      loadCount: 0,
      editModel: 'V',
      assetPackageCode: '', // 资产包编号
      isAdd: false, // 添加资产包弹窗控件
      isEdit: false, // 编辑和查看资产包弹窗控件
      // 搜索表单
      queryParas: {
        pageIndex: 1,
        pageSize: 10,
        assetPackageStatus: '2'
      },
      form: {},
      componentList: [
        {
        // 产品名称
          label: this.$i18n.messages[this.$i18n.locale].bankPrice.projectName,
          disabled: false,
          value: 'projectId',
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseSelect,
          labelWidth: '160px',
          clearable: true,
          queryName: 'productType',
          queryValue: 'productType',
          componentName: SelectProductName
        },
        {
        // 再保理银行
          label: this.$i18n.messages[this.$i18n.locale].bankPrice.factorBankName,
          disabled: false,
          value: 'factorBankId',
          labelWidth: '160px',
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseSelect,
          clearable: true,
          componentName: SelectReFactoringBank
        },
        {
          // 付款确认书编号
          label: this.$i18n.messages[this.$i18n.locale].bankPrice.payCode,
          disabled: false,
          value: 'assetCode',
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput,
          labelWidth: '160px',
          clearable: true,
          componentName: 'el-input'
        },
        {
        // 账款到期日
          label: this.$i18n.messages[this.$i18n.locale].bankPrice.dueDate,
          value: 'dueDate',
          componentName: 'el-date-picker',
          clearable: true,
          labelWidth: '160px',
          format: 'timestamp'
        },
        {
          // 是否录入
          label: this.$i18n.messages[this.$i18n.locale].bankPrice
            .costInputStatus,
          value: 'costInputStatus',
          labelWidth: '160px',
          clearable: true,
          options: [
            {
              id: '0',
              name: this.$t('common.no')
            },
            {
              id: '1',
              name: this.$t('common.yes')
            }
          ],
          componentName: BaseSelect
        },
        {
        // 资产包编号
          label: this.$i18n.messages[this.$i18n.locale].bankPrice.assetPackageCode,
          disabled: false,
          value: 'assetPackageCode',
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput,
          labelWidth: '160px',
          clearable: true,
          componentName: 'el-input'
        },
        {
        // 资产包状态
          label: this.$i18n.messages[this.$i18n.locale].bankPrice.assetPackageStatus,
          disabled: false,
          value: 'assetPackageStatus',
          labelWidth: '160px',
          clearable: true,
          dictTypeCode: 'bk_pkg_asset_package_status',
          componentName: SelectDictionary
        }
      ],
      columns: [
        {
        // 产品名称
          prop: 'projectName',
          align: 'left',
          headerAlign: 'center',
          width: '230px',
          showOverflowTooltip: true,
          label: this.$i18n.messages[this.$i18n.locale].bankPrice.projectName
        },
        {
        // 资产包编号
          prop: 'assetPackageCode',
          align: 'center',
          width: '240px',
          showOverflowTooltip: true,
          sortable: true,
          label: this.$i18n.messages[this.$i18n.locale].bankPrice.assetPackageCode
        },
        {
        // 计划管理人/再保理银行
          prop: 'factorBankName',
          headerAlign: 'center',
          align: 'left',
          showOverflowTooltip: true,
          width: '230px',
          label: this.$i18n.messages[this.$i18n.locale].bankPrice.factorBankName
        },
        {
        // 再报理总金额（元）
          prop: 'factoringMoneyTotal',
          headerAlign: 'center',
          align: 'right',
          showOverflowTooltip: true,
          width: '171px',
          sortable: true,
          label: this.$i18n.messages[this.$i18n.locale].bankPrice.factoringMoneyTotal,
          format: (row) => {
            return format.thousandFilter(row.factoringMoneyTotal)
          }
        },
        {
        // 账款到期日
          prop: 'dueDate',
          align: 'center',
          width: '115px',
          sortable: true,
          label: this.$i18n.messages[this.$i18n.locale].bankPrice.dueDate,
          format: (row) => {
            return format.formatDateTpcModify(row.dueDate)
          }
        },
        // 是否录入
        {
          prop: 'costInputStatus',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].bankPrice.costInputStatus,
          width: '130px',
          format (row) {
            return row.costInputStatus === '1'
              ? $this.$t('common.yes')
              : row.costInputStatus === '0' ? $this.$t('common.no') : ''
          }
        },
        {
        // 资产包状态
          prop: 'assetPackageStatus',
          align: 'center',
          width: '120px',
          label: this.$i18n.messages[this.$i18n.locale].bankPrice.assetPackageStatus,
          format: (row) => {
            return format.formatDict(row.assetPackageStatus, 'bk_pkg_asset_package_status')
          }
        },
        {
        // 资料准备时间
          prop: 'operateTime',
          align: 'center',
          width: '171px',
          sortable: true,
          label: this.$i18n.messages[this.$i18n.locale].bankPrice.operateTime
        }
      ],
      sourceData: []
    }
  },
  computed: {
    // api接口
    api () {
      return bankPrice.assetpkg
    }
  },
  // 切换路由刷新列表，不然缓存会造成非法操作
  activated () {
    this.handleFilter()
  },
  mounted () {
    // 触发loadCount去请求获得表格数据
    this.handleFilter()
  },
  methods: {
    getTableData (data) {
      data = data.map(item => {
        item.factoringMoneyTotal = Number(item.factoringMoneyTotal)
        return item
      })
    },
    // 文件列表判空
    computeFile (e) {
      for (let index in e) {
        let item = e[index]
        if (item < 1) {
          return false
        }
      }
      return true
    },
    // 重置搜索表单
    clearParam () {
      this.queryParas = {
        pageIndex: 1,
        pageSize: 10
      }
    },
    // 查看和编辑
    checkInfo (id) {
      this.editModel = 'E'
      this.assetPackageCode = id
      this.isAdd = true
    },
    // 单行操作栏
    operateItem (id, type) { // id:keyId;type:0 重新生成;1 下载
      let that = this
      let apiName = [that.$t('common.remake'), that.$t('common.downLoad')]
      let api = ['twofile', 'download']
      if (type === 1 && !id) {
        that.warning(that.$t('packageOperation.remakePrompt'))
        return
      }
      that.$confirm(that.$t('common.confirm') + apiName[type] + '？', that.$t('common.tip'), {
        type: 'warning'
      }).then(() => {
        // 下载
        if (type === 1) {
          fileApi.file.batchDownload({
            keyIds: id.fileList
          }).then(res => {
            downFile(res, id.assetPackageCode + '.zip')
          }).catch(res => {
            that.error(res.message)
          })
        // 重新生成
        } else {
          bankPrice.bankPrice[api[type]]({
            keyId: id
          }).then(function (res) {
            that.handleSubmit(res, function () {
              that.success(apiName[type] + that.$t('common.successfully'))
              // 重置表格
              that.$refs.dataTable.loadTableData()
            })
          })
        }
      }).catch((e) => {})
    },
    // 提交回调
    handleSubmit (res, callback) {
      let that = this
      if (res.code === '0') {
        // 回调函数
        callback && callback()
      } else {
        that.error(res.message)
      }
    }
  }
}
</script>
