<template>
  <el-dialog v-model="visible" width="100%" v-loading="baseLoading" top="0" :close-on-click-modal="false" destroy-on-close>
    <!--基本信息-->
    <el-card shadow="hover">
      <el-row :span="24">
        <el-col>
          <el-table
            :data="salaryData.baseInfo"
            style="width: 100%">
            <el-table-column
              prop="realName"
              label="姓名"
              width="80">
            </el-table-column>
            <el-table-column
              prop="idCard"
              label="身份证号"
              width="200">
            </el-table-column>

            <el-table-column
              label="年份"
              width="300">
              <template #default>
                <el-date-picker
                  v-model="nowUser.nf"
                  type="year"
                  format="YYYY"
                  value-format="YYYY"
                  style="width: 200px"
                  placeholder="选择年">
                </el-date-picker>
              </template>
            </el-table-column>

            <el-table-column
              label="月份"
              width="300">
              <template #default>
                <el-date-picker
                  v-model="nowUser.yff"
                  type="month"
                  format="M"
                  value-format="M"
                  style="width: 200px"
                  placeholder="选择月">
                </el-date-picker>
              </template>
            </el-table-column>
            <el-table-column
              label="操作"
              width="200">
              <template #default>
                <el-button @click="searchUserInfo" type="primary" size="small">搜索</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
    </el-card>

    <el-card shadow="hover">
      <!--应发部分-->
      <el-row v-if="showAllContent">
        <el-col :span="24">
          <el-table
            :data="salaryData.baseInfo"
            size="small"
            border
          >
            <el-table-column label="应发部分">
              <el-table-column prop="postSalary" label="岗位工资" min-width="60" align="center"></el-table-column>
              <el-table-column prop="payWage" label="薪级工资" min-width="60" align="center"></el-table-column>
              <el-table-column label="见习期工资" prop="studentPay" min-width="60" align="center"></el-table-column>
              <el-table-column prop="liveAllowance" label="生活补贴" min-width="60" align="center"></el-table-column>
              <el-table-column prop="postAllowance" label="岗位津贴" min-width="60" align="center"></el-table-column>
              <el-table-column label="住房(租金)补贴" prop="houseSubsidies" min-width="60" align="center"></el-table-column>
              <el-table-column label="新职工住房补贴" prop="newHouseSubsidies" min-width="60" align="center"></el-table-column>
              <el-table-column label="回民补贴" prop="huiSubsidies" min-width="60" align="center"></el-table-column>
              <el-table-column label="养老保险补贴" prop="oldSubsidies" min-width="60" align="center"></el-table-column>
              <el-table-column label="教龄津贴" prop="ageAllowance" min-width="60" align="center"></el-table-column>
              <el-table-column label="特教补贴" prop="specialSubsidies" min-width="60" align="center"></el-table-column>
              <el-table-column label="特级教师津贴" prop="teacherAllowance" min-width="60" align="center"></el-table-column>
              <el-table-column label="特岗津贴(一)" prop="sPostAllowance1" min-width="60" align="center"></el-table-column>
              <el-table-column label="特岗津贴(二)" prop="sPostAllowance2" min-width="60" align="center"></el-table-column>
              <el-table-column label="其他" prop="other" min-width="60" align="center"></el-table-column>
              <el-table-column label="奖励性绩效工资" prop="meritPay" min-width="60" align="center"></el-table-column>
              <el-table-column label="乡镇工作补贴" prop="villageSubsidies" min-width="60" align="center"></el-table-column>
              <el-table-column label="临时性补贴" prop="temporarySubsidies" min-width="60" align="center"></el-table-column>
              <el-table-column label="上下班交通补贴" prop="trafficSubsidies" min-width="60" align="center"></el-table-column>
              <el-table-column label="保留津贴" prop="keepAllowance" min-width="60" align="center"></el-table-column>
              <el-table-column label="补发工资" prop="retroactivePay" min-width="60" align="center"></el-table-column>
              <el-table-column label="应休未休假补贴" prop="vacationMoney" min-width="60" align="center"></el-table-column>
              <el-table-column label="应发工资" prop="shouldPay" min-width="60" align="center"></el-table-column>
              <el-table-column label="基础专项绩效" prop="baseSalary" min-width="60" align="center"></el-table-column>
              <el-table-column label="基础工资应税收入" prop="shouldTaxMoney" min-width="60" align="center"></el-table-column>
            </el-table-column>
          </el-table>
          <el-tag type="danger">基础工资应税收入= 岗位工资 +薪级工资+见习期工资+生活补贴+岗位津贴+教龄津贴+特教补贴+特级教师津贴+特岗津贴（1）+特岗津贴（2）+奖励绩效性工资+乡镇工作补贴+临时补贴+保留津贴+应休未休假-个人补缴-其他扣款-医疗救助金 </el-tag>
        </el-col>
      </el-row>
      <!--应扣部分-->
      <el-row v-if="showAllContent">
        <el-col :span="24">
          <el-table
            size="small"
            border
            :data="salaryData.baseInfo"
            style="width: 100%">
            <el-table-column label="应扣部分">
              <el-table-column label="住房公积金" width="90" prop="houseFund"></el-table-column>
              <el-table-column label="医疗保险金" width="90" prop="medicalInsurance"></el-table-column>
              <el-table-column label="失业保险金" width="90" prop="unemployInsurance"></el-table-column>
              <el-table-column label="养老保险金" width="90" prop="endowInsurance"></el-table-column>
              <el-table-column label="工会费" width="60" prop="unionFee"></el-table-column>
              <el-table-column label="儿童统筹" width="90" prop="childrenWhole"></el-table-column>
              <el-table-column label="个人所得税" width="90" prop="personalTax"></el-table-column>
              <el-table-column label="其他扣款" width="90" prop="otherDeduction"></el-table-column>
              <el-table-column label="病事假扣款" width="90" prop="sickDeduction"></el-table-column>
              <el-table-column label="医疗救助基金" width="90" prop="medicalFund"></el-table-column>
              <el-table-column label="工伤保险" width="90" prop="inductrialInjury"></el-table-column>
              <el-table-column label="个人补缴" width="90" prop="personalPay"></el-table-column>
              <el-table-column label="应扣合计" width="90" prop="withhold"></el-table-column>
            </el-table-column>
          </el-table>
          <el-tag>个人所得税 = 个税计算数据中的《 累计应补(退)税额 》</el-tag>
        </el-col>
      </el-row>

      <!--劳务费-->
      <el-row>
        <el-col :span="24">
          <el-table size="small" border :data="allProjectData" show-summary>
            <el-table-column label="造单收入清单">
              <el-table-column label="造单部门" prop="deptName"></el-table-column>
              <el-table-column label="造单人" prop="createName"></el-table-column>
              <el-table-column label="项目编号" prop="projectNo"></el-table-column>
              <el-table-column label="项目名" prop="title"></el-table-column>
              <el-table-column label="金额" prop="realMoney"></el-table-column>
              <el-table-column label="付讫时间" prop="payTime"></el-table-column>
              <el-table-column label="免税或暂不交税" prop="freeTax">
                <template #default="scope">
                  <span v-if="scope.row.freeTax=='1'">√</span>
                </template>
              </el-table-column>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>

      <!--专项扣除-->
      <el-row>
        <el-col :span="24">
          <el-table
            size="small"
            border
            :data="salaryExtendData"
            style="width: 100%">
            <el-table-column label="个税计算数据">
              <el-table-column prop="currentIncome" label="应税收入" min-width="60" align="center"></el-table-column>
              <el-table-column prop="totalIncome" label="累计收入额" min-width="60" align="center"></el-table-column>
              <el-table-column prop="totalDeduction" label="累计减除费用" min-width="60" align="center"></el-table-column>
              <el-table-column prop="totalSpecialDecution" label="累计专项扣除" min-width="60" align="center"></el-table-column>
              <el-table-column label="累计专项扣除附加" align="center">
                <el-table-column prop="totalChildEdu" label="累计子女教育" min-width="60" align="center"></el-table-column>
                <el-table-column prop="totalConEdu"   label="累计继续教育" min-width="60" align="center"> </el-table-column>
                <el-table-column prop="totalHouseInterest" label="累计住房贷款利息" min-width="60" align="center"></el-table-column>
                <el-table-column prop="totalHouse" label="累计住房租金" min-width="60" align="center"></el-table-column>
                <el-table-column prop="totalSupportOld"  label="累计赡养老人" min-width="60" align="center"></el-table-column>
                <el-table-column prop="totalBabyMoney"  label="累计婴幼儿照护费用" min-width="60" align="center"></el-table-column>
              </el-table-column>
              <el-table-column prop="totalTaxMoney"  label="累计应纳税所得额" min-width="60" align="center"></el-table-column>
              <el-table-column prop="taxRate"  label="税率" min-width="60" align="center"></el-table-column>
              <el-table-column prop="quickDecution"  label="速算扣除数" min-width="60" align="center"></el-table-column>
              <el-table-column prop="totalRealTaxPay"  label="累计应扣缴税额" min-width="60" align="center"></el-table-column>
              <el-table-column prop="totalPrePayTax"  label="累计已预缴税额" min-width="60" align="center"></el-table-column>
              <el-table-column prop="totalRetrieveTax"  label="累计应补(退)税额" min-width="60" align="center"></el-table-column>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>

      <!--实发合计-->
      <el-row v-if="showAllContent">
        <el-col :span="24">
          <el-table
            size="small"
            border
            :data="[staticsData]"
            empty-text=" ">
            <el-table-column label="小计">
              <el-table-column label="应发工资" prop="shouldPay"></el-table-column>
              <el-table-column label="应扣合计" prop="shouldDedu"></el-table-column>
              <el-table-column label="实发工资" prop="realWage"></el-table-column>
              <el-table-column label="造单收入" prop="orderMoney"></el-table-column>
            </el-table-column>
          </el-table>
          <el-tag type="primary">实发工资= 应发工资-应扣合计 </el-tag>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, nextTick } from 'vue'
import { queryUserInfo, queryExtendSalaryInfo, checkAuth } from '/@/api/professional/salaries/teachersalary'

// 对话框显示状态
const visible = ref(false)

// 数据
const salaryData = reactive({
  baseInfo: [] as any[]
})

const nowUser = ref<any>({})
const baseLoading = ref(false)
const salaryExtendData = ref<any[]>([])
const allProjectData = ref<any[]>([])
const staticsData = reactive({
  shouldPay: 0,
  shouldDedu: 0,
  realWage: 0,
  orderMoney: 0,
  personTax: 0
})
const showAllContent = ref(false)

// 检查权限
const checkAuthMethod = async () => {
  try {
    const res = await checkAuth()
    showAllContent.value = res.data.data
  } catch (error) {
    // 检查权限失败
  }
}

// 构建用户信息
const makeUserInfo = (data: any) => {
  const row = JSON.parse(JSON.stringify(data))
  salaryData.baseInfo = []
  row.realName = (row.realName == "" || row.realName == null ? row.userName : row.realName)
  salaryData.baseInfo.push(row)

  staticsData.shouldPay = row.shouldPay || 0
  staticsData.shouldDedu = row.withhold || 0
  staticsData.realWage = row.realWage || 0
  staticsData.personTax = (row.personalTax as number) || 0
}

// 查询扩展薪资信息
const queryExtendSalaryInfoMethod = async (row: any) => {
  salaryExtendData.value = []
  const params = { nf: row.nf, yf: row.yf, teacherNo: row.teacherNo }
  try {
    const res = await queryExtendSalaryInfo(params)
    salaryExtendData.value.push(res.data.data.salaryTax)
    allProjectData.value = res.data.data.allProject
    staticsData.orderMoney = res.data.data.totalMoney
  } catch (error) {
    // 查询失败
  }
}

// 搜索用户信息
const searchUserInfo = async () => {
  baseLoading.value = true
  const data = { idCard: nowUser.value.idCard, nf: nowUser.value.nf, yf: nowUser.value.yff }
  try {
    const response = await queryUserInfo(data)
    if (response.data.data == null) {
      salaryData.baseInfo = []
      const obj: any = {}
      obj.realName = nowUser.value.realName
      obj.idCard = nowUser.value.idCard
      salaryData.baseInfo.push(obj)

      salaryExtendData.value = []
      Object.assign(staticsData, {
        shouldPay: 0,
        shouldDedu: 0,
        realWage: 0,
        orderMoney: 0,
        personTax: 0
      })
    } else {
      const resData = response.data.data
      resData.realName = nowUser.value.realName
      makeUserInfo(resData)
      await queryExtendSalaryInfoMethod(resData)
    }
  } catch (error) {
    // 查询失败
  } finally {
    baseLoading.value = false
  }
}

// 初始化
const init = (row: any) => {
  visible.value = true
  nextTick(() => {
    checkAuthMethod()
    nowUser.value = JSON.parse(JSON.stringify(row))
    nowUser.value.yff = row.yf
    makeUserInfo(row)
    queryExtendSalaryInfoMethod(row)
  })
}

// 暴露方法
defineExpose({
  init
})
</script>

<style scoped>
</style>
