<template>
	<div>
		<el-dialog :title="title" :visible="visible" :before-close="handleClose" :close-on-click-modal="false"
			width="50%">
			<div>
				<el-form ref="formInfo" label-position="top" :model="parameter" label-width="150px" :rules="rules">
					<el-row :gutter="24">
						<el-col :span="8">
							<el-form-item :label="$t('domestic.name')" prop="name">
								<el-input clearable :placeholder="$t('domestic.tips')+$t('domestic.name')"
									v-model="parameter.name"></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item :label="$t('domestic.nameEn')" prop="nameEn">
								<el-input clearable :placeholder="$t('domestic.tips')+$t('domestic.nameEn')"
									v-model="parameter.nameEn"></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item :label="$t('domestic.code')" prop="code">
								<el-input clearable :placeholder="$t('domestic.tips')+$t('domestic.code')"
									v-model="parameter.code"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row :gutter="24">
						<el-col :span="8">
							<el-form-item :label="$t('domestic.rateType')" prop="rateType">
								<el-select v-model="parameter.rateType"
									:placeholder="$t('domestic.tips')+$t('domestic.rateType')">
									<el-option v-for="item in hllxList" :key="item.value" :label="item.name"
										:value="item.value">
									</el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item :label="$t('domestic.type')" prop="type">
								<el-select v-model="typeList" multiple collapse-tags
									:placeholder="$t('domestic.tips')+$t('domestic.type')">
									<el-option v-for="item in kmList" :key="item.value" :label="item.name"
										:value="item.value">
									</el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item :label="$t('domestic.unit')" prop="unit">
								<el-select v-model="parameter.unit"
									:placeholder="$t('international.tips')+$t('international.unit')">
									<el-option v-for="item in sjdwList" :key="item.value" :label="item.name"
										:value="item.value">
									</el-option>
								</el-select>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col>
							<el-form-item :label="$t('domestic.memo')" prop="memo">
								<el-input clearable type="textarea" :rows="2"
									:placeholder="$t('domestic.tips')+$t('domestic.memo')" v-model="parameter.memo">
								</el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</el-form>
			</div>
			<span slot="footer" class="dialog-footer">
				<el-button type="primary" @click="handleSave">{{$t('domestic.save')}}</el-button>
				<el-button @click="handleClose">{{$t('domestic.cancel')}}</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
	import dictUtils from '@/components/dict/DictSelectUtils.js'
	import domesticSubject from '@/api/basicInfo/domesticSubject.js'
	export default {
		name: 'domesticDialog',
		components: {},
		data() {
			return {
				visible: false,
				title: '新增',
				rules: {
					name: [{
						required: true,
						message: this.$t('domestic.tips') + this.$t('domestic.name'),
						trigger: 'blur'
					}],
					nameEn: [{
						required: true,
						message: this.$t('domestic.tips') + this.$t('domestic.nameEn'),
						trigger: 'blur'
					}],
					code: [{
						required: true,
						message: this.$t('domestic.tips') + this.$t('domestic.code'),
						trigger: 'blur'
					}],
				},
				queryParam: {
					pageBean: {
						page: 1,
						pageSize: -1,
					},
				},
				typeList: [],
				parameter: {
					name: "",
					nameEn: "",
					type: "",
					code: "",
					parentId: '',
					type: "",
					unit: "",
					memo: ""
				},
				kmList: [],
				hllxList: [],
				sjdwList:[]
			}
		},
		created() {
			if (this.$i18n.locale === 'zh-CN') {
				this.kmList = dictUtils.getDictItemsFromCache('subjec_type')
				this.hllxList = dictUtils.getDictItemsFromCache('hllx')
				this.sjdwList = dictUtils.getDictItemsFromCache('sjdw')
			} else {
				this.kmList = dictUtils.getDictItemsFromCache('subjec_type')
				this.hllxList = dictUtils.getDictItemsFromCache('hllx_en')
				this.sjdwList = dictUtils.getDictItemsFromCache('sjdw')
			}
		},
		mounted() {},
		methods: {
			edit(val) {
				this.title = this.$t('common.edit')
				this.visible = true
				domesticSubject.detailId(val, res => {
					if (res) {
						this.parameter = res
						this.typeList = this.parameter.type.split(',')
						this.parameter.rateType = this.parameter.rateType.toString()
					}
				})

			},
			add(val) {
				this.parameter.parentId = val
				this.title = this.$t('common.add')
				this.visible = true
				this.typeList=[]
				this.parameter.rateType = this.hllxList[0].value
				this.parameter.unit=this.sjdwList[0].value
				this.typeList.push(this.kmList[0].value)
			},

			// 关闭新增人员窗口
			handleClose() {
				this.typeList = []
				this.visible = false
				this.title = ""
				this.parameter = {
					name: "",
					nameEn: "",
					type: "",
					code: "",
					parentId: "",
					type: "",
					unit: "",
					memo: ""
				}
			},
			// 保存
			handleSave() {
				let str = ''
				this.typeList.forEach(e => {
					str += e + ","
				})
				if (str.length > 0) {
					str = str.substr(0, str.length - 1);
				}
				this.parameter.type = str
				this.$refs.formInfo.validate(valid => {
					if (valid) {
						if (this.title === '添加' || this.title === "add") {
							domesticSubject.addData(this.parameter, res => {
								if (res.state) {
									this.$notify({
										type: 'success',
										message: this.$t('domestic.success'),
										duration: 2000
									})
									this.$emit('getData');
									this.handleClose()
								}
							})
						} else if (this.title === '编辑' || this.title === "edit") {
							domesticSubject.editData(this.parameter, res => {
								if (res.state) {
									this.$notify({
										type: 'success',
										message: this.$t('domestic.success'),
										duration: 2000
									})
									this.$emit('getData');
									this.handleClose()
								}
							})
						}
					}
				})
			},
			//打卡关联弹框
			openCorrelation() {
				console.log(1);
				this.$refs.correlationDialog.add()
			}
		}
	}
</script>

<style lang="scss" scoped>

</style>
