<template>
  <div class="app-container">
    <el-card>
      <!-- 顶部切换 -->
      <div class="tab-container">
        <el-radio-group v-model="currentTab" size="large">
          <el-radio-button value="non-cadre">非干部</el-radio-button>
          <el-radio-button value="cadre">干部</el-radio-button>
        </el-radio-group>
      </div>

      <!-- 查询条件 -->
      <div class="filter-container">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" class="search-form">
          <el-form-item label="姓名" prop="employeeName">
            <el-input v-model="queryParams.employeeName" placeholder="请输入姓名" clearable style="width: 200px" />
          </el-form-item>
          
          <el-form-item label="考核月份" prop="assessmentMonth">
            <el-date-picker v-model="queryParams.assessmentMonth" type="month" placeholder="请选择考核月份" 
              value-format="YYYY-MM" style="width: 200px" />
          </el-form-item>
          
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">查询</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 操作按钮 -->
      <div class="operation-container">
        <!-- 非干部操作按钮 -->
        <div v-if="currentTab === 'non-cadre'" class="left-buttons">
          <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
          <el-button type="warning" plain icon="Download" @click="handleExport">导出</el-button>
        </div>
        <!-- 干部模式时显示空白占位 -->
        <div v-else class="left-buttons"></div>
        
        <div class="right-buttons">
          <el-button type="primary" @click="generateMonthlyAssessment">生成本月考核表</el-button>
        </div>
      </div>

      <!-- 非干部数据表格 -->
      <div v-if="currentTab === 'non-cadre'">
        <el-table v-loading="loading" :data="nonCadreList" border fit highlight-current-row style="width: 100%; margin-top: 20px;">
          <el-table-column label="大队" prop="brigade" align="center" min-width="100" />
          <el-table-column label="用工形式" prop="employmentType" align="center" min-width="100" />
          <el-table-column label="员工编号" prop="employeeCode" align="center" min-width="120" />
          <el-table-column label="员工姓名" prop="employeeName" align="center" min-width="120" />
          <el-table-column label="考核组" prop="assessmentGroup" align="center" min-width="100" />
          <el-table-column label="分管班组长" prop="teamLeader" align="center" min-width="120" />
          <el-table-column label="分管主管" prop="supervisor" align="center" min-width="120" />
          <el-table-column label="红线指标触发次数" prop="redLineTriggerCount" align="center" min-width="140" />
          <el-table-column label="红线指标依据" prop="redLineBasis" align="center" min-width="120" show-overflow-tooltip />
          <el-table-column label="核心指标分值" prop="coreIndicatorScore" align="center" min-width="120" />
          <el-table-column label="核心指标依据" prop="coreIndicatorBasis" align="center" min-width="120" show-overflow-tooltip />
          <el-table-column label="其他指标分值" prop="otherIndicatorScore" align="center" min-width="120" />
          <el-table-column label="其他指标依据" prop="otherIndicatorBasis" align="center" min-width="120" show-overflow-tooltip />
          
          <el-table-column label="其他指标中的安全（仅含SOC/站品控检查扣分）分值" prop="safetyWithSocScore" align="center" min-width="200" show-overflow-tooltip />
          <el-table-column label="其他指标中的安全（仅含SOC/站品控检查扣分）依据" prop="safetyWithSocBasis" align="center" min-width="200" show-overflow-tooltip />
          
          <el-table-column label="其他指标中的安全（不含SOC/站品控检查扣分）分值" prop="safetyWithoutSocScore" align="center" min-width="200" show-overflow-tooltip />
          <el-table-column label="其他指标中的安全（不含SOC/站品控检查扣分）依据" prop="safetyWithoutSocBasis" align="center" min-width="200" show-overflow-tooltip />
          
          <el-table-column label="其他指标中的非安全指标扣分" prop="nonSafetyDeduction" align="center" min-width="160" show-overflow-tooltip />
          <el-table-column label="其他指标中的非安全指标扣分依据" prop="nonSafetyDeductionBasis" align="center" min-width="160" show-overflow-tooltip />
          
          <el-table-column label="非核心安全+核心扣分" prop="nonCoreSafetyCoreDeduction" align="center" min-width="140" />
          <el-table-column label="SOC/站品控检查的涉及核心、安全指标扣分" prop="socSafetyCoreDeduction" align="center" min-width="200" show-overflow-tooltip />
          <el-table-column label="SOC/站品控检查的涉及核心、安全指标扣分依据" prop="socSafetyCoreDeductionBasis" align="center" min-width="200" show-overflow-tooltip />
          
          <el-table-column label="分管员工数量" prop="managedEmployeeCount" align="center" min-width="120" />
          <el-table-column label="扣分平均值" prop="averageDeduction" align="center" min-width="100" />
          <el-table-column label="总分" prop="totalScore" align="center" min-width="100" sortable />
          <el-table-column label="奖励明细" prop="rewardDetails" align="center" min-width="120" show-overflow-tooltip />
          <el-table-column label="惩罚明细" prop="penaltyDetails" align="center" min-width="120" show-overflow-tooltip />
          <el-table-column label="奖励（元）" prop="rewardAmount" align="center" min-width="100" />
          <el-table-column label="扣罚（元）" prop="penaltyAmount" align="center" min-width="100" />
          <el-table-column label="考核结果" prop="assessmentResult" align="center" min-width="100" />
          <el-table-column label="考核结果备注" prop="assessmentResultRemark" align="center" min-width="140" show-overflow-tooltip />
          <el-table-column label="应用方式" prop="applicationMethod" align="center" min-width="100" />
          <el-table-column label="应用方式备注" prop="applicationMethodRemark" align="center" min-width="140" show-overflow-tooltip />
          <el-table-column label="是否豁免" prop="isExempted" align="center" min-width="100">
            <template #default="scope">
              <el-tag :type="scope.row.isExempted ? 'success' : 'info'">
                {{ scope.row.isExempted ? '是' : '否' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="是否豁免备注" prop="exemptionRemark" align="center" min-width="140" show-overflow-tooltip />
          <el-table-column label="考核月份" prop="assessmentMonth" align="center" min-width="120" />

          <el-table-column label="操作" align="center" width="150" fixed="right">
            <template #default="scope">
              <el-button size="small" type="primary" link @click="handleEdit(scope.row, 'non-cadre')">修改</el-button>
              <el-button size="small" type="danger" link @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 干部数据表格 -->
      <div v-else>
        <el-table v-loading="loading" :data="cadreList" border fit highlight-current-row style="width: 100%; margin-top: 20px;">
          <el-table-column label="员工姓名" prop="employeeName" align="center" min-width="120" />
          <el-table-column label="岗位" prop="position" align="center" min-width="120" />
          <el-table-column label="区域" prop="area" align="center" min-width="100" />
          <el-table-column label="红线扣分" prop="redLineDeduction" align="center" min-width="100" />
          <el-table-column label="红线扣分豁免情况" prop="redLineExemption" align="center" min-width="140" show-overflow-tooltip />
          <el-table-column label="违规排名扣分" prop="violationRankingDeduction" align="center" min-width="140" />
          <el-table-column label="违规排名扣分豁免情况" prop="violationRankingExemption" align="center" min-width="180" show-overflow-tooltip />
          <el-table-column label="技能排名扣分" prop="skillRankingDeduction" align="center" min-width="140" />
          <el-table-column label="技能排名扣分豁免情况" prop="skillRankingExemption" align="center" min-width="180" show-overflow-tooltip />
          <el-table-column label="总分" prop="totalScore" align="center" min-width="100" sortable />
          <el-table-column label="考核结果" prop="assessmentResult" align="center" min-width="120" />
          <el-table-column label="考核结果备注" prop="assessmentResultRemark" align="center" min-width="140" show-overflow-tooltip />
          <el-table-column label="考核月份" prop="assessmentMonth" align="center" min-width="120" />
          <el-table-column label="应用方式" prop="applicationMethod" align="center" min-width="120" />
          <el-table-column label="应用方式备注" prop="applicationMethodRemark" align="center" min-width="160" show-overflow-tooltip />

          <el-table-column label="操作" align="center" width="150" fixed="right">
            <template #default="scope">
              <el-button size="small" type="primary" link @click="handleEdit(scope.row, 'cadre')">修改</el-button>
              <el-button size="small" type="danger" link @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 分页 -->
      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" 
        v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>

    <!-- 编辑/新增弹窗 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="80%" :close-on-click-modal="false">
      <!-- 非干部表单 -->
      <el-form v-if="dialog.type === 'non-cadre'" :model="nonCadreForm" ref="formRef" :rules="nonCadreRules" label-width="180px" class="form-container">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="大队" prop="brigade">
              <el-input v-model="nonCadreForm.brigade" placeholder="请输入大队" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="用工形式" prop="employmentType">
              <el-select v-model="nonCadreForm.employmentType" placeholder="请选择用工形式" style="width: 100%">
                <el-option v-for="item in employmentTypeOptions" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="员工编号" prop="employeeCode">
              <el-input v-model="nonCadreForm.employeeCode" placeholder="请输入员工编号" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="员工姓名" prop="employeeName">
              <el-input v-model="nonCadreForm.employeeName" placeholder="请输入员工姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="考核组" prop="assessmentGroup">
              <el-input v-model="nonCadreForm.assessmentGroup" placeholder="请输入考核组" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="考核月份" prop="assessmentMonth">
              <el-date-picker v-model="nonCadreForm.assessmentMonth" type="month" placeholder="请选择考核月份" 
                value-format="YYYY-MM" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="分管班组长" prop="teamLeader">
              <el-input v-model="nonCadreForm.teamLeader" placeholder="请输入分管班组长" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="分管主管" prop="supervisor">
              <el-input v-model="nonCadreForm.supervisor" placeholder="请输入分管主管" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="分管员工数量" prop="managedEmployeeCount">
              <el-input-number v-model="nonCadreForm.managedEmployeeCount" :min="0" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-divider content-position="left">红线指标</el-divider>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="红线指标触发次数" prop="redLineTriggerCount">
              <el-input-number v-model="nonCadreForm.redLineTriggerCount" :min="0" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="红线指标依据" prop="redLineBasis">
              <el-input v-model="nonCadreForm.redLineBasis" type="textarea" :rows="2" placeholder="请输入红线指标依据" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-divider content-position="left">核心指标</el-divider>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="核心指标分值" prop="coreIndicatorScore">
              <el-input-number v-model="nonCadreForm.coreIndicatorScore" :min="0" :precision="2" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="核心指标依据" prop="coreIndicatorBasis">
              <el-input v-model="nonCadreForm.coreIndicatorBasis" type="textarea" :rows="2" placeholder="请输入核心指标依据" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-divider content-position="left">其他指标</el-divider>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="其他指标分值" prop="otherIndicatorScore">
              <el-input-number v-model="nonCadreForm.otherIndicatorScore" :min="0" :precision="2" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="其他指标依据" prop="otherIndicatorBasis">
              <el-input v-model="nonCadreForm.otherIndicatorBasis" type="textarea" :rows="2" placeholder="请输入其他指标依据" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-divider content-position="left">其他指标中的安全（仅含SOC/站品控检查扣分）</el-divider>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="分值" prop="safetyWithSocScore">
              <el-input-number v-model="nonCadreForm.safetyWithSocScore" :min="0" :precision="2" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="依据" prop="safetyWithSocBasis">
              <el-input v-model="nonCadreForm.safetyWithSocBasis" type="textarea" :rows="2" placeholder="请输入依据" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-divider content-position="left">其他指标中的安全（不含SOC/站品控检查扣分）</el-divider>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="分值" prop="safetyWithoutSocScore">
              <el-input-number v-model="nonCadreForm.safetyWithoutSocScore" :min="0" :precision="2" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="依据" prop="safetyWithoutSocBasis">
              <el-input v-model="nonCadreForm.safetyWithoutSocBasis" type="textarea" :rows="2" placeholder="请输入依据" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-divider content-position="left">其他指标中的非安全指标扣分</el-divider>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="扣分" prop="nonSafetyDeduction">
              <el-input-number v-model="nonCadreForm.nonSafetyDeduction" :min="0" :precision="2" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="依据" prop="nonSafetyDeductionBasis">
              <el-input v-model="nonCadreForm.nonSafetyDeductionBasis" type="textarea" :rows="2" placeholder="请输入依据" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-divider content-position="left">综合扣分</el-divider>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="非核心安全+核心扣分" prop="nonCoreSafetyCoreDeduction">
              <el-input-number v-model="nonCadreForm.nonCoreSafetyCoreDeduction" :min="0" :precision="2" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="SOC/站品控检查的涉及核心、安全指标扣分" prop="socSafetyCoreDeduction">
              <el-input-number v-model="nonCadreForm.socSafetyCoreDeduction" :min="0" :precision="2" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="SOC/站品控检查的涉及核心、安全指标扣分依据" prop="socSafetyCoreDeductionBasis">
              <el-input v-model="nonCadreForm.socSafetyCoreDeductionBasis" type="textarea" :rows="2" placeholder="请输入依据" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-divider content-position="left">统计信息</el-divider>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="扣分平均值" prop="averageDeduction">
              <el-input-number v-model="nonCadreForm.averageDeduction" :min="0" :precision="2" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="总分" prop="totalScore">
              <el-input-number v-model="nonCadreForm.totalScore" :min="0" :precision="2" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="奖励（元）" prop="rewardAmount">
              <el-input-number v-model="nonCadreForm.rewardAmount" :min="0" :precision="2" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="扣罚（元）" prop="penaltyAmount">
              <el-input-number v-model="nonCadreForm.penaltyAmount" :min="0" :precision="2" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="考核结果" prop="assessmentResult">
              <el-select v-model="nonCadreForm.assessmentResult" placeholder="请选择考核结果" style="width: 100%">
                <el-option v-for="item in assessmentResultOptions" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="应用方式" prop="applicationMethod">
              <el-select v-model="nonCadreForm.applicationMethod" placeholder="请选择应用方式" style="width: 100%">
                <el-option v-for="item in applicationMethodOptions" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="奖励明细" prop="rewardDetails">
              <el-input v-model="nonCadreForm.rewardDetails" type="textarea" :rows="2" placeholder="请输入奖励明细" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="惩罚明细" prop="penaltyDetails">
              <el-input v-model="nonCadreForm.penaltyDetails" type="textarea" :rows="2" placeholder="请输入惩罚明细" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="考核结果备注" prop="assessmentResultRemark">
              <el-input v-model="nonCadreForm.assessmentResultRemark" type="textarea" :rows="2" placeholder="请输入考核结果备注" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="应用方式备注" prop="applicationMethodRemark">
              <el-input v-model="nonCadreForm.applicationMethodRemark" type="textarea" :rows="2" placeholder="请输入应用方式备注" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-divider content-position="left">豁免信息</el-divider>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="是否豁免" prop="isExempted">
              <el-switch v-model="nonCadreForm.isExempted" active-text="是" inactive-text="否" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否豁免备注" prop="exemptionRemark">
              <el-input v-model="nonCadreForm.exemptionRemark" type="textarea" :rows="2" placeholder="请输入豁免备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <!-- 干部表单 -->
      <el-form v-else :model="cadreForm" ref="formRef" :rules="cadreRules" label-width="180px" class="form-container">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="员工姓名" prop="employeeName">
              <el-input v-model="cadreForm.employeeName" placeholder="请输入员工姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="岗位" prop="position">
              <el-input v-model="cadreForm.position" placeholder="请输入岗位" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="区域" prop="area">
              <el-input v-model="cadreForm.area" placeholder="请输入区域" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="考核月份" prop="assessmentMonth">
              <el-date-picker v-model="cadreForm.assessmentMonth" type="month" placeholder="请选择考核月份" 
                value-format="YYYY-MM" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="考核结果" prop="assessmentResult">
              <el-select v-model="cadreForm.assessmentResult" placeholder="请选择考核结果" style="width: 100%">
                <el-option v-for="item in assessmentResultOptions" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="应用方式" prop="applicationMethod">
              <el-select v-model="cadreForm.applicationMethod" placeholder="请选择应用方式" style="width: 100%">
                <el-option v-for="item in applicationMethodOptions" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-divider content-position="left">红线扣分</el-divider>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="红线扣分" prop="redLineDeduction">
              <el-input-number v-model="cadreForm.redLineDeduction" :min="0" :precision="2" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="红线扣分豁免情况" prop="redLineExemption">
              <el-input v-model="cadreForm.redLineExemption" type="textarea" :rows="2" placeholder="请输入豁免情况" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-divider content-position="left">违规排名扣分</el-divider>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="违规排名扣分" prop="violationRankingDeduction">
              <el-input-number v-model="cadreForm.violationRankingDeduction" :min="0" :precision="2" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="违规排名扣分豁免情况" prop="violationRankingExemption">
              <el-input v-model="cadreForm.violationRankingExemption" type="textarea" :rows="2" placeholder="请输入豁免情况" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-divider content-position="left">技能排名扣分</el-divider>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="技能排名扣分" prop="skillRankingDeduction">
              <el-input-number v-model="cadreForm.skillRankingDeduction" :min="0" :precision="2" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="技能排名扣分豁免情况" prop="skillRankingExemption">
              <el-input v-model="cadreForm.skillRankingExemption" type="textarea" :rows="2" placeholder="请输入豁免情况" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-divider content-position="left">统计信息</el-divider>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="总分" prop="totalScore">
              <el-input-number v-model="cadreForm.totalScore" :min="0" :precision="2" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="考核结果备注" prop="assessmentResultRemark">
              <el-input v-model="cadreForm.assessmentResultRemark" type="textarea" :rows="2" placeholder="请输入考核结果备注" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="应用方式备注" prop="applicationMethodRemark">
              <el-input v-model="cadreForm.applicationMethodRemark" type="textarea" :rows="2" placeholder="请输入应用方式备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialog.visible = false">取消</el-button>
          <el-button type="primary" @click="submitForm">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, getCurrentInstance } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// API导入（需要根据实际API路径调整）
import { getNonCadreMonthlyAssessList, addNonCadreMonthlyAssess, updateNonCadreMonthlyAssess, deleteNonCadreMonthlyAssess } from '@/api/performance/nonCadreMonthlyAssess.js'

const { proxy } = getCurrentInstance()

// 响应式数据
const loading = ref(false)
const total = ref(0)
const queryFormRef = ref()
const formRef = ref()
const currentTab = ref('non-cadre')

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  employeeName: '',
  assessmentMonth: ''
})

// 非干部表单数据
const nonCadreForm = reactive({
  brigade: '',
  employmentType: '',
  employeeCode: '',
  employeeName: '',
  assessmentGroup: '',
  teamLeader: '',
  supervisor: '',
  managedEmployeeCount: 0,
  assessmentMonth: '',
  redLineTriggerCount: 0,
  redLineBasis: '',
  coreIndicatorScore: 0,
  coreIndicatorBasis: '',
  otherIndicatorScore: 0,
  otherIndicatorBasis: '',
  safetyWithSocScore: 0,
  safetyWithSocBasis: '',
  safetyWithoutSocScore: 0,
  safetyWithoutSocBasis: '',
  nonSafetyDeduction: 0,
  nonSafetyDeductionBasis: '',
  nonCoreSafetyCoreDeduction: 0,
  socSafetyCoreDeduction: 0,
  socSafetyCoreDeductionBasis: '',
  averageDeduction: 0,
  totalScore: 0,
  rewardAmount: 0,
  penaltyAmount: 0,
  assessmentResult: '',
  applicationMethod: '',
  rewardDetails: '',
  penaltyDetails: '',
  assessmentResultRemark: '',
  applicationMethodRemark: '',
  isExempted: false,
  exemptionRemark: ''
})

// 干部表单数据
const cadreForm = reactive({
  employeeName: '',
  position: '',
  area: '',
  assessmentMonth: '',
  redLineDeduction: 0,
  redLineExemption: '',
  violationRankingDeduction: 0,
  violationRankingExemption: '',
  skillRankingDeduction: 0,
  skillRankingExemption: '',
  totalScore: 0,
  assessmentResult: '',
  applicationMethod: '',
  assessmentResultRemark: '',
  applicationMethodRemark: ''
})

// 弹窗配置
const dialog = reactive({
  visible: false,
  title: '',
  type: 'non-cadre'
})

// 数据列表
const nonCadreList = ref([])
const cadreList = ref([])

// 选项配置
const employmentTypeOptions = [
  { label: '正式员工', value: 'formal' },
  { label: '合同工', value: 'contract' },
  { label: '临时工', value: 'temporary' },
  { label: '实习生', value: 'intern' }
]

const assessmentResultOptions = [
  { label: '优秀', value: 'excellent' },
  { label: '良好', value: 'good' },
  { label: '合格', value: 'qualified' },
  { label: '不合格', value: 'unqualified' }
]

const applicationMethodOptions = [
  { label: '直接应用', value: 'direct' },
  { label: '加权计算', value: 'weighted' },
  { label: '特殊处理', value: 'special' }
]

// 非干部表单验证规则
const nonCadreRules = {
  employeeName: [{ required: true, message: '员工姓名不能为空', trigger: 'blur' }],
  employmentType: [{ required: true, message: '用工形式不能为空', trigger: 'change' }],
  assessmentGroup: [{ required: true, message: '考核组不能为空', trigger: 'blur' }],
  assessmentMonth: [{ required: true, message: '考核月份不能为空', trigger: 'change' }]
}

// 干部表单验证规则
const cadreRules = {
  employeeName: [{ required: true, message: '员工姓名不能为空', trigger: 'blur' }],
  position: [{ required: true, message: '岗位不能为空', trigger: 'blur' }],
  assessmentMonth: [{ required: true, message: '考核月份不能为空', trigger: 'change' }],
  assessmentResult: [{ required: true, message: '考核结果不能为空', trigger: 'change' }]
}

// 获取数据列表
const getList = async () => {
  loading.value = true
  try {
    if (currentTab.value === 'non-cadre') {
      const res = await getNonCadreMonthlyAssessList(queryParams)
      nonCadreList.value = res.rows || []
      total.value = res.total || 0
    } else {
      // 干部数据API（待实现）
      cadreList.value = []
      total.value = 0
    }
  } catch (error) {
    console.error('获取数据失败:', error)
    ElMessage.error('获取数据失败')
  } finally {
    loading.value = false
  }
}

// 查询
const handleQuery = () => {
  queryParams.pageNum = 1
  getList()
}

// 重置查询
const resetQuery = () => {
  queryFormRef.value?.resetFields()
  queryParams.pageNum = 1
  getList()
}

// 新增
const handleAdd = () => {
  dialog.visible = true
  dialog.type = currentTab.value
  dialog.title = currentTab.value === 'non-cadre' ? '新增非干部月度考核' : '新增干部月度考核'
  
  // 重置表单数据
  if (currentTab.value === 'non-cadre') {
    Object.keys(nonCadreForm).forEach(key => {
      if (typeof nonCadreForm[key] === 'number') {
        nonCadreForm[key] = 0
      } else if (typeof nonCadreForm[key] === 'boolean') {
        nonCadreForm[key] = false
      } else {
        nonCadreForm[key] = ''
      }
    })
  } else {
    Object.keys(cadreForm).forEach(key => {
      if (typeof cadreForm[key] === 'number') {
        cadreForm[key] = 0
      } else if (typeof cadreForm[key] === 'boolean') {
        cadreForm[key] = false
      } else {
        cadreForm[key] = ''
      }
    })
  }
}

// 编辑
const handleEdit = (row, type) => {
  dialog.visible = true
  dialog.type = type
  dialog.title = type === 'non-cadre' ? '编辑非干部月度考核' : '编辑干部月度考核'
  
  // 填充表单数据
  if (type === 'non-cadre') {
    Object.keys(nonCadreForm).forEach(key => {
      if (row[key] !== undefined) {
        nonCadreForm[key] = row[key]
      }
    })
  } else {
    Object.keys(cadreForm).forEach(key => {
      if (row[key] !== undefined) {
        cadreForm[key] = row[key]
      }
    })
  }
}

// 删除
const handleDelete = async (row) => {
  try {
    await ElMessageBox.confirm('确认删除该考核记录吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    
    await deleteNonCadreMonthlyAssess(row.id)
    ElMessage.success('删除成功')
    getList()
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('删除失败')
    }
  }
}

// 提交表单
const submitForm = async () => {
  const valid = await formRef.value?.validate()
  if (!valid) return
  
  try {
    if (dialog.type === 'non-cadre') {
      if (dialog.title === '新增非干部月度考核') {
        await addNonCadreMonthlyAssess(nonCadreForm)
        ElMessage.success('新增成功')
      } else {
        await updateNonCadreMonthlyAssess(nonCadreForm)
        ElMessage.success('更新成功')
      }
    } else {
      // 干部数据API（待实现）
      ElMessage.success('操作成功')
    }
    
    dialog.visible = false
    getList()
  } catch (error) {
    ElMessage.error('操作失败')
  }
}

// 导出
const handleExport = async () => {
  try {
    ElMessage.success('导出功能开发中')
  } catch (error) {
    ElMessage.error('导出失败')
  }
}

// 生成本月考核表
const generateMonthlyAssessment = async () => {
  try {
    ElMessage.success('生成本月考核表功能开发中')
  } catch (error) {
    ElMessage.error('生成失败')
  }
}

// 监听Tab切换
const handleTabChange = () => {
  queryParams.pageNum = 1
  getList()
}

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

<style lang="less" scoped>
.app-container {
  padding: 20px;
}

.tab-container {
  margin-bottom: 20px;
  text-align: left;
}

.filter-container {
//  margin-bottom: 20px;
}

.search-form {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.operation-container {
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.left-buttons {
  display: flex;
  gap: 10px;
}

.right-buttons {
  display: flex;
  gap: 10px;
}

.form-container {
  max-height: 70vh;
  overflow-y: auto;
  padding-right: 10px;
}

.el-divider {
  margin: 20px 0;
}

:deep(.el-form-item__label) {
  font-weight: 500;
}
</style>
