<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 @input="resetSearch" clearable standout="bg-primary text-white" v-model="msg.CateName" label="系列名称"
            @clear="resetSearch" maxlength="20" />
        </div>
      </div>
    </div>
    <div class="page-content">
      <q-table :pagination="msg" :loading="loading" no-data-label="暂无相关数据" flat class="sticky-column-table"
        separator="none" :data="dataList" :columns="columns">
        <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,
    deleteCourseCategory
  } from '../../api/course/index'
  import categoryForm from '../../components/course/category-form'

  export default {
    meta: {
      title: "课程系类"
    },
    components: {
      categoryForm,
    },
    data() {
      return {
        msg: {
          PageIndex: 1,
          PageSize: 10,
          rowsPerPage: 10,
          PageCount: 1,
          CateName: "", //系列名称
        },
        currentUrl: "",
        dataList: [],
        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.getcategorypage()
    },
    methods: {
      //重新查询
      resetSearch() {
        this.msg.PageIndex = 1;
        this.getcategorypage();
      },
      changePage(val) {
        this.msg.PageIndex = val;
        this.getcategorypage()
      },
      //删除系列
      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(() => {

        });
      },
      //刷新页面
      refreshPage() {
        this.getcategorypage();
      },
      //获取课程系列分页列表
      getcategorypage() {
        this.isShow = false;
        queryCourseCategoryPage(this.msg).then(res => {
          this.isShow = true;
          this.loading = false;
          this.dataList = 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>
