<template>
  <PageShell>
    <div class="apply-page">
      <div class="apply-card">
        <div class="apply-header">
          <div class="apply-title">{{ pageTitle }}</div>
          <div class="apply-actions">
            <el-button @click="handleBack">返回</el-button>
          </div>
        </div>

        <el-form :model="queryParams" :inline="true" class="apply-search-form">
          <el-form-item class="apply-search-name-item">
            <el-input v-model="queryParams.keyword" placeholder="请输入岗位" clearable>
              <template #prefix>
                <el-icon><Search /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item class="apply-search-btn-item">
            <el-button type="primary" icon="Search" />
          </el-form-item>
          <el-form-item class="apply-search-select-item">
            <el-select v-model="queryParams.auditStatus" placeholder="审核状态" clearable>
              <el-option v-for="item in applyStatusOptions" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item class="apply-search-select-item">
            <el-select v-model="queryParams.ability" placeholder="测评能力" clearable>
              <el-option v-for="item in abilityOptions" :key="item" :label="item" :value="item" />
            </el-select>
          </el-form-item>
          <div class="apply-toolbar-actions">
            <el-button>录用</el-button>
            <el-button type="primary">导出列表</el-button>
          </div>
        </el-form>

        <el-table :data="applyList" class="apply-list-table">
          <el-table-column label="姓名" prop="name" min-width="160">
            <template #default="scope">
              <el-button link type="primary" @click="handleOpenDetail(scope.row)">{{ scope.row.name }}</el-button>
            </template>
          </el-table-column>
          <el-table-column label="性别" prop="gender" width="100" align="center" />
          <el-table-column label="手机号" prop="phone" min-width="180" align="center" />
          <el-table-column label="状态" min-width="160" align="center">
            <template #default="scope">
              <span class="apply-status-text">
                <span class="apply-status-dot" :class="`is-${scope.row.statusType}`"></span>
                {{ scope.row.statusText }}
              </span>
            </template>
          </el-table-column>
          <el-table-column label="测评时间" prop="evaluateTime" min-width="180" align="center" />
          <el-table-column label="操作" min-width="220" align="center">
            <template #default="scope">
              <template v-if="scope.row.statusText !== '学员已拒绝'">
                <el-button link type="primary">测评详情</el-button>
                <el-button link type="primary">下载附件简历</el-button>
                <template v-if="scope.row.statusText === '待回复'">
                  <el-button link type="primary" @click="handleHire">录用</el-button>
                  <el-button link type="primary" @click="handleReject">不录用</el-button>
                </template>
              </template>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :page="queryParams.pageNum" :limit="queryParams.pageSize" :total="total" />
      </div>

      <el-dialog v-model="hireDialog.visible" title="审核说明" width="680px" append-to-body>
        <div class="result-dialog-content">
          <div class="result-line"><span class="result-label">结果:</span><span class="result-value">录用</span></div>
          <div class="result-upload-row">
            <el-button>上传文件</el-button>
          </div>
          <div class="result-file-list">
            <div v-for="item in hireDialog.files" :key="item" class="result-file-item">{{ item }}</div>
          </div>
          <div class="result-remark-row">
            <span class="result-label top">说明</span>
            <el-input v-model="hireDialog.remark" type="textarea" :rows="5" placeholder="请输入" />
          </div>
          <el-checkbox v-model="hireDialog.agree">我已阅读并同意服务 offer 确认前免责协议</el-checkbox>
        </div>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="hireDialog.visible = false">返回</el-button>
            <el-button type="primary" @click="hireDialog.visible = false">确定</el-button>
          </div>
        </template>
      </el-dialog>

      <el-dialog v-model="rejectDialog.visible" title="审核说明" width="520px" append-to-body>
        <div class="result-dialog-content compact">
          <div class="result-line"><span class="result-label">结果:</span><span class="result-value">不录用</span></div>
          <div class="result-remark-row compact">
            <span class="result-label top">备注:</span>
            <el-input v-model="rejectDialog.remark" type="textarea" :rows="4" placeholder="说明" />
          </div>
        </div>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="rejectDialog.visible = false">取消</el-button>
            <el-button type="primary" @click="rejectDialog.visible = false">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </PageShell>
</template>

<script setup name="PostManageApplyList" lang="ts">
import { computed, getCurrentInstance, reactive, type ComponentInternalInstance } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import PageShell from '@/components/PageShell/index.vue';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const router = useRouter();
const route = useRoute();
const pageTitle = computed(() => {
  const evaluationName = route.query.evaluationName as string | undefined;
  return evaluationName ? `${evaluationName} 报名列表` : '报名列表';
});

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  keyword: '',
  auditStatus: '',
  ability: ''
});

const total = 243;

const applyStatusOptions = [
  { label: '录用', value: 'adopted' },
  { label: '不采用', value: 'rejected' },
  { label: '待回复', value: 'pending' },
  { label: '学员已拒绝', value: 'closed' }
];

const abilityOptions = ['测评能力'];

const applyList = reactive([
  {
    id: 1,
    name: '小张小赵白有主张',
    gender: '男',
    phone: '18438573743',
    statusText: '录用',
    statusType: 'success',
    evaluateTime: '2025-03-21 10:27:56'
  },
  {
    id: 2,
    name: '小张小赵白有主张',
    gender: '男',
    phone: '18438573743',
    statusText: '不录用',
    statusType: 'danger',
    evaluateTime: '2025-03-21 10:27:56'
  },
  {
    id: 3,
    name: '小张小赵白有主张',
    gender: '男',
    phone: '18438573743',
    statusText: '待回复',
    statusType: 'warning',
    evaluateTime: '2025-03-21 10:27:56'
  },
  {
    id: 4,
    name: '小张小赵白有主张',
    gender: '男',
    phone: '18438573743',
    statusText: '学员已拒绝',
    statusType: 'danger',
    evaluateTime: '2025-03-21 10:27:56'
  }
]);

const hireDialog = reactive({
  visible: false,
  remark: '',
  agree: true,
  files: ['文档.docx', '附件.png（上传...）', '组件123.jpg']
});

const rejectDialog = reactive({
  visible: false,
  remark: ''
});

const handleBack = () => {
  proxy?.$tab.closePage();
  router.push((route.query.backPath as string) || '/postManage');
};

const handleHire = () => {
  hireDialog.visible = true;
};

const handleReject = () => {
  rejectDialog.visible = true;
};

const handleOpenDetail = (row: { id: number; name: string }) => {
  router.push({
    path: '/postManage/apply-detail',
    query: {
      applyId: String(row.id),
      name: row.name,
      postId: String(route.query.postId || '')
    }
  });
};
</script>

<style scoped>
.apply-page {
  min-height: 100%;
}

.apply-card {
  min-height: calc(100vh - 180px);
  padding: 12px 16px 16px;
  background: #fff;
  border-radius: 4px;
  display: flex;
  flex-direction: column;
}

.apply-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.apply-title {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
}

.apply-search-form {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}

.apply-search-name-item {
  width: 280px;
  margin-bottom: 0;
}
.apply-search-btn-item {
  margin-left: -80px;
}

.apply-search-btn-item,
.apply-search-select-item {
  margin-bottom: 0;
}

.apply-search-select-item {
  width: 140px;
}

.apply-toolbar-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-left: auto;
}

.apply-list-table :deep(.el-table__header th) {
  background: #eef4ff;
}

.apply-list-table {
  flex: 1;
}

.apply-status-text {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.apply-status-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #c0c4cc;
}

.apply-status-dot.is-success {
  background: #67c23a;
}

.apply-status-dot.is-danger {
  background: #f56c6c;
}

.apply-status-dot.is-warning {
  background: #e6a23c;
}

.apply-card :deep(.pagination-container) {
  display: flex;
  justify-content: flex-end;
  margin-top: auto;
  padding-top: 20px;
  padding: 0;
}

.dialog-footer {
  display: flex;
  justify-content: center;
  gap: 16px;
}

.result-dialog-content {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 8px 8px 0;
}

.result-dialog-content.compact {
  gap: 16px;
}

.result-line {
  display: flex;
  align-items: center;
  gap: 8px;
}

.result-label {
  width: 44px;
  color: #303133;
  flex-shrink: 0;
}

.result-label.top {
  padding-top: 8px;
}

.result-value {
  color: #303133;
}

.result-upload-row {
  padding-left: 44px;
}

.result-file-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding-left: 44px;
}

.result-file-item {
  color: #606266;
  font-size: 13px;
}

.result-remark-row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
}

.result-remark-row.compact :deep(.el-textarea) {
  flex: 1;
}
</style>
