<!--
 * @Author: qinzhenpen qzp1807@126.com
 * @Date: 2024-10-31 14:59:14
 * @LastEditors: qinzhenpen qzp1807@126.com
 * @LastEditTime: 2025-05-16 18:25:56
 * @FilePath: \corporate-finance\src\views\teacher\working-capital.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="app-container score" v-loading="loading">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="用户名：">
        <el-input v-model="scoreParams.name" placeholder="" clearable>
          <template #suffix>
            <el-button type="primary" link color="#51bcb5" @click="getList">
              <!-- <img src="@/assets/images/tea搜索.png" alt="" /> -->
            </el-button>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="学号：">
        <el-input v-model="scoreParams.studentId" placeholder="" clearable>
          <template #suffix>
            <el-button type="primary" link color="#51bcb5" @click="getList">
              <!-- <img src="@/assets/images/tea搜索.png" alt="" /> -->
            </el-button>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="班级：">
        <el-select v-model="scoreParams.classId" placeholder="" clearable @change="getList">
          <el-option v-for="school in teachingClassList" :key="school.schoolClassId" :label="school.className" :value="school.schoolClassId" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="exportExcel">导出</el-button>
        <el-button type="success" @click="scoreweight">权重设置</el-button>
        <el-button type="success" @click="updateScore">成绩更新</el-button>
      </el-form-item>
    </el-form>
    <div class="table-info1">
      <div class="table-hide">
        <el-table :data="workingCapitalList" stripe border style="width: 100%">
          <el-table-column prop="stuRank" label="排名" align="center" width="120" />
          <el-table-column prop="name" label="用户名" align="center" />
          <el-table-column prop="username" label="学号" align="center" />
          <el-table-column prop="className" label="班级" align="center" />
          <el-table-column prop="score" label="实训成绩" align="center" />
          <el-table-column label="成绩详情" align="center">
            <template #default="{ row }">
              <el-button link type="success" @click="getClassScoreDetail(row)">查看详情</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <pagination v-show="workingCapitalTotal > 0" :total="workingCapitalTotal" v-model:page="scoreParams.index" v-model:limit="scoreParams.size" @pagination="getList" />
    <el-dialog v-model="WeightStart" title="权重设置" width="500" class="F_dialog teacher-dialog">
      <el-form ref="ruleFormRef" :model="scoreWeightObj" class="demo-form" :rules="rules">
        <el-form-item label="市场需求挖掘:" prop="marketResearchWeight" label-width="150">
          <el-input v-model.number="scoreWeightObj.marketResearchWeight" placeholder="" clearable>
            <template #suffix>%</template>
          </el-input>
        </el-form-item>
        <el-form-item label="产品规划:" prop="productPlanningWeight" label-width="150">
          <el-input v-model.number="scoreWeightObj.productPlanningWeight" placeholder="" clearable>
            <template #suffix>%</template>
          </el-input>
        </el-form-item>
        <el-form-item label="产品投放测试:" prop="productLaunchTestWeight" label-width="150">
          <el-input v-model.number="scoreWeightObj.productLaunchTestWeight" placeholder="" clearable>
            <template #suffix>%</template>
          </el-input>
        </el-form-item>
        <el-form-item label="供应渠道管理:" prop="supplyChannelWeight" label-width="150">
          <el-input v-model.number="scoreWeightObj.supplyChannelWeight" placeholder="" clearable>
            <template #suffix>%</template>
          </el-input>
        </el-form-item>
        <el-form-item label="产品评估与考核:" prop="productEvaluationWeight" label-width="150">
          <el-input v-model.number="scoreWeightObj.productEvaluationWeight" placeholder="" clearable>
            <template #suffix>%</template>
          </el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer" style="text-align: center">
          <el-button type="primary" @click="workingCapitalWeightHandle">确定</el-button>
          <el-button @click="WeightStart = false">取消</el-button>
        </div>
      </template>
    </el-dialog>
    <el-dialog v-model="scoreDetailsStart" title="成绩详情" width="50%" class="F_dialog teacher-dialog">
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        height="400"
        :header-cell-style="{
          color: '#fff',
          fontSize: '14px',
          backgroundColor: '#02a7f0!important',
        }"
      >
        <el-table-column prop="module" label="实训任务" align="center" />
        <el-table-column prop="score" label="任务状态" align="center">
          <template #default="{ row }">
            <span>已完成</span>
          </template>
        </el-table-column>
        <el-table-column prop="score" label="任务成绩" align="center" />
      </el-table>
    </el-dialog>
  </div>
</template>
<script setup>
import * as indexApi from "@/api/teacher";
const { proxy } = getCurrentInstance();
import useUserStore from "@/store/modules/user";
const userStore = useUserStore();
const workingCapitalList = ref([]);
const loading = ref(false);
const scoreParams = ref({
  classId: "",
  index: 1,
  size: 10,
  schoolId: userStore.userInfo.schoolId,
});
const scoreDetailsStart = ref(false);
const tableData = ref([]);
const WeightStart = ref(false);
// 权重
const workingCapitalTotal = ref(0);
const scoreWeightObj = ref({});
const rules = {
  marketResearchWeight: [
    { required: true, message: "请输入市场需求挖掘权重", trigger: "blur" },
    { type: "number", message: "权重必须为数字值" },
  ],
  productEvaluationWeight: [
    { required: true, message: "请输入产品评估与考核权重", trigger: "blur" },
    { type: "number", message: "权重必须为数字值" },
  ],
  productLaunchTestWeight: [
    { required: true, message: "请输入产品投放测试权重", trigger: "blur" },
    { type: "number", message: "权重必须为数字值" },
  ],
  productPlanningWeight: [
    { required: true, message: "请输入产品规划权重", trigger: "blur" },
    { type: "number", message: "权重必须为数字值" },
  ],
  supplyChannelWeight: [
    { required: true, message: "请输入供应渠道管理权重", trigger: "blur" },
    { type: "number", message: "权重必须为数字值" },
  ],
};
const buildScoreQuery = () => {
  const { studentId, ...query } = scoreParams.value;
  return {
    ...query,
    userName: studentId,
  };
};
const getList = () => {
  loading.value = true;
  indexApi.getScoreList(buildScoreQuery()).then((res) => {
    workingCapitalList.value = res.data.list;
    workingCapitalTotal.value = res.data.total;
    loading.value = false;
  });
};
const scoreweight = () => {
  WeightStart.value = true;
  indexApi.getWeightList({ schoolId: userStore.userInfo.schoolId }).then((res) => {
    scoreWeightObj.value = res.data;
    for (const key in scoreWeightObj.value) {
      if (key !== "schoolId") {
        scoreWeightObj.value[key] = scoreWeightObj.value[key] * 100;
      }
    }
  });
};
const workingCapitalWeightHandle = () => {
  proxy.$refs.ruleFormRef.validate((valid) => {
    if (valid) {
      let params = {};
      let sum = 0;
      for (const key in scoreWeightObj.value) {
        if (key !== "schoolId") {
          params[key] = scoreWeightObj.value[key] / 100;
          sum += scoreWeightObj.value[key] / 100;
        }
      }
      if (sum !== 1) {
        proxy.$message.error("权重合计为100%");
        return;
      }
      params.schoolId = userStore.userInfo.schoolId;
      indexApi.updateWeight(params).then((res) => {
        proxy.$message.success("权重设置成功");
        WeightStart.value = false;
      });
    }
  });
};
const classList = ref([]);
const teachingClassList = computed(() => classList.value.filter((item) => item.classType === "TEACHING"));
const getClassList = () => {
  indexApi.getClassListBySchoolId({ schoolId: userStore.userInfo.schoolId }).then((res) => {
    classList.value = res.data;
    if (scoreParams.value.classId && !teachingClassList.value.some((item) => item.schoolClassId === scoreParams.value.classId)) {
      scoreParams.value.classId = "";
    }
  });
};
const exportExcel = () => {
  const { index, size, ...query } = buildScoreQuery();
  indexApi.exportScoreList(query).then((res) => {
    //   打开怎么是空的
    const blob = new Blob([res], { type: "application/vnd.ms-excel" });
    const url = URL.createObjectURL(blob);
    const link = document.createElement("a");
    link.href = url;
    link.download = "成绩表.xlsx";
    link.click();
    URL.revokeObjectURL(url);
  });
};
const getClassScoreDetail = (row) => {
  indexApi.getScoreDetail({ userId: row.userId, classId: scoreParams.value.classId }).then((res) => {
    tableData.value = res.data;
    scoreDetailsStart.value = true;
  });
};
// 更新成绩
const updateScore = () => {
  loading.value = true;

  const params = { schoolId: userStore.userInfo.schoolId };
  if (scoreParams.value.classId) {
    params.teachingClassId = scoreParams.value.classId;
  }
  indexApi.doRankOne(params).then((res) => {
    proxy.$message.success("成绩更新成功");
    loading.value = false;
    getList();
  });
};
// 拿到当前路由信息
onMounted(() => {
  getList();
  getClassList();
});
</script>
<style lang="scss" scoped>
.score {
  background: #ffffff;
  height: calc(100vh - 70px);
}
</style>
