<!-- 自定义选择资产 -->
<template>
  <div class="customAssetSelectPop">
    <dialog-edit :visible.sync="dialogVisible" :editModel="editModel" :form="form" :title="$t('effectDealFeedBackMain.customSelAsset')" :autoSave="false" :footerBtnShow="false">
      <search-form :formValue.sync="queryParas" @handleFilter="handleFilter" @clearParam="myclearParam" :componentList="componentList" :isShowResetBtn="isShowResetBtn" :isShowFoldBtn="isShowFoldBtn" :isShowSearchBtn="isShowSearchBtn" class="search-form-wd">
        <template slot="beforeBtnArea">
          <btn-upload :btnDisplay="true"
                      type="primary"
                      fileType=".xls,.xlsx"
                      :uploadApi="uploadApi"
                      :uploadName="$t('effectDealFeedBackMain.importAuditAssets')"
                      btnSize="medium"
                      @uploadSuccess="uploadSuccess"></btn-upload>
        </template>
      </search-form>
      <data-table :sourceData.sync="tableDatas" :columns="columns" :queryParas.sync="queryParas" :api="assApi" :loadCount="loadCount" :loadPage="loadPage" :showPage="false" ref="dataTable">
        <!-- <template slot="invalidAssetFlaghead" slot-scope="scope">
          <el-checkbox :index="scope.data.$index"  v-model="scope.data.column.prop" @change="allReviewFlag(scope.data.column.prop)" true-label="1" false-label="2"></el-checkbox>
        </template>
        <template slot="invalidAssetFlag" slot-scope="scope">
          <el-checkbox :index="scope.data.$index"  v-model="scope.data.row.invalidAssetFlag" true-label="1" false-label="2" @change="checkbox(scope.data.row)"></el-checkbox>
        </template> -->
        <template slot="creditor" slot-scope="scope">
          <span-plus :index="scope.data.$index" :text="scope.data.row.creditor"></span-plus>
        </template>
        <template slot="debtor" slot-scope="scope">
          <span-plus :index="scope.data.$index" :text="scope.data.row.debtor"></span-plus>
        </template>
      </data-table>
      <div v-if="importFlag || (formData.length>0&&this.form.oldAuditAssetScope!=='3')" v-text="$t('common.importInfo')+tableDatas.length+$t('common.importDataInfo')"></div>
        <template slot="footer">
            <!--确定-->
            <auth-button :label="$t('projectCompany.btnDone')"
                         type="primary"
                         size="medium"
                         @click="determine"></auth-button>
        </template>
    </dialog-edit>
    <!-- <import-success-dialog v-if="importVisible" :visible.sync="importVisible" :exportAttrs="exportAttrs" :isExport="true" :attr="{saveFlag: importFlag}" :form="importForm" :editModel="''" @close="importantClose"></import-success-dialog> -->
  </div>
</template>

<script>
import { DialogEdit, authButton, searchForm, DataTable, btnUpload, BaseSelect, SpanPlus } from 'newepoch-components'
import otherFileSetupApi from '@/api/assetoutfeedback/otherFileSetup/otherFileSetupApi'
import BasePage from '@/pages/baseEditPage'
import importSuccessDialog from '@/components/element/assetoutfeedback/dialogForm'
import { UPLOAD_API } from '@/constant'
export default {
  extends: new BasePage(),
  components: { DialogEdit, authButton, searchForm, DataTable, btnUpload, BaseSelect, SpanPlus, importSuccessDialog },
  props: {
    form: {
      type: Object,
      default () {
        return {}
      }
    },
    editModel: {
      type: String,
      default () {
        return ''
      }
    },
    formData: {
      type: Array,
      default () {
        return []
      }
    },
    index: {
      type: Number,
      default () {
        return 0
      }
    }
  },
  computed: {
    assApi () {
      return otherFileSetupApi.getOtherAsset
    }
    // tableData: {
    //   set: function () {
    //     if (this.uploadFlag) {
    //       this.tableDatas = this.data
    //     } else {
    //       this.tableDatas = this.formData
    //     }
    //   },
    //   get: function () {
    //     return this.tableDatas
    //   }
    // }
  },
  watch: {
    data: function () {
      if (this.uploadFlag) {
        this.tableDatas = this.data
      } else {
        this.tableDatas = this.formData
      }
    }
  },
  data () {
    return {
      uploadApi: UPLOAD_API,
      isShowSearchBtn: false,
      isShowResetBtn: false,
      isShowFoldBtn: false,
      showSelection: true,
      importVisible: false,
      importFlag: false,
      uploadFlag: false, // 是否上传
      tableDatas: [],
      queryParas: {
        pageIndex: 1,
        pageSize: 10
      },
      exportAttrs: {},
      importForm: {},
      loadPage: 'getList',
      selectedRows: [],
      loadCount: 0,
      data: [],
      componentList: [
        {
          // 产品
          value: 'projectName',
          componentName: 'el-input',
          disabled: true,
          label: this.$i18n.messages[this.$i18n.locale].common.product,
          labelWidth: '45px',
          name: 'projectName',
          cols: [8, 8, 8, 8],
          prop: 'projectName'
        }, {
          // 期数
          value: 'periods',
          componentName: 'el-input',
          disabled: true,
          label: this.$i18n.messages[this.$i18n.locale].ladingNoticeHandle.specialPlanPeriods,
          labelWidth: '45px',
          placeholder: ' ',
          name: 'periods',
          cols: [8, 8, 8, 8],
          prop: 'periods'
        }, {
          // 文件
          value: 'fileName',
          disabled: true,
          componentName: 'el-input',
          label: this.$i18n.messages[this.$i18n.locale].traceMgr.file,
          labelWidth: '45px',
          name: 'fileName',
          cols: [8, 8, 8, 8],
          prop: 'fileName'
        }// , {
        //   // 是否抽中
        //   value: 'ifsSelected',
        //   componentName: BaseSelect,
        //   label: this.$i18n.messages[this.$i18n.locale].effectDealFeedBackMain.ifsSelected,
        //   labelWidth: '120px',
        //   placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseSelect + this.$i18n.messages[this.$i18n.locale].effectDealFeedBackMain.ifsSelected,
        //   name: 'ifsSelected',
        //   options: [{
        //     id: '0',
        //     name: this.$i18n.messages[this.$i18n.locale].common.no
        //   }, {
        //     id: '1',
        //     name: this.$i18n.messages[this.$i18n.locale].common.yes
        //   }],
        //   cols: [8, 8, 8, 8],
        //   prop: 'ifsSelected'
        // }, {
        //   // 付款确认书编号
        //   value: 'assetCode',
        //   maxlength: 50,
        //   componentName: 'el-input',
        //   label: this.$i18n.messages[this.$i18n.locale].common.payCode,
        //   placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput + this.$i18n.messages[this.$i18n.locale].common.payCode,
        //   labelWidth: '120px',
        //   name: 'assetCode',
        //   cols: [8, 8, 8, 8],
        //   prop: 'assetCode'
        // }, {
        //   // 供应商名称
        //   value: 'creditor',
        //   componentName: 'el-input',
        //   label: this.$i18n.messages[this.$i18n.locale].feedbackMaterialIndex.gys,
        //   labelWidth: '120px',
        //   maxlength: 255,
        //   placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput + this.$i18n.messages[this.$i18n.locale].feedbackMaterialIndex.gys,
        //   name: 'creditor',
        //   cols: [8, 8, 8, 8],
        //   prop: 'creditor'
        // }, {
        //   // 项目公司名称
        //   value: 'debtor',
        //   componentName: 'el-input',
        //   label: this.$i18n.messages[this.$i18n.locale].feedbackMaterialIndex.xmg,
        //   labelWidth: '120px',
        //   maxlength: 255,
        //   placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput + this.$i18n.messages[this.$i18n.locale].feedbackMaterialIndex.xmg,
        //   name: 'debtor',
        //   cols: [8, 8, 8, 8],
        //   prop: 'debtor'
        // }
      ],
      columns: [
        // {
        //   // 是否无效
        //   prop: 'invalidAssetFlag',
        //   align: 'center',
        //   width: '20px',
        //   slot: true,
        //   headslot: true,
        //   label: ''
        // },
        {
          // 项目
          prop: 'projectName',
          align: 'left',
          width: '160px',
          label: this.$i18n.messages[this.$i18n.locale].common.product
        }, {
          // 期数
          prop: 'specialPlanPeriods',
          align: 'center',
          width: '50px',
          label: this.$i18n.messages[this.$i18n.locale].ladingNoticeHandle.specialPlanPeriods
        }, {
          // 付款确认书编号
          prop: 'assetCode',
          align: 'left',
          width: '160px',
          label: this.$i18n.messages[this.$i18n.locale].common.payCode
        }, {
          // 供应商名称
          prop: 'creditor',
          align: 'left',
          width: '140px',
          slot: true,
          label: this.$i18n.messages[this.$i18n.locale].feedbackMaterialIndex.gys
        }, {
          // 项目公司名称
          prop: 'debtor',
          align: 'left',
          width: '160px',
          slot: true,
          label: this.$i18n.messages[this.$i18n.locale].feedbackMaterialIndex.xmg
        }, {
          // 是否抽中
          prop: 'ifsSelected',
          align: 'center',
          width: '50px',
          label: this.$i18n.messages[this.$i18n.locale].effectDealFeedBackMain.ifsSelected,
          format (row) {
            return row.ifsSelected === '0' ? '否' : '是'
          }
        }
      ]
    }
  },
  mounted () {
    let periods = this.form.periods === '0' ? '' : this.form.periods
    this.queryParas = Object.assign(this.queryParas, this.form, { periods: periods })
    if (this.formData.length > 0 && this.form.oldAuditAssetScope !== '3') {
      this.tableDatas = this.formData
    } else {
      this.$nextTick(() => {
        this.handleFilter()
      })
    }
  },
  methods: {
    // 导入
    uploadSuccess (returnData) {
      this.queryParas = Object.assign(this.queryParas, { 'excelFileId': returnData.fileID })
      this.assApi.defaultImport(this.queryParas).then(res => {
        if (res.code === '0' && res.data) {
          // this.importForm = res.data
          // this.importForm = Object.assign(this.importForm, { 'successNum': (Number(res.data.totalNum) - Number(res.data.invalidAssetsNum)).toString() })
          // // eslint-disable-next-line no-unneeded-ternary
          // this.importFlag = Number(this.importForm.invalidAssetsNum) > 0 ? true : false
          // this.exportAttrs = {
          //   total: 'totalNum', // 总数
          //   fail: 'invalidAssetsNum', // 失败数
          //   success: 'successNum', // 成功数
          //   file: 'fileKeyId' // 文件Id
          // }

          this.uploadFlag = true
          this.data = res.data
          this.importFlag = true
        } else {
          this.error('导入审核资产失败')
        }
      })
    },
    // 清空查询条件
    myclearParam () {
      this.queryParas = {
        pageIndex: 1,
        pageSize: 10
      }
      this.queryParas = Object.assign(this.queryParas, this.form)
    },
    // 确定
    determine () {
      let arr = []
      this.tableDatas.forEach(res => {
        // if (res.keyId !== '' || res.invalidAssetFlag === '1') {
        //   arr.push(res)
        // }
        arr.push(res)
      })
      let obj = {
        keyId: this.form.keyId !== '' ? this.form.keyId : '',
        fileCode: this.form.keyId !== '' ? '' : this.form.fileCode
      }
      this.$emit('selectedRowsData', arr, obj, this.index)
      this.dialogVisible = false
    }
    // 选择所有
    // allReviewFlag (headCheck) {
    //   if (this.data.length > 0) {
    //     this.data.forEach((res, i) => {
    //       this.data[i].invalidAssetFlag = headCheck
    //     })
    //   }
    // },
    // 导入画面关闭
    // importantClose () {
    //   // console.log(1)
    // }
  }
}
</script>

<style lang="scss" scope>
@import '~@/styles/assetoutfeedback/common.scss'
</style>
