<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 ref="filter" filled v-model="keyWords" label="系列名称">
            <template v-slot:append>
              <q-icon v-if="keyWords !== ''" name="clear" class="cursor-pointer" @click="resetFilter" />
            </template>
          </q-input>
        </div>
      </div>
    </div>
    <div class="page-content">
      <!--<div class="q-pa-md q-gutter-sm">
        <q-tree v-if="isShow" :nodes="data" node-key="CateId" label-key="CateName" children-key="ChildList"
          :filter="keyWords" :filter-method="myFilterMethod" default-expand-all no-results-label="暂无相关数据">
          <template v-slot:default-header="prop">
            <div class="row items-center">
              <div class="text-weight-bold text-primary">{{ prop.node.CateName }}</div>
              <template v-if="prop.node.ChildList && prop.node.ChildList.length>0">
                &nbsp;
                <q-btn round icon="add" size="xs" @click="EditCategory(null)" />
              </template>
              &nbsp;
              <q-btn round icon="edit" size="xs" @click="EditCategory(prop.node)" />
            </div>
          </template>
        </q-tree>
        
      </div>-->
      <q-table :loading="loading" no-data-label="暂无相关数据" flat class="sticky-column-table"
        separator="none" :data="data" :columns="columns" row-key="name">
        <template v-slot:top="props">
          <div class="col-2 q-table__title">系列信息</div>
          <q-space />
          <div class="page-option">
            <q-btn color="accent" size="sm" class="q-mr-md" icon="add" label="新增系列" @click="EditCategory(null)" />
          </div>
        </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:body-cell-CateId="props">
          <q-td :props="props">
            <q-btn flat size="xs" icon="edit" color="accent" style="font-weight:400" label="编辑"
              @click="EditCategory(props.row)" />
            <q-btn flat size="xs" icon="delete" color="negative" style="font-weight:400;" label="删除"
              @click="deleteCategory(props.row)" />
          </q-td>
        </template>
        <template v-slot:bottom>
          <q-pagination class="full-width justify-end" v-model="msg.PageIndex" color="primary" :max="msg.PageCount"
            :input="true" @input="changePage" />
        </template>
      </q-table>
      <category-form v-if="isShowCategory" :save-obj="categoryObj" @close="closeCagegoryForm" @success="refreshPage">
      </category-form>
    </div>
  </div>
</template>

<script>
  import {
    queryCourseCategoryPage
  } from '../../api/course/index'
  import categoryForm from '../../components/course/category-form'

  export default {
    meta: {
      title: "课程系类"
    },
    components: {
      categoryForm,
    },
    data() {
      return {
        msg:{
          PageIndex:1,
          PageSize:20,
          PageCount:1
        },
        currentUrl: "",
        data: [],
        keyWords: '',
        isShowCategory: false,
        isShow: false,
        categoryObj: {},
        loading:true,
        columns: [
          {
            name: 'CateName',
            label: '系列名称',
            field: 'CateName',
            align: 'left'
          },
          {
            name: 'CourseCount',
            label: '关联课程',
            align: 'left',
            field: row => row.CourseCount,
            format: (val, row) => `${val}个课程`
          },
          {
            name: 'Status',
            label: '状态',
            align: 'left',
            field: row => row.Status
          },
          {
            name: 'UpdateByName',
            label: '修改人',
            align: 'left',
            field: row => row.UpdateByName
          },
          {
            name: 'UpdateTimeStr',
            label: '修改时间',
            align: 'left',
            field: row => row.UpdateTimeStr
          },
          {
            name: 'CateId',
            label: '操作',
            field: 'CateId'
          }
        ],
      }
    },
    
    mounted() {
      this.currentUrl = this.$route.path
      this.getcoursecategorytree()
    },
    methods: {
      changePage(val) {
        this.msg.PageIndex = val;
        this.getcoursecategorytree()
      },
       //删除系列
      deleteCategory(item) {
        let delMsg = {
          CateId: item.CateId,
        };
        this.$q.dialog({
          title: '提示信息',
          message: '是否确定删除该系列？',
          cancel: true,
          persistent: true,
          ok: "确定",
          cancel: "取消",
        }).onOk(() => {
          deleteCourseCategory(delMsg).then(res => {
            if (res.Code == 1) {
              this.$q.notify({
                icon: 'iconfont icon-chenggong',
                color: 'accent',
                timeout: 2000,
                message: '删除成功！',
                position: 'top'
              })
              this.refreshPage();
            }
          })
        }).onCancel(() => {

        });
      },
      //搜索分类
      myFilterMethod(node, filter) {
        return node.CateName && node.CateName.indexOf(filter) > -1;
      },
      //重置搜索关键词
      resetFilter() {
        this.keyWords = ''
        this.$refs.filter.focus()
      },
      //刷新页面
      refreshPage() {
        this.getcoursecategorytree();
      },
      //获取课程分类树形结构
      getcoursecategorytree() {
        this.isShow = false;
        queryCourseCategoryPage({
          PageSize:this.msg.PageSize,
          PageIndex:this.msg.PageIndex
        }).then(res => {
          this.isShow = true;
          this.loading=false;
          this.data = res.Data.PageData;
          this.msg.PageCount=res.Data.PageCount
        })
      },
      //新增修改角色
      EditCategory(obj) {
        if (obj) {
          this.categoryObj = obj
        } else {
          this.categoryObj = null
        }
        this.isShowCategory = true;
      },
      //关闭弹窗
      closeCagegoryForm() {
        this.isShowCategory = false
      }
    }
  }

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

</style>
