﻿<template>
	<el-dialog :title="!dataForm.id ? '新建' :  isDetail ? '详情':'编辑'" :close-on-click-modal="false" :visible.sync="visible" class="NCC-dialog NCC-dialog_center" lock-scroll width="1200px">
		<el-row :gutter="15" class="" >
				<el-form ref="elForm" :model="dataForm" size="small" label-width="100px" label-position="right" :disabled="!!isDetail" :rules="rules">
					<el-col :span="12">
						<el-form-item label="商家名称" prop="name">
							<el-input v-model="dataForm.name" placeholder="商家名称" clearable required :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="商家分类" prop="category">
							<el-select v-model="dataForm.category" placeholder="请选择" clearable :style='{"width":"100%"}' filterable >
								<el-option v-for="(item, index) in categoryOptions" :key="index" :label="item.fullName" :value="item.id" ></el-option>
							</el-select>
						</el-form-item>
					</el-col>

					<el-col :span="12">
						<el-form-item label="区域，省市" prop="area">
							<NCC-Address v-model="dataForm.area" placeholder="请选择" clearable required :level="2" >
							</NCC-Address>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="均价/人" prop="avgPrice">
							<el-input v-model="dataForm.avgPrice" placeholder="均价/人" clearable :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="详细地址" prop="adress">
							<el-input v-model="dataForm.adress" placeholder="请输入" clearable :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="营业时间" prop="businessHours">
							<el-input v-model="dataForm.businessHours" placeholder="请输入" clearable :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="联系电话" prop="phone">
							<el-input v-model="dataForm.phone" placeholder="联系电话" clearable required :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="关联用户" prop="reUserId">
							<user-select v-model="dataForm.reUserId" placeholder="请选择" clearable required >
							</user-select>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="排序码" prop="sortCode">
							<el-input v-model="dataForm.sortCode" placeholder="排序码" clearable :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="描述" prop="description">
							<el-input v-model="dataForm.description" placeholder="请输入" show-word-limit :style='{"width":"100%"}' type='textarea' :autosize='{"minRows":4,"maxRows":4}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="商家Logo" prop="logo">
							<NCC-UploadImg v-model="dataForm.logo" required :fileSize="5" sizeUnit="MB" :limit="1" >
							</NCC-UploadImg>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="图片" prop="images">
							<NCC-UploadImg v-model="dataForm.images" :fileSize="5" sizeUnit="MB" :limit="9" >
							</NCC-UploadImg>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="标签" prop="tags">
							<el-input v-model="dataForm.tags" placeholder="标签英文,隔开" clearable :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24" v-if="false" >
						<el-form-item label="创建用户" prop="creatorUserId">
							<el-input v-model="dataForm.creatorUserId" placeholder="系统自动生成" readonly >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24" v-if="false" >
						<el-form-item label="创建时间" prop="creatorTime">
							<el-input v-model="dataForm.creatorTime" placeholder="系统自动生成" readonly >
							</el-input>
						</el-form-item>
					</el-col>
				</el-form>
		</el-row>
		<span slot="footer" class="dialog-footer">
			<el-button @click="visible = false">取 消</el-button>
			<el-button type="primary" @click="dataFormSubmit()" v-if="!isDetail">确 定</el-button>
		</span>
	</el-dialog>
</template>
<script>
	import request from '@/utils/request'
	import { getDictionaryDataSelector } from '@/api/systemData/dictionary'
	import { previewDataInterface } from '@/api/systemData/dataInterface'
	export default {
		components: {},
		props: [],
		data() {
			return {
				loading: false,
				visible: false,
				isDetail: false,
				dataForm: {
					id:'',
					name:undefined,
					area:undefined,
					avgPrice:undefined,
					adress:undefined,
					businessHours:undefined,
					phone:undefined,
					sortCode:undefined,
					description:undefined,
					logo:undefined,
					images:undefined,
					tags:undefined,
					creatorUserId:undefined,
					creatorTime:undefined,
					category:undefined,
				},
				categoryOptions:[],
				rules: {
					name:[
						{
							required:true,
							message:'请输入商家名称',
							trigger:'blur'
						},
					],
					area:[
						{
							required:true,
							message:'请输入区域，省市',
							trigger:'change'
						},
					],
					phone:[
						{
							required:true,
							message:'请输入联系电话',
							trigger:'blur'
						},
					],
					logo:[
						{
							required:true,
							message:'请输入商家Logo',
							trigger:'click'
						},
					],
				},
			}
		},
		computed: {},
        watch: {},
        created() {
			this.getcategoryOptions();
		},
		mounted() {
        },
		methods: {
			getcategoryOptions(){
				getDictionaryDataSelector('286675106090976517').then(res => {
					this.categoryOptions = res.data.list
				});
			},
			goBack() {
                this.$emit('refresh')
            },
			init(id, isDetail) {
				this.dataForm.id = id || 0;
                this.visible = true;
                this.isDetail = isDetail || false;
				this.$nextTick(() => {
					this.$refs['elForm'].resetFields();
					if (this.dataForm.id) {
						request({
							url: '/api/Food/BaseBusiness/' + this.dataForm.id,
							method: 'get'
						}).then(res =>{
							this.dataForm = res.data;
						})
					}
				})
			},
			dataFormSubmit() {
				this.$refs['elForm'].validate((valid) => {
                    if (valid) {
                        if (!this.dataForm.id) {
							let dataForm  = {...this.dataForm};
							dataForm.logo = dataForm.logo && JSON.stringify(dataForm.logo);
							dataForm.images = dataForm.logo && JSON.stringify(dataForm.images);
                            request({
                                url: `/api/Food/BaseBusiness`,
                                method: 'post',
                                data: dataForm,
                            }).then((res) => {
                                this.$message({
                                    message: res.msg,
                                    type: 'success',
                                    duration: 1000,
                                    onClose: () => {
                                        this.visible = false,
                                            this.$emit('refresh', true)
                                    }
                                })
                            })
                        } else {
                            request({
                                url: '/api/Food/BaseBusiness/' + this.dataForm.id,
                                method: 'PUT',
                                data: this.dataForm
                            }).then((res) => {
                                this.$message({
                                    message: res.msg,
                                    type: 'success',
                                    duration: 1000,
                                    onClose: () => {
                                        this.visible = false
                                        this.$emit('refresh', true)
                                    }
                                })
                            })
                        }
                    }
                })
			},
		}
	}
</script>
