<template>
	<div class="node-wrap">
		<div class="node-wrap-box start-node" :class="[disabled ? 'node-wrap-box--disabled' : '', `node-wrap-box--${nodeConfig.local_status}`]">
			<div class="title" style="background: var(--el-color-info)">
				<el-icon class="icon"><user-filled /></el-icon>
				<span class="title_label">{{ nodeConfig.nodeName }}</span>
				<!--				<span v-show="!isEditTitle" class="title_label" @click="editTitle('box_nodeTitle')"
					>{{ nodeConfig.nodeName }}<el-icon 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>-->
			</div>
			<div class="content" @click="show">
				<span>{{ toText(nodeConfig) }}</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>{{ form.nodeName }}</label>
					<!--					<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(2, form.nodeAssigneeList)">选择角色</el-button>
							<div class="tags-list">
								<el-tag v-for="(role, index) in form.nodeAssigneeList" :key="role.id" type="info" closable @close="delRole(index)">{{
									role.name
								}}</el-tag>
							</div>
						</el-form-item>
						<el-alert v-if="!form.nodeAssigneeList?.length" title="不指定则默认所有人都可发起此审批" type="info" :closable="false" />
					</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.isEditTitle = false
			this.drawer = true
		},
		editTitle(refName) {
			this.isEditTitle = true
			this.$nextTick(() => {
				this.$refs[refName]?.focus()
			})
		},
		saveTitle() {
			this.isEditTitle = false
		},
		selectHandle(type, data) {
			this.select(type, data)
		},
		delRole(index) {
			this.form.nodeAssigneeList.splice(index, 1)
		},
		save() {
			this.$emit('update:modelValue', this.form)
			this.drawer = false
		},
		toText(nodeConfig) {
			if (nodeConfig.nodeAssigneeList && nodeConfig.nodeAssigneeList.length > 0) {
				return nodeConfig.nodeAssigneeList.map(item => item.name).join('、')
			} else {
				return '所有人'
			}
		}
	}
}
</script>

<style></style>
