<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 class="page-option">
        <q-btn color="accent" class="q-mr-md" icon="add" label="新增分类" @click="EditCategory(null)" />
      </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>
      <category-form v-if="isShowCategory" :save-obj="categoryObj" @close="closeCagegoryForm" @success="refreshPage">
      </category-form>
    </div>
  </div>
</template>

<script>
  import {
    queryCourseCategoryTree
  } from '../../api/course/index'
  import categoryForm from '../../components/course/category-form'
  export default {
    meta: {
      title: "课程分类"
    },
    components: {
      categoryForm,
    },
    data() {
      return {
        currentUrl: "",
        data: [],
        keyWords: '',
        isShowCategory: false,
        isShow: false,
        categoryObj: {},
      }
    },
    mounted() {
      this.currentUrl = this.$route.path
      this.getcoursecategorytree()
    },
    methods: {
      //搜索分类
      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;
        queryCourseCategoryTree({}).then(res => {
          this.isShow = true;
          this.data = res.Data;
        })
      },
      //新增修改角色
      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>
