<template>
	<lay-form :model="roleInfo" style="max-width:96%; margin:30px 0;">
		<lay-form-item label="角色名称" prop="FName" required>
			<lay-input v-model="roleInfo.FName"></lay-input>
		</lay-form-item>
		<lay-form-item label="角色描述" prop="FRemark">
			<lay-textarea placeholder="请输入角色描述" v-model="roleInfo.FRemark"></lay-textarea>
		</lay-form-item>
		<lay-form-item>
			<lay-button type="normal" @click="submitClick">{{ roleInfo.FID ? "修改" : "新增" }}</lay-button>
			<lay-button @click="cancelClick">取消</lay-button>
		</lay-form-item>
	</lay-form>
</template>

<script>
	import {
		ref
	} from 'vue';
	import {
		updateRoleModel,
		insertRoleModel
	} from "/src/api/api/user";

	export default {
		setup() {
			const roleInfo = ref({});
			return {
				roleInfo
			}
		},
		props: {
			roleInfoObj: {
				type: Object,
				default: () => {},
			}
		},
		mounted() {
			let roleInfo = JSON.parse(JSON.stringify(this.roleInfoObj));
			roleInfo.FName = roleInfo.FName || " ";
			roleInfo.FCode = roleInfo.FCode || "";
			roleInfo.FRemark = roleInfo.FRemark || "";
			this.roleInfo = roleInfo;
			console.log(this.roleInfo)
		},
		methods: {
			stopClick() {

			},
			cancelClick() {
				this.$emit('cancelClick', false);
			},
			async submitClick() {
				this.roleInfo.FName = this.roleInfo.FName.trim();
				if (this.roleInfo.FName == "") {
					return false;
				}
				let result = 0;
				this.roleInfo.TTT = "Role";
				if (this.roleInfo.FID && this.roleInfo.FID > 0) {
					result = await updateRoleModel(this.roleInfo, "User") || 0;
				} else {
					this.roleInfo.FAddUser = -1;
					this.roleInfo.FEditUser = -1;
					result = await insertRoleModel(this.roleInfo, "User") || 0;
				}
				if (result > 0) {
					layer.msg("操作成功", {
						time: 1500,
						icon: 1
					});
					this.$emit('cancelClick', true);
				} else {
					layer.msg("操作失败", {
						time: 1000,
						icon: 2
					})
				}
			}
		}
	}
</script>
