<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          @click="handleSearchList()"
          type="primary"
          size="small"
        >
          查询结果
        </el-button>
        <el-button
          style="float: right; margin-right: 15px"
          @click="handleResetSearch('listQuery')"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="listQuery"
          :data="haveGpaStu"
          ref="listQuery"
          size="small"
          label-width="140px"
          @keyup.enter.native="handleSearchList()"
        >
          <el-form-item label="上课年份：" prop="yearTime">
            <el-select clearable v-model="listQuery.yearTime">
              <el-option
                v-for="item in courseTimeList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="上课学期：" prop="term">
            <el-input
              style="width: 203px"
              v-model="listQuery.term"
              plceholder=""
            ></el-input>
          </el-form-item>
          <el-form-item label="课程代码：" prop="courseName">
            <el-input
              style="width: 203px"
              v-model="listQuery.courseName"
              placeholder=""
            ></el-input>
          </el-form-item>

          <el-form-item label="姓名：" prop="customerName">
            <el-input
              style="width: 203px"
              v-model="listQuery.customerName"
              placeholder=""
            ></el-input>
          </el-form-item>

          <el-form-item label="审课学校：" prop="auditSchool">
            <el-select clearable v-model="listQuery.auditSchool">
              <el-option
                v-for="item in auditSchoolList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="微信号：" prop="wechatNumber">
            <el-input
              style="width: 203px"
              v-model="listQuery.wechatNumber"
              placeholder=""
            ></el-input>
          </el-form-item>

          <el-form-item label="成绩录入时间：" prop="inputTime">
            <el-date-picker
              v-model="inputTime"
              type="daterange"
              align="right"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd HH:mm:ss"
              :default-time="['00:00:00', '23:59:59']"
              :picker-options="pickerOptions"
            >
            </el-date-picker>
          </el-form-item>
        </el-form>
      </div>
    </el-card>

    <div class="table-container">
      <el-table
        ref="productTable"
        :data="list"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        v-loading="listLoading"
        border
        @sort-change="changeTableSort"
      >
        <!-- <el-table-column
          type="selection"
          width="60"
          align="center"
        ></el-table-column> -->
        <el-table-column label="姓名" align="center">
          <template slot-scope="scope">
            <div
              @click="handleStuDetail(scope.row.userId)"
              class="handleStuDetail"
            >
              {{ scope.row.name }}
            </div></template
          >
        </el-table-column>

        <el-table-column label="学校" align="center">
          <template slot-scope="scope">{{ scope.row.school }}</template>
        </el-table-column>

        <el-table-column label="学生上课年份" align="center">
          <template slot-scope="scope">{{ scope.row.yearTime }}</template>
        </el-table-column>
        <el-table-column label="学生上课学期" align="center">
          <template slot-scope="scope">{{ scope.row.term }}</template>
        </el-table-column>

        <el-table-column label="课程1" align="center">
          <el-table-column align="center" label="课程">
            <template slot-scope="scope">
              {{
                scope.row.userCourseList[0].csoCourseCode ||
                  scope.row.userCourseList[0].auditCourseCode ||
                  scope.row.userCourseList[0].ownCourseCode
              }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="分数">
            <template slot-scope="scope">
              {{ scope.row.userCourseList[0].gpa }}</template
            >
          </el-table-column>
        </el-table-column>

        <el-table-column label="课程2" align="center">
          <el-table-column align="center" label="课程">
            <template slot-scope="scope" v-if="scope.row.userCourseList[1]">
              {{
                scope.row.userCourseList[1].csoCourseCode ||
                  scope.row.userCourseList[1].auditCourseCode ||
                  scope.row.userCourseList[1].ownCourseCode
              }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="分数">
            <template slot-scope="scope" v-if="scope.row.userCourseList[1]">
              {{ scope.row.userCourseList[1].gpa }}</template
            >
          </el-table-column>
        </el-table-column>

        <el-table-column label="课程3" align="center">
          <el-table-column align="center" label="课程">
            <template slot-scope="scope" v-if="scope.row.userCourseList[2]">
              {{
                scope.row.userCourseList[2].csoCourseCode ||
                  scope.row.userCourseList[2].auditCourseCode ||
                  scope.row.userCourseList[2].ownCourseCode
              }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="分数">
            <template slot-scope="scope" v-if="scope.row.userCourseList[2]">
              {{ scope.row.userCourseList[2].gpa }}</template
            >
          </el-table-column>
        </el-table-column>

        <el-table-column label="课程4" align="center">
          <el-table-column align="center" label="课程">
            <template slot-scope="scope" v-if="scope.row.userCourseList[3]">
              {{
                scope.row.userCourseList[3].csoCourseCode ||
                  scope.row.userCourseList[3].auditCourseCode ||
                  scope.row.userCourseList[3].ownCourseCode
              }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="分数">
            <template slot-scope="scope" v-if="scope.row.userCourseList[3]">
              {{ scope.row.userCourseList[3].gpa }}</template
            >
          </el-table-column>
        </el-table-column>

        <el-table-column label="成绩录入时间" align="center" prop="updateTime">
          <!-- <template slot-scope="scope">{{
            scope.row.studyCareAllocateTime
          }}</template> -->
        </el-table-column>

        <el-table-column label="审课大学" align="center" prop="auditSchool">
        </el-table-column>
        <el-table-column label="USSTID" align="center">
          <template
            slot-scope="scope"
            v-if="scope.row.auditSchool == '上海理工大学'"
          >
            <div v-if="scope.row.usstid">{{ scope.row.usstid }}</div>
            <div v-else>
              <el-button
                size="mini"
                @click="addUsst(scope.row.termCourseId, scope.row.userId)"
                >编辑</el-button
              >
            </div>
          </template>
        </el-table-column>

        <el-table-column label="成绩模板" align="center">
          <template slot-scope="scope">
            <!-- <el-button
              v-if="scope.row.auditSchool == '上海理工大学'"
              @click="
                output(scope.row.userId, scope.row.termCourseId, scope.row.name)
              "
              type="mini"
              >导出</el-button
            > -->
            <el-button
              v-if="scope.row.auditSchool == '上海理工大学'"
              @click="
                outputPDF(
                  scope.row.userId,
                  scope.row.termCourseId,
                  scope.row.name
                )
              "
              type="mini"
              >导出</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- <el-card class="operate-container" shadow="never" align="right">
      <el-button class="allOutput">一键导出</el-button>
    </el-card> -->
    <el-dialog
      title="编辑USSTID"
      :visible.sync="USStVisible"
      @close="cancel('editUsst')"
    >
      <el-form :model="editUsst" ref="editUsst">
        <div class="line">
          <el-form-item label="添加USSTID" prop="usstid">
            <el-input type="textarea" autosize v-model="editUsst.usstId">
            </el-input>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel('editUsst')">取 消</el-button>
        <el-button type="primary" @click="submitEditForm('editUsst')"
          >确 定</el-button
        >

        <!-- <el-button type="primary" @click="test">test</el-button> -->
      </div>
    </el-dialog>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total,sizes,prev, pager, next,jumper"
        :page-size="pageSize"
        :page-sizes="[5, 10, 15]"
        :current-page.sync="pageNum"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import {
  auditSchool,
  school,
  courseTime,
  getHaveScoreStu,
  addUSST
} from "@/api/cso2";
import { formatDate } from "@/utils/date";

const defaultListQuery = {
  pageNum: 1,
  pageSize: 10
};
export default {
  name: "haveScoreStu",
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      pageSize: null,
      pageNum: null,
      total: null,
      listLoading: true,
      multipleSelection: [],

      // checked: true,
      // checkedStatus:true,
      USStVisible: false,
      pickerOptions: {},
      inputTime: [],
      chatTime: [],
      paymentTime: [],
      schoolList: [],
      courseTimeList: [],
      auditSchoolList: [],
      isLopFile: false,
      isCreditFile: false,
      editUsst: {
        termCourseId: 0,
        userId: ""
        // usstId:""
      },
      haveGpaStu: []
    };
  },

  watch: {
    inputTime(newValue, oldValue) {
      if (this.inputTime) {
        [
          this.listQuery.starGapInputTime,
          this.listQuery.endGapInputTime
        ] = this.inputTime;
      } else {
        this.listQuery.starGapInputTime = undefined;
        this.listQuery.endGapInputTime = undefined;
      }
    },
    chatTime(newValue, oldValue) {
      if (this.chatTime) {
        [
          this.listQuery.starChatTime,
          this.listQuery.endChatTime
        ] = this.chatTime;
      } else {
        this.listQuery.starChatTime = undefined;
        this.listQuery.endChatTime = undefined;
      }
    },
    paymentTime(newValue, oldValue) {
      if (this.paymentTime) {
        [
          this.listQuery.starPaymentTime,
          this.listQuery.endPaymentTime
        ] = this.paymentTime;
      } else {
        this.listQuery.starPaymentTime = undefined;
        this.listQuery.endPaymentTime = undefined;
      }
    }
  },
  filters: {
    formatDateTime(time) {
      if (time == null || time === "") {
        return "N/A";
      }
      let date = new Date(time);
      return formatDate(date, "yyyy-MM-dd hh:mm:ss");
    }
  },
  created() {
    this.getList();
    this.getSchool();
    this.getCourseTime();
    this.getAuditSchool();
  },

  methods: {
    //后来加的
    getHaveScoreStu() {
      getHaveScoreStu(this.listQuery).then(res => {
        this.$forceUpdate();
        //  this.haveGpaStu=res
        console.log(res, "getHaveScoreStu");
      });
    },

    getList() {
      this.listLoading = true;
      getHaveScoreStu(this.listQuery).then(res => {
        this.listLoading = false;

        this.list = res.data.list;
        this.pageNum = res.data.pageNum;
        this.pageSize = res.data.pageSize;
        this.total = res.data.total;
        console.log(this.list, "this.list");
        this.$forceUpdate();
      });
    },
    getSchool() {
      school().then(res => {
        // this.schoolList = res.data;
        //     console.log(res, "school");
        res.data.forEach(item => {
          let obj = {};
          obj.value = item;
          obj.label = item;
          this.schoolList.push(obj);
        });
        //   console.log(this.schoolList, "this.schoolList");
      });
    },
    getAuditSchool() {
      auditSchool().then(res => {
        // this.schoolList = res.data;
        //  console.log(res, "auditSchool");
        res.data.forEach(item => {
          let obj = {};
          obj.value = item;
          obj.label = item;
          this.auditSchoolList.push(obj);
        });
        //  console.log(this.auditSchoolList,"this.auditSchoolList");
      });
    },
    getCourseTime() {
      courseTime().then(res => {
        res.data.forEach(item => {
          let obj = {};
          obj.value = item;
          obj.label = item;
          this.courseTimeList.push(obj);
        });
      });
    },
    //添加usstid
    addUsst(termCourseId, userId) {
      this.USStVisible = true;
      this.editUsst.termCourseId = termCourseId;
      this.editUsst.userId = userId;
    },

    submitEditForm(formName) {
      this.$confirm("是否提交", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        //拿到整个公告
        addUSST(this.editUsst).then(response => {
          this.$message({
            type: "success",
            message: "提交成功",
            duration: 1000
          });
          this.$refs[formName].resetFields();
          this.USStVisible = false;

          this.getList();
          this.$forceUpdate();
        });
      });
    },

    cancel(formName) {
      this.USStVisible = false;
      this.$refs[formName].resetFields();
    },

    handleSearchList() {
      //搜索
      this.listQuery.pageNum = 1;
      this.getList();
    },
    output(userId, termCourseId) {
      this.$router.push({
        path: "/pms/gpaPdf",
        name: "gpaPdf",
        params: {
          userId: userId,
          termCourseId: termCourseId
        }
      });
      // console.log(123);
    },
    outputPDF(userId, termCourseId) {
      this.$router.push({
        path: "/pms/NewGpaPdf",
        name: "NewGpaPdf",
        query: {
          userId: userId,
          termCourseId: termCourseId
        }
      });
      // console.log(123);
    },

    handleStuDetail(id) {
      // 跳到学员详情
      this.$router.push({
        path: "/pms/stuDetail",
        query: {
          id: id
        }
      });
      // console.log(123);
    },
    handleCurrentChange(val) {
      // 翻页
      this.listQuery.pageNum = val;
      this.getList();
    },
    handleSizeChange(val) {
      //切换展示条数
      this.listQuery.pageSize = val;
      this.getList();
    },

    handleResetSearch(formName) {
      this.$refs[formName].resetFields();
      // 重置
      // this.selectProductCateValue = [];
      this.inputTime = undefined;
      this.isCreditFile = false;
      this.isLopFile = false;
      // this.chatTime = undefined;
      // this.paymentTime = undefined;
      this.listQuery = Object.assign({}, defaultListQuery);
      this.getList();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    changeTableSort(column) {
      // console.log("prop", column.prop);
      // console.log("order", column.order);
      // console.log("column", column);
      if (column.prop == "addTime") {
        this.listQuery.secondSort = null;
        if (column.order == "ascending") {
          this.listQuery.firstSort = 0;
        } else if (column.order == "descending") {
          this.listQuery.firstSort = 1;
        }
      }
      if (column.prop == "nextChatTime") {
        this.listQuery.firstSort = null;
        if (column.order == "ascending") {
          this.listQuery.secondSort = 0;
        } else if (column.order == "descending") {
          this.listQuery.secondSort = 1;
        }
      }
      if (column.order == null) {
        this.listQuery.secondSort = this.listQuery.firstSort = null;
      }
      this.getList();
      console.log("changeTableSort");
    }
  }
};
</script>
<style lang="scss" scoped>
.handleStuDetail {
  cursor: pointer;
}
</style>
