<template>
  <section class="loanList">
    <ai-list tabs>
      <ai-title slot="title" title="贷款审核"/>
      <template #tabs>
        <el-tabs>
          <el-tab-pane label="信贷审核">
            <ai-card hideTitle class="mar-t16">
              <template #content>
                <ai-search-bar class="mar-t8">
                  <template #left>
                    <ai-select placeholder="机构类型" :selectList="dict.getDict('financialOrganizationType')"
                               v-model="search.organizationType" @change="page.current=1,getTableData()"/>
                    <ai-select placeholder="审批状态" :selectList="dict.getDict('financialLoanApplyStatus')"
                               v-model="search.status" @change="page.current=1,getTableData()"/>
                    <ai-search label="申请时间">
                      <el-date-picker size="small" v-model="search.applyTime" type="daterange"
                                      @change="handleFilterApplyTime" start-placeholder="开始时间" end-placeholder="结束时间"
                                      :default-time="['00:00:00','23:59:59']" value-format="yyyy-MM-dd HH:mm:ss"/>
                    </ai-search>
                  </template>
                  <template #right>
                    <el-input size="small" placeholder="搜索产品名称、身份证、企业、贷款机构" v-model="search.enterpriseName" clearable
                              @change="page.current=1,getTableData()"/>
                  </template>
                </ai-search-bar>
                <ai-table :tableData="tableData" :total="page.total" :current.sync="page.current" :size.sync="page.size"
                          @getList="getTableData" :col-configs="colConfigs" :dict="dict">
                  <el-table-column slot="options" label="操作" fixed="right" align="center">
                    <template slot-scope="{row}">
                      <el-button type="text" @click="showDetail(row.id)">详情</el-button>
                    </template>
                  </el-table-column>
                </ai-table>
              </template>
            </ai-card>
          </el-tab-pane>
          <el-tab-pane label="信贷统计" lazy>
            <loan-sta :instance="instance" :dict="dict" :permissions="permissions"/>
          </el-tab-pane>
        </el-tabs>
      </template>
    </ai-list>
  </section>
</template>

<script>
import {mapState} from "vuex";
import LoanSta from "./loanSta";

export default {
  name: "loanList",
  components: {LoanSta},
  props: {
    instance: Function,
    dict: Object,
    permissions: Function
  },
  computed: {
    ...mapState(['user']),
    colConfigs() {
      return [
        {label: "产品名称", prop: "productName"},
        {label: "联系人", prop: "name"},
        {label: "联系方式", prop: "phone"},
        {label: "身份证号", render: (h, {row}) => h('p', this.idCardNoUtil.hideId(row.idNumber)), width: 160},
        {label: "企业主体", prop: "enterpriseName"},
        {label: "贷款金额(万)", prop: "loanAmount"},
        {label: "申请时间", prop: "createTime", width: 160},
        {label: "贷款机构", prop: "organizationName"},
        {label: "机构类型", prop: "organizationType", dict: "financialOrganizationType"},
        {label: "状态", prop: "status", align: "center", dict: "financialLoanApplyStatus"},
        {slot: "options"}
      ]
    }
  },
  data() {
    return {
      search: {name: ""},
      page: {current: 1, size: 10, total: 0},
      tableData: []
    }
  },
  methods: {
    getTableData() {
      this.instance.post("/appfinancialloanapply/list", null, {
        params: {...this.page, ...this.search}
      }).then(res => {
        if (res?.data) {
          this.tableData = res.data?.records
          this.page.total = res.data.total
        }
      })
    },
    showDetail(id) {
      this.$router.push({query: {id}})
    },
    handleFilterApplyTime() {
      this.page.current = 1
      this.search.applyStartDate = this.search.applyTime?.[0].substring(0, 10)
      this.search.applyEndDate = this.search.applyTime?.[1].substring(0, 10)
      this.getTableData()
    },
  },
  created() {
    this.getTableData()
  }
}
</script>

<style lang="scss" scoped>
.loanList {
  height: 100%;

  .mar-t8 {
    margin-top: 8px;
  }

  .mar-t16 {
    margin-top: 16px;
  }

  .staCard {
    text-align: center;
    margin: 0 16px 16px 0;
    box-shadow: 0 4px 6px -2px rgba(15, 15, 21, 0.15);
    border-radius: 4px;
    padding: 16px;
    box-sizing: border-box;
    background: #fff;

    &:last-of-type {
      margin-right: 0;
    }
  }
}
</style>
