{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="repeat"></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="50" show-word-limit></el-input>
			  </el-form-item>
			  <el-form-item label="提示" prop="tips">
			    <el-input v-model="form.tips" class="col-12 col-sm-3 pl-0 pr-0" placeholder="Tips" maxlength="100" 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="null">公共任务</el-radio>
			      <el-radio :label="1">企业</el-radio>
			      <el-radio :label="0">个人</el-radio>
			    </el-radio-group>
			  </el-form-item>

			  <el-form-item label="完成事件" prop="event">
			  	<el-select v-model="form.event" filterable clearable placeholder="请选择" @change="getRepeatStatus">
				    <el-option
				      v-for="v in eventData"
				      :key="v.value"
				      :label="v.text"
				      :value="v.value"
				      :disabled="v.is_company!=form.is_company">
				      <span class="text-left pr-3">{{ v.text }}</span>
				      <span style="float: right; color: #8492a6; font-size: 13px" v-if="v.is_company==null">公用</span>
				      <span style="float: right; color: #8492a6; font-size: 13px" v-else-if="v.is_company==1">企业专属</span>
				      <span style="float: right; color: #8492a6; font-size: 13px" v-else-if="v.is_company==0">个人专属</span>
				    </el-option>
				</el-select>
			  </el-form-item>
			  
			  <el-form-item label="奖励积分" prop="score">
			    <el-input v-model="form.score" class="col-12 col-sm-2 pl-0 pr-0" placeholder="Score">
			    </el-input>
			  </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="sort">
			    <el-input v-model="form.sort" class="col-12 col-sm-1 pl-0 pr-0" placeholder="Sort"></el-input>
			    <p class="small text-secondary mb-0">正序，1>2>3，最小为1，最大为99</p>
			  </el-form-item>

			  <el-form-item label="任务完成链接" prop="url">
			    <el-input v-model="form.url" class="col-12 col-sm-6 pl-0 pr-0" placeholder="URL"></el-input>
			  </el-form-item>

			</div>
			<div v-show="activeName=='repeat'">
			  <el-form-item label="日期重复" prop="repeat">
			    <el-radio-group class="ml-0 ml-sm-2" v-model="form.repeat">
			      <el-radio :label="0">不重复</el-radio>
			      <el-radio :label="1" :disabled="repeat_disabled">每日</el-radio>
			      <el-radio :label="2" :disabled="repeat_disabled">每周</el-radio>
			      <el-radio :label="3" :disabled="repeat_disabled">每月</el-radio>
			    </el-radio-group>
			  </el-form-item>

			  <el-form-item label="重复次数" prop="repeat_sum">
			    <el-input v-model="form.repeat_sum" class="col-12 col-sm-1 pl-0 pr-0"></el-input>
			    <p class="small text-secondary mb-0" v-if="form.repeat==0">重复{{form.repeat_sum}}次</p>
			    <p class="small text-secondary mb-0" v-if="form.repeat==1">每日{{form.repeat_sum}}次</p>
			    <p class="small text-secondary mb-0" v-if="form.repeat==2">每周{{form.repeat_sum}}次</p>
			    <p class="small text-secondary mb-0" v-if="form.repeat==3">每月{{form.repeat_sum}}次</p>
			  </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="complete_sum">
				    <el-input v-model.number="form.complete_sum" 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="ScoreTaskEvent"}
	var ScoreTaskEvent=[];
	{else/}
	var ScoreTaskEvent={:json_encode($ScoreTaskEvent,JSON_UNESCAPED_UNICODE)};
	{/empty}
	{empty name="Find"}
	var url='/score/add';
	var is_edit=false;
	var formData={
				title:'',
				tips:'',
				is_company:null,
				score:'',
				event:'',
				status:1,
				sort:99,
				stock:0,
				repeat:0,
				repeat_sum:1,
				url:'',
				surplus_stock:'',
				add_stock:'',
				complete_sum:'',
				start_time:'',
				end_time:'',
			};
	{else/}
	var url='/score/edit/{$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,
			is_edit:is_edit,
			eventData:ScoreTaskEvent,
			repeat_disabled:false,
	        rules: {
	          title: [
	            { required: true, message: '请输入名称', trigger: 'blur' },
	          ],
	          event: [
	            { required: true, message: '请选择完成事件', trigger: 'change' },
	          ],
	          score: [
	            { required: true, message: '请输入奖励积分', trigger: 'blur' },
	          ],
	          status: [
	            { required: true, message: '请选择状态', trigger: 'blur' },
	          ],
	          stock: [
	            { required: true, message: '请输入库存', trigger: 'blur' },
	          ],
	          repeat: [
	            { required: true, message: '请选择重复模式', trigger: 'blur' },
	          ],
	          repeat_sum: [
	            { required: true, message: '请输入重复次数', trigger: 'blur' },
	          ],
	        }
		},
		watch:{
		    view:XYBase.viewWatch,
		},
		created(){
			XYBase.created(this);
		},
		methods:{
			getRepeatStatus(){
				for (var i = 0; i < this.eventData.length; i++) {
					if (this.eventData[i].value==this.form.event) {
						if (this.eventData[i].repeat) {
							this.repeat_disabled=false;
						}else{
							this.form.repeat=0;
							this.repeat_disabled=true;
						}
					}
				}
			},
			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}