<template>
  <div class="curricula">
    <!-- 查询条件 -->
    <div class="curricula-search">
      <div class="curricula-search-condition">
        <el-input v-model="search" prefix-icon="el-icon-search">
          <el-button slot="append" @click="queryMyCourseList">搜索</el-button>
        </el-input>
      </div>
      <div class="curricula-search-button">
        <el-select v-if="userType === 'STUDENT'" v-model="teacher_id">
          <el-option label="-请选择-" value="" />
          <el-option
            v-for="item in teacher_list"
            :key="item.teacher_id"
            :label="item.teacher_name"
            :value="item.teacher_id"
          />
        </el-select>
        <el-select v-model="finish_status">
          <el-option label="-请选择-" :value="-1" />
          <el-option
            v-for="item in finish_status_list"
            :key="userType === 'TEACHER' ? item.finish_status : item.status"
            :label="item.name"
            :value="userType === 'TEACHER' ? item.finish_status : item.status"
          ></el-option>
        </el-select>
        <el-button type="primary" @click="queryMyCourseList">查询</el-button>
      </div>
    </div>
    <!-- 课程列表 -->
    <div class="curricula-container">
      <div class="curricula-container-title">
        <div>
          <span class="title">课程列表</span>
          <span v-if="userType === 'STUDENT'" class="tip">
            你也可以去 学习中心 选择更多的课程。
          </span>
        </div>
        <div v-if="userType === 'TEACHER'">
          <el-button class="create" @click="$router.push('/create_course')">
            <div><svg-icon icon-class="create" /><span>创建课程</span></div>
          </el-button>
        </div>
      </div>
      <div class="curricula-container-list">
        <el-table :data="courseList">
          <el-table-column
            :prop="userType === 'TEACHER' ? 'name' : 'course_name'"
            label="课程名称"
            width="320"
          />
          <el-table-column
            v-if="userType === 'STUDENT'"
            label="教师"
            prop="teacher_name_desc"
            width="200"
          />
          <el-table-column label="课程周期">
            <template slot-scope="{ row }">
              <template v-if="userType === 'TEACHER'">
                <i class="el-icon-date" /> {{ row.begin_date }} - {{ row.end_date }}
              </template>
              <template v-else>
                <i class="el-icon-date" /> {{ row.date_space_view_text }}
              </template>
            </template>
          </el-table-column>
          <el-table-column v-if="userType === 'TEACHER'" fixed="right" width="80">
            <template slot-scope="{ row }">
              <el-dropdown trigger="click">
                <span class="el-dropdown-link">
                  <svg-icon icon-class="more"></svg-icon>
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item @click.native="edit(row.id)">
                    <svg-icon icon-class="edit" /> 编辑
                  </el-dropdown-item>
                  <el-dropdown-item @click.native="studentList(row.id)">
                    <svg-icon icon-class="students" /> 学生列表
                  </el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </template>
          </el-table-column>
          <el-table-column v-if="userType === 'STUDENT'" label="状态">
            <template slot-scope="{ row }">
              <span class="status-name" :style="{ 'background-color': statusColor(row.status) }" />
              <span :style="{ color: statusColor(row.status) }">{{ row.status_name }}</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <el-pagination
      background
      :page-sizes="[10, 20, 30, 40, 50]"
      :page-size="page_capacity"
      layout="prev, pager, next, total, sizes, jumper"
      :total="total_count"
      :current-page="cur_page"
      @prev-click="changePage"
      @next-click="changePage"
      @current-change="changePage"
      @size-change="changePageSize"
    />
  </div>
</template>

<script>
import { updateWordPack } from '@/utils/i18n';
import { PageQueryMyCourseList, PageQueryMyJoinCourseList_Student } from '@/api/table';
import {
  GetFinishStatusList_Course,
  GetMyJoinCourseTeacherList_Student,
  GetStudentCourseStatusList
} from '@/api/select';

export default {
  name: 'CurriculaList',
  data() {
    return {
      userType: this.$store.state.user.user_type,
      search: '',
      finish_status_list: [],
      teacher_list: [],
      teacher_id: '',
      finish_status: -1,
      page_capacity: 10,
      cur_page: 1,
      total_count: 0,
      courseList: []
    };
  },
  created() {
    this.queryMyCourseList();
    this.getFinishStatusList_Course();
    if (this.userType === 'STUDENT') {
      this.getMyJoinCourseTeacherList_Student();
    }
    updateWordPack({
      word_key_list: ['Learn_New_Courses']
    });
  },
  methods: {
    getFinishStatusList_Course() {
      if (this.userType === 'TEACHER') {
        GetFinishStatusList_Course().then(({ finish_status_list }) => {
          this.finish_status_list = finish_status_list;
        });
      } else {
        GetStudentCourseStatusList().then(({ status_list }) => {
          this.finish_status_list = status_list;
        });
      }
    },
    getMyJoinCourseTeacherList_Student() {
      GetMyJoinCourseTeacherList_Student().then(({ teacher_list }) => {
        this.teacher_list = teacher_list;
      });
    },
    selectTab(status, i) {
      this.status = status;
      this.curTab = i;
      this.search();
    },
    changePage(newPage) {
      this.cur_page = newPage;
      this.queryMyCourseList();
    },
    changePageSize(pageSize) {
      this.page_capacity = pageSize;
      this.queryMyCourseList();
    },
    queryMyCourseList() {
      if (this.userType === 'TEACHER') {
        const queryCriteria = {
          finish_status: this.finish_status,
          name: this.search,
          page_capacity: this.page_capacity,
          cur_page: this.cur_page
        };
        PageQueryMyCourseList(queryCriteria).then(({ course_list, total_count }) => {
          this.courseList = course_list;
          this.total_count = total_count;
        });
      } else {
        let data = {
          teacher_id: this.teacher_id,
          status_list: [this.finish_status],
          course_name: this.search,
          page_capacity: this.page_capacity,
          cur_page: this.cur_page
        };
        PageQueryMyJoinCourseList_Student(data).then(({ course_list, total_count }) => {
          this.courseList = course_list;
          this.total_count = total_count;
        });
      }
    },
    statusColor(val) {
      if (val === 0) {
        return '#68CEFA';
      }
      if (val === 1) {
        return '#2ECE5B';
      }
      if (val === 3) {
        return '#f90';
      }
      if (val === 4) {
        return '#4F8EEC';
      }
    },
    studentList(id) {
      this.$router.push(`/student_list/index/${id}`);
    },
    edit(id) {
      this.$router.push(`/create_course_step_table/course_info?id=${id}`);
    }
  }
};
</script>

<style lang="scss">
@import '~@/styles/mixin.scss';

.curricula {
  @include pagination;

  &-search {
    display: flex;
    justify-content: space-between;

    &-condition {
      > .el-input {
        width: 528px;
      }
    }

    &-button {
      .el-button {
        width: 114px;
      }

      .el-select {
        width: 225px;
        margin-right: 12px;
      }
    }
  }

  &-container {
    width: 100%;
    min-height: calc(100vh - 325px);
    margin-top: 16px;
    background-color: #fff;
    border-radius: 8px;

    &-title {
      height: 88px;
      padding: 24px 32px;
      display: flex;
      justify-content: space-between;
      align-items: center;

      .title {
        font-size: 20px;
        font-weight: 700;
        margin-right: 36px;
      }

      .tip {
        color: #aaa;
      }

      .create {
        width: 138px;

        div {
          display: flex;
          justify-content: space-around;
        }
      }
    }

    &-list {
      @include list;

      margin-top: 0;

      .el-dropdown {
        cursor: pointer;

        .svg-icon {
          font-size: 19px;
        }
      }

      .status-name {
        display: inline-block;
        width: 8px;
        height: 8px;
        border-radius: 50%;
        margin-right: 8px;
        margin-bottom: 1px;
      }
    }
  }
}
</style>

<style scope>
.el-dropdown-menu .svg-icon {
  margin-right: 8px;
}
</style>
