<template>
  <div class="app-container">
    <el-dialog
      title="编辑大纲"
      :visible.sync="editNoticeVisible"
      @close="cancel('editOutlineInfo')"
    >
      <el-form :model="editOutlineInfo" ref="editOutlineInfo">
        <div class="line">
          <el-form-item label="编辑大纲状态" prop="state">
            <el-select clearable v-model="editOutlineInfo.state">
              <el-option
                v-for="item in options"
                :key="item.state"
                :label="item.label"
                :value="item.state"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="转让给其他老师" prop="adminId">
            <el-select
              clearable
              v-model="editOutlineInfo.adminId"
              :disabled="editOutlineInfo.state == 2"
            >
              <el-option
                v-for="item in teacherList"
                :key="item.id"
                :label="item.nickName"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel('editOutlineInfo')">取 消</el-button>
        <el-button type="primary" @click="submitEditForm('editOutlineInfo')"
          >确 定</el-button
        >

        <!-- <el-button type="primary" @click="test">test</el-button> -->
      </div>
    </el-dialog>
    <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="cso课程名称" align="center">
          <template slot-scope="scope">
            {{ scope.row.csoName }}
          </template>
        </el-table-column>

        <el-table-column label="cso课程代码" align="center">
          <template slot-scope="scope">{{ scope.row.csoCode }}</template>
        </el-table-column>

        <el-table-column label="学生所在学校" align="center">
          <template slot-scope="scope">{{ scope.row.ownSchool }}</template>
        </el-table-column>
        <!-- <el-table-column label="审课学姐" align="center">
          <template slot-scope="scope">{{ scope.row.ownSchool }}</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="提交时间"
          :sortable="'custom'"
          align="center"
          prop="addTime"
        >
          <template slot-scope="scope">{{ scope.row.addTime }}</template>
        </el-table-column>

        <el-table-column label="大纲进度" align="center">
          <template slot-scope="scope">
            {{
              scope.row.state == 1
                ? "进行中"
                : scope.row.state == 2
                ? "已完成"
                : ""
            }}
          </template>
        </el-table-column>

        <el-table-column label="归属老师" align="center">
          <template slot-scope="scope">{{ scope.row.nickName }}</template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="
                editOutline(scope.row.adminId, scope.row.state, scope.row.id)
              "
              >编辑</el-button
            >
          </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 {
  // xuefuMasterAllStu,
  csoOutline,
  UpdateCsoOutline,
  ChangeCsoOutline,
  adminList
} from "@/api/cso2";
import { formatDate } from "@/utils/date";

const defaultListQuery = {
  pageNum: 1,
  pageSize: 10
};
export default {
  name: "editPlan",
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      pageSize: null,
      pageNum: null,
      total: null,
      listLoading: true,
      multipleSelection: [],
      editNoticeVisible: false,
      inputTime: [],
      chatTime: [],
      paymentTime: [],
      noticeVisible: false,
      teacherList: [],
      editOutlineInfo: {
        state: "",
        adminId: ""
      },
      options: [
        {
          state: 1,
          label: "进行中"
        },
        {
          state: 2,
          label: "已完成"
        }
      ],
      value: "",
      value2: "",
      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.getTeacherList();
  },

  methods: {
    test(aa) {
      if (this.userStatus == "") {
        this.userStatus = undefined;
      }
    },
    getList() {
      csoOutline(this.listQuery).then(res => {
        this.listLoading = false;

        this.list = JSON.parse(JSON.stringify(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();
      });
    },
    getTeacherList() {
      adminList(3).then(res => {
        this.teacherList = res.data;
        console.log(this.teacherList, "getTeacherList");
      });
    },
    editOutline(adminId, state, id) {
      this.editNoticeVisible = true;

      this.editOutlineInfo.adminId = adminId;
      this.editOutlineInfo.state = state;
      this.editOutlineInfo.id = id;
      console.log(this.editOutlineInfo, "editOutline");
    },
    submitEditForm(formName) {
      //   this.editOutlineInfo.id = this.updateId;

      this.$confirm("确认提交？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        // console.log(this.editOutlineInfo, "submitEditForm");
        let item = this.list.filter(val => {
          return val.id == this.editOutlineInfo.id;
        });
        // console.log(item, "item.state");
        // console.log(this.editOutlineInfo.state, item.state);
        if (this.editOutlineInfo.state !== item[0].state) {
          UpdateCsoOutline(this.editOutlineInfo).then(response => {
            this.$message({
              type: "success",
              message: "提交成功"
            });

            this.$refs[formName].resetFields();
            this.editNoticeVisible = false;

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

        if (this.editOutlineInfo.adminId !== item[0].adminId) {
          ChangeCsoOutline(this.editOutlineInfo).then(response => {
            this.$message({
              type: "success",
              message: "提交成功"
            });

            this.$refs[formName].resetFields();
            this.editNoticeVisible = false;

            this.getList();
            this.$forceUpdate();
          });
        }
      });
    },
    cancel(formName) {
      this.noticeVisible = false;
      this.editNoticeVisible = false;
      this.$refs[formName].resetFields();
    },
    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();
    },

    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>
