<template>
  <section class="productList">
    <ai-list>
      <ai-title slot="title" title="产品发布管理" isShowBottomBorder>
        <template #rightBtn>
          <el-button type="primary" @click="$router.push({hash:'#productRecommend'})"
                     v-if="permissions('app_appfinancialproductrecommend_detail')">推荐产品
          </el-button>
        </template>
      </ai-title>
      <template #content>
        <ai-search-bar>
          <template #left>
            <ai-select placeholder="机构类型" v-model="search.organizationType" @change="page.current=1,getTableData()"
                       :selectList="dict.getDict('financialOrganizationType')"/>
            <ai-select v-model="search.repaymentTimeline" :selectList="dict.getDict('productRepaymentTimeline')"
                       placeholder="还款期限"
                       @change="page.current=1,getTableData()"/>
            <ai-select v-model="search.guaranteeMode" :selectList="dict.getDict('productGuaranteeMode')"
                       placeholder="担保方式"
                       @change="page.current=1,getTableData()"/>
            <ai-select v-model="search.isHot" :selectList="dict.getDict('yesOrNo')"
                       placeholder="热门推荐"
                       @change="page.current=1,getTableData()"/>
          </template>
          <template #right>
            <el-input size="small" placeholder="搜索产品名称" v-model="search.productName" clearable
                      @change="page.current=1,getTableData()"/>
          </template>
        </ai-search-bar>
        <ai-search-bar>
          <template #left>
            <el-button type="primary" icon="iconfont iconAdd" @click="handleEdit()" v-if="isFinanceUser">添加产品
            </el-button>
          </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="hot" label="热门推荐" align="center" v-if="!isFinanceUser">
            <template slot-scope="{row}">
              <el-switch v-model="row.isHot" active-value="1" inactive-value="0" @change="handleIsHot(row)"/>
            </template>
          </el-table-column>
          <el-table-column slot="interestRateMin" label="参考利率" align="center">
            <template slot-scope="{row}">
              {{ row.interestRateMin }}%~{{ row.interestRateMax }}%
            </template>
          </el-table-column>
          <el-table-column slot="options" label="操作" fixed="right" align="center" width="200px">
            <template slot-scope="{row}">
              <el-button type="text" @click="showDetail(row.id)">详情</el-button>
              <template v-if="isFinanceUser">
                <el-button type="text" @click="handleEdit(row.id)" v-if="['2','1'].includes(row.status)">编辑</el-button>
                <el-button type="text" @click="handleDelete(row.id)" v-if="['2','1'].includes(row.status)">删除</el-button>
              </template>
            </template>
          </el-table-column>
        </ai-table>
      </template>
    </ai-list>
  </section>
</template>

<script>
import {mapState} from "vuex";

export default {
  name: "productList",
  props: {
    instance: Function,
    dict: Object,
    permissions: Function
  },
  computed: {
    ...mapState(['user']),
    isFinanceUser() {
      return !!this.user.financeUser?.id
    }
  },
  data() {
    return {
      search: {productName: "", queryType: 0},
      page: {current: 1, size: 10, total: 0},
      tableData: [],
      colConfigs: [
        {label: "产品名称", prop: "productName", width: "160px"},
        {label: "机构类型", prop: "organizationType", dict: "financialOrganizationType"},
        {label: "贷款额度(万)", prop: "loanMax", align: "center", width: 120},
        {label: "参考利率(%)", slot: 'interestRateMin', align: "center", width: 120},
        {label: "还款期限", prop: "repaymentTimeline", dict: "productRepaymentTimeline", align: "center"},
        {label: "担保方式", prop: "guaranteeModeLabel", width: 200},
        {label: "还款方式", prop: "repaymentMethod", dict: "productRepaymentMethod"},
        {label: "操作人", prop: "createUserName", align: "center"},
        {label: "状态", prop: "status", dict: "productStatus", align: "center"},
        {label: "热门推荐", slot: "hot"},
        {slot: "options"}
      ]
    }
  },
  methods: {
    getTableData() {
      this.instance.post("/appfinancialproduct/list", null, {
        params: {...this.page, ...this.search}
      }).then(res => {
        if (res?.data) {
          this.tableData = res.data?.records.map(e => {
            let guaranteeModeLabel = e.guaranteeMode?.split(",").map(g => this.dict.getLabel("productGuaranteeMode", g))?.toString()
            return {...e, guaranteeModeLabel}
          })
          this.page.total = res.data.total
        }
      })
    },
    showDetail(id) {
      this.$router.push({query: {id}})
    },
    handleEdit(id) {
      this.$router.push({query: {id}, hash: "#add"})
    },
    handleDelete(ids) {
      this.$confirm("是否要删除该产品?").then(() => {
        this.instance.post("/appfinancialproduct/delete", null, {
          params: {ids}
        }).then(res => {
          if (res?.code == 0) {
            this.$message.success("删除成功!")
            this.getTableData()
          }
        })
      }).catch(() => 0)
    },
    handleIsHot(row) {
      let {id, isHot} = row
      this.instance.post("appfinancialproduct/setIsHot", null, {
        params: {id, isHot}
      }).then(res => {
        if (res?.code == 0) {
          this.$message.success("修改成功!")
          this.getTableData()
        } else {

        }
      })
    }
  },
  created() {
    this.getTableData()
  }
}
</script>

<style lang="scss" scoped>
.productList {
  height: 100%;
}
</style>
