<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="list"
          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"
              placeholder=""
            ></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>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>学员列表</span>
    </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 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>

        <!-- 课程1 -->
        <el-table-column label="课程1" align="center">
          <el-table-column align="center" label="课程">
            <template slot-scope="scope" v-if="scope.row.userCourseList[0]">
              {{
                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 }} -->
              <el-input
                v-if="scope.row.userCourseList[0]"
                v-model="scope.row.userCourseList[0].gpa"
                placeholder="打分"
                size="mini"
              ></el-input>
            </template>
          </el-table-column>
        </el-table-column>
        <!-- 课程2 -->
        <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]">
              <el-input
                v-model="scope.row.userCourseList[1].gpa"
                placeholder="打分"
                size="mini"
              ></el-input>
            </template>
          </el-table-column>
        </el-table-column>
        <!-- 课程3 -->
        <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]">
              <el-input
                v-model="scope.row.userCourseList[2].gpa"
                placeholder="打分"
                size="mini"
              ></el-input>
            </template>
          </el-table-column>
        </el-table-column>
        <!-- 课程4 -->
        <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]">
              <el-input
                v-model="scope.row.userCourseList[3].gpa"
                placeholder="打分"
                size="mini"
              ></el-input>
            </template>
          </el-table-column>
        </el-table-column>

        <el-table-column label="审课大学" align="center">
          <template slot-scope="scope">{{ scope.row.auditSchool }}</template>
        </el-table-column>

        <el-table-column label="成绩录入" align="center">
          <template slot-scope="scope">
            <el-button
              @click="handleUpdateGpaAll(scope.row.userCourseList)"
              size="mini"
              >确定</el-button
            >
          </template>
        </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>
    </div>
    <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,
  getNeedScoreStu,
  UpdateNeedScoreStu,
  addUSST
} from "@/api/cso2";
import { formatDate } from "@/utils/date";

const defaultListQuery = {
  pageNum: 1,
  pageSize: 10
};
export default {
  name: "needScoreStu",
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      total: null,
      pageSize: null,
      pageNum: null,
      listLoading: true,
      multipleSelection: [],
      checked: true,
      checkedStatus: true,
      // showCourseCode:true,
      USStVisible: false,
      pickerOptions: {},
      inputTime: [],
      chatTime: [],
      paymentTime: [],
      schoolList: [],
      courseTimeList: [],
      auditSchoolList: [],
      noGpaStuInfo: {},
      editNoGpaStu: {
        userCourseList: {
          userCourseId: 0,
          userId: "",
          gpa: 0
        }
      },
      editUsst: {
        termCourseId: 0,
        userId: ""
        // usstId:""
      },
      isLopFile: false,
      isCreditFile: false
    };
  },

  watch: {
    inputTime(newValue, oldValue) {
      if (this.inputTime) {
        [
          this.listQuery.starAllocateTime,
          this.listQuery.endAllocateTime
        ] = this.inputTime;
      } else {
        this.listQuery.starAllocateTime = undefined;
        this.listQuery.endAllocateTime = 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: {
    handleUpdateGpaAll(userCourseList) {
      let arr = [];
      userCourseList.forEach(item => {
        if (item.gpa) {
          let obj = {};
          obj.userCourseId = item.id;
          obj.gpa = item.gpa;
          arr.push(obj);
        }
      });
      if (arr.length == 0) {
        this.$message({
          type: "error",
          message: "请先打分再提交",
          duration: 1000
        });
      } else {
        this.$confirm("是否要确认", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(res => {
          UpdateNeedScoreStu(arr).then(res => {
            this.$message({
              type: "success",
              message: "提交成功",
              duration: 1000
            });

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

    getList() {
      this.listLoading = true;
      getNeedScoreStu(this.listQuery).then(res => {
        // console.log(res, "getNeedScoreStuData");
        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(this.schoolList, "this.schoolList");
        res.data.forEach(item => {
          let obj = {};
          obj.value = item;
          obj.label = item;
          this.schoolList.push(obj);
        });
      });
    },
    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();
    },

    handleStuDetail(id) {
      // 测试用的
      this.$router.push({
        path: "/pms/stuDetail",
        query: {
          id: id
        }
      });
      // 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) {
      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>
