<template>
	<lay-form :model="userInfo" style="max-width:96%; margin:30px 0;" ref="layFormRef">
		<lay-form-item label="用户名" prop="FUser" required>
			<lay-input v-model="userInfo.FUser"></lay-input>
		</lay-form-item>
		<lay-form-item label="密码" prop="FPassword" required>
			<lay-input v-model="userInfo.FPassword" type="password"></lay-input>
		</lay-form-item>
		<lay-form-item label="姓名" prop="FName" required>
			<lay-input v-model="userInfo.FName"></lay-input>
		</lay-form-item>
		<!-- <lay-form-item label="工厂" prop="FFactoryID" required>
			<lay-select v-model="userInfo.FFactoryID" required>
				<lay-select-option v-for="(fIdv,fIdx) in factoryLists" key="fIdx" :value="fIdv.FID+''"
					:label="fIdv.FName +' - '+ fIdv.FCode"></lay-select-option>
			</lay-select>
		</lay-form-item>
		<lay-form-item label="角色" prop="FRoleID">
			<lay-select v-model="userInfo.FRoleID" required>
				<lay-select-option v-for="(rIdv,rIdx) in roleLists" key="rIdx" :value="rIdv.FID+''" :label="rIdv.FName">
				</lay-select-option>
			</lay-select>
		</lay-form-item> -->


		<lay-form-item label="权限" prop="FName" required>
			<view class="sItem">
				<lay-col md="7" sm="6" xs="24">
					<select class="form-select" required :value="fctoryID1">
						<option :value="-1">请选择模式</option>
						<option v-for="(fIdv, fIdx) in factoryLists" key="fIdx" :value="fIdv.FID">
							{{ fIdv.FName + '-' + fIdv.FCode }}</option>
					</select>
				</lay-col>
				<lay-col md="7" sm="6" xs="24">
					<select class="form-select" required :value="roleID1">
						<option :value="-1">请选择角色</option>
						<option v-for="(rIdv, rIdx) in roleLists" key="rIdx" :value="rIdv.FID">{{ rIdv.FName }}
						</option>
					</select>
				</lay-col>
				<lay-col md="2" sm="2" xs="12">
					<lay-icon type="layui-icon-add-circle" color="#5FB878" size="24px" @click="addSelect"></lay-icon>
				</lay-col>
			</view>
			<view class="sItem" v-for="(item, i) of items" :key="i" :data-row="i">
				<lay-col md="7" sm="6" xs="24">
					<select class="form-select" required :value="item.FFactoryID">
						<option selected :value="-1">请选择模式</option>
						<option v-for="(fIdv, fIdx) in factoryLists" key="fIdx" :value="fIdv.FID">
							{{ fIdv.FName + '-' + fIdv.FCode }}</option>
					</select>
				</lay-col>
				<lay-col md="7" sm="6" xs="24">
					<select class="form-select" required :value="item.FRole">
						<option selected :value="-1">请选择角色</option>
						<option v-for="(rIdv, rIdx) in roleLists" key="rIdx" :value="rIdv.FID">{{ rIdv.FName }}
						</option>
					</select>
				</lay-col>
				<lay-col md="2" sm="2" xs="12">
					<lay-icon type="layui-icon-reduce-circle" color="#FF5722" size="24px"
						@click="removeSelect($event, i)"></lay-icon>
				</lay-col>
			</view>
		</lay-form-item>

		<lay-form-item label="手机号" prop="FPhone">
			<lay-input v-model="userInfo.FPhone"></lay-input>
		</lay-form-item>
		<lay-form-item label="状态" prop="FState" required>
			<view required v-for="(sIdv, sIdx) in stateLists" key="sIdx">
				<lay-radio v-model="userInfo.FState" name="FState" :value="sIdv.FValue">{{ sIdv.FName }}</lay-radio>
			</view>
		</lay-form-item>
		<lay-form-item>
			<lay-button type="normal" @click="submitClick">{{ userInfo.FID ? "修改" : "新增" }}</lay-button>
			<lay-button @click="cancelClick">取消</lay-button>
		</lay-form-item>
	</lay-form>
</template>

<script>
import {
	ref,
	reactive
} from 'vue';
import {
	updateDataInfo,
	insertDataInfo
} from "/src/api/api/common";
import {
	getRoleList,
	GetPower,
	CreatePower
} from "/src/api/api/user";
import{
	getFactoryList
}from "/src/api/api/factory";


export default {
	setup() {
		const userInfo = ref({});
		const stateLists = ref([]);
		const factoryLists = ref([]);
		const roleLists = ref([]);
		const selectCount = 0;
		const items = ref([]);
		const roleID1=-1;
		const fctoryID1=-1;
		return {
			userInfo,
			stateLists,
			factoryLists,
			roleLists,
			selectCount,
			items,
			roleID1,
			fctoryID1
		}
	},
	props: {
		userInfoObj: {
			type: Object,
			default: () => { },
		},
		stateList: {
			type: Array,
			default: () => [],
		},
		factoryList: {
			type: Array,
			default: () => [],
		}
	},
	created(){
		this._getRoleList();
		this._getFactoryList();
	},
	mounted() {
		//this.factoryLists = JSON.parse(JSON.stringify(this.factoryList));
		this.stateLists = JSON.parse(JSON.stringify(this.stateList));
		let userInfo = JSON.parse(JSON.stringify(this.userInfoObj));
		userInfo.FUser = userInfo.FUser || " ";
		userInfo.FPassword = userInfo.FPassword || "123456";
		userInfo.FName = userInfo.FName || " ";
		//userInfo.FFactoryID = (userInfo.FFactoryID || " ") + "";
		userInfo.FState = (userInfo.FState || "1") + "";
		userInfo.FRoleID = (userInfo.FRoleID || "") + "";
		// this._getRoleList();
		// this._getFactoryList();
		this.userInfo = userInfo;
		this._GetPower(userInfo);
		console.log(userInfo)
	},
	
	methods: {
		//获取角色集合
		async _getRoleList() {
			this.roleLists = await getRoleList() || [];
		},
		async _getFactoryList(){
            this.factoryLists=await getFactoryList()||[];
		},
		async _InsertUerAndPower(data,data2){
			await InsertUerAndPower(data,data2);
		},
		async _GetPower(data) {
			let result = await GetPower(data) || [];
			if (result.length > 0) {



				this.roleID1 = result[0].FRole;
				this.fctoryID1 = result[0].FFactoryID;
				document.getElementsByClassName("sItem")[0].getElementsByClassName("form-select")[0].value=result[0].FFactoryID;
				document.getElementsByClassName("sItem")[0].getElementsByClassName("form-select")[1].value=result[0].FRole;
				result.splice(0, 1);
				this.items = result;
			}
			else{
				this.roleID1 = -1;
				this.fctoryID1 = -1;
			}

		},
		cancelClick() {
			this.$emit('cancelClick', false);
		},
		addSelect() {
			this.items.push({ FFactoryID: '-1', FRole: '-1' });
		},
		removeSelect(even, e) {
			even.target.parentElement.parentElement.remove()
		},
		async submitClick() {
			this.userInfo.FUser = this.userInfo.FUser.trim();
			this.userInfo.FPassword = this.userInfo.FPassword.trim();
			this.userInfo.FName = this.userInfo.FName.trim();

			//this.userInfo.FFactoryID = this.userInfo.FFactoryID.trim();
			//this.userInfo.FRoleID = this.userInfo.FRoleID || -1;
			//this.userInfo.FRoleName = role == null ? "" : role.FName;

			let sItemsCount = document.querySelectorAll(".sItem").length;

			let arr = [];
			let arr1=[];
			let arr2=[];

			let role = this.roleLists.find(s => s.FID == this.userInfo.FRoleID);

			let result = 0;
			if (this.userInfo.FUser == "" || this.userInfo.FPassword == "" || this.userInfo.FName == "") {
				return false;
			}
			if (this.userInfo.FID && this.userInfo.FID > 0) {


				for (let i = 0; i < sItemsCount; i++) {
					if (document.querySelectorAll(".sItem")[i].querySelectorAll("select")[0].value > -1 && document.querySelectorAll(".sItem")[i].querySelectorAll("select")[1].value > -1
					&&document.querySelectorAll(".sItem")[i].querySelectorAll("select")[0].value!=""&&document.querySelectorAll(".sItem")[i].querySelectorAll("select")[1].value!="") {
						arr1.push(document.querySelectorAll(".sItem")[i].querySelectorAll("select")[0].value);
						arr2.push(document.querySelectorAll(".sItem")[i].querySelectorAll("select")[1].value);
						arr.push({
							FUserID: this.userInfo.FID,
							FFactoryID: document.querySelectorAll(".sItem")[i].querySelectorAll("select")[0].value,
							FRole: document.querySelectorAll(".sItem")[i].querySelectorAll("select")[1].value
						});
					}
					else {
						layer.msg("权限不能为空", {
							time: 1000,
							icon: 2
						})
						return false;
					}


				}
				this.userInfo.FFactoryList=","+arr1.join(",")+",";
				this.userInfo.FRoleList=","+arr2.join(",")+",";
				result = await updateDataInfo(this.userInfo, "User") || 0;
				result = await CreatePower(arr) || 0;

			} else {
				this.userInfo.FAddUser = -1;
				this.userInfo.FEditUser = -1;



				for (let i = 0; i < sItemsCount; i++) {
					if (document.querySelectorAll(".sItem")[i].querySelectorAll("select")[0].value > -1 && document.querySelectorAll(".sItem")[i].querySelectorAll("select")[1].value > -1
					&&document.querySelectorAll(".sItem")[i].querySelectorAll("select")[0].value!=""&&document.querySelectorAll(".sItem")[i].querySelectorAll("select")[1].value!="") {
						arr1.push(document.querySelectorAll(".sItem")[i].querySelectorAll("select")[0].value);
						arr2.push(document.querySelectorAll(".sItem")[i].querySelectorAll("select")[1].value);
						arr.push({
							FUserID: 0,
							FFactoryID: document.querySelectorAll(".sItem")[i].querySelectorAll("select")[0].value,
							FRole: document.querySelectorAll(".sItem")[i].querySelectorAll("select")[1].value
						});
					}
					else {
						layer.msg("权限不能为空", {
							time: 1000,
							icon: 2
						})
						return false;
					}

					this.userInfo.FFactoryList=","+arr1.join(",")+",";
					this.userInfo.FRoleList=","+arr2.join(",")+",";
				}

				if(arr.length>0)
				{

					result = await insertDataInfo(this.userInfo, "User") || 0;
					for(let i=0;i<arr.length;i++)
					{
						arr[i].FUserID=result;						
					}
					result = await CreatePower(arr) || 0;
				}
				//result = await insertDataInfo(this.userInfo, "User") || 0;
			}
			
			

			if (result > 0) {
				layer.msg("操作成功", {
					time: 1500,
					icon: 1
				});
				this.$emit('cancelClick', true);
			} else {
				layer.msg("操作失败", {
					time: 1000,
					icon: 2
				})
			}
		}
	}
}
</script>
