<template>
  <div class="audit-container">
    <el-container class="h-full">
      <!-- 左侧分类侧边栏 -->
      <el-aside width="200px" class="bg-white mr-4 h-full sidebar">
        <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="bg-white h-full main-content p-0">
        <!-- 搜索与头部区域 -->
        <div class="header-section">
          <div class="flex items-center mb-4">
             <el-input v-model="queryParams.keyword" placeholder="请输入" style="width: 300px" clearable>
                <template #append>
                   <el-button type="primary" icon="Search" class="bg-blue-500 text-white search-btn"></el-button>
                </template>
             </el-input>
          </div>
          
          <div class="flex justify-between items-center mb-4">
            <div class="tabs">
              <span class="tab-item active">全部(265)</span>
              <span class="tab-item">已审核(225)</span>
              <span class="tab-item">待审核(10)</span>
              <span class="tab-item">未通过(20)</span>
            </div>
            <div>
              <el-button type="primary" plain class="batch-pass-btn">批量通过</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 :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="id" label="编号" width="80" show-overflow-tooltip />
            <el-table-column prop="companyName" label="公司名称" min-width="100" show-overflow-tooltip />
            <el-table-column prop="type" label="招聘类型" min-width="120" show-overflow-tooltip />
            <el-table-column prop="creditCode" label="统一社会信用代码" min-width="140" show-overflow-tooltip />
            <el-table-column label="状态" width="70" align="center">
              <template #default="scope">
                <el-switch v-model="scope.row.status" />
              </template>
            </el-table-column>
            <el-table-column label="审核状态" width="90" align="center">
              <template #default="scope">
                <span :class="getAuditStatusClass(scope.row.auditStatus)">{{ scope.row.auditStatusLabel }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="remark" label="备注" min-width="80" show-overflow-tooltip />
            <el-table-column prop="address" label="办公地址" min-width="100" show-overflow-tooltip />
            <el-table-column label="授权证明" width="90" align="center">
              <template #default="scope">
                <img :src="scope.row.certImg" class="cert-img" />
              </template>
            </el-table-column>
            <el-table-column prop="contact" label="联系人" width="80" align="center" show-overflow-tooltip />
            <el-table-column prop="phone" label="手机号" width="110" align="center" show-overflow-tooltip />
            <el-table-column label="操作" width="140" align="center" fixed="right">
              <template #default="scope">
                <el-link type="primary" :underline="false" style="font-size: 13px" @click="handleView(scope.row)">查看</el-link>
                
                <el-dropdown trigger="click" @command="(cmd: string) => handleAuditCommand(cmd, scope.row)" placement="bottom-end">
                  <el-button type="success" size="small" class="ml-2 audit-btn" v-if="scope.row.auditStatus === 'pending'">
                    审核
                  </el-button>
                  <template #dropdown>
                    <el-dropdown-menu class="dark-dropdown">
                      <el-dropdown-item command="pass" class="text-white">
                        <el-icon><i-ep-check /></el-icon> 审核通过
                      </el-dropdown-item>
                      <el-dropdown-item command="reject" class="text-white">
                        <el-icon><i-ep-close /></el-icon> 审核驳回
                      </el-dropdown-item>
                    </el-dropdown-menu>
                  </template>
                </el-dropdown>
              </template>
            </el-table-column>
          </el-table>

          <div class="pagination-wrapper mt-4">
             <el-pagination background layout="total, sizes, prev, pager, next" :total="243" />
          </div>
        </div>
      </el-main>
    </el-container>

    <!-- 审核说明弹窗 -->
    <el-dialog v-model="auditDialogVisible" width="500px" :show-close="true" custom-class="audit-dialog">
      <template #header>
        <span class="font-bold text-gray-800 text-base">审核说明</span>
      </template>
      <el-form :model="auditForm" label-width="60px" class="mt-4">
        <div class="mb-5 text-gray-700 ml-4 font-medium">结果：{{ auditForm.result === 'pass' ? '通过' : '驳回' }}</div>
        
        <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="flex justify-end mt-2">
          <el-button @click="auditDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="confirmAudit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue';
import { useRouter } from 'vue-router';
import { Search } from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus';

const router = useRouter();
const filterText = ref('');
const queryParams = reactive({ keyword: '' });

const activeMenu = ref('1');

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

const tableData = ref([
  {
    id: 'BZ6542',
    companyName: 'XXXX公司',
    type: '全职、兼职、实习',
    creditCode: '234234234',
    status: true,
    auditStatus: 'pending',
    auditStatusLabel: '待审核',
    remark: '',
    email: 'BS5656665',
    address: 'XXXX',
    certImg: 'https://via.placeholder.com/40',
    contact: '张三',
    phone: '1844637778'
  },
  {
    id: 'BZ6542',
    companyName: 'XXXX公司',
    type: '全职、兼职、实习',
    creditCode: '234234234',
    status: true,
    auditStatus: 'passed',
    auditStatusLabel: '已审核',
    remark: '',
    email: 'BS5656665',
    address: 'XXXX',
    certImg: 'https://via.placeholder.com/40',
    contact: '李四',
    phone: '1844637778'
  },
  {
    id: 'BZ6542',
    companyName: 'XXXX公司',
    type: '全职、兼职、实习',
    creditCode: '234234234',
    status: false,
    auditStatus: 'rejected',
    auditStatusLabel: '未通过',
    remark: '信誉不行',
    email: 'BS5656665',
    address: 'XXXX',
    certImg: 'https://via.placeholder.com/40',
    contact: '李四',
    phone: '1844637778'
  }
]);

const handleSelectionChange = (val: any) => {
  // console.log(val);
};

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

const handleView = (row: any) => {
  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 auditForm = reactive({
  result: 'pass',
  remark: ''
});

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

const confirmAudit = () => {
  if (auditForm.result === 'reject' && !auditForm.remark.trim()) {
    ElMessage.error('驳回时备注为必填项！');
    return;
  }
  ElMessage.success('审核操作成功');
  auditDialogVisible.value = false;
};
</script>

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

.sidebar {
  border-radius: 4px;
  .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;
}

.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;
}

: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;
    }
  }
}

.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>
