<template>
  <div class="audit-container">
    <el-container class="audit-layout h-full">
      <!-- 左侧分类侧边栏 -->
      <el-aside width="250px" class="mr-4 h-full sidebar modern-card">
        <div class="sidebar-title">审核类型</div>
        <div class="p-4">
          <el-input v-model="filterText" placeholder="请输入" :prefix-icon="Search" />
        </div>
        <el-menu :default-active="activeMenu" class="border-none mt-2" @select="handleMenuSelect">
          <el-menu-item index="1" :class="{'menu-item-active': activeMenu === '1'}">
            <template #title>
              <el-icon><i-ep-operation /></el-icon>
              <span>招聘单位</span>
            </template>
          </el-menu-item>
          <el-menu-item index="2" :class="{'menu-item-active': activeMenu === '2'}">
            <template #title>
              <el-icon><i-ep-document /></el-icon>
              <span>岗位</span>
            </template>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <!-- 右侧主体内容 -->
      <el-main class="h-full main-content p-0 modern-card">
        <!-- 搜索与头部区域 -->
        <div class="header-section">
          <div class="flex items-center mb-4">
             <el-input v-model="queryParams.applyNo" placeholder="请输入编号或关键词" style="width: 300px" clearable @keyup.enter="handleQuery">
                <template #append>
                   <el-button type="primary" icon="Search" class="bg-blue-500 text-white search-btn" @click="handleQuery"></el-button>
                </template>
             </el-input>
          </div>
          
          <div class="flex justify-between items-center mb-4">
            <div class="tabs">
              <span class="tab-item" :class="{ active: queryParams.auditResult === undefined }" @click="handleTabClick(undefined)">全部</span>
              <span class="tab-item" :class="{ active: queryParams.auditResult === 1 }" @click="handleTabClick(1)">已审核</span>
              <span class="tab-item" :class="{ active: queryParams.auditResult === 0 }" @click="handleTabClick(0)">待审核</span>
              <span class="tab-item" :class="{ active: queryParams.auditResult === 2 }" @click="handleTabClick(2)">未通过</span>
            </div>
            <div>
              <el-button type="primary" plain class="batch-pass-btn" @click="handleBatchPass">批量通过</el-button>
              <el-button type="warning" plain class="batch-reject-btn">批量驳回</el-button>
            </div>
          </div>
        </div>

        <!-- 表格区域 -->
        <div class="table-section pb-4 px-4">
          <el-table v-loading="loading" :data="tableData" style="width: 100%" @selection-change="handleSelectionChange" header-cell-class-name="custom-header">
            <el-table-column type="selection" width="40" align="center" />
            
            <!-- 通用列：申请编号 -->
            <el-table-column prop="applyNo" label="编号" width="100" show-overflow-tooltip />

            <!-- 企业审核列 -->
            <template v-if="activeMenu === '1'">
              <el-table-column prop="companyName" label="公司名称" min-width="150" show-overflow-tooltip />
              <el-table-column prop="creditCode" label="统一社会信用代码" min-width="180" show-overflow-tooltip />
            </template>

            <!-- 岗位审核列 -->
            <template v-else>
              <el-table-column prop="companyName" label="公司名称" min-width="120" show-overflow-tooltip />
              <el-table-column prop="postName" label="岗位" min-width="100" show-overflow-tooltip />
              <el-table-column prop="postLevelName" label="岗位等级" width="90" align="center">
                <template #default="scope">
                  <span>{{ scope.row.postLevelName || scope.row.postLevel || '-' }}</span>
                </template>
              </el-table-column>
              <el-table-column label="办公地点" min-width="120" show-overflow-tooltip>
                <template #default="scope">
                  <span>{{ scope.row.workProvince || '' }} {{ scope.row.workCity || '' }}</span>
                </template>
              </el-table-column>
            </template>

            <el-table-column label="审核状态" width="90" align="center">
              <template #default="scope">
                <span :class="getAuditStatusClass(scope.row.auditResult)">{{ getAuditStatusLabel(scope.row.auditResult) }}</span>
              </template>
            </el-table-column>

            <el-table-column prop="auditRemark" label="备注" min-width="100" show-overflow-tooltip />

            <!-- 岗位审核特有列 -->
            <template v-if="activeMenu === '2'">
              <el-table-column prop="postTypeName" label="岗位类型" width="100" align="center">
                <template #default="scope">
                  <span>{{ scope.row.postTypeName || scope.row.postType || '-' }}</span>
                </template>
              </el-table-column>
              <el-table-column prop="recruitNum" label="招聘人数" width="90" align="center">
                <template #default="scope">
                  <span>{{ scope.row.recruitNum || '不限' }}</span>
                </template>
              </el-table-column>
              <el-table-column prop="salaryRange" label="薪资" width="120" align="center">
                <template #default="scope">
                  <span>{{ scope.row.salaryRange || '-' }}</span>
                </template>
              </el-table-column>
              <el-table-column label="报名时间" min-width="160" align="center" show-overflow-tooltip>
                <template #default="scope">
                  <span v-if="scope.row.registrationStartDate || scope.row.registrationEndDate">
                    {{ scope.row.registrationStartDate }} 至 {{ scope.row.registrationEndDate }}
                  </span>
                  <span v-else>不限</span>
                </template>
              </el-table-column>
            </template>

            <!-- 企业审核特有列：审核时间 -->
            <el-table-column v-if="activeMenu === '1'" prop="auditTime" label="审核时间" width="160" align="center" show-overflow-tooltip />

            <el-table-column label="操作" width="120" align="center" fixed="right">
              <template #default="scope">
                <el-link type="primary" :underline="false" style="font-size: 13px" @click="handleView(scope.row)">查看</el-link>
                
                <el-button type="success" size="small" class="ml-2 audit-btn" v-if="scope.row.auditResult === 0" @click="handleOpenAudit(scope.row)">
                  审核
                </el-button>
              </template>
            </el-table-column>
          </el-table>

          <div class="pagination-wrapper mt-4">
             <pagination
                v-show="total > 0"
                :total="total"
                v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize"
                @pagination="getList"
             />
          </div>
        </div>
      </el-main>
    </el-container>

    <!-- 审核说明弹窗 -->
    <el-dialog v-model="auditDialogVisible" width="500px" :show-close="true" custom-class="audit-dialog">
      <template #header>
        <div class="dialog-header-line">
          <span class="font-bold text-gray-800 text-base">审核说明</span>
        </div>
      </template>
      
      <div class="tabs-container">
        <el-tabs v-model="auditForm.result" class="audit-tabs">
          <el-tab-pane label="通过" name="pass" />
          <el-tab-pane label="驳回" name="reject" />
        </el-tabs>
      </div>

      <el-form :model="auditForm" label-width="80px" class="mt-6 px-4">
        <el-form-item label="结果:">
          <span class="text-gray-700 font-medium">{{ auditForm.result === 'pass' ? '通过' : '驳回' }}</span>
        </el-form-item>
        
        <el-form-item>
          <template #label>
            <span v-if="auditForm.result === 'reject'" class="text-red-500 mr-1">*</span>
            备注:
          </template>
          <el-input
            v-model="auditForm.remark"
            type="textarea"
            :rows="4"
            placeholder="请输入审核备注说明..."
            :class="{'reject-border': auditForm.result === 'reject'}"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer-line flex justify-end">
          <el-button @click="auditDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="confirmAudit" class="confirm-btn">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { Search } from '@element-plus/icons-vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { listAudit, auditPass, auditReject, delAudit } from '@/api/system/audit';
import { AuditVO, AuditQuery } from '@/api/system/audit/types';

const router = useRouter();
const loading = ref(true);
const total = ref(0);
const ids = ref<Array<string | number>>([]);
const filterText = ref('');
const activeMenu = ref('1');

// 查询参数
const queryParams = reactive<AuditQuery>({
  pageNum: 1,
  pageSize: 10,
  applyNo: undefined,
  auditType: '1',
  auditResult: undefined
});

/** 查询审核信息列表 */
const getList = async () => {
  loading.value = true;
  const res = await listAudit(queryParams);
  tableData.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

const handleMenuSelect = (index: string) => {
  activeMenu.value = index;
  queryParams.auditType = index;
  handleQuery();
};

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

/** 重置按钮操作 */
const resetQuery = () => {
  queryParams.applyNo = undefined;
  handleQuery();
};

const tableData = ref<AuditVO[]>([]);

/** 多选框选中数据 */
const handleSelectionChange = (selection: AuditVO[]) => {
  ids.value = selection.map((item) => item.id);
};

const getAuditStatusClass = (status: number) => {
  const map: Record<number, string> = {
    0: 'text-blue-500',
    1: 'text-green-500',
    2: 'text-red-500'
  };
  return map[status] || 'text-gray-500';
};

const getAuditStatusLabel = (status: number) => {
  const map: Record<number, string> = {
    0: '待审核',
    1: '已通过',
    2: '未通过'
  };
  return map[status] || '未知';
};

const handleView = (row: AuditVO) => {
  if (activeMenu.value === '2') {
    router.push({ path: '/system/audit-hidden/job-detail', query: { id: row.id } });
  } else {
    router.push({ path: '/system/audit-hidden/detail', query: { id: row.id } });
  }
};

// 弹窗逻辑
const auditDialogVisible = ref(false);
const currentId = ref<string | number>('');
const auditForm = reactive({
  result: 'pass',
  remark: ''
});

const handleOpenAudit = (row: AuditVO) => {
  currentId.value = row.id;
  auditForm.result = 'pass';
  auditForm.remark = '';
  auditDialogVisible.value = true;
};

const handleAuditCommand = (cmd: string, row: AuditVO) => {
  currentId.value = row.id;
  auditForm.result = cmd;
  auditForm.remark = '';
  auditDialogVisible.value = true;
};

/** 确认审核 */
const confirmAudit = async () => {
  if (auditForm.result === 'reject' && !auditForm.remark.trim()) {
    ElMessage.error('驳回时备注为必填项！');
    return;
  }
  
  try {
    if (auditForm.result === 'pass') {
      await auditPass(currentId.value, auditForm.remark);
    } else {
      await auditReject(currentId.value, auditForm.remark);
    }
    ElMessage.success('审核操作成功');
    auditDialogVisible.value = false;
    getList();
  } catch (error) {
    ElMessage.error('审核操作失败');
  }
};

/** 批量通过 */
const handleBatchPass = async () => {
  if (ids.value.length === 0) {
    ElMessage.warning('请选择要通过的数据');
    return;
  }
  await ElMessageBox.confirm('是否确认通过选中的数据？');
  try {
    for (const id of ids.value) {
      await auditPass(id, '批量审核通过');
    }
    ElMessage.success('批量操作成功');
    getList();
  } catch (error) {
    ElMessage.error('批量操作失败');
  }
};

/** 状态页签切换 */
const handleTabClick = (status?: number) => {
  queryParams.auditResult = status;
  handleQuery();
};

onMounted(() => {
  getList();
});
</script>

<style scoped lang="scss">
.audit-container {
  padding: 20px;
  background-color: #f0f2f5;
  min-height: calc(100vh - 84px);
  height: calc(100vh - 84px);
  box-sizing: border-box;
}

.audit-layout {
  height: 100%;
  align-items: stretch;
}

.sidebar {
  border-radius: 4px;
  height: 100%;
  display: flex;
  flex-direction: column;

  :deep(.el-menu) {
    flex: 1;
  }

  .sidebar-title {
    padding: 20px;
    font-size: 16px;
    font-weight: 600;
  }
  .menu-item-active {
    background-color: #ecf5ff;
    color: #409eff;
    border-right: 2px solid #409eff;
  }
}

.main-content {
  border-radius: 4px;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.header-section {
  padding: 20px;
  border-bottom: 1px solid #e5e6eb;

  .search-btn {
    border-radius: 0 4px 4px 0;
  }

  .tabs {
    display: flex;
    gap: 30px;
    font-size: 14px;
    color: #4e5969;
    
    .tab-item {
      cursor: pointer;
      position: relative;
      padding-bottom: 8px;
      
      &.active {
        color: #1d2129;
        font-weight: 600;
        
        &::after {
          content: '';
          position: absolute;
          bottom: 0;
          left: 0;
          width: 100%;
          height: 2px;
          background-color: #409eff;
        }
      }
    }
  }
  
  .batch-pass-btn { background-color: #e8f3ff; border-color: #e8f3ff; color: #165dff; }
  .batch-reject-btn { background-color: #fff7e8; border-color: #fff7e8; color: #ff7d00; }
}

.table-section {
  padding-top: 15px;
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.table-section :deep(.el-table) {
  flex: 1;
}

:deep(.custom-header th) {
  background-color: #f7f8fa !important;
  color: #4e5969;
  font-weight: 500;
  padding: 8px 0;
}

.cert-img {
  width: 50px;
  height: 35px;
  object-fit: cover;
  border: 1px solid #e5e6eb;
  border-radius: 2px;
}

.audit-btn {
  background-color: #8cde9f;
  border-color: #8cde9f;
  &:hover {
    background-color: #72cf88;
  }
}

.pagination-wrapper {
  display: flex;
  justify-content: flex-end;
}

/* 深色浮层菜单样式 */
.dark-dropdown {
  background-color: #333333 !important;
  border: none !important;
  border-radius: 6px;
  padding: 5px 0;
  
  :deep(.el-dropdown-menu__item) {
    color: #fff;
    padding: 8px 16px;
    font-size: 13px;
    
    &:hover {
      background-color: #444444;
      color: #fff;
    }
    
    .el-icon {
      margin-right: 8px;
      font-size: 14px;
    }
  }
  
  /* 覆盖弹出框默认的白色箭头和样式 */
  &.el-popper {
    .el-popper__arrow::before {
      background: #333333 !important;
      border: none !important;
    }
  }
}

/* 弹窗样式优化 */
.audit-dialog {
  :deep(.el-dialog__header) {
    margin-right: 0;
    padding: 20px 24px;
    border-bottom: 1px solid #f0f0f0;
  }
  :deep(.el-dialog__body) {
    padding: 0;
  }
  :deep(.el-dialog__footer) {
    padding: 20px 24px;
    border-top: 1px solid #f0f0f0;
  }
}

.tabs-container {
  padding: 0 24px;
  background-color: #fff;
}

.audit-tabs {
  :deep(.el-tabs__header) {
    margin: 0;
  }
  :deep(.el-tabs__nav-wrap::after) {
    height: 1px;
    background-color: #f0f0f0;
  }
  :deep(.el-tabs__item) {
    font-size: 15px;
    height: 50px;
    line-height: 50px;
  }
}

.confirm-btn {
  background-color: #409eff;
  border-color: #409eff;
  padding: 0 25px;
}

.reject-border {
  :deep(.el-textarea__inner) {
    border-color: #165dff !important;
    box-shadow: 0 0 0 1px #165dff inset !important;
  }
}

/* 取消输入框的圆角以符合UI */
:deep(.el-textarea__inner) {
  border-radius: 4px;
}
</style>
