<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="180px">
        <el-form-item label="银行名称" prop="bankName">
          <el-input v-model="queryParams.bankName" placeholder="请输入银行名称" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="银行类型" prop="bankType">
          <el-select v-model="queryParams.bankType" placeholder="请选择银行类型" clearable>
            <el-option v-for="dict in bank_type" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="信用卡多账户合并出账" prop="multiAccountConsolidated">
          <el-select v-model="queryParams.multiAccountConsolidated" placeholder="请选择信用卡多账户合并出账" clearable>
            <el-option v-for="dict in is_accounting" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </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:bankBaseInfo:add']">新增</el-button>
          <el-button :disabled="multiple" icon="Delete" @click="handleDelete" v-hasPermi="['invest:bankBaseInfo:remove']">删除</el-button>
          <!-- <el-button
              @click="handleExport"
              icon="Download"
              v-hasPermi="['invest:bankBaseInfo:export']"
          >导出</el-button> -->
        </div>
      </div>
      <div class="content-con" v-loading="loading">
        <el-table v-loading="loading" :data="bankBaseInfoList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="银行全称" align="center" prop="bankName" />
          <el-table-column label="银行简称" align="center" prop="bankShortName" />
          <el-table-column label="银行类型" align="center" prop="bankType">
            <template #default="scope">
              <dict-tag :options="bank_type" :value="scope.row.bankType" />
            </template>
          </el-table-column>
          <el-table-column label="英文简称" align="center" prop="englishShortName" />
          <el-table-column label="信用卡多账户合并出账" align="center" prop="multiAccountConsolidated">
            <template #default="scope">
              <dict-tag :options="is_accounting" :value="scope.row.multiAccountConsolidated" />
            </template>
          </el-table-column>
          <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="650px" append-to-body>
      <el-form ref="bankBaseInfoRef" :model="form" :rules="rules" label-width="170px">
        <el-form-item label="银行全称" prop="bankName">
          <el-input v-model="form.bankName" placeholder="请输入银行全称" />
        </el-form-item>
        <el-form-item label="银行简称" prop="bankShortName">
          <el-input v-model="form.bankShortName" placeholder="请输入银行简称" />
        </el-form-item>
        <el-form-item label="银行类型" prop="bankType">
          <el-select v-model="form.bankType" placeholder="请选择银行类型">
            <el-option v-for="dict in bank_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="信用卡多账户合并出账" prop="multiAccountConsolidated">
          <el-select v-model="form.multiAccountConsolidated" placeholder="请选择信用卡多账户合并出账">
            <el-option v-for="dict in is_accounting" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="英文简称" prop="englishShortName">
          <el-input v-model="form.englishShortName" placeholder="请输入英文简称" />
        </el-form-item>
        <el-form-item label="归属省份" prop="province">
          <el-input v-model="form.province" placeholder="请输入归属省份" />
        </el-form-item>
        <el-form-item label="归属城市" prop="city">
          <el-input v-model="form.city" placeholder="请输入归属城市" />
        </el-form-item>
        <el-form-item label="统一社会信用代码" prop="unifiedCreditCode">
          <el-input v-model="form.unifiedCreditCode" placeholder="请输入统一社会信用代码" />
        </el-form-item>

        <el-form-item label="备注信息" prop="remark">
          <el-input v-model="form.remark" type="textarea" style="width: 492px" 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="BankBaseInfo">
import { listBankBaseInfo, getBankBaseInfo, delBankBaseInfo, addBankBaseInfo, updateBankBaseInfo } from '@/api/invest/bankBaseInfo'
// eslint-disable-next-line no-unused-vars
import { require } from '@/utils/require'
const { proxy } = getCurrentInstance()
const { is_accounting, bank_type } = proxy.useDict('is_accounting', 'bank_type')

const bankBaseInfoList = 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:bankBaseInfo:query'] },
  { id: 'edit', icon: 'Edit', title: '修改', hasPermi: ['invest:bankBaseInfo:edit'] },
  { id: 'delete', icon: 'Delete', title: '删除', hasPermi: ['invest:bankBaseInfo:remove'] }
])
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    bankName: null,
    bankShortName: null,
    bankType: null,
    multiAccountConsolidated: null
  },
  rules: {
    bankName: [{ required: true, message: '银行全称不能为空', trigger: 'blur' }],
    bankShortName: [{ required: true, message: '银行简称不能为空', trigger: 'blur' }],
    bankType: [{ required: true, message: '银行类型不能为空', trigger: 'blur' }],
    multiAccountConsolidated: [{ 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
  listBankBaseInfo(queryParams.value).then((response) => {
    bankBaseInfoList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

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

// 表单重置
function reset() {
  form.value = {
    id: null,
    bankName: null,
    bankShortName: null,
    bankType: null,
    province: null,
    city: null,
    unifiedCreditCode: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    delFlag: null,
    englishShortName: null,
    multiAccountConsolidated: '0',
    orderNum: null
  }
  proxy.resetForm('bankBaseInfoRef')
}

/** 搜索按钮操作 */
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
  getBankBaseInfo(_id).then((response) => {
    form.value = response.data
    open.value = true
    title.value = '修改银行基础信息'
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs.bankBaseInfoRef.validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateBankBaseInfo(form.value).then((response) => {
          proxy.$modal.msgSuccess('修改成功')
          open.value = false
          getList()
        })
      } else {
        addBankBaseInfo(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 delBankBaseInfo(_ids)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
    .catch(() => {})
}

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

getList()
</script>
