<!-- 合规抽样资产管理 -->
<template>
  <div>
    <up-down-layout :header="$t('complianceCheck.complianceAssetManageTitle')" :handleFlag.sync="handleFlag">
      <search-form @clearParam="clearParam" @handleFilter="handleFilter" :formValue.sync="queryParas"
                   v-model="queryParas" :componentList="componentList" slot="query"
                   :menuCode="menuId" searchPerms="search" resetPerms="reset"></search-form>
      <template v-slot:table>
        <data-table :handleFlag.sync="handleFlag" :showSelection="true" :queryParas="queryParas" :sourceData.sync="sourceData"
                    :selectedRows.sync="selectedRows" :api="api" :loadCount.sync="loadCount" operateBtnLayout="" ref="sourceTable"
                    :columns="columns" :menuFlag="true" :headTitle="$t('complianceCheck.complianceAssetManageTable')"
                    @getTableData="getTableData" @selectRowChange="selectRowChange" v-bind="elHeightObj">
          <template v-slot:headButton>
            <div class="headButton">
              <label>{{$t('complianceCheck.extractNumber')}}</label>
              <el-input class="headSpan" v-model.number="samplingAssetsOnceDTO.samplingNum" :placeholder="$t('complianceCheck.extractNumber')"></el-input>
              <auth-button type="primary" size="medium" icon="iconfont iconyijianchouqu" :loading="allLoad"
                           :label="$t('complianceCheck.randomExtract')" @click="randomExtract"></auth-button>
              <auth-button type="primary" size="medium" icon="iconfont iconArtboard" :label="$t('complianceCheck.pushSet')" @click="showEdit"></auth-button>
            </div>
          </template>
          <el-table-column :label="$t('common.operate')" align="center" width="150px">
            <template slot-scope="scope">
              <div>
                <auth-button v-if="scope.row.ifsCheck === '0'" class="iconCircle" icon="iconfont iconchouqu" :title="$t('complianceCheck.extract')" @click="extract(scope.row)"></auth-button>
                <auth-button v-if="scope.row.ifsCheck === '1'" class="iconCircle" icon="iconfont iconquxiaochouzhong" :title="$t('complianceCheck.extractCancel')" @click="extractCancel(scope.row)"></auth-button>
              </div>
            </template>
          </el-table-column>
        </data-table>
        <div class="footer">
          <div class="footerSpan">
            <span>{{$t('complianceCheck.extractTip1')}}{{resultForm.extractedCount}}{{$t('complianceCheck.extractTip2')}}{{resultForm.checkedCount}}{{$t('complianceCheck.extractTip3')}}</span>
          </div>
          <auth-button :loading="extractLoad" type="primary" size="medium" :label="$t('complianceCheck.extract')" @click="allExtract"></auth-button>
          <auth-button size="medium" :label="$t('complianceCheck.extractCancel')" @click="allExtractCancel"></auth-button>
        </div>
      </template>
    </up-down-layout>
    <pushSettingEdit :visible.sync="showDetail" :editModel="editModel" :form="item"></pushSettingEdit>
  </div>
</template>
<script>
import BasePage from '@/pages/basePage.js'
import { authButton, upDownLayout, searchForm, DataTable, SelectProductName, SelectCoreEnterpriseCascade, SelectFinancialInstitutionsCascade } from 'newepoch-components'
import complianceApi from '@/api/dataAudit/complianceApi.js'
import pushSettingEdit from '@/pages/dataAudit/compliance/pushSettingEdit.vue'
// import { regExp } from '@/validator/index.js'
import * as format from '@/filters/index'
import cn from '@/i18n/langs/cn.js'
import { regExp } from '@/validator/index.js'
import mixin from '@/pages/mixin'

const { complianceCheck, common } = cn
export default {
  name: 'complianceAssetManageList',
  extends: new BasePage(),
  mixins: [mixin],
  components: {
    upDownLayout,
    searchForm,
    DataTable,
    authButton,
    pushSettingEdit,
    SelectProductName,
    SelectCoreEnterpriseCascade,
    SelectFinancialInstitutionsCascade
  },
  data () {
    return {
      elOtherHeight: 140,
      showDetail: false,
      extractLoad: false,
      cancelLoad: false,
      allLoad: false,
      item: {},
      editModel: '',
      samplingAssetsOnceDTO: {
        extractUserId: '',
        extractUserName: '',
        loanDate: '',
        samplingNum: 30
      },
      resultForm: {
        extractedCount: 0,
        checkedCount: 0
      },
      queryParas: {
        loanDate: new Date(new Date().getFullYear(), new Date().getMonth() - 1).valueOf()
      },
      sourceData: [],
      componentList: [
        // 产品名称
        {
          label: complianceCheck.project,
          value: 'projectId',
          disabled: false,
          componentName: SelectProductName,
          labelWidth: '160px'
        },
        // 核心企业
        {
          label: complianceCheck.coreEnterprise,
          value: 'coreEnterpriseId',
          queryName: 'projectId',
          queryValue: 'projectId',
          disabled: false,
          componentName: SelectCoreEnterpriseCascade,
          labelWidth: '160px'
        },
        // 计划管理人/再保理银行
        {
          label: complianceCheck.planningManagerName,
          value: 'planningManagerId',
          queryName: 'projectId',
          queryValue: 'projectId',
          disabled: false,
          componentName: SelectFinancialInstitutionsCascade,
          labelWidth: '160px'
        },
        // 期数
        {
          label: complianceCheck.periods,
          value: 'specialPlanPeriods',
          disabled: false,
          componentName: 'el-input',
          placeholder: common.pleaseInput,
          labelWidth: '160px'
        },
        // 合规抽查审核状态
        {
          label: complianceCheck.auditStatus,
          value: 'auditStatus',
          disabled: false,
          componentName: 'SelectDictionary',
          dictTypeCode: 'check_examine_status',
          labelWidth: '160px'
        },
        // 是否抽中
        {
          label: complianceCheck.extracted,
          value: 'ifsCheck',
          disabled: false,
          componentName: 'SelectDictionary',
          dictTypeCode: 'ZDZD0024',
          labelWidth: '160px'
        },
        // 付款确认书编号
        {
          label: complianceCheck.payCode,
          value: 'assetCode',
          disabled: false,
          componentName: 'el-input',
          placeholder: common.pleaseInput,
          labelWidth: '160px'
        },
        // 年月
        {
          label: complianceCheck.years,
          value: 'loanDate',
          disabled: false,
          format: 'timestamp',
          componentName: 'el-date-picker',
          pickerOptions: {
            disabledDate: (val) => {
              let currentDate = (new Date(new Date().getFullYear(), new Date().getMonth() - 1)).valueOf()
              if (val.valueOf() > currentDate) {
                return true
              } else {
                return false
              }
            }
          },
          labelWidth: '160px',
          type: 'month'
        }
      ],
      columns: [
        // 核心企业
        {
          prop: 'coreEnterpriseName',
          align: 'left',
          label: complianceCheck.coreEnterprise,
          width: '200px',
          sortable: true
        },
        // 计划管理人/再保理银行
        {
          prop: 'planningManagerName',
          align: 'left',
          label: complianceCheck.planningManagerName,
          width: '200px',
          sortable: true
        },
        // 期数
        {
          prop: 'specialPlanPeriods',
          align: 'right',
          label: complianceCheck.periods,
          width: '120px',
          sortable: true
        },
        // 供应商名称
        {
          prop: 'creditor',
          align: 'left',
          label: complianceCheck.supplierName,
          width: '200px'
        },
        // 项目公司名称
        {
          prop: 'debtor',
          align: 'left',
          label: complianceCheck.projectCompanyName,
          width: '200px'
        },
        // 付款确认书编号
        {
          prop: 'assetCode',
          align: 'left',
          label: complianceCheck.payCode,
          width: '200px',
          sortable: true
        },
        // 是否抽中
        {
          prop: 'ifsCheck',
          align: 'center',
          label: complianceCheck.extracted,
          width: '120px',
          format: (row) => { return format.formatDict(row.ifsCheck, 'ZDZD0024') }
        },
        // 合规抽查审核状态
        {
          prop: 'auditStatus',
          align: 'center',
          label: complianceCheck.auditStatus,
          format: (row) => { return row.ifsCheck === '0' ? '—' : format.formatDict(row.auditStatus, 'check_examine_status') },
          width: '150px'
        }
      ]
    }
  },
  computed: {
    api () {
      return complianceApi.manageApi
    }
  },
  mounted () {
    this.handleFilter()
  },
  methods: {
    selectRowChange (row) {
      let _this = this
      _this.resultForm.checkedCount = row.length
    },
    // 重写查询
    handleFilter () {
      let _this = this
      // 期数数字校验
      if (!this.UtilJs.isEmpty(this.queryParas.specialPlanPeriods) && !regExp.number.test(this.queryParas.specialPlanPeriods)) {
        return this.warning(complianceCheck.specialPlanPeriodsCheck)
      }
      // 日期非空校验
      if (_this.UtilJs.isEmpty(_this.queryParas.loanDate)) {
        return this.warning(complianceCheck.chooseYearDays)
      }
      _this.$refs.sourceTable.loadTableData()
    },
    // 获取列表数据
    getTableData (val) {
      let _this = this
      _this.resultForm.extractedCount = 0
      val.forEach(it => {
        if (it.ifsCheck === '1') {
          _this.resultForm.extractedCount++
        }
      })
    },
    // 一键抽取
    randomExtract () {
      let _this = this
      if (_this.UtilJs.isEmpty(_this.samplingAssetsOnceDTO.samplingNum)) {
        _this.warning(complianceCheck.numberMsg)
        return
      }
      if (!regExp.number.test(_this.samplingAssetsOnceDTO.samplingNum) || _this.samplingAssetsOnceDTO.samplingNum === 0) {
        _this.warning(complianceCheck.extractMsg)
        return
      }
      _this.samplingAssetsOnceDTO.loanDate = _this.queryParas.loanDate
      _this.samplingAssetsOnceDTO.extractUserId = _this.$store.getters.user.userId
      _this.samplingAssetsOnceDTO.extractUserName = _this.$store.getters.user.userName
      _this.allLoad = true
      complianceApi.manageApi.randomExtract(_this.samplingAssetsOnceDTO).then(res => {
        if (res && res.data) {
          _this.success(complianceCheck.extractSucMsg)
          _this.handleFilter()
        } else {
          _this.error(complianceCheck.extractFailMsg)
        }
        _this.allLoad = false
      }).catch(() => {
        _this.allLoad = false
      })
    },
    // 推送配置
    showEdit () {
      this.item = Object.assign({}, { pushDay: this.queryParas.loanDate })
      this.editModel = 'E'
      this.showDetail = true
    },
    // 抽取
    extract (row) {
      let _this = this
      let postData = {
        dtos: [],
        extractUserId: _this.$store.getters.user.userId,
        extractUserName: _this.$store.getters.user.userName,
        loanDate: _this.queryParas.loanDate
      }
      postData.dtos.push(row)
      complianceApi.manageApi.extract(postData).then(res => {
        if (res && res.data) {
          _this.success(complianceCheck.extractSucMsg)
          _this.handleFilter()
        } else {
          _this.error(complianceCheck.extractFailMsg)
        }
      })
    },
    // 取消抽取
    extractCancel (row) {
      let _this = this
      complianceApi.manageApi.extractCancel({ assetInfoIdArray: [row.assetInfoId] }).then(res => {
        if (res && res.data) {
          _this.success(complianceCheck.cancelSucMsg)
          _this.handleFilter()
        } else {
          _this.error(complianceCheck.cancelFailMsg)
        }
      })
    },
    // 选中抽取
    allExtract () {
      let _this = this
      let flag = true
      if (_this.selectedRows.length === 0) {
        this.warning(complianceCheck.chooseExtractMsg)
        return
      }
      _this.selectedRows.some(item => {
        if (item.ifsCheck === '1') {
          this.warning(complianceCheck.chooseAsset)
          flag = false
          return true
        }
      })
      if (flag) {
        let batchSamplingListDto = {
          dtos: _this.selectedRows,
          extractUserId: _this.$store.getters.user.userId,
          extractUserName: _this.$store.getters.user.userName,
          loanDate: _this.queryParas.loanDate
        }
        _this.extractLoad = true
        complianceApi.manageApi.extract(batchSamplingListDto).then(res => {
          if (res && res.data) {
            _this.success(complianceCheck.extractSucMsg)
            _this.handleFilter()
          } else {
            _this.error(complianceCheck.cancelFailMsg)
          }
          _this.extractLoad = false
        }).catch(() => {
          _this.extractLoad = false
        })
      }
    },
    // 选中取消抽取
    allExtractCancel () {
      let _this = this
      let flag = true
      if (_this.selectedRows.length === 0) {
        this.warning(complianceCheck.chooseExtractCancelMsg)
        return
      }
      _this.selectedRows.some(item => {
        if (item.ifsCheck === '0') {
          this.warning(complianceCheck.chooseExtractAsset)
          flag = false
          return true
        }
      })
      if (flag) {
        let assetInfoIdArray = _this.selectedRows.map(it => {
          return it.assetInfoId
        })
        _this.cancelLoad = true
        complianceApi.manageApi.extractCancel({ assetInfoIdArray: assetInfoIdArray }).then(res => {
          if (res && res.data) {
            _this.success(complianceCheck.cancelSucMsg)
            _this.handleFilter()
          } else {
            _this.error(complianceCheck.cancelFailMsg)
          }
          _this.cancelLoad = false
        }).catch(() => {
          _this.cancelLoad = false
        })
      }
    },
    // 重置
    clearParam () {
      this.queryParas = {
        projectId: '',
        coreEnterpriseId: '',
        planningManagerId: '',
        specialPlanPeriods: '',
        auditStatus: '',
        ifsCheck: '',
        asstCode: '',
        loanDate: new Date(new Date().getFullYear(), new Date().getMonth() - 1).valueOf(),
        pageIndex: 1,
        pageSize: 10
      }
    }
  },
  watch: {
    'queryParas.projectId' (now, old) {
      if (now !== old) {
        this.$set(this.queryParas, 'coreEnterpriseId', '')
        this.$set(this.queryParas, 'planningManagerId', '')
      }
    }
  }
}
</script>
<style scoped lang="scss" src="@/styles/dataAudit/compliance/complianceAssetManageList.scss">
</style>
