<template>
	<div class="container">
		<div class="title">编辑变量</div>

		<div class="card-container">
			<div class="content-item" v-for="item in list" :key="item.hash">
				<div class="content">
					<div class="name">
						<span>字段</span>
						<a-input :default-value="item.name" v-model="item.name" />
					</div>
					<div class="name">
						<span>说明</span>
						<a-input :default-value="item.description" v-model="item.description" />
					</div>
					<div class="value">
						<span>默认值</span>
						<a-textarea :default-value="item.value" size="large" v-model="item.value" :auto-size="{ minRows: 3, maxRows: 5 }" />
					</div>
				</div>
				<a-icon class="delete" @click="deleteItems(item)" type="delete" />
			</div>
		</div>

		<div class="btn" @click="addVariables">新增字段</div>

		<div class="confirm-container">
			<div class="confirm" @click="confirm">确认</div>
			<div class="cancel" @click="cancel">取消</div>
		</div>
	</div>
</template>

<script>
export default {
	name: "EditVariables",
	data: function () {
		return {
			list: []
		}
	},
	mounted() {
		this.list = this.renderData;
	},
	props: {
		renderData: Array
	},
	methods: {
		addVariables() {
			this.list = [
				...this.list,
				{
					name: '',
					value: '',
					description: ''
				}
			]
		},
		deleteItems(target) {
			this.list = this.list.filter(item => item !== target);
		},
		confirm() {
			let flag;
			this.list.forEach(item => {
				if (!item.name) { flag = true }
				if (!item.value) { item.value = '\'\'' }
				if (!item.description) { item.description = '暂无说明' }
			});
			if (flag) {
				return this.bus.$emit('tips', {
					type: 'error',
					title: '字段的名称是必填项'
				})
			}
			this.axios({
			    url: window.api + '/api/SetUrlConfigVariables',
			    method: 'post',
				data: {
			    	list: this.list
				}
			}).then(data => {
			    data = data.data;
			    console.log(data);
				this.bus.$emit('updateUrlConfig');
			});
		},
		cancel() {
			try {
				this.$parent.close()
			} catch (e) {
				console.log(e);
			}
		}
	},
}
</script>

<style scoped lang="scss">
.container {
	height: 700px;
	width: 1200px;
	background-color: #282C34;
	padding: 0 20px;
	overflow: hidden;
	overflow-y: auto;
	.title {
		line-height: 60px;
		height: 60px;
		font-size: 24px;
		position: sticky;
		background-color: #282C34;
		top: 0;
		z-index: 1
	}
	.card-container {
		display: grid;
		grid-template-columns: 1fr 1fr;
		grid-gap: 50px;
	}
	.content-item {
		background-color: white(0.05);
		padding: 20px;
		display: grid;
		align-items: center;
		grid-template-columns: 1fr auto;
		input, textarea {
			text-indent: 10px;
		}
		.name, .value {
			display: grid;
			height: 40px;
			width: 100%;
			grid-template-columns: 40px 1fr;
			white-space: nowrap;
			align-items: center;
			gap: 30px;
		}
		.value {
			height: auto;
		}
		.delete {
			cursor: pointer;
			font-size: 24px;
			width: 50px;
			&:hover {
				color: red;
			}
		}
	}
	.btn {
		margin-top: 20px;
		height: 40px;
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 4px;
		border: 1px solid white(0.4);
		cursor: pointer;
		transition: all .2s;
		&:hover {
			border-color: white(0.9);
			color: white(0.9);
		}
	}
	.confirm-container {
		height: 60px;
		display: flex;
		align-items: flex-end;
		justify-content: flex-end;
		background: #282C34;
		position: sticky;
		bottom: 0;
		div {
			height: 40px;
			padding: 0 20px;
			display: flex;
			align-items: center;
			justify-content: center;
			background: white(0.1);
			margin-left: 10px;
			cursor: pointer;
			&:hover{
				background: white(0.2);
			}
		}
	}
}
</style>
