﻿<template>
	<transition name="el-zoom-in-center">
		<div class="NCC-preview-main">
			<div class="NCC-common-page-header">
				<el-page-header @back="goBack" :content="!dataForm.id ? '新建' : isDetail ? '详情' : '编辑'"/>
				<div class="options">
					<el-button type="primary" @click="dataFormSubmit()" v-if="!isDetail">确 定</el-button>
					<el-button @click="goBack">取 消</el-button>
				</div>
			</div>
			<el-row :gutter="15"  class="main" :style="{margin: '0 auto',width:'100%'}">
				<el-form ref="elForm" :model="dataForm" size="medium" label-width="110px" label-position="right" :disabled="!!isDetail" :rules="rules">
					<el-col :span="24">
						<el-form-item label="产品/套餐" prop="title">
							<el-input v-model="dataForm.title" placeholder="产品/套餐" clearable required :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="产品类型" prop="type">
							<el-radio-group v-model="dataForm.type" required :style='{}' >
								<el-radio v-for="(item, index) in typeOptions" :key="index" :label="item.id"  >{{item.fullName}}</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="会员等级" prop="level" v-if="dataForm.type==1">
							<el-select v-model="dataForm.level" placeholder="请选择" clearable :style='{"width":"100%"}' >
								<el-option v-for="(item, index) in levelOptions" :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="price">
							<el-input v-model="dataForm.price" placeholder="金额/单价" clearable required :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="原价" prop="originalPrice">
							<el-input v-model="dataForm.originalPrice" placeholder="请输入" clearable required :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="折扣" prop="discount">
							<el-input v-model="dataForm.discount" placeholder="请输入" clearable :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<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="12">
						<el-form-item label="有效期 (开始)" prop="validStart">
							<el-date-picker v-model="dataForm.validStart" placeholder="请选择" clearable :style='{"width":"100%"}' type='date' format="yyyy-MM-dd" value-format="timestamp" >
							</el-date-picker>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="有效期 (结束)" prop="validEnd">
							<el-date-picker v-model="dataForm.validEnd" placeholder="请选择" clearable :style='{"width":"100%"}' type='date' format="yyyy-MM-dd" value-format="timestamp" >
							</el-date-picker>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="允许使用优惠券" prop="isUseCoupo">
							<el-switch v-model="dataForm.isUseCoupo" :active-value="true" :inactive-value="false" >
							</el-switch>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="商品描述" prop="description">
							<NCC-Quill v-model="dataForm.description" placeholder="请输入内容..." required >
							</NCC-Quill>
						</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" v-if="dataForm.type !=1">
						<el-card class="mb-20" shadow="always">
							<el-col :span="6">
								<el-form-item label="限制使用人数" prop="isLimitUser">
									<el-switch v-model="dataForm.isLimitUser" :active-value="true" :inactive-value="false" >
									</el-switch>
								</el-form-item>
							</el-col>
							<el-col :span="8">
								<el-form-item label="限制人数" prop="limitUser">
									<el-input-number v-model="dataForm.limitUser"  :min="0"  placeholder="数字文本" :step="1" >
									</el-input-number>
								</el-form-item>
							</el-col>
							<el-col :span="8">
								<el-form-item label="最大限制人数" prop="limitUserMax">
									<el-input-number :min="dataForm.limitUser+1"  v-model="dataForm.limitUserMax" placeholder="不限制无需设置" :step="1" >
									</el-input-number>
								</el-form-item>
							</el-col>
							<el-col :span="6">
								<el-form-item label="是否免费" prop="free">
									<el-switch v-model="dataForm.free" :active-value="true" :inactive-value="false" >
									</el-switch>
								</el-form-item>
							</el-col>
							<el-col :span="8" v-if="dataForm.free">
								<el-form-item label="平台价格" prop="platFormPrice">
									<el-input v-model="dataForm.platFormPrice" placeholder="请输入" clearable :style='{"width":"100%"}' >
									</el-input>
								</el-form-item>
							</el-col>
							<el-col :span="10">
								<el-form-item label="允许分享类型" prop="allowShareUserType">
									<el-select v-model="dataForm.allowShareUserType" placeholder="请选择" clearable :style='{"width":"100%"}' multiple >
										<el-option v-for="(item, index) in allowShareUserTypeOptions" :key="index" :label="item.fullName" :value="item.enCode" ></el-option>
									</el-select>
								</el-form-item>
							</el-col>
						</el-card>
					</el-col>
					<el-col :span="24">
						<el-form-item label-width="0">
							<el-table :data="dataForm.baseProductDetailsList" size='mini'>
								<el-table-column type="index" width="50" label="序号" align="center" />
								<el-table-column prop="title" label="标题">
									<template slot-scope="scope">
										<el-input v-model="scope.row.title" placeholder="例如: 鸳鸯锅底 (1份)" clearable ></el-input>
									</template>
								</el-table-column>
								<el-table-column prop="isMultipleUse" label="允许多次使用">
									<template slot-scope="scope">
										<el-switch v-model="scope.row.isMultipleUse" :active-value="true" :inactive-value="false"  ></el-switch>
									</template>
								</el-table-column>
								<el-table-column prop="price" label="价格">
									<template slot-scope="scope">
										<el-input v-model="scope.row.price" placeholder="请输入" clearable ></el-input>
									</template>
								</el-table-column>
								<el-table-column prop="orgPrice" label="原价">
									<template slot-scope="scope">
										<el-input v-model="scope.row.orgPrice" placeholder="请输入" clearable ></el-input>
									</template>
								</el-table-column>
								<el-table-column prop="nums" label="数量">
									<template slot-scope="scope">
										<el-input-number v-model="scope.row.nums" placeholder="数字文本" ></el-input-number>
									</template>
								</el-table-column>
								<el-table-column label="操作" width="50">
									<template slot-scope="scope">
										<el-button size="mini" type="text" class="NCC-table-delBtn" @click="handleDelBaseProductDetailsEntityList(scope.$index)">删除</el-button>
									</template>
								</el-table-column>
							</el-table>
							<div class="table-actions" @click="addHandleBaseProductDetailsEntityList()">
								<el-button type="text" icon="el-icon-plus">新增</el-button>
							</div>
						</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-col :span="24" v-if="false" >
						<el-form-item label="修改用户" prop="lastModifyUserId">
							<el-input v-model="dataForm.lastModifyUserId" placeholder="系统自动生成" readonly >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24" v-if="false" >
						<el-form-item label="修改时间" prop="lastModifyTime">
							<el-input v-model="dataForm.lastModifyTime" placeholder="系统自动生成" readonly >
							</el-input>
						</el-form-item>
					</el-col>
				</el-form>
			</el-row>
		</div>
	</transition>
</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:'',
					title:undefined,
					type:"0",
					level:undefined,
					price:undefined,
					originalPrice:undefined,
					discount:undefined,
					tags:undefined,
					validStart:undefined,
					validEnd:undefined,
					description:undefined,
					images:[],
					isLimitUser:false,
					limitUser:undefined,
					free:false,
					platFormPrice:undefined,
					allowShareUserType:[],
					baseProductDetailsList:[],
					creatorUserId:undefined,
					creatorTime:undefined,
					lastModifyUserId:undefined,
					lastModifyTime:undefined,
				},
				rules: {
					title:[
						{
							required:true,
							message:'请输入产品/套餐',
							trigger:'blur'
						},
					],
					type:[
						{
							required:true,
							message:'请输入产品类型',
							trigger:'change'
						},
					],
					price:[
						{
							required:true,
							message:'请输入金额/单价',
							trigger:'blur'
						},
						{
							pattern:/(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/,
							message:'金额不正确',
							trigger:'blur'
						},
					],
					originalPrice:[
						{
							required:true,
							message:'请输入原价',
							trigger:'blur'
						},
						{
							pattern:/(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/,
							message:'请输入正确的金额',
							trigger:'blur'
						},
					],
					discount:[
						{
							pattern:/(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/,
							message:'请输入正确的折扣',
							trigger:'blur'
						},
					],
					description:[
						{
							required:true,
							message:'请输入商品描述',
							trigger:'blur'
						},
					], 
					platFormPrice:[
						{
							pattern:/(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/,
							message:'请输入正确的价格',
							trigger:'blur'
						},
					],
				},
				typeOptions:[{"fullName":"商品","id":"0"},{"fullName":"会员套餐","id":"1"}],
				levelOptions : [],
				allowShareUserTypeOptions : [],
			}
		},
		computed: {},
        watch: {},
        created() {
			this.getlevelOptions();
			this.getallowShareUserTypeOptions();
		},
		mounted() {
        },
		methods: {
			getlevelOptions(){
				previewDataInterface('303521699263939845').then(res => {
					this.levelOptions = res.data
				});
			},
			getallowShareUserTypeOptions(){
				getDictionaryDataSelector('303681362995447045').then(res => {
					this.allowShareUserTypeOptions = 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/Order/BaseProduct/' + this.dataForm.id,
							method: 'get'
						}).then(res =>{
							this.dataForm = res.data;
							if(!this.dataForm.images)this.dataForm.images=[];
						})
					}
				})
			},
			dataFormSubmit() {
				this.$refs['elForm'].validate((valid) => {
                    if (valid) {
                        if (!this.dataForm.id) {
                            request({
                                url: `/api/Order/BaseProduct`,
                                method: 'post',
                                data: this.dataForm,
                            }).then((res) => {
                                this.$message({
                                    message: res.msg,
                                    type: 'success',
                                    duration: 1000,
                                    onClose: () => {
                                        this.visible = false,
                                            this.$emit('refresh', true)
                                    }
                                })
                            })
                        } else {
                            request({
                                url: '/api/Order/BaseProduct/' + 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)
                                    }
                                })
                            })
                        }
                    }
                })
			},
			addHandleBaseProductDetailsEntityList() {
				let item = {
					title:undefined,
					// isUseCoupo:"1",
					price:undefined,
					orgPrice:undefined,
					nums:undefined,
				}
				this.dataForm.baseProductDetailsList.push(item)
			},
			handleDelBaseProductDetailsEntityList(index) {
				this.dataForm.baseProductDetailsList.splice(index, 1);
			},
		}
	}
</script>
