<template>
  <div class="app-container">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-left">
        <h2>科技老师工资</h2>
        <p class="page-desc">管理科技老师工资数据，包括业绩、提成、补贴、扣款等信息</p>
      </div>
      <div class="header-right">
        <el-button 
          type="success" 
          icon="el-icon-download"
          @click="handleExport"
          :loading="exportLoading"
          :disabled="!list || list.length === 0"
        >
          导出
        </el-button>
        <el-button 
          type="primary" 
          icon="el-icon-cpu"
          @click="showCalculateDialog"
          :loading="calculateLoading"
        >
          计算工资
        </el-button>
        <el-button 
          icon="el-icon-refresh" 
          @click="getList"
          :loading="loading"
        >
          刷新
        </el-button>
      </div>
    </div>

    <!-- 搜索区域 -->
    <div class="search-container">
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="50px">
        <el-form-item label="月份" prop="month">
          <el-date-picker
            v-model="queryParams.month"
            type="month"
            placeholder="选择月份"
            format="yyyyMM"
            value-format="yyyyMM"
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item label="门店" prop="storeId">
          <el-select 
            v-model="queryParams.storeId" 
            placeholder="请选择门店"
            clearable
            filterable
            style="width: 200px"
          >
            <el-option
              v-for="store in storeList"
              :key="store.id"
              :label="store.fullName"
              :value="store.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 数据表格 -->
    <div class="table-container">
      <NCC-table 
        v-loading="loading" 
        :data="list" 
        border 
        stripe
        height="calc(100vh - 480px)"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
      >
        <!-- 基本信息 -->
        <el-table-column prop="StoreName" label="门店名称" width="140" align="center" fixed="left">
          <template slot-scope="scope">
            <div class="store-name">
              <i class="el-icon-shop store-icon"></i>
              <span>{{ scope.row.StoreName || '无' }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="EmployeeName" label="员工姓名" width="130" align="center" fixed="left">
          <template slot-scope="scope">
            <div class="employee-name">
              <i class="el-icon-user employee-icon"></i>
              <span>{{ scope.row.EmployeeName || '无' }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="Position" label="岗位" width="100" align="center" fixed="left">
          <template slot-scope="scope">
            <span>{{ scope.row.Position || '无' }}</span>
          </template>
        </el-table-column>

        <!-- 业绩信息 -->
        <el-table-column label="业绩信息" align="center">
          <el-table-column prop="OrderAchievement" label="开单业绩" width="120" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.OrderAchievement) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="ConsumeAchievement" label="消耗业绩" width="120" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.ConsumeAchievement) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="RefundAchievement" label="退卡业绩" width="120" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.RefundAchievement) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="TotalPerformance" label="总业绩" width="120" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.TotalPerformance) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="ProjectCount" label="项目数" width="100" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.ProjectCount || '0' }}</span>
            </template>
          </el-table-column>
        </el-table-column>

        <!-- 提成信息 -->
        <el-table-column label="提成信息" align="center">
          <el-table-column prop="PerformanceCommissionRate" label="业绩提成比例" width="140" align="center">
            <template slot-scope="scope">
              <span>{{ formatPercent(scope.row.PerformanceCommissionRate) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="PerformanceCommissionAmount" label="业绩提成金额" width="140" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.PerformanceCommissionAmount) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="ConsumeCommissionRate" label="消耗提成比例" width="140" align="center">
            <template slot-scope="scope">
              <span>{{ formatPercent(scope.row.ConsumeCommissionRate) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="ConsumeCommissionAmount" label="消耗提成金额" width="140" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.ConsumeCommissionAmount) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="HandworkFee" label="手工费" width="120" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.HandworkFee) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="TotalCommission" label="提成合计" width="120" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.TotalCommission) }}</span>
            </template>
          </el-table-column>
        </el-table-column>

        <!-- 工资信息 -->
        <el-table-column label="工资信息" align="center">
          <el-table-column prop="BaseSalaryLevel" label="底薪档位" width="120" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.BaseSalaryLevel || '0' }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="BaseSalary" label="底薪" width="120" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.BaseSalary) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="CalculatedGrossSalary" label="核算应发工资" width="140" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.CalculatedGrossSalary) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="GuaranteedSalary" label="保底工资" width="120" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.GuaranteedSalary) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="GuaranteedLeaveDeduction" label="保底请假扣款" width="140" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.GuaranteedLeaveDeduction) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="GuaranteedBaseSalary" label="保底底薪" width="120" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.GuaranteedBaseSalary) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="GuaranteedSupplement" label="保底补差" width="120" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.GuaranteedSupplement) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="FinalGrossSalary" label="最终应发工资" width="140" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.FinalGrossSalary) }}</span>
            </template>
          </el-table-column>
        </el-table-column>

        <!-- 考勤信息 -->
        <el-table-column label="考勤信息" align="center">
          <el-table-column prop="WorkingDays" label="在店天数" width="100" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.WorkingDays || '0' }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="LeaveDays" label="请假天数" width="100" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.LeaveDays || '0' }}</span>
            </template>
          </el-table-column>
        </el-table-column>

        <!-- 补贴信息 -->
        <el-table-column label="补贴信息" align="center">
          <el-table-column prop="TransportationAllowance" label="车补" width="100" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.TransportationAllowance) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="LessRest" label="少休费" width="100" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.LessRest) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="FullAttendance" label="全勤奖" width="100" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.FullAttendance) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="MonthlyTrainingSubsidy" label="当月培训补贴" width="140" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.MonthlyTrainingSubsidy) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="MonthlyTransportSubsidy" label="当月交通补贴" width="140" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.MonthlyTransportSubsidy) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="LastMonthTrainingSubsidy" label="上月培训补贴" width="140" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.LastMonthTrainingSubsidy) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="LastMonthTransportSubsidy" label="上月交通补贴" width="140" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.LastMonthTransportSubsidy) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="TotalSubsidy" label="补贴合计" width="120" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.TotalSubsidy) }}</span>
            </template>
          </el-table-column>
        </el-table-column>

        <!-- 扣款信息 -->
        <el-table-column label="扣款信息" align="center">
          <el-table-column prop="MissingCard" label="缺卡扣款" width="120" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.MissingCard) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="LateArrival" label="迟到扣款" width="120" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.LateArrival) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="LeaveDeduction" label="请假扣款" width="120" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.LeaveDeduction) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="SocialInsuranceDeduction" label="扣社保" width="120" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.SocialInsuranceDeduction) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="RewardDeduction" label="扣除奖励" width="120" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.RewardDeduction) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="AccommodationDeduction" label="扣住宿费" width="120" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.AccommodationDeduction) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="StudyPeriodDeduction" label="扣学习期费用" width="140" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.StudyPeriodDeduction) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="WorkClothesDeduction" label="扣工作服费用" width="140" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.WorkClothesDeduction) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="TotalDeduction" label="扣款合计" width="120" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.TotalDeduction) }}</span>
            </template>
          </el-table-column>
        </el-table-column>

        <!-- 其他信息 -->
        <el-table-column label="其他信息" align="center">
          <el-table-column prop="Bonus" label="发奖金" width="100" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.Bonus) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="ReturnPhoneDeposit" label="退手机押金" width="120" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.ReturnPhoneDeposit) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="ReturnAccommodationDeposit" label="退住宿押金" width="120" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.ReturnAccommodationDeposit) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="LastMonthSupplement" label="补发上月" width="120" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.LastMonthSupplement) }}</span>
            </template>
          </el-table-column>
        </el-table-column>

        <!-- 支付信息 -->
        <el-table-column label="支付信息" align="center">
          <el-table-column prop="ActualSalary" label="实发工资" width="120" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.ActualSalary) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="MonthlyPaymentStatus" label="当月是否发放" width="140" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.MonthlyPaymentStatus || '无' }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="PaidAmount" label="支付金额" width="120" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.PaidAmount) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="PendingAmount" label="待支付金额" width="120" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.PendingAmount) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="MonthlyTotalPayment" label="当月支付总额" width="140" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.MonthlyTotalPayment) }}</span>
            </template>
          </el-table-column>
        </el-table-column>

        <!-- 其他字段 -->
        <el-table-column prop="IsNewStore" label="是否新店" width="100" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.IsNewStore || '无' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="NewStoreProtectionStage" label="新店保护阶段" width="120" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.NewStoreProtectionStage || '0' }}</span>
          </template>
        </el-table-column>
        <!-- 操作列 -->
        <el-table-column label="操作" width="120" align="left" fixed="right">
          <template slot-scope="scope">
            <el-button 
              type="primary" 
              size="mini" 
              icon="el-icon-view"
              @click="handleViewDetail(scope.row)"
            >
              查看详情
            </el-button>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="IsLocked" label="锁定状态" width="100" align="center" fixed="right">
          <template slot-scope="scope">
            <el-tag :type="scope.row.IsLocked === 1 ? 'danger' : 'success'" size="mini">
              {{ scope.row.IsLocked === 1 ? '已锁定' : '未锁定' }}
            </el-tag>
          </template>
        </el-table-column> -->
        <!-- <el-table-column prop="IsTerminated" label="离职状态" width="100" align="center" fixed="right">
          <template slot-scope="scope">
            <el-tag :type="scope.row.IsTerminated === 1 ? 'warning' : 'success'" size="mini">
              {{ scope.row.IsTerminated === 1 ? '已离职' : '在职' }}
            </el-tag>
          </template>
        </el-table-column> -->
      </NCC-table>
    </div>

    <!-- 计算工资弹窗 -->
    <el-dialog
      title="计算科技老师工资"
      :visible="calculateDialogVisible"
      width="500px"
      :close-on-click-modal="false"
      @close="handleCalculateDialogClose"
    >
      <el-form label-width="100px" label-position="right">
        <el-form-item label="选择年份" required>
          <el-date-picker
            v-model="calculateYear"
            type="year"
            placeholder="请选择年份"
            format="yyyy"
            value-format="yyyy"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="选择月份" required>
          <el-date-picker
            v-model="calculateMonth"
            type="month"
            placeholder="请选择月份"
            format="yyyyMM"
            value-format="yyyyMM"
            style="width: 100%"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCalculateDialogClose">取消</el-button>
        <el-button 
          type="primary" 
          @click="handleCalculateConfirm"
          :loading="calculateLoading"
        >
          确定
        </el-button>
      </div>
    </el-dialog>

    <!-- 详情弹窗 -->
    <tech-teacher-detail-dialog
      :visible.sync="detailDialogVisible"
      :detail-data="currentDetailData"
      @close="handleDetailDialogClose"
    />
  </div>
</template>

<script>
import { getTechTeacherSalaryList, calculateTechTeacherSalary } from '@/api/extend/techTeacherSalary'
import { getStoreSelector } from '@/api/extend/store'
import TechTeacherDetailDialog from './tech-teacher-detail-dialog.vue'

export default {
  name: 'TechTeacherSalary',
  components: {
    TechTeacherDetailDialog
  },
  data() {
    // 获取当前年月
    const getCurrentYear = () => {
      return new Date().getFullYear().toString()
    }
    const getCurrentMonth = () => {
      const now = new Date()
      const year = now.getFullYear()
      const month = String(now.getMonth() + 1).padStart(2, '0')
      return `${year}${month}`
    }

    return {
      loading: false,
      calculateLoading: false,
      exportLoading: false,
      list: [],
      total: 0,
      storeList: [],
      calculateDialogVisible: false,
      calculateYear: getCurrentYear(),
      calculateMonth: getCurrentMonth(),
      detailDialogVisible: false,
      currentDetailData: null,
      queryParams: {
        currentPage: 1,
        pageSize: 300, // 写死为300
        year: getCurrentYear(),
        month: getCurrentMonth(), // yyyyMM 格式
        storeId: ''
      }
    }
  },
  created() {
    this.getStoreList()
    this.getList()
  },
  methods: {
    // 获取门店列表
    async getStoreList() {
      try {
        const response = await getStoreSelector()
        this.storeList = response.data.list || []
      } catch (error) {
        console.error('获取门店列表失败:', error)
        this.storeList = []
      }
    },

    // 获取列表数据
    async getList() {
      this.loading = true
      try {
        // 处理年月：从month选择器中提取年份和月份
        let year = ''
        let month = ''
        
        if (this.queryParams.month && this.queryParams.month.length === 6) {
          // yyyyMM 格式，如 202512
          year = this.queryParams.month.substring(0, 4)
          month = this.queryParams.month.substring(4, 6)
        } else if (this.queryParams.year && this.queryParams.month) {
          // 分别选择年份和月份
          year = this.queryParams.year
          month = this.queryParams.month.length === 6 
            ? this.queryParams.month.substring(4, 6) 
            : this.queryParams.month
        } else {
          // 使用默认值（当前年月）
          const now = new Date()
          year = now.getFullYear().toString()
          month = String(now.getMonth() + 1).padStart(2, '0')
        }

        const params = {
          currentPage: this.queryParams.currentPage,
          pageSize: this.queryParams.pageSize,
          Year: year,
          Month: month,
          StoreId: this.queryParams.storeId || ''
        }

        const response = await getTechTeacherSalaryList(params)

        if (response.code === 200) {
          this.list = response.data.list || []
          this.total = (response.data.pagination && response.data.pagination.total) || 0
        } else {
          this.$message.error(response.msg || '获取数据失败')
        }
      } catch (error) {
        console.error('获取科技老师工资列表失败:', error)
        this.$message.error('获取数据失败')
      } finally {
        this.loading = false
      }
    },

    // 搜索
    handleQuery() {
      this.queryParams.currentPage = 1
      this.getList()
    },

    // 重置搜索
    resetQuery() {
      const getCurrentYear = () => {
        return new Date().getFullYear().toString()
      }
      const getCurrentMonth = () => {
        const now = new Date()
        const year = now.getFullYear()
        const month = String(now.getMonth() + 1).padStart(2, '0')
        return `${year}${month}`
      }

      this.queryParams = {
        currentPage: 1,
        pageSize: 300, // 写死为300
        year: getCurrentYear(),
        month: getCurrentMonth(),
        storeId: ''
      }
      this.getList()
    },

    // 显示计算工资弹窗
    showCalculateDialog() {
      const getCurrentYear = () => {
        return new Date().getFullYear().toString()
      }
      const getCurrentMonth = () => {
        const now = new Date()
        const year = now.getFullYear()
        const month = String(now.getMonth() + 1).padStart(2, '0')
        return `${year}${month}`
      }
      this.calculateYear = getCurrentYear()
      this.calculateMonth = getCurrentMonth()
      this.calculateDialogVisible = true
    },

    // 关闭计算工资弹窗
    handleCalculateDialogClose() {
      this.calculateDialogVisible = false
    },

    // 确认计算工资
    async handleCalculateConfirm() {
      if (!this.calculateYear || !this.calculateMonth) {
        this.$message.warning('请选择年份和月份')
        return
      }

      // 从calculateMonth中提取月份（yyyyMM格式，如202512）
      const year = this.calculateYear
      const month = this.calculateMonth.length === 6 
        ? this.calculateMonth.substring(4, 6) 
        : this.calculateMonth

      this.calculateLoading = true
      try {
        const response = await calculateTechTeacherSalary(year, month)
        if (response.code === 200) {
          this.$message.success('计算工资成功')
          this.calculateDialogVisible = false
          this.getList()
        } else {
          this.$message.error(response.msg || '计算工资失败')
        }
      } catch (error) {
        console.error('计算工资失败:', error)
        this.$message.error(error.message || '计算工资失败')
      } finally {
        this.calculateLoading = false
      }
    },

    // 导出功能
    async handleExport() {
      if (!this.list || this.list.length === 0) {
        this.$message.warning('暂无数据可导出')
        return
      }

      this.exportLoading = true
      try {
        // 动态导入 xlsx 库
        const XLSX = await import('xlsx')

        // 构建表头
        const headers = [
          '门店名称',
          '员工姓名',
          '岗位',
          '开单业绩',
          '消耗业绩',
          '退卡业绩',
          '总业绩',
          '项目数',
          '底薪档位',
          '底薪',
          '业绩提成比例',
          '业绩提成金额',
          '消耗提成比例',
          '消耗提成金额',
          '手工费',
          '提成合计',
          '在店天数',
          '请假天数',
          '车补',
          '少休费',
          '全勤奖',
          '当月培训补贴',
          '当月交通补贴',
          '上月培训补贴',
          '上月交通补贴',
          '补贴合计',
          '核算应发工资',
          '保底工资',
          '保底请假扣款',
          '保底底薪',
          '保底补差',
          '最终应发工资',
          '缺卡扣款',
          '迟到扣款',
          '请假扣款',
          '扣社保',
          '扣除奖励',
          '扣住宿费',
          '扣学习期费用',
          '扣工作服费用',
          '扣款合计',
          '发奖金',
          '退手机押金',
          '退住宿押金',
          '补发上月',
          '实发工资',
          '当月是否发放',
          '支付金额',
          '待支付金额',
          '当月支付总额',
          '是否新店',
          '新店保护阶段',
          '锁定状态',
          '离职状态'
        ]

        // 构建数据行
        const dataRows = this.list.map(item => [
          item.StoreName || '无',
          item.EmployeeName || '无',
          item.Position || '无',
          this.formatMoney(item.OrderAchievement),
          this.formatMoney(item.ConsumeAchievement),
          this.formatMoney(item.RefundAchievement),
          this.formatMoney(item.TotalPerformance),
          item.ProjectCount || '0',
          item.BaseSalaryLevel || '0',
          this.formatMoney(item.BaseSalary),
          this.formatPercent(item.PerformanceCommissionRate),
          this.formatMoney(item.PerformanceCommissionAmount),
          this.formatPercent(item.ConsumeCommissionRate),
          this.formatMoney(item.ConsumeCommissionAmount),
          this.formatMoney(item.HandworkFee),
          this.formatMoney(item.TotalCommission),
          item.WorkingDays || '0',
          item.LeaveDays || '0',
          this.formatMoney(item.TransportationAllowance),
          this.formatMoney(item.LessRest),
          this.formatMoney(item.FullAttendance),
          this.formatMoney(item.MonthlyTrainingSubsidy),
          this.formatMoney(item.MonthlyTransportSubsidy),
          this.formatMoney(item.LastMonthTrainingSubsidy),
          this.formatMoney(item.LastMonthTransportSubsidy),
          this.formatMoney(item.TotalSubsidy),
          this.formatMoney(item.CalculatedGrossSalary),
          this.formatMoney(item.GuaranteedSalary),
          this.formatMoney(item.GuaranteedLeaveDeduction),
          this.formatMoney(item.GuaranteedBaseSalary),
          this.formatMoney(item.GuaranteedSupplement),
          this.formatMoney(item.FinalGrossSalary),
          this.formatMoney(item.MissingCard),
          this.formatMoney(item.LateArrival),
          this.formatMoney(item.LeaveDeduction),
          this.formatMoney(item.SocialInsuranceDeduction),
          this.formatMoney(item.RewardDeduction),
          this.formatMoney(item.AccommodationDeduction),
          this.formatMoney(item.StudyPeriodDeduction),
          this.formatMoney(item.WorkClothesDeduction),
          this.formatMoney(item.TotalDeduction),
          this.formatMoney(item.Bonus),
          this.formatMoney(item.ReturnPhoneDeposit),
          this.formatMoney(item.ReturnAccommodationDeposit),
          this.formatMoney(item.LastMonthSupplement),
          this.formatMoney(item.ActualSalary),
          item.MonthlyPaymentStatus || '无',
          this.formatMoney(item.PaidAmount),
          this.formatMoney(item.PendingAmount),
          this.formatMoney(item.MonthlyTotalPayment),
          item.IsNewStore || '无',
          item.NewStoreProtectionStage || '0',
          item.IsLocked === 1 ? '已锁定' : '未锁定',
          item.IsTerminated === 1 ? '已离职' : '在职'
        ])

        // 合并表头和数据
        const excelData = [headers, ...dataRows]

        // 创建工作表
        const ws = XLSX.utils.aoa_to_sheet(excelData)

        // 设置列宽
        const colWidths = headers.map(() => ({ wch: 15 }))
        ws['!cols'] = colWidths

        // 创建工作簿
        const wb = XLSX.utils.book_new()
        XLSX.utils.book_append_sheet(wb, ws, '科技老师工资')

        // 生成文件名
        const now = new Date()
        const timestamp = now.getFullYear() + 
          String(now.getMonth() + 1).padStart(2, '0') + 
          String(now.getDate()).padStart(2, '0') + 
          String(now.getHours()).padStart(2, '0') + 
          String(now.getMinutes()).padStart(2, '0') + 
          String(now.getSeconds()).padStart(2, '0')
        const fileName = `科技老师工资_${timestamp}.xlsx`

        // 导出文件
        XLSX.writeFile(wb, fileName)

        this.$message.success('导出成功')
      } catch (error) {
        console.error('导出失败:', error)
        this.$message.error('导出失败，请重试')
      } finally {
        this.exportLoading = false
      }
    },

    // 格式化金额
    formatMoney(value) {
      if (value === null || value === undefined || value === '') {
        return '0.00'
      }
      return Number(value).toFixed(2)
    },

    // 格式化百分比
    formatPercent(value) {
      if (value === null || value === undefined || value === '') {
        return '0.00%'
      }
      return (Number(value) * 1).toFixed(2) + '%'
    },

    // 查看详情
    handleViewDetail(row) {
      this.currentDetailData = row
      this.detailDialogVisible = true
    },

    // 关闭详情弹窗
    handleDetailDialogClose() {
      this.detailDialogVisible = false
      this.currentDetailData = null
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  padding: 20px;
  background: #f5f5f5;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 20px;
  padding: 20px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);

  .header-left {
    h2 {
      margin: 0 0 8px 0;
      color: #303133;
      font-size: 20px;
      font-weight: 600;
    }

    .page-desc {
      margin: 0;
      color: #909399;
      font-size: 14px;
    }
  }

  .header-right {
    display: flex;
    gap: 12px;
  }
}

.search-container {
  margin-bottom: 20px;
  padding: 20px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.table-container {
  margin-bottom: 20px;
  padding: 20px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.store-name,
.employee-name {
  display: flex;
  align-items: center;
  gap: 6px;

  .store-icon,
  .employee-icon {
    color: #409EFF;
    font-size: 16px;
  }
}

// 表格样式优化
::v-deep .el-table {
  .el-table__header-wrapper {
    th {
      background-color: #f5f7fa;
      color: #606266;
      font-weight: 600;
    }
  }

  .el-table__body-wrapper {
    .el-table__row {
      &:hover {
        background-color: #f5f7fa;
      }
    }
  }
}
</style>

