<template>
	<div class="addDataModel">
		<el-dialog
			title="收购企业备案编辑"
			:visible.sync="addData.dialogEditVisible"
			width="1000px"
			center
			:close-on-click-modal="false"
		>
			<el-form
				:label-width="labelwidthStyle"
				:rules="rules"
				ref="addFormData"
				:model="addFormData"
			>
				<el-row>
					<el-col :span="12">
						<el-form-item label="所在行政区域:" prop="szxzqy">
							<el-select
								:disabled="true"
								v-model="addFormData.szxzqy"
								placeholder="请选择"
								clearable
							>
								<el-option
									v-for="item in dicOptions"
									:key="item.dictCode"
									:label="item.dictName"
									:value="item.dictCode"
								>
								</el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="企业名称:" prop="qymc">
							<el-input
								v-model="addFormData.qymc"
								clearable
							></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="12">
						<el-form-item label="统一社会信用代码:" prop="tyshxydm">
							<el-input
								v-model="addFormData.tyshxydm"
								clearable
							></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="地址:" prop="xxdz">
							<el-input
								v-model="addFormData.xxdz"
								clearable
							></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="12">
						<el-form-item label="联系人:" prop="lxr">
							<el-input
								v-model="addFormData.lxr"
								style="width: 100%"
								clearable
							></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="电话:" prop="dh">
							<el-input
								v-model="addFormData.dh"
								clearable
							></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="12">
						<el-form-item label="备案机关:" prop="bajg">
							<el-input
								disabled
								v-model="addFormData.bajg"
								clearable
							></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="备案日期:" prop="barq">
							<el-date-picker
								v-model="addFormData.barq"
								type="date"
								value-format="yyyy-MM-dd"
								style="width: 100%"
								clearable
							></el-date-picker>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button
					type="primary"
					@click="btnSave(0, 'addFormData')"
					class="iconfont iconbaocun"
					>保存</el-button
				>
				<el-button
					type="primary"
					@click="btnSave(1, 'addFormData')"
					class="iconfont icontijiao"
					>提交</el-button
				>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import {
	editData,
	getDetail,
} from '@/api/grainCoManage/acquisitionQualification/index'

export default {
	props: {
		addData: Object,
	},
	data() {
		return {
			labelwidthStyle: '150px',
			addFormData: {},
			rules: {
				szxzqy: [
					{
						required: true,
						message: '所在行政区域',
						trigger: 'blur',
					},
				],
				qymc: [
					{ required: true, message: '企业名称', trigger: 'blur' },
				],
				tyshxydm: [
					{
						required: true,
						message: '统一社会信用代码',
						trigger: 'blur',
					},
				],
				xxdz: [{ required: true, message: '地址', trigger: 'blur' }],
				lxr: [{ required: true, message: '联系人', trigger: 'blur' }],
				dh: [{ required: true, message: '电话', trigger: 'blur' }],
				bajg: [
					{ required: true, message: '备案机关', trigger: 'blur' },
				],
				barq: [
					{ required: true, message: '备案日期', trigger: 'blur' },
				],
			},
			dicOptions: [],
		}
	},
	watch: {},
	created() {
		// console.log("this.addData.id", this.addData.id);
		// console.log("this.addData.dicOptions", this.addData.dicOptions);
		// 所在行政区(字典)
		this.dicOptions = this.addData.dicOptions
		// 所在行政区下拉选带出数据
		this.addFormData.szxzqy = JSON.parse(
			sessionStorage.getItem('userInfo')
		).areaCode

		let bajg = sessionStorage.getItem('orgName')
		if (bajg.indexOf('发改委') > -1) {
			bajg = bajg.substring(0, bajg.indexOf('发改委'))
			bajg = `天津市${bajg}发展和改革委员会`
		}
		this.addFormData.bajg = bajg

		if (this.addData.id) {
			this.getInfo(this.addData.id)
		}
	},
	methods: {
		btnSave(type, formName) {
			let flag
			if (type === 0) {
				// 保存-待提交
				this.addFormData.sjzt = 1
				flag = '保存成功'
			} else {
				// 提交-已提交
				this.addFormData.sjzt = 2
				flag = '提交成功'
			}
			// 创建单位
			this.addFormData.cjdw = sessionStorage.getItem('orgCode')
			// 创建用户
			this.addFormData.cjyh = JSON.parse(
				sessionStorage.getItem('userInfo')
			).userId
			this.$refs[formName].validate((valid, dom) => {
				if (valid) {
					editData(this.addFormData)
						.then((res) => {
							this.$message.success(flag)
							this.addData.dialogEditVisible = false
							this.$emit('getList')
						})
						.catch((err) => {
							this.$message.error('操作失败')
							this.addData.dialogEditVisible = false
						})
				}
			})
		},
		getInfo() {
			getDetail(this.addData.id)
				.then((res) => {
					this.addFormData = res
				})
				.catch((err) => {
					this.$message.error(err)
					this.addData.dialogEditVisible = false
				})
		},
	},
}
</script>

<style scoped="scoped">
</style>
