<template>
	<div class="node-wrap">
		<div class="node-wrap-box" :class="[disabled ? 'node-wrap-box--disabled' : '', `node-wrap-box--${nodeConfig.local_status}`]">
			<div class="title" style="background: var(--el-color-primary)">
				<el-icon class="icon"><promotion /></el-icon>
				<span v-show="!isEditTitle" class="title_label" @click="editTitle('box_nodeTitle')"
					>{{ nodeConfig.nodeName }}<el-icon v-if="!disabled" class="edit-icon"><edit /></el-icon
				></span>
				<el-input
					v-show="isEditTitle"
					ref="box_nodeTitle"
					v-model="nodeConfig.nodeName"
					clearable
					size="small"
					@blur="saveTitle"
					@keyup.enter="saveTitle"
				></el-input>
				<el-icon v-if="!disabled" class="close" @click.stop="delNode()"><close /></el-icon>
			</div>
			<div class="content" @click="show">
				<span v-if="toText(nodeConfig)">{{ toText(nodeConfig) }}</span>
				<span v-else class="placeholder">请选择人员</span>
			</div>
		</div>
		<add-node v-model="nodeConfig.childNode" :disabled="disabled"></add-node>
		<el-drawer v-model="drawer" title="抄送人设置" destroy-on-close append-to-body :size="500" class="aDrawer">
			<template #header>
				<div class="node-wrap-drawer__title">
					<label v-show="!isEditTitle" @click="editTitle('nodeTitle')"
						>{{ form.nodeName }}<el-icon class="node-wrap-drawer__title-edit"><edit /></el-icon
					></label>
					<el-input v-show="isEditTitle" ref="nodeTitle" v-model="form.nodeName" clearable @blur="saveTitle" @keyup.enter="saveTitle"></el-input>
				</div>
			</template>
			<el-container>
				<el-main>
					<el-form label-position="top">
						<el-form-item label="选择要抄送的人员">
							<el-button type="primary" icon="plus" round @click="selectHandle(1, form.nodeAssigneeList)">选择人员</el-button>
							<div class="tags-list">
								<el-tag v-for="(user, index) in form.nodeAssigneeList" :key="user.id" closable @close="delUser(index)">{{ user.name }}</el-tag>
							</div>
						</el-form-item>
						<el-form-item label="">
							<el-checkbox v-model="form.allowSelection" label="允许发起人自选抄送人"></el-checkbox>
						</el-form-item>
					</el-form>
				</el-main>
				<el-footer>
					<el-button type="primary" @click="save">保存</el-button>
					<el-button @click="drawer = false">取消</el-button>
				</el-footer>
			</el-container>
		</el-drawer>
	</div>
</template>

<script>
import addNode from './addNode'

export default {
	components: {
		addNode
	},
	inject: ['select'],
	props: {
		modelValue: { type: Object, default: () => {} },
		disabled: {
			type: Boolean,
			default: false
		}
	},
	data() {
		return {
			nodeConfig: {},
			drawer: false,
			isEditTitle: false,
			form: {}
		}
	},
	watch: {
		modelValue() {
			this.nodeConfig = this.modelValue
		}
	},
	mounted() {
		this.nodeConfig = this.modelValue
	},
	methods: {
		show() {
			if (this.disabled) return
			this.form = {}
			this.form = JSON.parse(JSON.stringify(this.nodeConfig))
			this.drawer = true
		},
		editTitle(refName) {
			if (this.disabled) return
			this.isEditTitle = true
			this.$nextTick(() => {
				if (this.$refs[refName]) {
					this.$refs[refName].focus()
				}
			})
		},
		saveTitle() {
			this.isEditTitle = false
		},
		save() {
			this.$emit('update:modelValue', this.form)
			this.drawer = false
		},
		delNode() {
			this.$emit('update:modelValue', this.nodeConfig.childNode)
		},
		delUser(index) {
			this.form.nodeAssigneeList.splice(index, 1)
		},
		selectHandle(type, data) {
			this.select(type, data)
		},
		toText(nodeConfig) {
			if (nodeConfig.nodeAssigneeList && nodeConfig.nodeAssigneeList.length > 0) {
				const users = nodeConfig.nodeAssigneeList.map(item => item.name).join('、')
				return users
			} else {
				if (nodeConfig.allowSelection) {
					return '发起人自选'
				} else {
					return false
				}
			}
		}
	}
}
</script>

<style></style>
