<template>
  <div class="page-body">
    <div class="page-search row items-center">
      <div class="col row wrap q-mr-lg q-col-gutter-md">
        <div class="col-3">
          <q-input @change="resetSearch" clearable standout="bg-primary text-white" v-model="msg.CourseName"
            label="课程名称" />
        </div>
        <div class="col-3">
          <select-tree   :treeData='TreeCategoryList'
            :defaultArray="returnString" nodeKey="CateId" :multiple="true" labelKey="CateName" childrenKey="ChildList"
            tipText="课程分类" @getChild="getChild"></select-tree>
        </div>
        <div class="col-3">
          <q-select @input="resetSearch" standout="bg-primary text-white" v-model="msg.Status" :options="ShowOpts"
            emit-value map-options label="状态" />
        </div>
      </div>
      <div class="page-option">
        <q-btn color="accent" class="q-mr-md" icon="add" label="新增课程" @click="EditCourse(null)" />
      </div>
    </div>
    <div class="page-content">
      <q-table :pagination="msg" :loading="loading" no-data-label="暂无相关数据" flat class="sticky-tow-column-table"
        separator="none" title="课程信息" :data="data" :columns="columns" row-key="name">
        <template v-slot:body-cell-CoverImg="props">
          <q-td :props="props">
            <q-avatar square size="100px">
              <img :src="props.value" />
            </q-avatar>
          </q-td>
        </template>
        <template v-slot:body-cell-CourseIntro="props">
          <q-td :props="props">
            <span v-html="props.value"></span>
          </q-td>
        </template>
        <template v-slot:body-cell-Status="props">
          <q-td :props="props">
            <q-badge :color="props.value==1?'negative':'primary'" :label="props.value==0?'正常':'禁用'" />
          </q-td>
        </template>
        <template v-slot:bottom>
          <q-pagination class="full-width justify-end" v-model="msg.pageIndex" color="primary" :max="pageCount"
            :input="true" @input="changePage" />
        </template>
        <template v-slot:body-cell-optioned="props">
          <q-td :props="props">
            <q-btn flat size="xs" icon="edit" color="accent" style="font-weight:400" label="教案"
              @click="gotoSub(props.row,'teachplan')"></q-btn>
            <q-btn flat size="xs" icon="edit" color="accent" style="font-weight:400" label="章节"
              @click="gotoSub(props.row,'chapter')"></q-btn>
            <q-btn flat size="xs" icon="edit" color="accent" style="font-weight:400" label="编辑"
              @click="EditCourse(props.row)" />
          </q-td>
        </template>
      </q-table>
      <course-form v-if="isShowCourseForm" :save-obj="courseObjOption" @close="closeMenuSaveForm"
        @success="refreshPage">
      </course-form>
    </div>
  </div>
</template>
<script>
  import {
    queryCoursePage,
    queryCourseCategoryTree,
  } from '../../api/course/index'
  import courseForm from '../../components/course/course-form'
  import selectTree from '../../components/common/select-tree'
  export default {
    meta: {
      title: "课程管理"
    },
    components: {
      courseForm,
      selectTree
    },
    data() {
      return {
        currentUrl: "",
        columns: [{
            name: 'CourseId',
            label: '课程编号',
            field: 'CourseId',
            align: 'left'
          },
          {
            name: 'CourseName',
            required: true,
            label: '课程名称',
            align: 'left',
            field: row => row.CourseName
          },
          {
            name: 'CateName',
            label: '所属分类',
            field: 'CateName',
            align: 'left'
          },
          {
            name: 'CoverImg',
            label: '课程封面',
            field: 'CoverImg',
            align: 'left'
          },
          {
            name: 'CourseIntro',
            label: '课程介绍',
            field: 'CourseIntro',
            align: 'left'
          },
          {
            name: 'CreateByName',
            label: '创建人',
            field: 'CreateByName',
            align: 'left'
          },
          {
            name: 'CreateTimeStr',
            label: '创建时间',
            field: 'CreateTimeStr',
            align: 'left'
          },
          {
            name: 'Status',
            label: '状态',
            align: 'left',
            field: 'Status'
          },
          {
            name: 'optioned',
            label: '操作',
            field: 'CourseId'
          }
        ],
        data: [],
        loading: true,
        ShowOpts: [{
            label: '全部',
            value: '-1'
          },
          {
            label: '正常',
            value: '0'
          },
          {
            label: '删除',
            value: '1'
          }
        ],
        msg: {
          pageIndex: 1,
          pageSize: 12,
          rowsPerPage: 12,
          CourseName: "",
          QCateIds: "",
          Status: "-1",
        },
        //课程分类树形列表
        TreeCategoryList: [],
        returnString: [], //默认值
        pageCount: 0,
        isShowCourseForm: false,
        courseObjOption: null,
      }
    },
    mounted() {
      this.getCategorytree();
      this.currentUrl = this.$route.path
      this.getcourselist();
    },
    methods: {
      gotoSub(obj, routeStr) {
        var tempStr = '/course/' + routeStr + '?CourseId=' + obj.CourseId;
        this.$router.push({
          path: tempStr
        });
      },
      //分类改变
      getChild(cateObj) {
        var tempStr = "";
        if (cateObj && cateObj != '') {
          tempStr = cateObj;
        }
        this.msg.QCateIds = tempStr;
        this.msg.pageIndex = 1;
        this.getcourselist();
      },
      getCategorytree() {
        this.TreeCategoryList = [];
        var qMsg = {}
        queryCourseCategoryTree(qMsg).then(res => {
          this.TreeCategoryList = res.Data;
        })
      },
      //重新查询
      resetSearch() {
        this.msg.pageIndex = 1;
        this.getcourselist();
      },
      //翻页
      changePage(val) {
        this.msg.pageIndex = val;
        this.getcourselist()
      },
      //获取菜单分页列表
      getcourselist() {
        this.loading = true;
        queryCoursePage(this.msg).then(res => {
          this.loading = false
          this.data = res.Data.PageData
          this.pageCount = res.Data.PageCount
        }).catch(() => {
          this.loading = false
        })
      },
      //刷新页面
      refreshPage() {
        this.isShowCourseForm = false;
        this.getcourselist();
      },
      //新增修改菜单
      EditCourse(obj) {
        if (obj) {
          this.courseObjOption = obj
        } else {
          this.courseObjOption = null
        }
        this.isShowCourseForm = true
      },
      //关闭弹窗
      closeMenuSaveForm() {
        this.isShowCourseForm = false
      }
    }
  }

</script>
<style lang="sass">
  @import url('~assets/css/table.sass');

</style>
