<template>
  <div class="app-container">
    <!-- 列表 -->
    <!-- 搜索 -->
    <page-search
      ref="searchRef"
      :search-config="searchConfig"
      @query-click="handleQueryClick"
      @reset-click="handleResetClick"
    />
    <!-- 列表 -->
    <page-content
      ref="contentRef"
      :content-config="contentConfig"
      @add-click="handleAddClick"
      @edit-click="handleEditClick"
      @export-click="handleExportClick"
      @search-click="handleSearchClick"
      @toolbar-click="handleToolbarClick"
      @operat-click="handleOperatClick"
      @filter-change="handleFilterChange"
    >
      <template #status="scope">
        <el-tag :type="scope.row[scope.prop] == 1 ? 'success' : 'info'">
          {{ scope.row[scope.prop] == 1 ? "启用" : "禁用" }}
        </el-tag>
      </template>
      <template #options="scope">
        {{ returnOptionsLabel(scope.prop, scope.row[scope.prop]) }}
      </template>
      <template #accountBalance="scope">
        {{ scope.row.accountBalance }}
      </template>

      <template #owedAmount="scope">
        {{
          scope.row.creditAmount - scope.row.accountBalance > 0
            ? scope.row.creditAmount - scope.row.accountBalance
            : 0
        }}
      </template>

      <template #expireRemindDay="scope">
        <el-text>{{ scope.row.expireRemindDay }}号</el-text>
      </template>
      <template #expireRemind="scope">
        <el-text>{{ scope.row.expireRemind }}月前</el-text>
      </template>
      <template #remainingAmount="scope">
        {{ scope.row.creditAmount - scope.row.accountBalance }}
      </template>
      <template #gender="scope">
        <DictLabel v-model="scope.row[scope.prop]" code="gender" />
      </template>
      <template #mobile="scope">
        <el-text>{{ scope.row[scope.prop] }}</el-text>
        <copy-button
          v-if="scope.row[scope.prop]"
          :text="scope.row[scope.prop]"
          style="margin-left: 2px"
        />
      </template>
      <template #tools="scope">
        <el-button
          type="primary"
          link
          size="small"
          @click="
            handleOperatClick({
              name: 'detail',
              row: scope.row,
              column: scope.column,
              $index: scope.$index,
            })
          "
        >
          查看明细
        </el-button>

        <el-button type="primary" link size="small" @click="handleEditClick(scope.row)">
          编辑
        </el-button>

        <el-button
          type="primary"
          link
          :style="{ color: scope.row.repaymentMethod == 'order' ? '#999' : '' }"
          size="small"
          @click="
            handleOperatClick({
              name: 'huankuan',
              row: scope.row,
              column: scope.column,
              $index: scope.$index,
            })
          "
        >
          还款
        </el-button>

        <el-button
          type="primary"
          link
          size="small"
          @click="
            handleOperatClick({
              name: 'huankuan_detail',
              row: scope.row,
              column: scope.column,
              $index: scope.$index,
            })
          "
        >
          还款记录
        </el-button>

        <el-button
          type="primary"
          link
          size="small"
          @click="
            handleOperatClick({
              name: 'delete',
              row: scope.row,
              column: scope.column,
              $index: scope.$index,
            })
          "
        >
          删除
        </el-button>
      </template>
    </page-content>

    <!-- 新增 -->
    <page-modal ref="addModalRef" :modal-config="addModalConfig" @submit-click="handleSubmitClick">
      <template #formFooter>
        <el-form-item label="" label-width="140">
          <p>一经创建无法更改还款方式</p>
        </el-form-item>
      </template>
    </page-modal>

    <!-- 编辑 -->
    <page-modal
      ref="editModalRef"
      :modal-config="editModalConfig"
      @submit-click="handleSubmitClick"
    ></page-modal>
    <!-- 还款 -->
    <huanKuan ref="refHuanKuan" @success="refresh"></huanKuan>
    <!-- 还款记录 -->
    <huanKuanRecord ref="refHuanKuanRecord"></huanKuanRecord>
  </div>
</template>

<script setup lang="ts">
import creditApi from "@/api/order/credit";
import huanKuan from "./components/detail/credit_repayment.vue";
import huanKuanRecord from "./components/detail/credit_RePaymentRecord.vue";
import type { IObject, IOperatData } from "@/components/CURD/types";
import usePage from "@/components/CURD/usePage";
import addModalConfig from "./config/add";
import contentConfig from "./config/content";
import editModalConfig from "./config/edit";
import searchConfig from "./config/search";
import { returnOptionsLabel } from "./config/config";
import { ElMessageBox, ElMessage } from "element-plus";
const {
  searchRef,
  contentRef,
  addModalRef,
  editModalRef,
  handleQueryClick,
  handleResetClick,
  // handleAddClick,
  // handleEditClick,
  handleSubmitClick,
  handleExportClick,
  handleSearchClick,
  handleFilterChange,
} = usePage();

// 新增
async function handleAddClick() {
  addModalRef.value?.setModalVisible();
  // addModalConfig.formItems[2]!.attrs!.data =
}
// 编辑
async function handleEditClick(row: IObject) {
  editModalRef.value?.handleDisabled(false);
  editModalRef.value?.setModalVisible();
  // 根据id获取数据进行填充
  // const data = await creditApi.getFormData(row.id);
  console.log({ ...row });
  const item = editModalConfig?.formItems.find((item) => item.prop == "repaymentMethod");
  if (item) {
    item.options = (item.options ?? []).filter((v) => v.value == row.repaymentMethod);
  }
  editModalRef.value?.setFormData({ ...row, url: [row.url] });
}
function refresh() {
  contentRef.value?.fetchPageData();
}
const router = useRouter();
// 其他工具栏
function handleToolbarClick(name: string) {}
// 其他操作列
async function handleOperatClick(data: IOperatData) {
  console.log(data);

  if (data.name == "detail") {
    router.push({
      name: "creditDetail",
      query: {
        id: data.row.id,
        repaymentMethod: data.row.repaymentMethod,
      },
    });
    return;
  }
  if (data.name == "huankuan" && data.row.repaymentMethod == "total") {
    refHuanKuanShow(data.row, {});
    return;
  }
  if (data.name == "huankuan_detail") {
    refHuanKuanRecordShow(data.row, {});
    return;
  }
  if (data.name == "delete") {
    ElMessageBox.alert("是否删除挂账人" + data.row.debtor + "？", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
      callback: async (action: string) => {
        if (action == "confirm") {
          const res = await creditApi.delete(data.row.id);
          if (res.code == 200) {
            ElMessage.success("删除成功");
            refresh();
          }
        }
      },
    });
    return;
  }
}

// 还款
const refHuanKuan = ref();
function refHuanKuanShow(row: any, order: any) {
  refHuanKuan.value.show(row, order);
}
const refHuanKuanRecord = ref();
function refHuanKuanRecordShow(row: any, order: any) {
  refHuanKuanRecord.value.show(row, order);
}
</script>
