<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>
      <div class="page-option">
        <q-btn color="accent" class="q-mr-md" icon="add" label="新增功能权限" @click="EditMenu(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-Status="props">
          <q-td :props="props">
            <q-toggle size="md" color="primary" :false-value="1" :true-value="0" v-model="props.row.Status"
              title="注意：关闭后，分类将无法正常使用." @input="DeleteMenu(props.row)" />
          </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="EditMenu(props.row)" />
          </q-td>
        </template>
      </q-table>
      <menufunction-form v-if="isShowMenuForm" :save-obj="menuObjOption" :paramObj="MenuId" @close="closeMenuSaveForm" @success="refreshPage">
      </menufunction-form>
    </div>
  </div>
</template>

<script>
  import {
    queryMenuFunctionPage,
    saveMenuFunctionStatus
  } from '../../api/system/index'
  import menufunctionForm from '../../components/system/menufunction-form'
  export default {
    meta: {
      title: "菜单管理"
    },
    components: {
      menufunctionForm,
    },
    data() {
      return {
        currentUrl: "",
        columns: [ 
          {
            name: 'MenuName',
            required: true,
            label: '菜单名称',
            align: 'left',
            field: row => row.MenuName
          },
          {
            name: 'FunctionName',
            label: '功能名称',
            field: 'FunctionName',
            align: 'left'
          },
          {
            name: 'FunctionCode',
            label: '功能编码',
            field: 'FunctionCode',
            align: 'left'
          },
          {
            name: 'Remarks',
            label: '备注',
            field: 'Remarks',
            align: 'left'
          },
          {
            name: 'Status',
            label: '状态',
            align: 'left',
            field: 'Status'
          },
          {
            name: 'optioned',
            label: '操作',
            field: 'MenuId'
          }
        ],
        data: [],
        loading: true,
        msg: {
          pageIndex: 1,
          pageSize: 12,
          rowsPerPage: 12,
          MenuId: 0
        },
        MenuId: 0,
        pageCount: 0,
        isShowMenuForm: false,
        menuObjOption: null,
      }
    },
    mounted() {
      if (this.$route.query.menuId) {
        this.MenuId = this.$route.query.menuId;
        this.msg.MenuId = this.MenuId
      }
      this.currentUrl = this.$route.path
      this.getmenufunctionpage()
    },
    methods: {
      //重新查询
      resetSearch() {
        this.msg.pageIndex = 1;
        this.getmenufunctionpage();
      },
      //翻页
      changePage(val) {
        this.msg.pageIndex = val;
        this.getmenufunctionpage()
      },
      //获取菜单分页列表
      getmenufunctionpage() {
        this.loading = true;
        queryMenuFunctionPage(this.msg).then(res => {
          this.loading = false;
          this.data = res.Data.PageData;
          this.pageCount = res.Data.PageCount;
        }).catch(() => {
          this.loading = false
        })
      },
      //刷新页面
      refreshPage() {
        if (!this.menuObjOption) {
          this.msg.pageIndex = 1;
        }
        this.getmenufunctionpage()
      },
      //新增修改菜单
      EditMenu(obj) {
        if (obj) {
          this.menuObjOption = obj
        } else {
          this.menuObjOption = null
        }
        this.isShowMenuForm = true
      },
      //关闭弹窗
      closeMenuSaveForm() {
        this.isShowMenuForm = false
      },
      //修改菜单状态
      DeleteMenu(obj) {
        var that = this;
        var delMsg = {
          Id: obj.Id,
          Status: obj.Status
        };
        var tipMsg = "";
        if (delMsg.Status == 0) {
          tipMsg = "是否启用【" + obj.FunctionName + "】菜单?";
        } else {
          tipMsg = "是否禁用【" + obj.FunctionName + "】菜单?";
        }
        this.$q.dialog({
          title: '提示信息',
          message: tipMsg,
          cancel: true,
          persistent: true,
          ok: "确定",
          cancel: "取消",
        }).onOk(() => {
          saveMenuFunctionStatus(delMsg).then(res => {
            if (res.Code == 1) {
              that.$q.notify({
                icon: 'iconfont icon-chenggong',
                color: 'accent',
                timeout: 2000,
                message: '操作成功',
                position: 'top'
              })
              that.getmenufunctionpage();
            }
          })
        }).onCancel(() => {
          obj.Status = obj.Status == 1 ? 0 : 1;
        });
      }
    }
  }

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

</style>
