<template>
  <div class="app-container">
    <div
      class="background-color-fff padding-20 border-radius-8px margin-bottom-20"
    >
      <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item label="用户ID" prop="userId">
          <el-input
            v-model="queryParams.userId"
            placeholder="请输入用户ID"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <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="cardNumber">
          <el-input
            v-model="queryParams.cardNumber"
            placeholder="请输入银行卡号"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="用户姓名" prop="cardHolder">
          <el-input
            v-model="queryParams.cardHolder"
            placeholder="请输入持卡人姓名"
            clearable
            @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>

    <div
      class="background-color-fff padding-20 border-radius-8px margin-bottom-20"
    >
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['service:userCard:add']"
          >
            新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['service:userCard:edit']"
          >
            修改
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['service:userCard:remove']"
          >
            删除
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['service:userCard:export']"
          >
            导出
          </el-button>
        </el-col>
        <right-toolbar
          v-model:showSearch="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>
    </div>

    <div
      class="background-color-fff padding-20 border-radius-8px margin-bottom-20"
    >
      <el-table
        v-loading="loading"
        :data="userCardList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="银行卡ID" align="center" prop="cardId" />
        <el-table-column label="用户ID" align="center" prop="userId" />
        <el-table-column
          label="卡类型 (1: 借记卡, 2: 信用卡)"
          align="center"
          prop="cardType"
        >
          <template #default="{ row }">
            <el-tag :type="row.cardType === 1 ? 'success' : 'info'">
              {{ row.cardType === 1 ? "借记卡" : "信用卡" }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="银行名称" align="center" prop="bankName" />
        <el-table-column label="银行卡号" align="center" prop="cardNumber" />
        <el-table-column
          label="卡号哈希"
          align="center"
          prop="cardNumberHash"
        />
        <el-table-column label="持卡人姓名" align="center" prop="cardHolder" />
        <el-table-column
          label="卡号后四位"
          align="center"
          prop="cardLastFour"
        />
        <el-table-column label="预留手机号" align="center" prop="phoneNumber" />
        <el-table-column
          label="是否默认 (0: 否, 1: 是)"
          align="center"
          prop="isDefault"
        >
          <template #default="scope">
            <el-tag :type="scope.row.isDefault === 0 ? 'info' : 'success'">
              {{ scope.row.isDefault === 0 ? "否" : "是" }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="状态 (0: 正常, 1: 冻结, 2: 删除)"
          align="center"
          prop="status"
        >
          <template #default="scope">
            <el-tag
              :type="
                scope.row.status === 0
                  ? 'success'
                  : scope.row.status === 1
                    ? 'warning'
                    : 'danger'
              "
            >
              {{
                scope.row.status === 0
                  ? "正常"
                  : scope.row.status === 1
                    ? "冻结"
                    : "删除"
              }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="绑定时间"
          align="center"
          prop="bindTime"
          width="180"
        >
          <template #default="scope">
            <span>{{ parseTime(scope.row.bindTime, "{y}-{m}-{d}") }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="验证时间"
          align="center"
          prop="verifiedAt"
          width="180"
        >
          <template #default="scope">
            <span>{{ parseTime(scope.row.verifiedAt, "{y}-{m}-{d}") }}</span>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <el-button
              link
              type="primary"
              icon="Edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['service:userCard:edit']"
            >
              修改
            </el-button>
            <el-button
              link
              type="primary"
              icon="Delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['service:userCard:remove']"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <!-- 添加或修改用户银行卡对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form
        ref="userCardRef"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="银行名称" prop="bankName">
          <el-input v-model="form.bankName" placeholder="请输入银行名称" />
        </el-form-item>
        <el-form-item label="银行卡号" prop="cardNumber">
          <el-input v-model="form.cardNumber" placeholder="请输入银行卡号" />
        </el-form-item>
        <el-form-item label="卡号哈希" prop="cardNumberHash">
          <el-input
            v-model="form.cardNumberHash"
            placeholder="请输入卡号哈希"
          />
        </el-form-item>
        <el-form-item label="持卡人姓名" prop="cardHolder">
          <el-input v-model="form.cardHolder" placeholder="请输入持卡人姓名" />
        </el-form-item>
        <el-form-item label="卡号后四位" prop="cardLastFour">
          <el-input
            v-model="form.cardLastFour"
            placeholder="请输入卡号后四位"
          />
        </el-form-item>
        <el-form-item label="预留手机号" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入预留手机号" />
        </el-form-item>
        <el-form-item label="是否默认 (0: 否, 1: 是)" prop="isDefault">
          <el-select
            v-model="form.isDefault"
            placeholder="请选择是否默认 (0: 否, 1: 是)"
          >
            <el-option
              v-for="dict in sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="绑定时间" prop="bindTime">
          <el-date-picker
            clearable
            v-model="form.bindTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择绑定时间"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="验证时间" prop="verifiedAt">
          <el-date-picker
            clearable
            v-model="form.verifiedAt"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择验证时间"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
      </el-form>
      <template #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="UserCard">
import {
  listUserCard,
  getUserCard,
  delUserCard,
  addUserCard,
  updateUserCard,
} from "@/api/service/userCard";

const { proxy } = getCurrentInstance();
const { sys_yes_no } = proxy.useDict("sys_yes_no");

const userCardList = 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 data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userId: null,
    cardType: null,
    bankName: null,
    cardNumber: null,
    cardNumberHash: null,
    cardHolder: null,
    cardLastFour: null,
    phoneNumber: null,
    isDefault: null,
    status: null,
    bindTime: null,
    verifiedAt: null,
  },
  rules: {
    userId: [{ required: true, message: "用户ID不能为空", trigger: "blur" }],
    cardType: [
      {
        required: true,
        message: "卡类型 (1: 借记卡, 2: 信用卡)不能为空",
        trigger: "change",
      },
    ],
    bankName: [
      { required: true, message: "银行名称不能为空", trigger: "blur" },
    ],
    cardNumber: [
      { required: true, message: "银行卡号不能为空", trigger: "blur" },
    ],
    cardNumberHash: [
      { required: true, message: "卡号哈希不能为空", trigger: "blur" },
    ],
    cardHolder: [
      { required: true, message: "持卡人姓名不能为空", trigger: "blur" },
    ],
    cardLastFour: [
      { required: true, message: "卡号后四位不能为空", trigger: "blur" },
    ],
    isDefault: [
      {
        required: true,
        message: "是否默认 (0: 否, 1: 是)不能为空",
        trigger: "change",
      },
    ],
    status: [
      {
        required: true,
        message: "状态 (0: 正常, 1: 冻结, 2: 删除)不能为空",
        trigger: "change",
      },
    ],
    bindTime: [
      { required: true, message: "绑定时间不能为空", trigger: "blur" },
    ],
    createTime: [
      { required: true, message: "创建时间不能为空", trigger: "blur" },
    ],
    updateTime: [
      { required: true, message: "更新时间不能为空", trigger: "blur" },
    ],
  },
});

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

/** 查询用户银行卡列表 */
function getList() {
  loading.value = true;
  listUserCard(queryParams.value).then((response) => {
    userCardList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

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

// 表单重置
function reset() {
  form.value = {
    cardId: null,
    userId: null,
    cardType: null,
    bankName: null,
    cardNumber: null,
    cardNumberHash: null,
    cardHolder: null,
    cardLastFour: null,
    phoneNumber: null,
    isDefault: null,
    status: null,
    bindTime: null,
    verifiedAt: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    delFlag: null,
  };
  proxy.resetForm("userCardRef");
}

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

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

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

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加用户银行卡";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _cardId = row.cardId || ids.value;
  getUserCard(_cardId).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改用户银行卡";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["userCardRef"].validate((valid) => {
    if (valid) {
      if (form.value.cardId != null) {
        updateUserCard(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addUserCard(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _cardIds = row.cardId || ids.value;
  proxy.$modal
    .confirm('是否确认删除用户银行卡编号为"' + _cardIds + '"的数据项？')
    .then(function () {
      return delUserCard(_cardIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "service/userCard/export",
    {
      ...queryParams.value,
    },
    `userCard_${new Date().getTime()}.xlsx`,
  );
}

getList();
</script>
