<template>
  <div class="app-container">
    <div class="search-con">
      <div class="title">查询条件</div>
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="queryParams.name" placeholder="请输入名称" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="queryParams.type" placeholder="请选择类型" clearable>
            <el-option v-for="dict in debit_infors_type" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="放贷机构" prop="lenders">
          <el-input v-model="queryParams.lenders" placeholder="请输入放贷机构" clearable @keyup.enter="handleQuery" />
        </el-form-item>
      </el-form>
      <div class="search-btn-con">
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button type="info" icon="Refresh" @click="resetQuery">重置</el-button>
      </div>
    </div>
    <div class="main-con">
      <div class="title-con">
        <div class="title">基本信息</div>
        <div class="operate-btn-con">
          <el-button @click="handleAdd" icon="Plus" v-hasPermi="['invest:debitInfors:add']">新增</el-button>
          <el-button :disabled="multiple" icon="Delete" @click="handleDelete" v-hasPermi="['invest:debitInfors:remove']">删除</el-button>
          <!-- <el-button @click="handleExport" icon="Download" v-hasPermi="['invest:debitInfors:export']">导出</el-button> -->
        </div>
      </div>
      <div class="content-con" v-loading="loading">
        <el-table v-loading="loading" :data="debitInforsList" @selection-change="handleSelectionChange" height="calc(100% - 0.62rem)">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="名称" align="center" prop="name" />
          <el-table-column label="类型" align="center" width="100" prop="type">
            <template #default="scope">
              <dict-tag :options="debit_infors_type" :value="scope.row.type" />
            </template>
          </el-table-column>
          <el-table-column label="放贷机构" align="center" prop="lenders" />
          <el-table-column label="贷款额度" align="center" width="120" prop="loanLimit" />
          <el-table-column label="贷款利率" align="center" width="120" prop="lendingRate" />
          <el-table-column label="贷款期限" align="center" width="120" prop="loanTerm" />
          <el-table-column label="还款方式" align="center" prop="repaymentMethod" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template v-slot="scope">
              <div class="ctrl-btn d-flex">
                <el-tooltip v-for="item in operateList" :key="item.id" class="item" effect="dark" :content="item.title" placement="top">
                  <el-button :icon="item.icon" v-hasPermi="item.hasPermi" circle @click="handleOperate(item.id, scope.row)"></el-button>
                </el-tooltip>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination small background layout="total, prev, pager, next" :total="total" @current-change="handleCurrentChange" />
      </div>
    </div>
    <!-- 添加或修改信贷产品管理对话框 -->
    <el-dialog :title="title" v-model="open" width="50%" append-to-body destroy-on-close>
      <el-form ref="debitInforsRef" :model="form" :rules="rules" :inline="true" label-width="150px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option v-for="dict in debit_infors_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="放贷机构" prop="lenders">
          <el-input v-model="form.lenders" placeholder="请输入放贷机构" />
        </el-form-item>
        <el-form-item label="贷款额度" prop="loanLimit">
          <el-input v-model="form.loanLimit" placeholder="请输入贷款额度" />
        </el-form-item>
        <el-form-item label="贷款利率" prop="lendingRate">
          <el-input v-model="form.lendingRate" placeholder="请输入贷款利率" />
        </el-form-item>
        <el-form-item label="贷款期限" prop="loanTerm">
          <el-input v-model="form.loanTerm" placeholder="请输入贷款期限" />
        </el-form-item>
        <el-form-item label="还款方式" prop="repaymentMethod">
          <el-input v-model="form.repaymentMethod" placeholder="请输入还款方式" />
        </el-form-item>
        <el-form-item label="申请方式" prop="applyMethod">
          <el-input v-model="form.applyMethod" placeholder="请输入申请方式" />
        </el-form-item>
        <el-form-item label="贷款用途" prop="loanPurpose">
          <el-input v-model="form.loanPurpose" placeholder="请输入贷款用途" />
        </el-form-item>
        <el-form-item label="查询次数" prop="creditQueryCount">
          <el-input v-model="form.creditQueryCount" placeholder="请输入查询次数" />
        </el-form-item>
        <el-form-item label="负债机构数" prop="debtCount">
          <el-input v-model="form.debtCount" placeholder="请输入负债机构数" />
        </el-form-item>
        <el-form-item label="信用卡张数" prop="creditCardCount">
          <el-input v-model="form.creditCardCount" placeholder="请输入信用卡张数" />
        </el-form-item>
        <el-form-item label="信用卡使用率" prop="creditCardUsageRate">
          <el-input v-model="form.creditCardUsageRate" placeholder="请输入信用卡使用率" />
        </el-form-item>
        <el-form-item label="贷款余额" prop="loanBalance">
          <el-input v-model="form.loanBalance" placeholder="请输入贷款余额" />
        </el-form-item>
        <el-form-item label="适用客群" style="width: 850px" prop="applicableCustomerGroups">
          <el-input v-model="form.applicableCustomerGroups" rows="3" type="textarea" placeholder="请输入适用客群" />
        </el-form-item>
        <el-form-item label="征信条件" style="width: 850px" prop="creditConditions">
          <el-input v-model="form.creditConditions" rows="3" type="textarea" placeholder="请输入征信条件" />
        </el-form-item>
        <el-form-item label="所需材料" style="width: 850px" prop="requiredMaterials">
          <el-input v-model="form.requiredMaterials" rows="2" type="textarea" placeholder="请输入所需材料" />
        </el-form-item>
        <el-form-item label="负债要求" style="width: 850px" prop="debtRequirements">
          <el-input v-model="form.debtRequirements" rows="2" type="textarea" placeholder="请输入负债要求" />
        </el-form-item>
      </el-form>
      <template v-if="title !== '查看信贷产品管理'" #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="DebitInfors">
import { listDebitInfors, getDebitInfors, delDebitInfors, addDebitInfors, updateDebitInfors } from '@/api/invest/debitInfors'
// eslint-disable-next-line no-unused-vars
import { require } from '@/utils/require'
const { proxy } = getCurrentInstance()
const { debit_infors_type } = proxy.useDict('debit_infors_type')

const debitInforsList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref('')
const operateList = ref([
  { id: 'view', icon: 'View', title: '查看', hasPermi: ['invest:debitInfors:query'] },
  { id: 'edit', icon: 'Edit', title: '修改', hasPermi: ['invest:debitInfors:edit'] },
  { id: 'delete', icon: 'Delete', title: '删除', hasPermi: ['invest:debitInfors:remove'] }
])
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    type: null,
    lenders: null
  },
  rules: {
    name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
    lenders: [{ required: true, message: '放贷机构不能为空', trigger: 'blur' }],
    type: [{ required: true, message: '类型不能为空', trigger: 'blur' }],
    loanLimit: [{ required: true, message: '贷款额度不能为空', trigger: 'blur' }],
    lendingRate: [{ required: true, message: '贷款利率不能为空', trigger: 'blur' }],
    loanTerm: [{ required: true, message: '贷款期限不能为空', trigger: 'blur' }],
    repaymentMethod: [{ required: true, message: '还款方式不能为空', trigger: 'blur' }],
    applyMethod: [{ required: true, message: '申请方式不能为空', trigger: 'blur' }]
  }
})

const handleOperate = (operate, row) => {
  switch (operate) {
    case 'view':
      handleView(row)
      break
    case 'edit':
      handleUpdate(row)
      break
    case 'delete':
      handleDelete(row)
      break
    default:
      break
  }
}

const { queryParams, form, rules } = toRefs(data)

/** 查询信贷产品管理列表 */
function getList() {
  loading.value = true
  listDebitInfors(queryParams.value).then((response) => {
    debitInforsList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    type: null,
    lenders: null,
    applicableCustomerGroups: null,
    requiredMaterials: null,
    creditConditions: null,
    lendingRate: null,
    loanTerm: null,
    repaymentMethod: null,
    loanLimit: null,
    loanPurpose: null,
    applyMethod: null,
    creditQueryCount: null,
    debtCount: null,
    creditCardCount: null,
    creditCardUsageRate: null,
    loanBalance: null,
    debtRequirements: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    delFlag: null,
    remark: null
  }
  proxy.resetForm('debitInforsRef')
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}

// 分页
const handleCurrentChange = (val) => {
  queryParams.value.pageNum = val
  getList()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}

// 查看
const handleView = (row) => {
  title.value = '查看信贷产品管理'
  form.value = row
  open.value = true
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = '添加信贷产品管理'
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getDebitInfors(_id).then((response) => {
    form.value = response.data
    open.value = true
    title.value = '修改信贷产品管理'
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs.debitInforsRef.validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateDebitInfors(form.value).then((response) => {
          proxy.$modal.msgSuccess('修改成功')
          open.value = false
          getList()
        })
      } else {
        addDebitInfors(form.value).then((response) => {
          proxy.$modal.msgSuccess('新增成功')
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal
    .confirm('是否确认删除选中的数据项？')
    .then(function () {
      return delDebitInfors(_ids)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
    .catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    'invest/debitInfors/export',
    {
      ...queryParams.value
    },
    `debitInfors_${new Date().getTime()}.xlsx`
  )
}

getList()
</script>
