{extend name="main"}
{block name="style"}
<style type="text/css">
	.w-100px{width: 100px;}
	.w-200px{width: 200px;}
</style>
{/block}
{block name="body"}
<el-tabs v-model="activeName" class="bg-white px-3">
	<el-tab-pane label="服务信息" name="basic"></el-tab-pane>
	<el-tab-pane label="价格" name="price"></el-tab-pane>
	<el-tab-pane label="库存" name="stock"></el-tab-pane>
	<el-tab-pane label="自动化" name="auto"></el-tab-pane>
</el-tabs>
<div class="bg-white p-3">
	<div class="table-responsive">
		<el-form :model="form" @submit.native.prevent="submitForm" :rules="rules" ref="form" label-width="120px" :label-position="labelPosition">

			<div v-show="activeName=='basic'">
			  <el-form-item label="名称" prop="title">
			    <el-input v-model="form.title" class="col-12 col-sm-3 pl-0 pr-0" placeholder="Title" maxlength="30" show-word-limit></el-input>
			  </el-form-item>

			  <el-form-item label="服务人群" prop="is_company">
			    <el-radio-group class="ml-0 ml-sm-2" v-model="form.is_company">
			      <el-radio :label="1" @change="setVaType()">企业</el-radio>
			      <el-radio :label="0" @change="setVaType()">个人</el-radio>
			    </el-radio-group>
			  </el-form-item>

			  <el-form-item label="类型" prop="type">
			    <el-radio-group class="ml-0 ml-sm-2" v-model="form.type">
			      <el-radio :label="v.value" v-for="(v,index) in ValueAddedType" @change="setValueText(v)" v-if="showVa(v)">{{v.title}}</el-radio>
			    </el-radio-group>
			  </el-form-item>
			  
			  <el-form-item label="数量" prop="value">
			    <el-input v-model="form.value" class="col-12 col-sm-2 pl-0 pr-0" placeholder="Value">
			    	<template slot="append" v-if="value_text">{{value_text}}</template>
			    </el-input>
			    <p class="small text-secondary mb-0"></p>
			  </el-form-item>

			  <el-form-item label="状态" prop="status">
			    <el-radio-group class="ml-0 ml-sm-2" v-model="form.status">
			      <el-radio :label="1">上架[默认]</el-radio>
			      <el-radio :label="0">下架</el-radio>
			    </el-radio-group>
			  </el-form-item>

			  <el-form-item label="有效期" prop="effective_day">
			    <el-input v-model="form.effective_day" class="col-12 col-sm-1 pl-0 pr-0"></el-input>
			    <p class="small text-secondary mb-0">单位：天</p>
			  </el-form-item>

			  <el-form-item label="优先级" prop="level">
			    <el-input v-model="form.level" class="col-12 col-sm-1 pl-0 pr-0" placeholder="Level"></el-input>
			    <p class="small text-secondary mb-0">优先级倒序，3>2>1，最小为1，最大为999</p>
			  </el-form-item>

			</div>
			<div v-show="activeName=='price'">
			  <el-form-item label="售价" prop="price">
			    <el-input v-model="form.price" class="col-12 col-sm-1 pl-0 pr-0" placeholder="Price"></el-input>
			  </el-form-item>
			</div>
			<div v-show="activeName=='stock'">
				<div v-if="is_edit">
				  <el-form-item label="取消库存限制" prop="cancel_stock">
  					<el-checkbox v-model="form.cancel_stock" :true-label="1" :false-label="0">{{form.cancel_stock?'取消库存限制':'不变'}}</el-checkbox>
				  </el-form-item>
				  <el-form-item label="追加库存" prop="add_stock">
				    <el-input v-model.number="form.add_stock" class="col-12 col-sm-1 pl-0 pr-0" placeholder="Stock"></el-input>
				  </el-form-item>
				  <el-form-item label="库存">
			      	<div class="small text-secondary" v-if="form.cancel_stock||(!form.cancel_stock && !form.stock && !form.add_stock)">
			      		<div>原库存：不限制</div>
			      	</div>
				  	<div v-else>
				      	<div class="small text-secondary" v-if="form.add_stock">
				      		<div>原库存：{{form.stock}}</div>
				      		<div>原剩余库存：{{form.surplus_stock}}</div>
				      		<div>追加：{{form.add_stock}}</div>
				      		<div>追加后库存：{{form.stock+form.add_stock}}</div>
				      		<div>追加后剩余库存：{{form.surplus_stock+form.add_stock}}</div>
				      	</div>
				      	<div class="small text-secondary" v-else>
				      		<div>原库存：{{form.stock}}</div>
				      		<div>原剩余库存：{{form.surplus_stock}}</div>
				      	</div>
				  	</div>
				  </el-form-item>
				</div>
				<div v-else>
				  <el-form-item label="库存" prop="stock">
				    <el-input v-model.number="form.stock" class="col-12 col-sm-1 pl-0 pr-0" placeholder="Stock"></el-input>
				    <p class="small text-secondary mb-0">为0则不限制</p>
				  </el-form-item>

				  <el-form-item label="剩余库存" prop="surplus_stock">
				    <el-input v-model.number="form.surplus_stock" class="col-12 col-sm-1 pl-0 pr-0" placeholder="Stock"></el-input>
				    <p class="small text-secondary mb-0">为空则自动计算</p>
				  </el-form-item>

				  <el-form-item label="已售" prop="sold">
				    <el-input v-model.number="form.sold" class="col-12 col-sm-1 pl-0 pr-0" placeholder="Sold"></el-input>
				    <p class="small text-secondary mb-0">为空则自动计算</p>
				  </el-form-item>
				</div>
			</div>
			<div v-show="activeName=='auto'">
			  <el-form-item label="发布时间" prop="start_time">
			    <el-date-picker
			      v-model="form.start_time"
			      align="right"
			      type="date"
			      format="yyyy-M-d"
			      value-format="yyyy-M-d"
			      placeholder="选择日期">
			    </el-date-picker>
				<p class="small text-secondary mb-0">到发布时间后未上架状态会自动变更为已上架</p>
			  </el-form-item>

			  <el-form-item label="结束时间" prop="end_time">
			    <el-date-picker
			      v-model="form.end_time"
			      align="right"
			      type="date"
			      placeholder="选择日期"
			      format="yyyy-M-d"
			      value-format="yyyy-M-d">
			    </el-date-picker>
				<p class="small text-secondary mb-0">到结束时间后已上架状态会自动变更为已上架</p>
				<p class="small text-secondary mb-0">不设置结束时间则不会自动下架</p>
			  </el-form-item>
			</div>

		  <el-form-item>
		    <el-button type="primary" @click="submitForm('form')">立即保存</el-button>
		    <el-button @click="resetForm('form')">重置</el-button>
		  </el-form-item>
		</el-form>
	</div>
</div>
{/block}
{block name="script"}
<script type="text/javascript">
	{empty name="ValueAddedType"}
	var ValueAddedType=[];
	{else/}
	var ValueAddedType={:json_encode($ValueAddedType,JSON_UNESCAPED_UNICODE)};
	{/empty}
	{empty name="Find"}
	var url='/vip/addValueAdded';
	var is_edit=false;
	var formData={
				title:'',
				type:'',
				is_company:1,
				value:'',
				status:1,
				level:1,
				effective_day:3650,
				price:'',
				stock:0,
				surplus_stock:'',
				add_stock:'',
				sold:'',
				start_time:'',
				end_time:'',
			};
	{else/}
	var url='/vip/editValueAdded/{$Find.id}';
	var is_edit=true;
	var formData={:json_encode($Find,JSON_UNESCAPED_UNICODE)};
	{/empty}
</script>
<script type="text/javascript">
	var vm=new Vue({
		el:'#app',
		data:{
			view:XYBase.view,
			labelPosition:'right',
			activeName:'basic',
			form:formData,
			ValueAddedType:ValueAddedType,
			is_edit:is_edit,
			value_text:'',
	        rules: {
	          title: [
	            { required: true, message: '请输入名称', trigger: 'blur' },
	          ],
	          type: [
	            { required: true, message: '请选择类型', trigger: 'blur' },
	          ],
	          is_company: [
	            { required: true, message: '请选择服务人群', trigger: 'blur' },
	          ],
	          value: [
	            { required: true, message: '请输入数量', trigger: 'blur' },
	          ],
	          status: [
	            { required: true, message: '请选择状态', trigger: 'blur' },
	          ],
	          level: [
	            { required: true, message: '请输入优先级', trigger: 'blur' },
	          ],
	          effective_day: [
	            { required: true, message: '请输入有效期', trigger: 'blur' },
	          ],
	          price: [
	            { required: true, message: '请输入售价', trigger: 'blur' },
	          ],
	          stock: [
	            { required: true, message: '请输入库存', trigger: 'blur' },
	          ],
	        }
		},
		watch:{
		    view:XYBase.viewWatch,
		},
		created(){
			XYBase.created(this);
		},
		methods:{
			showVa(val){
				return val.is_company==this.form.is_company;
			},
			setVaType(val){
				this.form.type='';
				this.value_text='';
			},
			setValueText(val){
				this.value_text=val.type;
			},
			updateFormData(){
				var _this=this;
				$.getJSON(url,function(ret){
					if (ret){
						if (ret.code === 0) {
							_this.form=ret.data;
							return;
						}
					}
					window.location.reload();
				})
			},
			submitForm:function(formName) {
				var _this=this;
				var D=_this.form;
				_this.$refs[formName].validate((valid) => {
				  if (valid) {
					const loading = XYBase.openloading();
					$.ajax({
						url:url,
						data:D,
						type:'post',
						dataType:'json',
						success:function(ret){
					        loading.close();
					        if (ret) {
								if(ret.code == 0){
									XYBase.success(ret.msg);
									if(!_this.is_edit){
										_this.resetForm(formName);
									}else{
										_this.updateFormData();
									}
								}else{
									XYBase.msg(ret.msg);
								}
					        }else{
				          		XYBase.error(XYBase.config.system_network_error)
					        }
						},
						error:function(err){
					        loading.close();
				          	XYBase.error(XYBase.config.system_network_error)
						}
					});
				  } else {
					XYBase.warning('请先把表单填写完整。');
				    return false;
				  }
				});
			},
			resetForm:function(formName) {
				this.$refs[formName].resetFields();
			},
		}
	});
$(function(){
	reloadWindowSize();
	$(window).resize(function(){
		reloadWindowSize();
	});
});
function reloadWindowSize (argument) {
	var winW=$(window).width();
	if(winW <= 576){
		vm.labelPosition='top';
	}else{
		vm.labelPosition='right';
	}
}
</script>
{/block}