<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="460" draggable destroy-on-close @closed="$emit('closed')">
		<el-form class="radioItem" :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="120px" label-position="top">
			<div v-if="mode == 'add'">
				<div class="checkboxMain">
					<el-radio-group v-model="form.share.share_type" @change="shareChange">
						<el-radio label="team">部门共享</el-radio>
						<el-radio label="user">用户共享</el-radio>
					</el-radio-group>
				</div>
				<el-form-item label="请输入文件夹名称" prop="name" >
					<el-input v-model="form.name" placeholder="请输入文件夹名称" clearable></el-input>
				</el-form-item>
			</div>
			<div v-if="mode == 'edit'">
				<el-form-item label="选择共享" prop="share_type">
					<el-select style="width: 100%;" v-model="form.share_type" @change="shareChange">
						<el-option label="部门共享" value="team"></el-option>
						<el-option label="用户共享" value="user"></el-option>
					</el-select>
				</el-form-item>
			</div>
			<el-form-item label="选择要共享的部门" v-if="(mode!='edit' && form.share.share_type == 'team') || (mode=='edit' && form.share_type == 'team')">
				<el-select placeholder="请选择部门" v-model="form.share.share_ids" style="width: 100%;" filterable multiple collapse-tags @visible-change="getTeam">
					<el-option :label="item.name" :value="item.id" v-for="item in setMap.teamList" :key="item"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="选择要共享的用户" v-if="(mode!='edit' && form.share.share_type == 'user') || (mode=='edit' && form.share_type == 'user')">
				<el-select placeholder="请选择用户" v-model="form.share.share_ids" style="width: 100%;" filterable multiple collapse-tags @visible-change="getUser">
					<el-option :label="item.name" :value="item.id" v-for="item in setMap.userList" :key="item"></el-option>
				</el-select>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSave" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	name:"shareSave",
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新建共享文件夹',
				edit: '共享文件',
			},
			visible: false,
			isSave: false,

			setMap:{
				userList:[],
				teamList:[]
			},
			//共享的数据
			shareParams:{},
			//表单数据
			form: {
				share_type:"team",
				parent_id:"",
				name: "",
				share:{
					share_type:"team",
					share_ids:[]
				},
			},
			//验证规则
			rules: {
				name: [
					{required: true, message: '请输入文件夹名称'}
				],
			}
		}
	},
	mounted() {

	},
	methods: {
		shareChange(){
			this.form.share.share_ids = [];
		},
		async getUser() {
			const res = await this.$API.docs.manager.user.post();
			if(res.code == 200){
				this.setMap.userList = res.data;
			}
		},
		async getTeam() {
			const res = await this.$API.docs.manager.dept.post();
			if(res.code == 200){
				this.setMap.teamList = res.data;
			}
		},
		//显示
		open(mode='add'){
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit(){
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSave = true;
					let res;
					if(this.mode == 'add'){
						res = await this.$API.docs.manager.add.post(this.form);
					}
					if(this.mode == 'edit'){
						let params = this.setList();
						res = await this.$API.docs.share.add.post({share:params});
					}
					this.isSave = false;
					if(res.code == 200){
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					}
				}
			})
		},
		setList(){
			let arr = new Array();
			if(this.shareParams.share){
				this.shareParams.share.forEach(em=>{
					em.share_type = this.form.share_type;
					em.share_ids = this.form.share.share_ids
				})
				let shareList = this.shareParams.share.filter(em=>em.ids.length!=0);
				arr = shareList;
			}
			return arr
		},
		//表单注入数据
		setData(data){
			this.form.parent_id = data.folder_id;
			this.form.name = data.name;

			if(this.mode == 'edit'){
				this.shareParams = data;
				this.form.parent_id = data.parent_id;
				this.form.id = data.id;
			}
		}
	}
}
</script>

<style lang="scss" scoped>
	.radioItem{
		position: relative;
	}
	.checkboxMain{
		position: absolute;right: 0;top: -4px;z-index: 10;
	}
</style>
