<template>
	<div style="padding:10px" v-permission="['user_view']">
		<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" selection="multiple" :selected.sync="selected" row-key="id" :pagination.sync="serverPagination" @request="request" :loading="loading" :rows-per-page-label="$t('Rows per page')" :rows-per-page-options="[5,10,20,30,40,50,60,200,500]" :no-data-label="$t('No data')">
					<template slot="top-left" slot-scope="props">
						<q-input v-model="filter.username" type="text" prefix="用户名：" />&nbsp;&nbsp;
						<q-input v-model="filter.account" type="text" prefix="工号：" />&nbsp;&nbsp;
						<q-input v-model="filter.phoneNumber" type="text" prefix="手机：" />&nbsp;&nbsp;
							<q-select v-model="filter.delstatus" :options="yesnokindlist" prefix="异常：" style="width:160px" clearable />&nbsp;&nbsp;
						<q-select v-model="centerid" :options="centerlist" prefix="中心：" style="width:160px" clearable />&nbsp;&nbsp;
						<q-select v-model="departmentid" :options="departmentlist" prefix="部门：" style="width:160px" clearable />&nbsp;&nbsp;
						<q-btn push dense color="primary" icon="search" @click="search">{{$t('Search')}}</q-btn>&nbsp;&nbsp;
						<q-btn push dense color="primary" icon="add" @click="addUser">{{$t('Add')}}</q-btn>&nbsp;&nbsp;
						<q-btn push dense color="primary" icon="swap horiz" @click="updateconfim">微信人员更新拉取</q-btn>&nbsp;&nbsp;
						<!--<q-btn push dense color="primary" icon="swap horiz" @click="updateconfim2">微信人员新增拉取</q-btn>&nbsp;&nbsp;-->
						<!--<q-btn push dense color="primary" icon="swap horiz" @click="updateconfim3">校正部门信息</q-btn>&nbsp;&nbsp;-->
						<!-- <q-btn push dense color="primary" icon="swap horiz" @click="getDeltetList">异常用户筛查</q-btn>&nbsp;&nbsp; -->
						<!-- <q-btn push dense color="primary" icon="swap horiz" @click="getweixinisleaderindeptByaccount_user">同步微信上下级</q-btn> -->
					</template>
					<template slot="top-right" slot-scope="props">
						<q-btn flat round dense :icon="props.inFullscreen ? 'fullscreen_exit' : 'fullscreen'" @click="props.toggleFullscreen" />
					</template>
					<template slot="top-selection" slot-scope="props">
						<!-- <q-btn color="negative" flat round delete icon="delete" @click="delUsers" /> -->
						<q-btn icon="create" color="positive" label="启用" @click="delUsers_user(1)" />&nbsp;&nbsp;
						<q-btn icon="create" color="negative" label="停用" @click="delUsers_user(0)" />
					</template>
					<q-td slot="body-cell-id" slot-scope="props" :props="props" style="width:100px">
						<!-- <q-btn
              small
              round
              push
              glossy
              dense
              icon="edit"
              color="primary"
              @click="editUser(props.value)"
              v-permission="['user_edit_btn']"
            ></q-btn>
            <q-btn
              small
              round
              push
              glossy
              dense
              icon="delete"
              color="red"
              @click="delUser(props.value)"
              v-permission="['user_del_btn']"
            ></q-btn> -->
						<q-btn-dropdown color="secondary" label="操作" no-wrap>
							<q-list link>
								<q-item v-close-overlay @click.native="editUser(props.value)" v-permission="['user_edit_btn']">
									<q-item-main>
										<q-item-tile sublabel>修改</q-item-tile>
									</q-item-main>
								</q-item>
								<q-item v-close-overlay @click.native="editUserFromwx(props.value)" v-permission="['user_edit_btn']">
									<q-item-main>
										<q-item-tile sublabel>获取微信信息</q-item-tile>
									</q-item-main>
								</q-item>
								<q-item v-close-overlay @click.native="resetPassword_user(props.value)" v-permission="['user_del_btn']">
									<q-item-main>
										<q-item-tile sublabel>重置密码</q-item-tile>
									</q-item-main>
								</q-item>
								<q-item v-close-overlay @click.native="delUser(props.value)" v-permission="['user_del_btn']">
									<q-item-main>
										<q-item-tile sublabel>删除</q-item-tile>
									</q-item-main>
								</q-item>
								<q-item v-close-overlay>
									<q-item-main>
										<q-item-tile sublabel>关闭弹窗</q-item-tile>
									</q-item-main>
								</q-item>
							</q-list>
						</q-btn-dropdown>
					</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 v-if="subtype=='edit'">{{$t('Edit')}}</q-toolbar-title>
					<q-toolbar-title v-if="subtype=='add'">{{$t('Add')}}</q-toolbar-title>
				</q-toolbar>
				<q-toolbar slot="footer">
					<q-toolbar-title></q-toolbar-title>
					<q-btn round color="red" @click="saveUser_user_nopassword" v-if="subtype=='edit'">{{$t('Save')}}</q-btn>
					<q-btn round color="red" @click="saveUser_user" v-if="subtype=='add'">{{$t('Add')}}</q-btn>
					<q-btn round @click="editModal = false">{{$t('Cancel')}}</q-btn>
				</q-toolbar>

				<div class="layout-padding">
					<q-field :count="15" class="liststype">
						<q-input v-model="tempUser.account" :float-label="$t('Account')"  readonly disabled/>
					</q-field>
					<q-field :count="20" class="liststype">
						<q-input v-model="tempUser.username" float-label="用户名" />
					</q-field>
					<q-field :count="20" class="liststype" v-if="subtype=='add'">
						<q-input v-model="tempUser.password" type="password" :float-label="$t('Password')" />
					</q-field>

					<q-field :count="30" class="liststype">
						<q-input v-model="tempUser.email" :float-label="$t('Email')" />
					</q-field>
					<q-field :count="13" class="liststype">
						<q-input v-model="tempUser.phoneNumber" type="number" float-label="手机" />
					</q-field>
					<q-field class="liststype">
						<q-select v-model="tempUser.sex" :options="sexkindlist" float-label="性别" />
					</q-field>
					<q-field :count="13" class="liststype">
						<q-datetime v-model="tempUser.birthday" type="date" float-label="生日" />
					</q-field>
					<q-field :count="13" class="liststype">
						<q-input v-model="tempUser.telNumber" float-label="座机号码" />
					</q-field>
					<!-- <q-field :count="13" class="liststype">
            <q-input v-model="tempUser.nickname" float-label="微信昵称" />
          </q-field>-->
					<!-- <q-field :count="13" class="liststype">
            <q-input v-model="tempUser.avatar" float-label="头像" />
          </q-field>-->
					<q-field :count="13" class="liststype">
						<q-input v-model="tempUser.address" float-label="地址" />
					</q-field>
					<q-field :count="13" class="liststype">
						<q-input v-model="tempUser.mobile" float-label="移动小号" />
					</q-field>
					<q-field :count="13" class="liststype">
						<q-input v-model="tempUser.telecom" float-label="电信小号" />
					</q-field>
					<q-field :count="13" class="liststype">
						<q-input v-model="tempUser.office" float-label="办公地点" />
					</q-field>

					<q-field :count="13" class="liststype">
						<q-input v-model="tempUser.position" float-label="职务" />
					</q-field>
					<q-field :count="13" class="liststype">
						<q-input v-model="tempUser.identity" float-label="身份" />
					</q-field>
					<q-field :count="13" class="liststype">
						<q-input v-model="tempUser.departmentname" float-label="部门" />
						<q-btn class="bg-brown-5 text-white" label="选择部门" @click="selectModule" />
					</q-field>
					<!-- <q-field :count="13" class="liststype">
            <q-input v-model="tempUser.rolename" float-label="角色" />
            <q-btn class="bg-brown-5 text-white" label="选择角色" @click="selectroleModule" />
          </q-field>-->
					<q-field :count="13" class="liststype">
						<q-input v-model="tempUser.fuzename" float-label="负责部门" />
						<q-btn class="bg-brown-5 text-white" label="选择负责部门" @click="selectroleModule" />
					</q-field>
					<!-- <q-field :count="13" class="liststype">
            <q-datetime v-model="tempUser.addtime" type="date" float-label="生日" />
          </q-field>-->
					<q-field :count="13" class="liststype">
						<q-input v-model="tempUser.credit" float-label="信用分" />
					</q-field>
					<q-field :count="13" class="liststype">
						<q-input v-model="tempUser.political" float-label="政治面貌" />
					</q-field>
					<!-- <q-field :count="13" class="liststype">
            <q-input v-model="tempUser.auth" float-label="角色权限" />
          </q-field>-->
					<!-- <q-field :count="13" class="liststype">
            <q-input v-model="tempUser.appId" float-label="应用权限" />
          </q-field>-->
					<q-field :count="13" class="liststype">
						<q-select v-model="tempUser.paizhu" :options="yesnokindlist" float-label="派驻" />
					</q-field>

					<!-- <q-field class="liststype">
            <q-select v-model="tempUser.synchronous" :options="yeornoOptions" float-label="微信同步" />
          </q-field>-->
					<!-- <q-field class="liststype">
            <q-select v-model="tempUser.isadmin" :options="yesnokindlist" float-label="管理员" />
          </q-field>-->

					<q-field class="liststype">
						<q-select v-model="tempUser.status" :options="yesnokindlist" float-label="启用" />
					</q-field>
				</div>
			</q-modal-layout>
		</q-modal>
		<!-- 所属部门 -->
		<q-modal v-model="menuModal" maximized>
			<q-modal-layout>
				<q-toolbar slot="header">
					<q-btn flat round dense @click="menuModal = false" icon="reply" />
					<q-toolbar-title>{{$t('Selection module')}}</q-toolbar-title>
				</q-toolbar>
				<q-toolbar slot="footer">
					<q-toolbar-title></q-toolbar-title>
					<q-btn round :disable="!menuSelected" @click="moduleChange">{{$t('Select')}}</q-btn>
					<q-btn round color="red" @click="menuModal = false">{{$t('Cancel')}}</q-btn>
				</q-toolbar>

				<div class="layout-padding">
					<q-tree :nodes="menuList" default-expand-all :ticked.sync="menuSelected" node-key="id" />
				</div>
				<!-- <div class="layout-padding">
                    <q-tree :nodes="menuList" default-expand-all :ticked.sync="roleSelected" node-key="id"  />
        </div>-->
			</q-modal-layout>
		</q-modal>
		<!-- 负责部门 -->
		<q-modal v-model="roleModal" maximized>
			<q-modal-layout>
				<q-toolbar slot="header">
					<q-btn flat round dense @click="roleModal = false" icon="reply" />
					<q-toolbar-title>{{$t('Selection module')}}</q-toolbar-title>
				</q-toolbar>
				<q-toolbar slot="footer">
					<q-toolbar-title></q-toolbar-title>
					<q-btn round :disable="!roleSelected" @click="rolemoduleChange">{{$t('Select')}}</q-btn>
					<q-btn round color="red" @click="roleModal = false">{{$t('Cancel')}}</q-btn>
				</q-toolbar>

				<div class="layout-padding">
					<q-tree :nodes="roleList" default-expand-all :ticked.sync="roleSelected" node-key="id" />
				</div>
			</q-modal-layout>
		</q-modal>
	</div>
</template>

<script>
	import {
		editUserFromwx,
		getUserPagedList,
		synchronousDept,
		delUser,
		delUsers,
		saveUser,
		resetPassword,
		updateUsersStatus,
		synchronousAvatar,
		synchronousPersonal,
		saveUserNoPassword,
		getweixinisleaderindeptByaccount,
		updateRedis,
		getDeltetList_user,
		getUserListByPid //根据pid获取部门信息
	} from "@/service/user/user";
	import { getDeptAllMenu } from "@/service/department/dept_menu";
	import { getSysCodelistByKindcode } from "@/service/syscodelist/syscodelist";
	export default {
		name: "user",
		data() {
			return {
				serverData: [],
				menuModal: false,
				tempFunction: {
					id: 0,
					name: "",
					moduleId: 0,
					module: "",
					code: "",
					description: ""
				},
				menuList: [],
				roleList: [],
				menuSelected: [],
				roleSelected: [],
				menuModal: false,
				roleModal: false,
				serverPagination: {
					page: 1,
					rowsNumber: 0, // specifying this determines pagination is server-side
					rowsPerPage: 10, // current rows per page being displayed

				},
				centerid: "",
				departmentid: "",
				yesnokindlist: [],
				sexkindlist: [],
				centerlist: [], //中心list
				departmentlist: [], //部门
				columns: [{
						name: "account",
						label: "工号",
						field: "account",
						sortable: false,
						align: "center"
					},
					{
						name: "username",
						required: true,
						label: "用户",
						align: "center",
						field: "username",
						sortable: false
					},
					{
						name: "phoneNumber",
						label: "手机",
						field: "phoneNumber",
						sortable: false,
						align: "center"
					},
					{
						name: "departmentname",
						required: true,
						label: "所属部门",
						align: "center",
						field: "departmentname"
					},
					{
						name: "telNumber",
						label: "座机",
						field: "telNumber",
						sortable: false,
						align: "center"
					},

					{
						name: "position",
						required: true,
						label: "岗位",
						align: "center",
						field: "position"
					},
					{
						name: "email",
						required: true,
						label: "邮箱",
						align: "center",
						field: "email"
					},
					{
						name: "statusname",
						required: true,
						label: "启用状态",
						align: "center",
						field: "statusname"
					},
					,
					{
						name: "delstatusname",
						required: true,
						label: "异常账户",
						align: "center",
						field: "delstatusname"
					},
					{
						name: "id",
						required: true,
						label: this.$t("ID"),
						align: "center",
						field: "id"
					}
				],
				filter: {
					username: "",
					phoneNumber: "",
					account: "",
					delstatus:""
				},
				selected: [],
				loading: false,
				editModal: false,
				subtype: "add",
				tempUser: {
					id: 0,
					name: "",
					username: "",
					sex: "",
					email: "",
					phoneNumber: "",
					account: "",
					telNumber: "",
					nickname: "",
					address: "",
					mobile: "",
					telecom: "",
					office: "",
					department: "",
					position: "",
					identity: "",
					// role: "",
					addtime: "",
					credit: "",
					political: "",
					auth: "",
					appId: "",
					paizhu: "",
					fuze: "",
					isadmin: "",
					synchronous: "",
					status: "",
					avatar: "",
					departmentname: "",
					centername: "",
					department: "",
					fuzename: "",
					departmentid: "",
					centerid: ""
				}
			};
		},
		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,
					username: this.filter.username,
					account: this.filter.account,
					phoneNumber: this.filter.phoneNumber,
					centerid: this.centerid,
					departmentid: this.departmentid,
					delstatus:this.filter.delstatus
				};
				let dataRes = await getUserPagedList(query);
				let data = dataRes.data.data;
				this.serverPagination.rowsNumber = data.total;
				this.serverData = data.list;
				setTimeout(() => {
					this.loading = false;
				}, 500);
				this.getUserListByPid_user("1", "1");
				this.getYesNoCodelistByKindcode();
			},
			getUserListByPid_user_pid() {;
				this.getUserListByPid_user(this.centerid, "2");
			},
			/**
			 * 根据pid获取部门信息
			 */
			async getUserListByPid_user(pid, type) {
				let list = await getUserListByPid(pid);
				if(list.data.data.length > 0) {
					if(type == "1") {
						this.centerlist = [];
					} else {
						this.departmentlist = [];
					}

					for(var item of list.data.data) {
						let obj = {
							label: item.name,
							value: item.id
						};
						if(type == "1") {
							this.centerlist.push(obj);
						} else {
							this.departmentlist.push(obj);
						}
					}
				}
			},
			async delUser(id) {
				try {
					await this.$q.dialog({
						title: "批量操作",
						message: this.$t("Comfirm do it"),
						position: "right",
						ok: {
							push: true,
							label: "确认"
						},
						cancel: {
							push: true,
							color: "negative",
							label: "取消"
						}
					});
					await delUser({
						id: id
					});
					this.$q.notify({
						type: "positive",
						message: this.$t("Successfully"),
						position: "bottom-right"
					});
					this.search();
				} catch(e) {}
			},
			/**批量操作 */
			async delUsers_user(type) {
				try {
					await this.$q.dialog({
						title: this.$t("Batch do it"),
						message: this.$t("Comfirm do it"),
						ok: {
							push: true,
							label: "提交"
						},
						cancel: {
							push: true,
							color: "negative",
							label: "取消"
						}
					});
					// let ids={
					//   ids: JSON.stringify(
					//     this.selected.map(s => {
					//       return s.id;
					//     })
					//   )
					// }
					let idlist = this.selected.map(s => {
						return s.id;
					});
					await updateUsersStatus(idlist.join(","), type);
					this.$q.notify({
						type: "positive",
						message: this.$t("Successfully"),
						position: "bottom-right"
					});
					this.selected = [];
					this.search();
				} catch(e) {}
			},
			//从微信获取信息
			async editUserFromwx(id) {
				let dataRes = await editUserFromwx(id);
				if(dataRes.data.code == '200') {
					this.$q.notify({
						type: "positive",
						message: '拉取成功',
						position: "bottom-right"
					});
					this.search();
				}
			},
			/**用户修改 */
			editUser(id) {
				this.getYesNoCodelistByKindcode();
				this.getSexCodelistByKindcode();
				this.subtype = "edit";
				let editRole = this.serverData.filter(s => {
					return s.id === id;
				});
				if(editRole && editRole.length > 0) {
					this.tempUser = JSON.parse(JSON.stringify(editRole[0]));
					this.tempUser.isadmin = JSON.parse(
						JSON.stringify(editRole[0])
					).isadmin.toString();
					this.tempUser.sex = JSON.parse(
						JSON.stringify(editRole[0])
					).sex.toString();
					if(this.subtype == "add") {
						this.tempUser.avatar =
							"https://open.work.weixin.qq.com/wwopen/userQRCode?vcode=vcd8e47e71bd07fc16";
						this.tempUser.synchronous = "0";
					}
					this.menuSelected = [];
					let sttt = JSON.parse(
						JSON.stringify(editRole[0])
					).department.toString();
					let dd = sttt.split(",");
					dd.forEach(element => {
						this.menuSelected.push(parseInt(element));
					});
					this.roleSelected = [];
					let fuzett = JSON.parse(JSON.stringify(editRole[0])).fuze;;
					if(fuzett != null) {
						let fuzedd = fuzett.split(",");
						fuzedd.forEach(element => {
							this.roleSelected.push(parseInt(element));
						});
					}
				} else {
					return true;
				}
				this.editModal = true;
			},
			//保存，不更新密码
			async saveUser_user_nopassword() {
				//TODO 验证表单
				if(this.tempUser.account == "" || this.tempUser.account == null) {
					this.$q.notify({
						message: "请填写工号",
						type: "negative",
						icon: "error_outline",
						timeout: 3000,
						position: "center"
					});
					return false;
				}
				if(this.tempUser.username == "" || this.tempUser.username == null) {
					this.$q.notify({
						message: "请填用户名",
						type: "negative",
						icon: "error_outline",
						timeout: 3000,
						position: "center"
					});
					return false;
				}
				if(
					this.tempUser.phoneNumber == "" ||
					this.tempUser.phoneNumber == null
				) {
					this.$q.notify({
						message: "请填写手机号",
						type: "negative",
						icon: "error_outline",
						timeout: 3000,
						position: "center"
					});
					return false;
				}
				if(
					this.tempUser.departmentname == "" ||
					this.tempUser.departmentname == null
				) {
					this.$q.notify({
						message: "请选择所属部门",
						type: "negative",
						icon: "error_outline",
						timeout: 3000,
						position: "center"
					});
					return false;
				}

				await saveUserNoPassword(this.tempUser);
				this.$q.notify({
					type: "positive",
					message: this.$t("Successfully"),
					position: "bottom-right"
				});
				this.editModal = false;
				this.search();
			},
			/**
			 * 更新用户
			 */
			async saveUser_user() {
				//TODO 验证表单
				if(this.tempUser.account == "" || this.tempUser.account == null) {
					this.$q.notify({
						message: "请填写工号",
						type: "negative",
						icon: "error_outline",
						timeout: 3000,
						position: "center"
					});
					return false;
				}
				if(this.tempUser.username == "" || this.tempUser.username == null) {
					this.$q.notify({
						message: "请填用户名",
						type: "negative",
						icon: "error_outline",
						timeout: 3000,
						position: "center"
					});
					return false;
				}
				if(this.tempUser.password == "" || this.tempUser.password == null) {
					this.$q.notify({
						message: "请输入密码",
						type: "negative",
						icon: "error_outline",
						timeout: 3000,
						position: "center"
					});
					return false;
				}
				if(
					this.tempUser.phoneNumber == "" ||
					this.tempUser.phoneNumber == null
				) {
					this.$q.notify({
						message: "请填写手机号",
						type: "negative",
						icon: "error_outline",
						timeout: 3000,
						position: "center"
					});
					return false;
				}
				if(
					this.tempUser.departmentname == "" ||
					this.tempUser.departmentname == null
				) {
					this.$q.notify({
						message: "请选择所属部门",
						type: "negative",
						icon: "error_outline",
						timeout: 3000,
						position: "center"
					});
					return false;
				}

				await saveUser(this.tempUser);
				this.$q.notify({
					type: "positive",
					message: this.$t("Successfully"),
					position: "bottom-right"
				});
				this.editModal = false;
				this.search();
			},
			search() {
				this.updateRedis_use();
				this.request({
					pagination: this.serverPagination,
					filter: this.filter
				});
			},
			updateconfim() {
				this.$q.dialog({
					title: '微信人员更新拉取',
					message: '因数据量较大且会比对信息变化(手机、部门)执行时间较长，更新将在后台执行！',
					ok: '开始',
					cancel: '取消'
				}).then(() => {
					this.synchronousAvatar();
				}).catch(() => {
					this.$q.notify('已取消')
				})
			},
			updateconfim2() {
				this.$q.dialog({
					title: '微信人员新增拉取',
					message: '只拉取新增人员，并校对部门信息，更新将在后台执行！',
					ok: '开始',
					cancel: '取消'
				}).then(() => {
					this.synchronousPersonal();
				}).catch(() => {
					this.$q.notify('已取消')
				})
			},
			updateconfim3() {

				this.$q.dialog({
					title: '操作确认',
					message: '只会处理部门级别以下信息！',
					ok: '开始',
					cancel: '取消'
				}).then(() => {
					this.synchronousDept();
				}).catch(() => {
					this.$q.notify('已取消')
				})
			},
			async synchronousDept() {
				this.$q.notify({
					type: "warning",
					message: this.$t("操作已提交，请勿频繁操作"),
					position: "center",
					timeout: 1000, // 以毫秒为单位; 0意味着没有超时
				});
				await synchronousDept();
			},
			getDeltetList() {
			this.getDeltetList();
			},
				async getDeltetList() {
				this.$q.notify({
					type: "warning",
					message: this.$t("操作已提交，请勿频繁操作"),
					position: "center",
					timeout: 1000, // 以毫秒为单位; 0意味着没有超时
				});
				await getDeltetList_user();
			}, 
			

			/**同步微信头像 */
			async synchronousAvatar() {
				this.$q.notify({
					type: "warning",
					message: this.$t("操作已提交，请勿频繁操作"),
					position: "center",
					timeout: 1000, // 以毫秒为单位; 0意味着没有超时
				});
				await synchronousAvatar();

			},

			/**微信人员新增拉取*/
			async synchronousPersonal() {
				await synchronousPersonal();
			},
			/**同步微信头像 */
			async getweixinisleaderindeptByaccount_user() {
				await getweixinisleaderindeptByaccount();
				this.$q.notify({
					type: "positive",
					message: this.$t("Successfully"),
					position: "bottom-right"
				});
			},
			/**重置密码 */
			async resetPassword_user(id) {
				await resetPassword(id);
				this.$q.notify({
					type: "positive",
					message: this.$t("Successfully"),
					position: "bottom-right"
				});
			},
			async getYesNoCodelistByKindcode() {
				this.yesnokindlist = [];
				//  this.yeornoOptions=[];
				let kindlist_code = await getSysCodelistByKindcode("YES_NO");
				if(kindlist_code.data.data.length > 0) {
					for(var item of kindlist_code.data.data) {
						let obj = {
							label: item.codename,
							value: item.codevalue
						};
						this.yesnokindlist.push(obj);
						// this.yeornoOptions.push(obj);
					}
				}
			},
			async getSexCodelistByKindcode() {
				this.sexkindlist = [];
				//  this.yeornoOptions=[];
				let kindlist_code = await getSysCodelistByKindcode("SEX_TYPE");
				if(kindlist_code.data.data.length > 0) {
					for(var item of kindlist_code.data.data) {
						let obj = {
							label: item.codename,
							value: item.codevalue
						};
						this.sexkindlist.push(obj);
						// this.yeornoOptions.push(obj);
					}
				}
			},
			addUser() {
				this.getYesNoCodelistByKindcode();
				this.getSexCodelistByKindcode();
				this.tempUser = {};
				this.tempUser.status = "1";
				this.tempUser.sex = "1";
				this.tempUser.isadmin = "0";
				this.tempUser.synchronous = "1";
				this.tempUser.paizhu = "0";
				this.subtype = "add";
				this.editModal = true;
				this.menuSelected = [];
				this.roleSelected = [];
			},
			async selectModule() {
				let menuListRes = await getDeptAllMenu();
				let menuList = menuListRes.data.data.children;
				let reBuildMenuList = [];
				let forFn = function(menuList, newMenuList) {
					for(var item of menuList) {
						let menu = {
							id: item.id,
							label: item.title,
							name: item.name,
							key: item.key,
							title: item.title,
							tickStrategy: "leaf"
						};
						menu.children = [];
						if(item.children && item.children.length > 0) {
							forFn(item.children, menu.children);
						}
						newMenuList.push(menu);
					}
				};
				forFn(menuList, reBuildMenuList);
				this.menuList = reBuildMenuList;
				// this.menuSelected = this.tempFunction.moduleId || 0;
				this.menuModal = true;
			},
			moduleChange() {
				let self = this;
				// let selectMenu = this.findMenuById(this.menuSelected);
				self.tempUser.department = "";
				self.tempUser.departmentname = "";
				for(var i = 0; i < self.menuSelected.length; i++) {
					let selectMenu = self.findMenuById(self.menuSelected[i]);
					if(i > 0) {
						this.tempUser.department =
							this.tempUser.department + "," + selectMenu.id;
						this.tempUser.departmentname =
							this.tempUser.departmentname + "," + selectMenu.label;
					} else {
						this.tempUser.department = selectMenu.id;
						this.tempUser.departmentname = selectMenu.label;
					}
				}

				// this.tempUser.department = selectMenu.id;
				// this.tempUser.departmentname = selectMenu.label;
				this.menuModal = false;
			},
			findMenuById(id) {
				let menu = {};
				let _this = this;
				let getMenu = function(menuList) {
					for(var item of menuList) {
						if(item.id === id) {
							_this.editingMenu = item; //正在编辑的菜单,引用原始菜单
							menu = Object.assign({}, item);
							menu.children = null;
							break;
						} else {
							if(item.children && item.children.length > 0) {
								getMenu(item.children);
							}
						}
					}
				};
				getMenu(this.menuList);
				return menu;
			},
			async selectroleModule() {
				let roleListRes = await getDeptAllMenu();
				let roleList = roleListRes.data.data.children;
				let reBuildroleList = [];
				let forFn = function(roleList, newroleList) {
					for(var item of roleList) {
						let role = {
							id: item.id,
							label: item.title,
							name: item.name,
							key: item.key,
							title: item.title,
							tickStrategy: "leaf"
							// path: item.path,
							// icon: item.icon,
							// leftmemu: item.leftmemu,
							// functionCode: item.functionCode,
							// isLock: item.isLock,
							// sort: item.sort
						};
						role.children = [];
						if(item.children && item.children.length > 0) {
							forFn(item.children, role.children);
						}
						newroleList.push(role);
					}
				};
				forFn(roleList, reBuildroleList);
				this.roleList = reBuildroleList;

				this.roleModal = true;
			},
			rolemoduleChange() {
				let self = this;
				this.tempUser.fuze = "";
				this.tempUser.fuzename = "";
				for(var i = 0; i < self.roleSelected.length; i++) {
					let selectMenu = this.findRoleById(self.roleSelected[i]);
					if(i > 0) {
						this.tempUser.fuze = this.tempUser.fuze + "," + selectMenu.id;
						this.tempUser.fuzename =
							this.tempUser.fuzename + "," + selectMenu.label;
					} else {
						this.tempUser.fuze = selectMenu.id;
						this.tempUser.fuzename = selectMenu.label;
					}
				}
				// let selectMenu = this.findRoleById(this.roleSelected);
				// this.tempUser.role = selectMenu.id;
				// this.tempUser.rolename = selectMenu.label;
				this.roleModal = false;
			},
			findRoleById(id) {
				let menu = {};
				let _this = this;
				let getMenu = function(roleList) {
					for(var item of roleList) {
						if(item.id === id) {
							_this.editingMenu = item; //正在编辑的菜单,引用原始菜单
							menu = Object.assign({}, item);
							menu.children = null;
							break;
						} else {
							if(item.children && item.children.length > 0) {
								getMenu(item.children);
							}
						}
					}
				};
				getMenu(this.roleList);
				return menu;
			},
			//调用审批接口，更新缓存
			async updateRedis_use() {
				let menuListRes = await updateRedis();
			}

		},
		watch: {
			centerid() {
				this.getUserListByPid_user_pid();
			}
		},

		mounted() {
			this.request({
				pagination: this.serverPagination,
				filter: this.filter
			});
		}
	};
</script>
<style scoped>
	.liststype {
		width: 98%;
		/* padding-left: 4%; */
		/* float: left; */
	}
</style>