<script setup lang="tsx">
import { computed, reactive, ref, watch } from 'vue';
import type { UploadFile, UploadFiles } from 'element-plus';
import { ElButton, ElTag } from 'element-plus';
import {
  activatePerformanceTemplate,
  fetchPerformanceTemplatePage,
  uploadFile,
  uploadPerformanceTemplate
} from '@/service/api';
import { useUIPaginatedTable } from '@/hooks/common/table';
import BusinessFormDialog from '@/components/custom/business-form-dialog.vue';
import BusinessTableActionCell, { type BusinessTableAction } from '@/components/custom/business-table-action-cell';
import { formatDateTime } from './performance-shared';

defineOptions({ name: 'PerformanceTemplateDialog' });

const visible = defineModel<boolean>('visible', { default: false });

const emit = defineEmits<{
  updated: [];
}>();

type TemplatePageResponse = Awaited<ReturnType<typeof fetchPerformanceTemplatePage>>;

const searchParams = reactive<Api.Performance.Template.SearchParams>({
  pageNo: 1,
  pageSize: 10,
  templateName: undefined,
  activeFlag: undefined
});

const uploadForm = reactive({
  templateName: '',
  remark: '',
  activeFlag: true,
  file: null as File | null
});

const uploading = ref(false);
const activatingId = ref('');

const { columns, columnChecks, data, loading, getDataByPage, mobilePagination } = useUIPaginatedTable<
  TemplatePageResponse,
  Api.Performance.Template.Template
>({
  paginationProps: {
    currentPage: searchParams.pageNo,
    pageSize: searchParams.pageSize
  },
  api: () => fetchPerformanceTemplatePage(searchParams),
  transform: response => {
    if (!response.error && response.data) {
      return {
        data: response.data.list,
        pageNum: searchParams.pageNo ?? 1,
        pageSize: searchParams.pageSize ?? 10,
        total: response.data.total
      };
    }

    return {
      data: [],
      pageNum: 1,
      pageSize: 10,
      total: 0
    };
  },
  onPaginationParamsChange: params => {
    searchParams.pageNo = params.currentPage ?? 1;
    searchParams.pageSize = params.pageSize ?? 10;
  },
  columns: () => [
    { prop: 'index', type: 'index', label: '序号', width: 64 },
    { prop: 'templateName', label: '模板名称', minWidth: 170, showOverflowTooltip: true },
    // { prop: 'fileName', label: '文件名', minWidth: 200, showOverflowTooltip: true },
    // { prop: 'versionNo', label: '版本', width: 80 },
    {
      prop: 'activeFlag',
      label: '状态',
      width: 100,
      formatter: row => <ElTag type={row.activeFlag ? 'success' : 'info'}>{row.activeFlag ? '当前' : '历史'}</ElTag>
    },
    { prop: 'uploadUserName', label: '上传人', width: 110 },
    {
      prop: 'uploadTime',
      label: '上传时间',
      width: 180,
      formatter: row => formatDateTime(row.uploadTime)
    },
    {
      prop: 'operate',
      label: '操作',
      width: 110,
      align: 'center',
      fixed: 'right',
      formatter: row => <BusinessTableActionCell actions={getTemplateActions(row)} />
    }
  ]
});

const selectedFileName = computed(() => uploadForm.file?.name || '');

function getTemplateActions(row: Api.Performance.Template.Template): BusinessTableAction[] {
  return [
    {
      key: 'activate',
      label: row.activeFlag ? '已启用' : '启用',
      buttonType: 'primary',
      disabled: row.activeFlag || Boolean(activatingId.value),
      onClick: () => handleActivate(row)
    }
  ];
}

function handleFileChange(file: UploadFile, _files: UploadFiles) {
  const rawFile = file.raw;

  if (!rawFile) return;

  uploadForm.file = rawFile;
  if (!uploadForm.templateName) {
    uploadForm.templateName = rawFile.name.replace(/\.[^.]+$/u, '');
  }
}

async function handleUploadTemplate() {
  if (!uploadForm.file) {
    window.$message?.warning('请选择 Excel 模板文件');
    return;
  }
  if (!uploadForm.templateName.trim()) {
    window.$message?.warning('请输入模板名称');
    return;
  }

  uploading.value = true;
  const fileResult = await uploadFile(uploadForm.file, 'performance/templates');
  if (fileResult.error || !fileResult.data) {
    uploading.value = false;
    return;
  }

  const result = await uploadPerformanceTemplate({
    templateName: uploadForm.templateName.trim(),
    fileId: fileResult.data.id,
    fileName: uploadForm.file.name,
    activeFlag: uploadForm.activeFlag,
    remark: uploadForm.remark.trim() || undefined
  });
  uploading.value = false;

  if (result.error) return;

  window.$message?.success('绩效模板已上传');
  Object.assign(uploadForm, {
    templateName: '',
    remark: '',
    activeFlag: true,
    file: null
  });
  await getDataByPage(1);
  emit('updated');
}

async function handleActivate(row: Api.Performance.Template.Template) {
  activatingId.value = row.id;
  const { error } = await activatePerformanceTemplate(row.id);
  activatingId.value = '';

  if (error) return;

  window.$message?.success('绩效模板已启用');
  await getDataByPage(searchParams.pageNo ?? 1);
  emit('updated');
}

watch(visible, isVisible => {
  if (isVisible) {
    getDataByPage(1);
  }
});
</script>

<template>
  <BusinessFormDialog
    v-model="visible"
    title="绩效模板"
    preset="lg"
    append-to-body
    :show-footer="false"
    max-body-height="76vh"
  >
    <div class="performance-template-dialog">
      <ElCard shadow="never">
        <ElForm :model="uploadForm" label-position="top" class="performance-template-dialog__upload-form">
          <div class="performance-template-dialog__upload-grid">
            <ElFormItem label="模板名称" class="performance-template-dialog__field">
              <ElInput v-model="uploadForm.templateName" placeholder="请输入模板名称" />
            </ElFormItem>

            <ElFormItem label="Excel 文件" class="performance-template-dialog__field">
              <div class="performance-template-dialog__file-picker">
                <ElUpload
                  :auto-upload="false"
                  :show-file-list="false"
                  accept=".xlsx,.xls"
                  :limit="1"
                  :on-change="handleFileChange"
                >
                  <ElButton plain>
                    <template #icon>
                      <icon-mdi-upload class="text-icon" />
                    </template>
                    选择文件
                  </ElButton>
                </ElUpload>
                <div class="performance-template-dialog__file-hint">
                  {{ selectedFileName || '支持 .xlsx、.xls，选择后会在这里显示文件名' }}
                </div>
              </div>
            </ElFormItem>

            <ElFormItem
              label="上传后启用"
              class="performance-template-dialog__field performance-template-dialog__switch-field"
            >
              <div class="performance-template-dialog__switch-box">
                <span>上传后立即切换为当前模板</span>
                <ElSwitch v-model="uploadForm.activeFlag" />
              </div>
            </ElFormItem>

            <ElFormItem
              label="备注"
              class="performance-template-dialog__field performance-template-dialog__field--full"
            >
              <ElInput v-model="uploadForm.remark" type="textarea" :rows="3" maxlength="500" show-word-limit />
            </ElFormItem>
          </div>

          <div class="performance-template-dialog__actions">
            <ElButton type="primary" :loading="uploading" @click="handleUploadTemplate">上传模板</ElButton>
          </div>
        </ElForm>
      </ElCard>

      <ElCard shadow="never" body-class="business-table-card-body">
        <template #header>
          <div class="flex items-center justify-between gap-12px">
            <p class="text-16px font-600">模板列表</p>
            <ElSpace wrap alignment="center">
              <ElButton @click="getDataByPage()">
                <template #icon>
                  <icon-mdi-refresh class="text-icon" :class="{ 'animate-spin': loading }" />
                </template>
                刷新
              </ElButton>
              <TableColumnSetting v-model:columns="columnChecks" />
            </ElSpace>
          </div>
        </template>

        <div class="performance-template-dialog__table">
          <ElTable v-loading="loading" height="100%" border :data="data">
            <template v-for="col in columns" :key="String(col.prop)">
              <ElTableColumn v-bind="col" />
            </template>
          </ElTable>
        </div>

        <div class="mt-16px flex justify-end">
          <ElPagination
            v-if="mobilePagination.total"
            layout="total,prev,pager,next,sizes"
            v-bind="mobilePagination"
            @current-change="mobilePagination['current-change']"
            @size-change="mobilePagination['size-change']"
          />
        </div>
      </ElCard>
    </div>
  </BusinessFormDialog>
</template>

<style scoped lang="scss">
.performance-template-dialog {
  display: grid;
  gap: 16px;
}

.performance-template-dialog__upload-form {
  display: grid;
  gap: 16px;
}

.performance-template-dialog__upload-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 16px;
}

.performance-template-dialog__field {
  margin-bottom: 0;
}

.performance-template-dialog__field--full {
  grid-column: 1 / -1;
}

.performance-template-dialog__file-picker {
  display: grid;
  gap: 10px;
}

.performance-template-dialog__file-hint {
  min-height: 40px;
  display: flex;
  align-items: center;
  padding: 0 12px;
  overflow: hidden;
  border: 1px dashed var(--el-border-color);
  border-radius: 8px;
  background: var(--el-fill-color-light);
  color: var(--el-text-color-secondary);
  font-size: 12px;
  line-height: 1.5;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.performance-template-dialog__switch-field {
  align-self: stretch;
}

.performance-template-dialog__switch-box {
  height: 100%;
  min-height: 72px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 14px;
  border: 1px solid var(--el-border-color);
  border-radius: 8px;
  background: var(--el-fill-color-blank);
  color: var(--el-text-color-regular);
  font-size: 13px;
}

.performance-template-dialog__actions {
  display: flex;
  justify-content: flex-end;
}

.performance-template-dialog__table {
  height: 360px;
}

@media (max-width: 1080px) {
  .performance-template-dialog__upload-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .performance-template-dialog__switch-field {
    grid-column: 1 / -1;
  }
}

@media (max-width: 768px) {
  .performance-template-dialog__upload-grid {
    grid-template-columns: 1fr;
  }

  .performance-template-dialog__field--full,
  .performance-template-dialog__switch-field {
    grid-column: auto;
  }

  .performance-template-dialog__switch-box {
    min-height: 56px;
  }
}
</style>
