<template>
  <div class="app-container">
    <!-- <h1>selecting LOP审课中</h1> -->
    <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"
          ref="listQuery"
          size="small"
          label-width="140px"
          @keyup.enter.native="handleSearchList()"
        >
          <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="auditSenior">
            <el-select clearable v-model="listQuery.auditSenior">
              <el-option
                v-for="item in auditSeniorList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>

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

          <el-form-item label="报名年份：" prop="yearTime">
            <!-- <el-input
              style="width: 203px"
              v-model="listQuery.yearTime"
              placeholder=""
            ></el-input> -->
            <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="auditSchool">
            <el-input
              style="width: 203px"
              v-model="listQuery.auditSchool"
              placeholder=""
            ></el-input>
          </el-form-item>
          <el-form-item label="授课形式：" prop="teachWay">
            <el-input
              style="width: 203px"
              v-model="listQuery.teachWay"
              placeholder=""
            ></el-input>
          </el-form-item>
          <el-form-item label="年级：" prop="grade">
            <el-select clearable v-model="listQuery.grade" style="width: 203px">
              <el-option label="大学一年级" :value="1"></el-option>
              <el-option label="大学二年级" :value="2"></el-option>
              <el-option label="大学三年级" :value="3"></el-option>
              <el-option label="大学四年级" :value="4"></el-option>
              <el-option label="大学五年级" :value="5"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="专业：" prop="domian">
            <el-input
              style="width: 203px"
              v-model="listQuery.domian"
              placeholder=""
            ></el-input>
          </el-form-item>
          <el-form-item label="邮箱号：" prop="mail">
            <el-input
              style="width: 203px"
              v-model="listQuery.mail"
              placeholder=""
            ></el-input>
          </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-item label="">
            <el-checkbox v-model="isPassAuditCourse">审课通过</el-checkbox>
          </el-form-item>

          <!-- <el-form-item label="">
                <el-input style="width: 203px" v-model="listQuery.productSn" placeholder=""></el-input>
                <div style="width: 250px"></div>
              </el-form-item> -->
          <el-form-item label="提交时间：">
            <el-date-picker
              v-model="chatTime"
              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-item label="审课通过时间：">
            <el-date-picker
              v-model="paymentTime"
              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>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button class="btn-add" @click="handleAddProduct()" size="mini">
        添加
      </el-button>
      <!-- <el-button class="btn-add" @click="handleStuDetail()" size="mini">
            stuDetail
          </el-button> -->
    </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
              class="handleStuDetail"
              @click="handleStuDetail(scope.row.userId)"
            >
              {{ 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">
            <div v-show="scope.row.grade == 1">大学一年级</div>
            <div v-show="scope.row.grade == 2">大学二年级</div>
            <div v-show="scope.row.grade == 3">大学三年级</div>
            <div v-show="scope.row.grade == 4">大学四年级</div>
            <div v-show="scope.row.grade == 5">大学五年级</div>
          </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="审课学姐" align="center">
          <template slot-scope="scope">{{ scope.row.auditSenior }}</template>
        </el-table-column>

        <el-table-column label="审课1" align="center">
          <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
            }}
            <el-select
              clearable
              v-if="scope.row.userCourseList[0].state == 2"
              v-model="scope.row.userCourseList[0].state2"
            >
              <el-option label="审课通过" :value="3"> </el-option>
              <el-option label="审课未通过" :value="4"> </el-option>
            </el-select>
            <el-tag
              type="success"
              v-else-if="scope.row.userCourseList[0].state == 3"
              >通过</el-tag
            >
            <el-tag type="danger" v-else>未通过</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="审课2" align="center">
          <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
            }}
            <el-select
              clearable
              v-if="scope.row.userCourseList[1].state == 2"
              v-model="scope.row.userCourseList[1].state2"
            >
              <el-option label="审课通过" :value="3"> </el-option>
              <el-option label="审课未通过" :value="4"> </el-option>
            </el-select>
            <el-tag
              type="success"
              v-else-if="scope.row.userCourseList[1].state == 3"
              >通过</el-tag
            >
            <el-tag type="danger" v-else>未通过</el-tag>
          </template>
        </el-table-column>

        <el-table-column label="审课3" align="center">
          <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
            }}
            <el-select
              clearable
              v-if="scope.row.userCourseList[2].state == 2"
              v-model="scope.row.userCourseList[2].state2"
            >
              <el-option label="审课通过" :value="3"> </el-option>
              <el-option label="审课未通过" :value="4"> </el-option>
            </el-select>
            <el-tag
              type="success"
              v-else-if="scope.row.userCourseList[2].state == 3"
              >通过</el-tag
            >
            <el-tag type="danger" v-else>未通过</el-tag>
          </template>
        </el-table-column>

        <el-table-column label="审课4" align="center">
          <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
            }}
            <el-select
              clearable
              v-if="scope.row.userCourseList[3].state == 2"
              v-model="scope.row.userCourseList[3].state2"
            >
              <el-option label="审课通过" :value="3"> </el-option>
              <el-option label="审课未通过" :value="4"> </el-option>
            </el-select>
            <el-tag
              type="success"
              v-else-if="scope.row.userCourseList[3].state == 3"
              >通过</el-tag
            >
            <el-tag type="danger" v-else>未通过</el-tag>
          </template>
        </el-table-column>

        <el-table-column label="审课5" align="center">
          <template slot-scope="scope" v-if="scope.row.userCourseList[4]"
            >{{
              scope.row.userCourseList[4].csoCourseCode ||
                scope.row.userCourseList[4].auditCourseCode ||
                scope.row.userCourseList[4].ownCourseCode
            }}
            <el-select
              clearable
              v-if="scope.row.userCourseList[4].state == 2"
              v-model="scope.row.userCourseList[4].state2"
            >
              <el-option label="审课通过" :value="3"> </el-option>
              <el-option label="审课未通过" :value="4"> </el-option>
            </el-select>
            <el-tag
              type="success"
              v-else-if="scope.row.userCourseList[4].state == 3"
              >通过</el-tag
            >
            <el-tag type="danger" v-else>未通过</el-tag>
          </template>
        </el-table-column>

        <!-- <el-table-column label="审课3" align="center">
          <template slot-scope="scope" v-if="scope.row.userCourseList[2]">{{
            scope.row.userCourseList[2].ownCourseCode
          }}</template>
        </el-table-column>
        <el-table-column label="审课4" align="center">
          <template slot-scope="scope" v-if="scope.row.userCourseList[3]">{{
            scope.row.userCourseList[3].ownCourseCode
          }}</template>
        </el-table-column>
        <el-table-column label="审课5" align="center">
          <template slot-scope="scope" v-if="scope.row.userCourseList[4]">{{
            scope.row.userCourseList[4].ownCourseCode
          }}</template>
        </el-table-column>
        <el-table-column label="审课6" align="center">
          <template slot-scope="scope" v-if="scope.row.userCourseList[5]">{{
            scope.row.userCourseList[5].ownCourseCode
          }}</template>
        </el-table-column> -->

        <el-table-column
          label="分配时间"
          :sortable="'custom'"
          align="center"
          prop="nextChatTime"
        >
          <template slot-scope="scope">{{
            scope.row.studyCareAllocateTime | formatDateTime
          }}</template>
        </el-table-column>

        <el-table-column
          label="提交时间"
          :sortable="'custom'"
          align="center"
          prop="addTime"
        >
          <template slot-scope="scope">{{
            scope.row.commitTime | formatDateTime
          }}</template>
        </el-table-column>

        <el-table-column label="审课通过时间" align="center" prop="updateTime">
          <template slot-scope="scope">{{
            scope.row.updateTime | formatDateTime
          }}</template>
        </el-table-column>

        <!-- <el-table-column label="选课数量" align="center">
            <template slot-scope="scope">{{
              scope.row.chooseCourseNum
            }}</template>
          </el-table-column> -->

        <!-- <el-table-column label="审课通过数" align="center">
            <template slot-scope="scope">{{ scope.row.passCourseNum }}</template>
          </el-table-column> -->

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

        <el-table-column label="LOP确认" align="center">
          <template slot-scope="scope">
            <el-button
              @click="confirm(scope.row.userCourseList)"
              type="mini"
              :disabled="scope.row.showConfirmDisabled"
              >确认</el-button
            >
            <!-- <el-button @click="confirm(scope.row.userCourseList)" type="mini"
              >确认</el-button
            > -->
          </template>
        </el-table-column>

        <!-- <el-table-column label="最近沟通记录" align="center">
              <template slot-scope="scope">{{ scope.row.context }}</template>
            </el-table-column> -->

        <!-- <el-table-column
              label="下次沟通时间"
              :sortable="'custom'"
              align="center"
              prop="nextChatTime"
            >
              <template slot-scope="scope">{{ scope.row.nextChatTime }}</template>
            </el-table-column> -->
      </el-table>
    </div>

    <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 { selectCourse, school, courseTime, LOPState } from "@/api/cso";
import { getAuditSenior } from "@/api/cso2";
import { formatDate } from "@/utils/date";

const defaultListQuery = {
  pageNum: 1,
  pageSize: 10
};
export default {
  name: "selecting",
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: [],
      pageSize: null,
      pageNum: null,
      total: null,
      listLoading: true,
      multipleSelection: [],
      auditSeniorQuery: {},
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      },
      inputTime: [],
      chatTime: [],
      paymentTime: [],
      schoolList: [],
      courseTimeList: [],
      auditSeniorList: [],
      passStateList: [
        {
          value: 1,
          label: "全部信息待确认"
        },
        {
          value: 2,
          label: "转学分待确认"
        },
        {
          value: 3,
          label: "已转学分"
        }
      ],
      isLopFile: false,
      isCreditFile: false,
      // checked: false,
      checkList: [],
      isPassAuditCourse: false
      //   showConfirm: true
    };
  },
  // computed: {
  //   starInputTime() {
  //     return this.inputTime[0]
  //   },
  //   endInputTime() {
  //     return this.inputTime[1]
  //   }
  // },
  watch: {
    "listQuery.yearTime"(newValue, oldValue) {
      // console.log(newValue, oldValue,this.listQuery.yearTime,'=====');
      if (this.listQuery.yearTime) {
        this.auditSeniorQuery.yearTime = this.listQuery.yearTime;
      } else {
        this.auditSeniorQuery.yearTime = undefined;
      }
      this.getAuditSeniorList();
    },
    "listQuery.term"(newValue, oldValue) {
      // console.log(newValue, oldValue,this.listQuery.yearTime,'=====');
      if (this.listQuery.term) {
        this.auditSeniorQuery.term = this.listQuery.term;
      } else {
        this.auditSeniorQuery.term = undefined;
      }
      this.getAuditSeniorList();
    },

    isPassAuditCourse(newValue, oldValue) {
      if (this.isPassAuditCourse) {
        this.auditSeniorQuery.isPassAuditCourse = 0;
      } else {
        this.auditSeniorQuery.isPassAuditCourse = null;
      }
      this.getAuditSeniorList();
    },
    //分配时间
    inputTime(newValue, oldValue) {
      if (this.inputTime) {
        [
          this.listQuery.starAllocateTime,
          this.listQuery.endAllocateTime
        ] = this.inputTime;
        // console.log(this.inputTime,'==inputTime');
        //  [
        //     this.auditSeniorQuery.starAllocateTime,
        //     this.auditSeniorQuery.endAllocateTime
        // ] = this.inputTime;
      } else {
        this.listQuery.starAllocateTime = undefined;
        this.listQuery.endAllocateTime = undefined;
        //  this.auditSeniorQuery.starAllocateTime = undefined,
        //   this.auditSeniorQuery.endAllocateTime = undefined
      }
      // this.getAuditSeniorList()
    },
    //提交时间
    chatTime(newValue, oldValue) {
      if (this.chatTime) {
        [this.listQuery.starTime, this.listQuery.endTime] = this.chatTime;
        [
          this.auditSeniorQuery.starTime,
          this.auditSeniorQuery.endTime
        ] = this.chatTime;
      } else {
        this.listQuery.starTime = undefined;
        this.listQuery.endTime = undefined;
        (this.auditSeniorQuery.starTime = undefined),
          (this.auditSeniorQuery.endTime = undefined);
      }
      this.getAuditSeniorList();
    },
    //审课通过时间
    paymentTime(newValue, oldValue) {
      if (this.paymentTime) {
        [
          this.listQuery.auditPassStarTime,
          this.listQuery.auditPassEndTime
        ] = this.paymentTime;
        [
          this.auditSeniorQuery.auditPassStarTime,
          this.auditSeniorQuery.auditPassEndTime
        ] = this.paymentTime;
      } else {
        (this.auditSeniorQuery.auditPassStarTime = undefined),
          (this.auditSeniorQuery.auditPassEndTime = undefined);
        this.listQuery.auditPassStarTime = undefined;
        this.listQuery.auditPassEndTime = undefined;
      }
      this.getAuditSeniorList();
    }
  },
  filters: {
    formatDateTime(time) {
      if (time == null || time === "") {
        return "";
      }
      let date = new Date(time);
      // return formatDate(date, "yyyy-MM-dd hh:mm:ss");
      return formatDate(date, "yyyy-MM-dd");
    }
  },
  created() {
    this.getList();
    this.getSchool();
    this.getCourseTime();
    this.getAuditSeniorList();
  },

  methods: {
    test(aa) {
      if (this.userStatus == "") {
        this.userStatus = undefined;
      }
    },
    getList() {
      if (this.isPassAuditCourse) {
        this.listQuery.isPassAuditCourse = 0;
      } else {
        this.listQuery.isPassAuditCourse = undefined;
      }
      this.listLoading = true;
      selectCourse(2, 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;
        // this.showConfirm =
        res.data.list.forEach(item => {
          let res = item.userCourseList.every(val => {
            return val.state !== 2;
          });
          item.showConfirmDisabled = res;
          //   console.log(res, "res666");
        });
        // console.log(this.showConfirm, "this.showConfirm");
        // res.data.list.forEach(item => {
        //   item.checkList = [];
        //   item.userCourseList.forEach(val => {
        //     val.state = "";
        //     // val.checkList = [];
        //   });
        //   //   item.userCourseList.checked = false;
        // });
        this.list = JSON.parse(JSON.stringify(res.data.list));
        // console.log(this.list, "this.list");
        this.$forceUpdate();
      });
    },
    //得到审课学姐
    getAuditSeniorList() {
      getAuditSenior(this.auditSeniorQuery).then(res => {
        this.auditSeniorList = [];
        // this.schoolList = res.data;
        // console.log(this.schoolList, "this.schoolList");
        res.data.forEach(item => {
          let obj = {};
          obj.value = item;
          obj.label = item;
          this.auditSeniorList.push(obj);
        });
        this.$forceUpdate();
        // console.log(this.auditSeniorList,'学姐列表');
      });
    },
    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);
        });
      });
    },
    getCourseTime() {
      courseTime().then(res => {
        res.data.forEach(item => {
          let obj = {};
          obj.value = item;
          obj.label = item;
          this.courseTimeList.push(obj);
        });
      });
    },

    handleSearchList() {
      //搜索
      this.listQuery.pageNum = 1;
      this.getList();
    },
    handleAddProduct() {
      // 添加
      this.$router.push({ path: "/pms/addProduct" });
    },
    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.chatTime = undefined;
      this.paymentTime = undefined;
      this.isCreditFile = false;
      this.isLopFile = false;
      this.isPassAuditCourse = false;
      // this.chatTime = undefined;
      // this.paymentTime = undefined;
      this.listQuery = Object.assign({}, defaultListQuery);
      this.getList();
      this.getAuditSeniorList();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    changeTableSort(column) {
      // console.log("prop", column.prop);
      // console.log("order", column.order);
      // console.log("column", column);
      if (column.prop == "nextChatTime") {
        this.listQuery.secondSort = null;
        if (column.order == "ascending") {
          this.listQuery.firstSort = 0;
        } else if (column.order == "descending") {
          this.listQuery.firstSort = 1;
        }
      }
      if (column.prop == "addTime") {
        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");
    },
    confirm(userCourseList) {
      //   console.log(userCourseList, "userCourseList");
      let arr = [];
      userCourseList.forEach(item => {
        if (item.state2) {
          let obj = {};
          obj.id = item.id;
          obj.state = item.state2;
          arr.push(obj);
        }
      });
      if (arr.length == 0) {
        this.$message({
          type: "error",
          message: "请选择审课状态",
          duration: 1000
        });
      } else {
        this.$confirm("是否要确认", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(res => {
          LOPState(arr).then(res => {
            this.$message({
              type: "success",
              message: "提交成功",
              duration: 1000
            });

            this.getList();
            this.$forceUpdate();
          });
        });
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.handleStuDetail {
  cursor: pointer;
}
.selectClass {
  display: flex;
  align-items: center;
  .el-select {
    width: 100px;
  }
}
</style>
