<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 filled v-model="msg.SubjectName" 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>
          <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-SubjectIcon="props">
          <q-td :props="props">
            <img v-if="props.row.SubjectIcon" :src="props.row.SubjectIcon" style="width:40px;height:40px;" alt="" />
          </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:body-cell-Id="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="deleteSubject(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>
      <subject-form v-if="isShowSubject" :save-obj="subjectObj" @close="closeSubjectForm" @success="refreshPage">
      </subject-form>
    </div>
  </div>
</template>

<script>
  import {
    queryCourseSubjectPage,
    deleteCourseSubject,
  } from '../../api/course/index'
  import subjectForm from '../../components/course/subject-form'

  export default {
    meta: {
      title: "课程科目"
    },
    components: {
      subjectForm
    },
    data() {
      return {
        msg: {
          PageIndex: 1,
          PageSize: 10,
          rowsPerPage: 10,
          PageCount: 1,
          SubjectName: "", //科目名称
        },
        dataList: [],
        isShowSubject: false,
        subjectObj: {},
        loading: true,
        columns: [{
            name: 'SubjectName',
            label: '科目名称',
            field: 'SubjectName',
            align: 'left'
          }, {
            name: 'SubjectIcon',
            label: '图标',
            field: 'SubjectIcon',
            align: 'left'
          },
          {
            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: 'Id',
            label: '操作',
            field: 'Id'
          }
        ],
      }
    },

    mounted() {
      this.getSubjectPage();
    },
    methods: {
      //重新查询
      resetSearch() {
        this.msg.PageIndex = 1;
        this.getSubjectPage();
      },
      changePage(val) {
        this.msg.PageIndex = val;
        this.getSubjectPage()
      },
      //删除系列
      deleteSubject(item) {
        let delMsg = {
          Id: item.Id,
        };
        this.$q.dialog({
          title: '提示信息',
          message: '是否确定删除该科目？',
          cancel: true,
          persistent: true,
          ok: "确定",
          cancel: "取消",
        }).onOk(() => {
          deleteCourseSubject(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.getSubjectPage();
      },
      //获取课程系列分页列表
      getSubjectPage() {
        this.loading = true;
        queryCourseSubjectPage(this.msg).then(res => {
          this.loading = false;
          if(res.Code==1){
            this.dataList = res.Data.PageData;
            this.msg.PageCount = res.Data.PageCount
          }
        })
      },
      //新增修改课程系列
      EditCategory(obj) {
        if (obj) {
          this.subjectObj = obj
        } else {
          this.subjectObj = null
        }
        this.isShowSubject = true;
      },
      //关闭弹窗
      closeSubjectForm() {
        this.isShowSubject = false
      }
    }
  }
</script>
<style lang="sass">
  @import url('~assets/css/table.sass')
</style>