<template>
  <section class="grabList">
    <ai-list isTabs>
      <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="订单状态" v-model="search.status" @change="page.current=1,getTableData()"
                               :selectList="dict.getDict('financingDemandStatus')"/>
                    <ai-select placeholder="融资主体类型" v-model="search.applyType" @change="page.current=1,getTableData()"
                               :selectList="dict.getDict('financingDemandApplyType')"/>
                    <ai-search label="申请时间">
                      <el-date-picker size="small" v-model="search.applyTime" type="daterange"
                                      @change="handleFilterApplyTime"
                                      :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>
            <needs-statistics :instance="instance" :dict="dict" :permissions="permissions"/>
          </el-tab-pane>
        </el-tabs>
      </template>
    </ai-list>
  </section>
</template>

<script>
import {mapState} from "vuex";
import NeedsStatistics from "./needsStatistics";

export default {
  name: "grabList",
  components: {NeedsStatistics},
  props: {
    instance: Function,
    dict: Object,
    permissions: Function
  },
  computed: {
    ...mapState(['user'])
  },
  data() {
    return {
      search: {status: '',applyType: '',applyTime: '',enterpriseName:''},
      page: {current: 1, size: 10, total: 0},
      tableData: [],
      colConfigs: [
        {label: "联系人", prop: "name"},
        {label: "联系方式", prop: "phone"},
        {label: "企业名称", prop: "enterpriseName"},
        {label: "意向金额(万)", prop: "loanAmount"},
        {label: "申请时间", prop: "createTime"},
        {label: "贷款经理", prop: "auditUserName"},
        {label: "贷款银行", prop: "organizationName"},
        {label: "放款金额(万)", prop: "auditAmount"},
        {label: "状态", prop: "status", dict: "financingDemandStatus"},
        {label: "备注", prop: "remark"},
        {slot: "options"}
      ]
    }
  },
  methods: {
    getTableData() {
      let status = this.search.status || 999
      this.instance.post("/appfinancingdemand/list", null, {
        params: {...this.page, ...this.search, organizationId: this.user.financeUser?.organizationId, status}
      }).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.createTimeStart = this.search.applyTime?.[0].substring(0, 10)
      this.search.createTimeEnd = this.search.applyTime?.[1].substring(0, 10)
      this.getTableData()
    }
  },
  created() {
    this.getTableData()
  }
}
</script>

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

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

  .mar-t8 {
    margin-top: 8px;
  }
}
</style>
