<template>
  <div class="main">
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px]"
    >
      <el-form-item label="编码" prop="code">
        <el-input
          v-model="form.code"
          placeholder="请输入编码"
          clearable
          class="!w-[180px]"
        />
      </el-form-item>
      <el-form-item label="编码描述" prop="description">
        <el-input
          v-model="form.description"
          clearable
          placeholder="请输入编码描述~"
          class="!w-[180px]"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon('ri:search-line')"
          :loading="loading"
          @click="onSearch"
        >
          {{ t("buttons:Search") }}
        </el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
          {{ t("buttons:Reset") }}
        </el-button>
      </el-form-item>
    </el-form>
    <PureTableBar title="编码管理" :columns="columns" @refresh="onSearch">
      <template #buttons>
        <el-button
          v-if="hasAuth('code:btn:add')"
          type="primary"
          :disabled="!hasAuth('code:btn:add')"
          :icon="useRenderIcon(AddFill)"
          @click="openDialog('新增')"
        >
          {{ t("buttons:Add") }}
        </el-button>
        <el-button
          v-if="hasAuth('code:btn:import')"
          type="primary"
          :icon="useRenderIcon(AddFill)"
          :disabled="!hasAuth('code:btn:import')"
          @click="showUploadArea = !showUploadArea"
        >
          {{ showUploadArea ? t("buttons:Hide") : t("buttons:Import") }}
        </el-button>
      </template>
      <template v-slot="{ size, dynamicColumns }">
        <!-- 上传文件区域 -->
        <el-card v-if="showUploadArea" shadow="never">
          <el-upload
            ref="uploadRef"
            v-model:file-list="fileList"
            drag
            action="#"
            class="w-full"
            :auto-upload="false"
            :before-upload="beforeUpload"
            :before-remove="beforeRemove"
          >
            <div class="el-upload__text">
              <IconifyIconOffline
                :icon="UploadIcon"
                width="26"
                :limit="1"
                class="m-auto mb-2"
              />
              可点击或拖拽上传
            </div>
          </el-upload>
          <template #footer>
            <div class="flex items-center justify-center">
              <el-button
                v-if="hasAuth('code:btn:uploadTemplate')"
                class="w-full my-2"
                :disabled="!hasAuth('code:btn:uploadTemplate')"
                @click="onDownloadTemplate"
                >{{ t("buttons:DownLodedTemplate") }}</el-button
              >
              <el-popconfirm
                v-if="hasAuth('code:btn:import')"
                title="是否确认上传?"
                @confirm="handleUpload"
              >
                <template #reference>
                  <el-button
                    class="w-full my-2"
                    type="primary"
                    :disabled="uploadStatus || !hasAuth('code:btn:import')"
                    >{{ t("buttons:ConfirmUpload") }}</el-button
                  >
                </template>
              </el-popconfirm>
            </div>
          </template>
        </el-card>
        <div
          v-if="selectedNum > 0"
          v-motion-fade
          class="bg-[var(--el-fill-color-light)] w-full h-[46px] mb-2 pl-4 flex items-center"
        >
          <div class="flex-auto">
            <span
              style="font-size: var(--el-font-size-base)"
              class="text-[rgba(42,46,54,0.5)] dark:text-[rgba(220,220,242,0.5)]"
            >
              已选 {{ selectedNum }} 项
            </span>
            <el-button type="primary" text @click="onSelectionCancel">
              {{ t("buttons:Deselect") }}
            </el-button>
          </div>
          <el-popconfirm
            v-if="hasAuth('code:btn:delete')"
            title="是否确认删除?"
            @confirm="onbatchDel"
          >
            <template #reference>
              <el-button
                type="danger"
                text
                class="mr-1"
                :disabled="selectedNum < 0 || !hasAuth('code:btn:delete')"
              >
                {{ t("buttons:DeleteInBatches") }}
              </el-button>
            </template>
          </el-popconfirm>
        </div>
        <pure-table
          ref="tableRef"
          row-key="id"
          adaptive
          border
          stripe
          :adaptiveConfig="{ offsetBottom: 45 }"
          align-whole="center"
          table-layout="auto"
          :loading="loading"
          :size="size"
          :data="dataList"
          :columns="dynamicColumns"
          :pagination="pagination"
          :paginationSmall="size === 'small' ? true : false"
          :header-cell-style="{
            background: 'var(--el-fill-color-light)',
            color: 'var(--el-text-color-primary)'
          }"
          @selection-change="handleSelectionChange"
          @page-size-change="handleSizeChange"
          @page-current-change="handleCurrentChange"
        >
          <template #operation="{ row }">
            <el-button
              class="reset-margin"
              link
              type="primary"
              :size="size"
              :disabled="!hasAuth('code:btn:update')"
              :icon="useRenderIcon(EditPen)"
              @click="openDialog('修改', row)"
            >
              {{ t("buttons:Update") }}
            </el-button>
            <el-popconfirm
              :title="`是否确认删除编码名为 ${row.code} 的这条数据`"
              @confirm="handleDelete(row)"
            >
              <template #reference>
                <el-button
                  class="reset-margin"
                  link
                  type="danger"
                  :size="size"
                  :disabled="!hasAuth('code:btn:delete')"
                  :icon="useRenderIcon(Delete)"
                >
                  {{ t("buttons:Delete") }}
                </el-button>
              </template>
            </el-popconfirm>
          </template>
        </pure-table>
      </template>
    </PureTableBar>
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "CodeAdmin"
});
import { ref } from "vue";
import { useCode } from "./utils/hook";
import { useI18n } from "vue-i18n";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import Delete from "@iconify-icons/ep/delete";
import EditPen from "@iconify-icons/ep/edit-pen";
import Refresh from "@iconify-icons/ep/refresh";
import AddFill from "@iconify-icons/ri/add-circle-line";
import UploadIcon from "@iconify-icons/ri/upload-2-line";
import { hasAuth } from "@/utils/auth";
const { t } = useI18n();

/**
 * 表格Ref
 */
const tableRef = ref();
const formRef = ref();
const {
  form,
  dataList,
  loading,
  pagination,
  columns,
  selectedNum,
  showUploadArea,
  fileIds,
  fileList,
  uploadStatus,
  beforeUpload,
  handleUpload,
  beforeRemove,
  openDialog,
  onSearch,
  resetForm,
  handleDelete,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  onSelectionCancel,
  onbatchDel,
  onDownloadTemplate
} = useCode(tableRef);
</script>

<style scoped lang="scss">
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}

:deep(.el-button:focus-visible) {
  outline: none;
}

.main-content {
  margin: 24px 24px 0 !important;
}

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