<template>
    <div style="padding:10px">
        <q-card inline class="fit shadow-6">
            <div>
              <!-- <a-button type="primary">hello world</a-button> -->
            </div>
            <q-card-main style="height:80%">
                <q-table ref="table" color="primary" :data="serverData" :columns="columns" separator="cell" selection="none" 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.namezh" type="text" :prefix="$t('Role name') + '：'" />&nbsp;&nbsp;
                        <q-input v-model="filter.name" type="text" :prefix="$t('Role code') + '：'" />&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('User list')" @click="editRoleUser(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="closeEditModal" icon="reply" />
                    <q-toolbar-title>
                      {{$t('Editing role')}}&nbsp;
                        <q-chip small>{{roleName}}</q-chip>&nbsp;{{$t('User under')}}
                    </q-toolbar-title>
                </q-toolbar>
                <q-toolbar slot="footer">
                    <q-toolbar-title>
                    </q-toolbar-title>
                    <q-btn round   color="red"  @click="modifyRoleUser">{{$t('Save')}}</q-btn>
                    <q-btn round @click="closeEditModal">{{$t('Cancel')}}</q-btn>
                </q-toolbar>
                <!--  <div style="padding:20px">
                    <q-table ref="usertable" color="primary" :data="roleUser.serverData" :columns="roleUser.columns" separator="cell" row-key="id" :pagination.sync="roleUser.serverPagination"
                            @request="roleUserRequest" :loading="roleUser.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="roleUser.filter.account" type="text" :prefix="$t('Account') + '：'" />&nbsp;&nbsp;
                            <q-input v-model="roleUser.filter.username" type="text" :prefix="$t('Name') + '：'" />&nbsp;&nbsp;
                            <q-btn push dense color="primary" icon="search" @click="searchRoleUser">{{$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="secondary" label='新增' @click="modifyRoleUser(props.value,1)"></q-btn>
                            <q-btn v-if="props.row.isAdd==1" glossy color="secondary" label='删除' @click="modifyRoleUser(props.value,0)"></q-btn>
                        </q-td>
                    </q-table>
                </div>-->
          
            <!-- 选择个人 -->
              <div class=" row q-mb-lg q-ml-lg q-mr-lg" >
                  <div class="col-2 q-ml-lg" style="height:80vh;overflow:auto;">
                  </div>
                  <div class="col-4 q-mt-lg" style="height:80vh;overflow:auto;background-color: cornsilk;">
                      <organizationAnt  v-model="orgList" @staffNode="staffNode" />
                  </div>
                  <div class="col-4 q-mt-lg" style="height:80vh;overflow:auto;background-color: cornsilk;" >
                      <div class="q-mb-lg q-mt-lg">
                          显示已选择的成员，共{{selectStaff.length}}人
                      </div>
                      <div v-if="!selectStaff || selectStaff.length ===0" class="text-grey-5">
                          没有选择任何成员
                      </div>
                      <div v-else >
                          <div class="row shadow-1" v-for="node in selectStaff" :key="node.id"  >
                              <div class="col" style="display:flex;align-items: center;">
                                  <q-icon name="person" size="16px"/>
                                  <div>
                                  {{' '+node.title}}&nbsp;<span style="color: #64b5fe">{{ node.no  ? node.no:'' }}</span>
                                  </div>
                              </div>
                              <div class="col">
                                  <q-btn class=" float-right" flat color="primary" icon="clear" @click="deleteNode(node)"/>
                              </div>
                          </div>
                      </div>
                  </div>
              </div>
            </q-modal-layout> 
        </q-modal>
    </div>
</template>
<script>
import { getRolePagedList } from "@/service/permission/role";
import { getUserRoleList, editRoleUser,editUserRoleList} from "@/service/user/user";
import organizationAnt from '@/components/organization-ant'
export default {
  components: {organizationAnt},
  // name: "roleuser",
  data() {
    return {
      orgList:[],//选中的树节点
      selectStaff:[],//选中的人
      treeKey:[],
      resdata:[],
      expandedKeys: [],
      autoExpandParent: true,
      checkedKeys: [],
      selectedKeys: [],
      namefilter:"",
      leaderSelected: [],
      leaderList: [],
      tempFunction:{
          contactList:[{}],
      },
      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",
          sortable: true
        },
        {
          name: "name",
          label: this.$t("Role code"),
          field: "name",
          sortable: true,
          align: "left",
        },
        {
          name: "id",
          required: true,
          label: this.$t("ID"),
          align: "left",
          field: "id"
        }
      ],
      filter: {
        name: "",
        namezh: ""
      },
      loading: false,
      editModal: false,
      roleId: 0,
      roleName: "",
      roleUser: {
        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",
          sortable: true
        },
        {
          name: "account",
          label: this.$t("Account"),
          field: "account",
          sortable: true,
          align: "left"
        },
        {
          name: "email",
          label: this.$t("Email"),
          field: "email",
          sortable: true,
          align: "left"
        },
        {
          name: "phoneNumber",
          label: this.$t("Phone"),
          field: "phoneNumber",
          sortable: true,
          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: "",
          roleId: ""
        },
        loading: false
      }
    };
  },
  methods: {
    staffNode(nodeList) {//组织机构树人员选择后组件的回调方法
        this.selectStaff = nodeList
    },
    // checkStaffNode(nodeList){//组织机构树人员选择后组件的回调方法-返回选中的node
    //     this.selectStaff = nodeList
    // },
    // checkStaffKey(keyList){//组织机构树人员选择后组件的回调方法-返回key
    //     this.selectStaff = keyList
    // },
    //  删除节点事件
    deleteNode(node) {
      if (!node) {
        return false;
      }
      var staff = this.selectStaff.filter(staff => staff.key !== node.key);
      this.selectStaff = staff;
      this.orgList = this.selectStaff.map(item=>item.key)
    },
    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,
        name: this.filter.name,
        namezh: this.filter.namezh
      };
      let dataRes = await getRolePagedList(query);
      let data = dataRes.data.data;
      this.serverPagination.rowsNumber = data.total;
      this.serverData = data.list;
      setTimeout(() => {
        this.loading = false;
      }, 500);
    },
    async editRoleUser(props) {
      this.roleId = props.value;
      this.roleName = props.row.namezh;
      this.editModal = true;
      let query = { role: this.roleId};
      let dataRes = await getUserRoleList(query);
      let data = dataRes.data.data;
      this.orgList = data.map(item=>item.userId);
    },
    closeEditModal(){//关闭弹层，清空树
      this.editModal=false
      this.orgList = []
    },
    search() {
      this.request({
        pagination: this.serverPagination,
        filter: this.filter
      });
    },
    searchRoleUser() {
      this.roleUserRequest({
        pagination: this.roleUser.serverPagination,
        filter: this.roleUser.filter
      });
    },
    async modifyRoleUser() {//保存用户角色信息
    let userIds = this.selectStaff.map(item=>item.key).join(",")
    let res = await editUserRoleList({
        roleId: this.roleId,
        userIds: userIds
      });

    console.log(res)

      this.$q.notify({
        type: "positive",
        message: this.$t("Added successfully"),
        position: "bottom-right"
      });
  
    }
  },
  mounted() {
    this.request({
      pagination: this.serverPagination,
      filter: this.filter
    });
  }
};
</script>
