<template>
    <div style="padding:10px">
        <q-card inline class="fit shadow-6">
            <q-card-main style="height:80%">
                <q-table ref="table" color="primary" :data="serverData" :columns="columns" separator="cell" row-key="id" :rows-per-page-options="[5,10,20,30,40,50,60,200,500]" :pagination.sync="serverPagination" @request="request"
                         :loading="loading" :rows-per-page-label="$t('Rows per page')" :no-data-label="$t('No data')">
                    <template slot="top-left" slot-scope="props">
                        <q-input v-model="filter.account" type="text" :prefix="$t('Account') + '：'" />&nbsp;&nbsp;
                        <q-input v-model="filter.username" type="text" :prefix="$t('Name') + '：'" />&nbsp;&nbsp;
                        <q-btn push dense color="primary" icon="search" @click="search">{{$t('Search')}}</q-btn>&nbsp;&nbsp;
                    </template>
                    <template slot="top-right" slot-scope="props">
                        <q-btn flat round dense :icon="props.inFullscreen ? 'fullscreen_exit' : 'fullscreen'" @click="props.toggleFullscreen" />
                    </template>
                    <q-td slot="body-cell-id" slot-scope="props" :props="props" style="width:150px">
                        <q-btn glossy color="secondary" :label="$t('Role list')" @click="editUserRole(props)"></q-btn>
                    </q-td>
                </q-table>
            </q-card-main>
        </q-card>
        <q-modal v-model="editModal" maximized>
            <q-modal-layout>
                <q-toolbar slot="header">
                    <q-btn flat round dense @click="editModal = false" icon="reply" />
                    <q-toolbar-title>
                      {{$t('Edit user')}}&nbsp;
                        <q-chip small>{{userName}}</q-chip>&nbsp;{{$t('List of roles')}}
                    </q-toolbar-title>
                </q-toolbar>
                <div style="padding:20px">
                    <q-table ref="usertable" color="primary" :data="userRole.serverData" :columns="userRole.columns" separator="cell" row-key="id" :pagination.sync="userRole.serverPagination"
                             @request="userRoleRequest" :loading="userRole.loading" :rows-per-page-label="$t('Rows per page')" :no-data-label="$t('No data')">
                        <template slot="top-left" slot-scope="props">
                            <q-input v-model="userRole.filter.name" type="text" :prefix="$t('Role name') + '：'" />&nbsp;&nbsp;
                            <q-input v-model="userRole.filter.code" type="text" :prefix="$t('Role code') + '：'" />&nbsp;&nbsp;
                            <q-btn push dense color="primary" icon="search" @click="searchUserRole">{{$t('Search')}}</q-btn>&nbsp;&nbsp;
                        </template>
                        <template slot="top-right" slot-scope="props">
                            <q-btn flat round dense :icon="props.inFullscreen ? 'fullscreen_exit' : 'fullscreen'" @click="props.toggleFullscreen" />
                        </template>
                        <q-td slot="body-cell-id" slot-scope="props" :props="props" style="width:100px">
                            <q-btn v-if="props.row.isAdd==2" glossy color="black" label="新增" @click="modifyUserRole(props.value,1)"></q-btn>
                            <q-btn v-if="props.row.isAdd==1" glossy color="secondary" label="删除" @click="delUserRole(props)"></q-btn>
                        </q-td>
                    </q-table>
                </div>
            </q-modal-layout>
        </q-modal>
    </div>
</template>

<script>
import { getUserPagedList,editRoleUser,delUserRole } from "@/service/user/user";
import { getRoleWithUserPagedList } from "@/service/permission/role";
export default {
  name: "userrole",
  data() {
    return {
      serverData: [],
      serverPagination: {
        page: 1,
        rowsNumber: 0, // specifying this determines pagination is server-side
        rowsPerPage: 10 // current rows per page being displayed
      },
      columns: [
        {
          name: "username",
          required: true,
          label: this.$t("Name"),
          align: "left",
          field: "username"
        },
        {
          name: "account",
          label: this.$t("Account"),
          field: "account",
          align: "left"
        },
        {
          name: "email",
          label: this.$t("Email"),
          field: "email",
          align: "left"
        },
        {
          name: "phoneNumber",
          label: this.$t("Phone"),
          field: "phoneNumber",
          align: "left"
        },
        {
          name: "position",
          required: true,
          label: this.$t("Position"),
          align: "left",
          field: "position"
        },
        {
          name: "id",
          required: true,
          label: this.$t("ID"),
          align: "left",
          field: "id"
        }
        
      ],
      filter: {
        account: "",
        username: ""
      },
      loading: false,
      editModal: false,
      userId: "",
      userName: "",
      userRole: {
        serverData: [],
        serverPagination: {
          page: 1,
          rowsNumber: 0, // specifying this determines pagination is server-side
          rowsPerPage: 10 // current rows per page being displayed
        },
        columns: [
          {
            name: "namezh",
            required: true,
            label: this.$t("Role name"),
            align: "left",
            field: "namezh"
          },
          {
            name: "name",
            label: this.$t("Role code"),
            field: "name",
            align: "left"
          },
          {
            name: "id",
            required: true,
            label: this.$t("ID"),
            align: "left",
            field: "id"
          }
        ],
        filter: {
          namezn: "",
          name: "",
          userId: ""
        },
        loading: false
      }
    };
  },
  methods: {
    async request(props) {
      this.loading = true;
      this.serverPagination = props.pagination;
      let table = this.$refs.table,
        { page, rowsPerPage, sortBy, descending } = props.pagination;
      let query = {
        pageNum:  page,
        pageSize: rowsPerPage,
        sortBy: sortBy,
        descending: descending,
        account: this.filter.account,
        username: this.filter.username
      };
      let dataRes = await getUserPagedList(query);
      let data = dataRes.data.data;
      this.serverPagination.rowsNumber = data.total;
      this.serverData = data.list;
      
      setTimeout(() => {
        this.loading = false;
      }, 500);
    },
    editUserRole(props) {
      this.userId = props.value;
      this.userName = props.row.name;
      this.userRole.filter = {
        namezh: "",
        name: "",
        userId: props.value
      };
      this.editModal = true;
      this.searchUserRole();
    },
    async userRoleRequest(props) {
      this.userRole.loading = true;
      this.userRole.serverPagination = props.pagination;
      let table = this.$refs.table,
        { page, rowsPerPage, sortBy, descending } = props.pagination;
      let query = {
        pageNum:  page,
        pageSize: rowsPerPage,
        sortBy: sortBy,
        descending: descending,
        //filter: this.userRole.filter
        namezh: this.userRole.filter.namezn,
        name: this.userRole.filter.name,
        userId: this.userRole.filter.userId
      };
      let dataRes = await getRoleWithUserPagedList(query);
      let data = dataRes.data.data;
      this.userRole.serverPagination.rowsNumber = data.total;
      
       
      this.userRole.serverData = data.list;
      
      setTimeout(() => {
        this.userRole.loading = false;
      }, 500);
    },

    search() {
      this.request({
        pagination: this.serverPagination,
        filter: this.filter
      });
    },
    searchUserRole() {
      this.userRoleRequest({
        pagination: this.userRole.serverPagination,
        filter: this.userRole.filter
      });
    },
    async modifyUserRole(roleId,action) {
      await editRoleUser({
        roleId: roleId,
        userId: this.userId,
        action: action
      });
      if (action == 1) {
        this.$q.notify({
          type: "positive",
          message: this.$t("Added successfully"),
          position: "bottom-right"
        });
      } else {
        this.$q.notify({
          type: "positive",
          message: this.$t("Successfully removed"),
          position: "bottom-right"
        });
      }
      this.searchUserRole();
    },
    async delUserRole(val){
    	 await delUserRole({
         roleId: val.row.id,
        userId: this.userId,
        action: 0
      });
        this.searchUserRole();
    }
    
  },
  mounted() {
    this.request({
      pagination: this.serverPagination,
      filter: this.filter
    });
  }
};
</script>
