<template>
	<div class="editTargetDiaBase-box">
		<el-dialog
		:visible.sync="isShow"
		:close-on-click-modal="false"
		:modal-append-to-body='false'
		@close="cancelHandle"
		custom-class="dialog"
		center
		width="700px"
    title="添加指标"
		v-dialogDrag>
			<div class="dialog-main">
				<el-form
				ref="targetForm"
				label-position="left"
				label-width="80px"
				hide-required-asterisk
				:model="formData"
				:rules="formRules">
					<el-form-item label="指标名称" prop="edb_name">
						<el-input
						v-model="formData.edb_name"
						style="width: 80%"
						placeholder="指标名称"></el-input>
					</el-form-item>
					<el-form-item label="所属目录" prop="menu">
						<el-cascader
						v-model="formData.menu"
						:options="options"
						:props="levelProps"
						style="width: 80%"
						clearable
						placeholder="请选择所属目录"/>
					</el-form-item>
					<el-form-item label="频率" prop="frequency">
						<span slot="label">频&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;率</span>
						<el-select 
						v-model="formData.frequency" 
						placeholder="请选择频率" 
						style="width:80%"
            disabled
						clearable>
							<el-option
								v-for="item in frequencyArr"
								:key="item"
								:label="item"
								:value="item">
							</el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="单位" prop="unit">
						<span slot="label">单&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;位</span>
						<el-select
						v-model="formData.unit"
						filterable
						allow-create
						style="width: 80%"
						placeholder="请输入单位">
							<el-option
								v-for="item in unitArr"
								:key="item"
								:label="item"
								:value="item">
							</el-option>
						</el-select>
					</el-form-item>
				</el-form>
			</div>
			<div class="dia-bot">
				<el-button type="primary" style="margin-right:20px" @click="addTarget">保存</el-button>
				<el-button type="primary" plain @click="cancelHandle">取消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import { dataBaseInterface } from '@/api/api.js';
import { unitArr } from '@/utils/defaultOptions';
import * as supplyApi from '@/api/modules/supplyApi.js';
export default {
	name:'',
	props: {
		isShow: {
			type: Boolean,
		},
		params: {
			type: Object,
		}
	},
	watch: {
		'isShow': {
			handler(newval) {
				newval && this.getMenu();
				const { EdbName,Frequency } = this.params;
				if(newval) this.formData = {
					edb_name: EdbName || '',
					menu: [],
					frequency: Frequency,
					unit: '万吨'
				}
			}
		}
	},
	data () {
		return {
			formData: {},
			formRules: {
				edb_name:[
					{ required: true, message: '指标名称不能为空', trigger: 'blur' },
				],
				menu:[
					{ required: true, message: '所属目录不能为空', trigger: 'blur' },
				],
				frequency:[
					{ required: true, message: '频率不能为空', trigger: 'blur' },
				],
				unit:[
					{ required: true, message: '单位不能为空', trigger: 'blur' },
				],
			},
			options:  [],
			unitArr,
			levelProps: {
				label: 'ClassifyName',
				value: 'ClassifyId',
				children: 'Children',
			},
			frequencyArr:['日度','周度','旬度','月度','季度','年度']
		};
	},
	methods: {
		/* 获取目录结构 */
		getMenu() {
			dataBaseInterface.menuListV3().then(res => {
				if(res.Ret === 200) {
					//this.filterNodes(res.Data.AllNodes);
					this.options = res.Data.AllNodes || [];
				}
			})
		},
		// 递归改变第三级目录结构
		filterNodes(arr) {
			arr.length && arr.forEach(item => {
				item.Children.length && this.filterNodes(item.Children)
				if(item.Level === 2) {
					delete item.Children
				}
			})
		},
		/* 保存 */
		async addTarget() {
			await this.$refs.targetForm.validate()
			let params = {
				VarietyEdbId: this.params.VarietyEdbId,
				EdbName: this.formData.edb_name,
				ClassifyId: this.formData.menu[this.formData.menu.length - 1],
				Frequency: this.formData.frequency,
				Unit: this.formData.unit
			}

			const res = await supplyApi.addIntoEtaBase(params);
			if(res.Ret !== 200) return
			this.$message.success('添加成功');
			this.cancelHandle();
			this.$emit('successBack');
      
		},
		cancelHandle() {
			this.formData = {}
			this.$emit('update:isShow',false)
		}
	},
}
</script>
<style lang='scss'>
.editTargetDiaBase-box {
	.dialog-main {
		padding-left: 50px;
	}
	.el-cascader .el-input {
		width: 100%;
	}
	.dia-bot {
		margin: 52px 0 30px;
		display: flex;
		justify-content: center;

	}
}
</style>