<script setup lang="ts">
import { ref } from "vue";
import { useShopArchive } from "./utils/hook";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";

import Delete from "~icons/ep/delete";
import EditPen from "~icons/ep/edit-pen";
import Refresh from "~icons/ep/refresh";
import AddFill from "~icons/ri/add-circle-line";
import View from "~icons/ep/view";

defineOptions({
  name: "ShopArchiveManage"
});

const formRef = ref();
const tableRef = ref();

const {
  form,
  loading,
  columns,
  dataList,
  pagination,
  contractStatusOptions,
  businessStatusOptions,
  billingModeOptions,
  onSearch,
  resetForm,
  handleAdd,
  handleUpdate,
  handleView,
  handleDelete,
  handleSizeChange,
  handleCurrentChange
} = useShopArchive(tableRef);
</script>

<template>
  <div class="main">
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      class="search-form bg-bg_color w-full pl-8 pt-[12px] overflow-auto"
    >
      <el-form-item label="门店名称：" prop="shopName">
        <el-input
          v-model="form.shopName"
          placeholder="请输入门店名称"
          clearable
          class="w-[180px]!"
        />
      </el-form-item>
      <el-form-item label="合同编号：" prop="contractNo">
        <el-input
          v-model="form.contractNo"
          placeholder="请输入合同编号"
          clearable
          class="w-[180px]!"
        />
      </el-form-item>
      <el-form-item label="合同状态：" prop="contractStatus">
        <el-select
          v-model="form.contractStatus"
          placeholder="请选择合同状态"
          clearable
          class="w-[160px]!"
        >
          <el-option
            v-for="item in contractStatusOptions"
            :key="item.code"
            :label="item.description"
            :value="item.code"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="经营状态：" prop="businessStatus">
        <el-select
          v-model="form.businessStatus"
          placeholder="请选择经营状态"
          clearable
          class="w-[160px]!"
        >
          <el-option
            v-for="item in businessStatusOptions"
            :key="item.code"
            :label="item.description"
            :value="item.code"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="计费模式：" prop="billingMode">
        <el-select
          v-model="form.billingMode"
          placeholder="请选择计费模式"
          clearable
          class="w-[160px]!"
        >
          <el-option
            v-for="item in billingModeOptions"
            :key="item.code"
            :label="item.description"
            :value="item.code"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon('ri/search-line')"
          :loading="loading"
          @click="onSearch"
        >
          搜索
        </el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <PureTableBar
      title="门店档案管理"
      :columns="columns"
      @refresh="onSearch"
    >
      <template #buttons>
        <el-button
          type="primary"
          :icon="useRenderIcon(AddFill)"
          @click="handleAdd"
        >
          新增档案
        </el-button>
      </template>
      <template v-slot="{ size, dynamicColumns }">
        <pure-table
          ref="tableRef"
          row-key="id"
          adaptive
          :adaptiveConfig="{ offsetBottom: 108 }"
          align-whole="center"
          table-layout="auto"
          :loading="loading"
          :size="size"
          :data="dataList"
          :columns="dynamicColumns"
          :pagination="{ ...pagination, size }"
          :header-cell-style="{
            background: 'var(--el-fill-color-light)',
            color: 'var(--el-text-color-primary)'
          }"
          @page-size-change="handleSizeChange"
          @page-current-change="handleCurrentChange"
        >
          <template #operation="{ row }">
            <el-button
              class="reset-margin"
              link
              type="primary"
              :size="size"
              :icon="useRenderIcon(View)"
              @click="handleView(row)"
            >
              查看
            </el-button>
            <el-button
              class="reset-margin"
              link
              type="primary"
              :size="size"
              :icon="useRenderIcon(EditPen)"
              @click="handleUpdate(row)"
            >
              编辑
            </el-button>
            <el-popconfirm
              :title="`是否确认删除门店档案 ${row.shopName}?`"
              @confirm="handleDelete(row)"
            >
              <template #reference>
                <el-button
                  class="reset-margin"
                  link
                  type="danger"
                  :size="size"
                  :icon="useRenderIcon(Delete)"
                >
                  删除
                </el-button>
              </template>
            </el-popconfirm>
          </template>
        </pure-table>
      </template>
    </PureTableBar>
  </div>
</template>

<style lang="scss" scoped>
.main-content {
  margin: 24px 24px 0 !important;
}

.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
</style>
