<template>
  <div class="p-2 auto-overflow-y">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="search">
        <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="90px" class="-mb-15px">
          <el-form-item label="账户名称" prop="accountName">
            <el-input v-model="queryParams.accountName" placeholder="请输入结算账户名称" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="账户代码" prop="accountCode">
            <el-input v-model="queryParams.accountCode" placeholder="请输入结算账户代码" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="联系电话" prop="telephone">
            <el-input v-model="queryParams.telephone" placeholder="请输入联系电话" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="银行卡号" prop="bankNumber">
            <el-input v-model="queryParams.bankNumber" placeholder="请输入银行卡号" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="账户状态" prop="status">
            <el-select v-model="queryParams.status" placeholder="请选择账户状态" clearable>
              <el-option v-for="dict in sys_normal_disable" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="管理员姓名" prop="adminName">
            <el-input v-model="queryParams.adminName" placeholder="请输入管理员姓名" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="证件编号" prop="idNumber">
            <el-input v-model="queryParams.idNumber" placeholder="请输入证件编号" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button v-hasPermi="['merchant:ptAccount:add']" type="primary" plain icon="Plus" @click="openForm('create')">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['system:post:edit']" type="success" plain icon="Edit" :disabled="single" @click="openForm('update')"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['merchant:ptAccount:remove']" type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()"
              >删除</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['merchant:ptAccount:export']" type="warning" plain icon="Download" @click="handleExport">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" :columns="columns" @query-table="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="ptAccountList" max-height="calc(100vh - 27rem)" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column v-if="false" label="结算账户Id" align="center" prop="accountId" />
        <el-table-column v-if="columns[0].visible" label="账户名称" align="center" prop="accountName" />
        <el-table-column v-if="columns[1].visible" label="账户代码" align="center" prop="accountCode" />
        <el-table-column v-if="columns[2].visible" label="联系电话" align="center" prop="telephone" />
        <el-table-column v-if="columns[3].visible" label="银行卡号" align="center" prop="bankNumber" />
        <el-table-column v-if="columns[4].visible" label="账户状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column v-if="columns[5].visible" label="管理员姓名" align="center" prop="adminName" />
        <el-table-column v-if="columns[6].visible" label="证件编号" align="center" prop="idNumber" />
        <el-table-column v-if="columns[7].visible" label="备注" align="center" prop="remark" />
        <el-table-column v-if="columns[8].visible" label="创建时间" align="center" prop="createTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button v-hasPermi="['merchant:ptAccount:edit']" link type="primary" icon="Edit" @click="openForm('update', scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button v-hasPermi="['merchant:ptAccount:remove']" link type="primary" icon="Delete" @click="handleDelete(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 翻页组件 -->
      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>
    <!-- 添加或修改结算账户对话框 -->
    <detail-form ref="detailFormRef" apped-to-body @success="getList" />
  </div>
</template>

<script setup name="PtAccount" lang="ts">
/** 模块导入 */
import { listPtAccount, delPtAccount } from '@/api/basics/merchant/ptAccount';
import { PtAccountVO } from '@/api/basics/merchant/ptAccount/types';
import DetailForm from '@/views/basics/merchant/ptAccount/DetailForm.vue';

defineOptions({ name: 'PtAccount' });

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
/** 当前页面引用的数据字典 */
const { sys_normal_disable } = toRefs<any>(proxy?.useDict('sys_normal_disable'));
// 是否加载遮罩层
const loading = ref(true);
// 是否显示查询项
const showSearch = ref(true);
const ptAccountList = ref<PtAccountVO[]>([]);
// 列表选中的记录Id
const ids = ref<Array<number | string>>([]);
// 单选
const single = ref(true);
// 多选
const multiple = ref(true);
// 总记录数
const total = ref(0);

// 查询窗体Ref
const queryFormRef = ref<ElFormInstance>();

const columns = ref<FieldOption[]>([
  { key: 0, label: `账户名称`, visible: true, children: [] },
  { key: 1, label: `账户代码`, visible: true, children: [] },
  { key: 2, label: `联系电话`, visible: true, children: [] },
  { key: 3, label: `银行卡号`, visible: true, children: [] },
  { key: 4, label: `账户状态`, visible: true, children: [] },
  { key: 5, label: `管理员姓名`, visible: true, children: [] },
  { key: 6, label: `证件编号`, visible: true, children: [] },
  { key: 7, label: `备注`, visible: true, children: [] },
  { key: 8, label: `创建时间`, visible: true, children: [] }
]);

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  accountName: '',
  accountCode: '',
  telephone: '',
  bankNumber: '',
  status: '',
  adminName: '',
  idNumber: '',
  params: {}
});

/** 查询结算账户列表 */
const getList = async () => {
  loading.value = true;
  const res = await listPtAccount(queryParams);
  ptAccountList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: PtAccountVO[]) => {
  ids.value = selection.map((item) => item.accountId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 删除按钮操作 */
const handleDelete = async (row?: PtAccountVO) => {
  const _accountIds = row?.accountId || ids.value;
  await proxy?.$modal.confirm('是否确认删除结算账户编号为"' + _accountIds + '"的数据项？').finally(() => (loading.value = false));
  await delPtAccount(_accountIds);
  await getList();
  proxy?.$modal.msgSuccess('删除成功');
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    '/backstage/merchant/ptAccount/export',
    {
      ...queryParams
    },
    `ptAccount_${new Date().getTime()}.xlsx`
  );
};
/** 弹出框(增加、修改) */
const detailFormRef = ref();
const openForm = (type: string, row?: any) => {
  const id = row?.accountId || ids.value[0];
  detailFormRef?.value.open(type, id);
  console.log(type, row);
};
onMounted(() => {
  getList();
});
</script>
