<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 @change="resetSearch" clearable standout="bg-primary text-white" v-model="msg.RoleName"
            label="角色名称" />
        </div>
        <div class="col-3">
          <q-select @input="resetSearch" standout="bg-primary text-white" v-model="msg.Status" :options="ShowOpts"
            emit-value map-options label="状态" />
        </div>
      </div>
      <div class="page-option">
        <q-btn color="accent" class="q-mr-md" icon="add" label="新增角色" @click="EditRole(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-td :props="props">
              <q-toggle size="md" color="primary" :false-value="1" :true-value="0" v-model="props.row.Status"
                title="注意：关闭后，分类将无法正常使用." @input="DeleteRole(props.row)" />
            </q-td>
          </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="EditRole(props.row)" />
          </q-td>
        </template>
      </q-table>
      <role-form v-if="isShowRoleForm" :save-obj="roleObjOption" @close="closeroleSaveForm" @success="refreshPage">
      </role-form>
    </div>
  </div>
</template>

<script>
  import {
    queryRolePage,
    setRoleStatus
  } from '../../api/system/index'
  import roleForm from '../../components/system/role-form'
  export default {
    meta: {
      title: "角色管理"
    },
    components: {
      roleForm,
    },
    data() {
      return {
        currentUrl: "",
        columns: [{
            name: 'RoleId',
            label: '角色编号',
            field: 'RoleId',
            align: 'left'
          },
          {
            name: 'RoleName',
            required: true,
            label: '角色名称',
            align: 'left',
            field: row => row.RoleName
          },
          {
            name: 'RoleIntro',
            label: '描述',
            field: 'RoleIntro',
            align: 'left'
          },
          {
            name: 'CreateByName',
            label: '创建人',
            field: 'CreateByName',
            align: 'left'
          },
          {
            name: 'CreateDateStr',
            label: '创建时间',
            field: 'CreateDateStr',
            align: 'left'
          },
          {
            name: 'Status',
            label: '状态',
            align: 'left',
            field: 'Status'
          },
          {
            name: 'optioned',
            label: '操作',
            field: 'RoleId'
          }
        ],
        data: [],
        loading: true,
        ShowOpts: [{
            label: '全部',
            value: '-1'
          },
          {
            label: '正常',
            value: '0'
          },
          {
            label: '删除',
            value: '1'
          }
        ],
        msg: {
          pageIndex: 1,
          pageSize: 12,
          rowsPerPage: 12,
          RoleName: "",
          Status: "-1",
        },
        pageCount: 0,
        isShowRoleForm: false,
        roleObjOption: null,
      }
    },
    mounted() {
      this.currentUrl = this.$route.path
      this.getRolelist()
    },
    methods: {
      //重新查询
      resetSearch() {
        this.msg.pageIndex = 1;
        this.getRolelist();
      },
      //翻页
      changePage(val) {
        this.msg.pageIndex = val;
        this.getRolelist()
      },
      //获取菜单分页列表
      getRolelist() {
        this.loading = true;
        queryRolePage(this.msg).then(res => {
          this.loading = false
          this.data = res.Data.PageData
          this.pageCount = res.Data.PageCount
        }).catch(() => {
          this.loading = false
        })
      },
      //刷新页面
      refreshPage() {
        if (!this.roleObjOption) {
          this.msg.pageIndex = 1;
          this.msg.RoleName = "";
          this.msg.Status = "-1";
        }
        this.getRolelist()
      },
      //新增修改角色
      EditRole(obj) {
        if (obj) {
          this.roleObjOption = obj
        } else {
          this.roleObjOption = null
        }
        this.isShowRoleForm = true
      },
      //关闭弹窗
      closeroleSaveForm() {
        this.isShowRoleForm = false
      },
      //修改角色状态
      DeleteRole(obj) {
        var that = this;
        var delMsg = {
          RoleId: obj.RoleId,
          Status: obj.Status
        };
        var tipMsg = "";
        if (delMsg.Status == 0) {
          tipMsg = "是否启用【" + obj.RoleName + "】角色?";
        } else {
          tipMsg = "是否禁用【" + obj.RoleName + "】角色?";
        }
        this.$q.dialog({
          title: '提示信息',
          message: tipMsg,
          cancel: true,
          persistent: true,
          ok: "确定",
          cancel: "取消",
        }).onOk(() => {
          setRoleStatus(delMsg).then(res => {
            if (res.Code == 1) {
              that.$q.notify({
                icon: 'iconfont icon-chenggong',
                color: 'accent',
                timeout: 2000,
                message: '操作成功',
                position: 'top'
              })
              that.getRolelist();
            }
          })
        }).onCancel(() => {
          obj.Status = obj.Status == 1 ? 0 : 1;
        });
      }
    }
  }

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

</style>
