<template>
  <div class="template">
    <main-menu cur-tab="TemplateList" />
    <!-- 查询条件 -->
    <div class="template-search">
      <div>
        <el-input v-model="template_name" class="search-name" prefix-icon="el-icon-search" @change="queryCourseList">
          <el-button slot="append" @click="queryCourseList">
            搜索
          </el-button>
        </el-input>
      </div>
      <div>
        <el-select v-model="release_status">
          <el-option v-for="item in releaseStatusList" :key="item.value" :label="item.name" :value="item.value" />
        </el-select>
        <el-button class="query-button" type="primary" @click="queryCourseList">
          查询
        </el-button>
      </div>
    </div>
    <!-- 模板列表 -->
    <div class="template-container">
      <div class="template-container-title">
        <el-button class="create" @click="$router.push('/create_course_step_table/course_info?is_template=true')">
          <div><svg-icon icon-class="create" /><span>创建模板</span></div>
        </el-button>
      </div>
      <div class="template-container-list">
        <el-table :data="courseList">
          <el-table-column prop="name" label="课程名称" width="320" />
          <el-table-column label="课程周期" width="210">
            <template slot-scope="{ row }">
              <i class="el-icon-date" /> {{ row.begin_date }} - {{ row.end_date }}
            </template>
          </el-table-column>
          <el-table-column label="已发布" width="80">
            <template slot-scope="{ row }">
              {{ row.is_release === 'true' ? '√' : '' }}
            </template>
          </el-table-column>
          <el-table-column label="所属机构" prop="org_name" />

          <el-table-column fixed="right" width="80">
            <template slot-scope="{ row: { id, is_release } }">
              <el-dropdown trigger="click">
                <span class="el-dropdown-link">
                  <svg-icon icon-class="more" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item @click.native="edit(id)">
                    <svg-icon icon-class="edit" /> 编辑
                  </el-dropdown-item>
                  <el-dropdown-item v-if="is_release === 'false'" @click.native="releaseCourse(id, true)">
                    <svg-icon icon-class="publish" /> 发布
                  </el-dropdown-item>
                  <el-dropdown-item v-else @click.native="releaseCourse(id, false)">
                    <svg-icon icon-class="undo" /> 取消发布
                  </el-dropdown-item>
                  <el-dropdown-item @click.native="deleteCourse(id)">
                    <svg-icon icon-class="delete" /> 删除
                  </el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </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 MainMenu from './components/MainMenu.vue';
import { PageQueryCourseList } from '@/api/table';
import { ReleaseCourse, DeleteCourse, IsHasEditDestCoursePopedom } from '@/api/course';

export default {
  components: {
    MainMenu
  },
  data() {
    return {
      // 查询条件
      template_name: '',
      page_capacity: 10,
      cur_page: 0,
      total_count: 0,
      release_status: -1,
      releaseStatusList: [
        {
          name: '全部',
          value: -1
        },
        {
          name: '已发布',
          value: 1
        },
        {
          name: '未发布',
          value: 0
        }
      ],
      // 课程列表
      courseList: []
    };
  },
  created() {
    this.queryCourseList();
  },
  methods: {
    changePage(newPage) {
      this.cur_page = newPage;
      this.queryCourseList();
    },
    changePageSize(pageSize) {
      this.page_capacity = pageSize;
      this.queryCourseList();
    },
    queryCourseList() {
      PageQueryCourseList({
        is_template: true,
        name: this.template_name,
        page_capacity: this.page_capacity,
        cur_page: this.cur_page,
        release_status: this.release_status
      }).then(({ course_list, total_count }) => {
        this.courseList = course_list;
        this.total_count = total_count;
      });
    },

    edit(id) {
      IsHasEditDestCoursePopedom({ id }).then(({ is_has_popedom }) => {
        if (is_has_popedom === 'true') {
          return this.$router.push(`/create_course_step_table/course_info?id=${id}&is_template=true`);
        }
        this.$message.warning('您没有编辑该模板的权限');
      });
    },

    releaseCourse(course_id, is_release) {
      ReleaseCourse({ course_id, is_release }).then(() => {
        this.queryCourseList();
        this.$message.success(`${is_release ? '' : '取消'}发布课程成功`);
      });
    },

    deleteCourse(id) {
      DeleteCourse({ id }).then(() => {
        this.queryCourseList();
        this.$message.success('删除课程成功');
      });
    },

    changeTab(tab) {
      this.$emit('changeTab', tab);
    }
  }
};
</script>

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

.template {
  @include pagination;

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

    .search-name {
      width: 528px;
    }

    .query-button {
      width: 114px;
      margin-left: 12px;
    }
  }

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

    &-title {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 24px 32px;
      font-size: 20px;
      font-weight: bold;

      .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;
        }
      }
    }
  }
}
</style>

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