{extend name="main"}
{block name="style"}
<style type="text/css">
	.flex-lable{width: 140px;color: #6c757d;text-align: right;padding-right: 10px;}
</style>
{/block}
{block name="body"}
<div class="bg-white p-3">
	<div class="flex pb-3">
		<div class="flex-1">
	        <el-button
	            type="primary"
	            size="mini"
	            @click="window.open('/score/add')">
	            创建积分任务
	        </el-button>
		</div>
		<div class="small text-secondary">当前共有：{{search.total}}个积分任务</div>
	</div>
	<div class="table-responsive">
		<el-table
	      	:data="tableData"
    		stripe
    		v-loading="search.ajax"
    		style="width: 100%;">
	      <el-table-column
	        prop="id"
	        label="ID"
	        width="60">
	      </el-table-column>
	      <el-table-column
	        label="名称">
		      <template slot-scope="scope">
		      	<div>{{scope.row.title}}</div>
		      	<div class="small text-secondary">{{scope.row.tips}}</div>
		      </template>
	      </el-table-column>
	      <el-table-column
	        prop="score"
	        label="奖励积分"
	        width="100">
	      </el-table-column>
	      <el-table-column
	        label="完成事件"
	        width="120">
		      <template slot-scope="scope">
		      	<span v-for="(v,index) in ScoreTaskEvent" v-if="v.value==scope.row.event">{{v.text}}</span>
		      </template>
	      </el-table-column>
	      <el-table-column
	        label="服务人群"
	        width="120"
	        :filter-multiple="false"
		    :filters="[{text: '企业', value: 1}, {text: '个人', value: 0}]"
		    :filter-method="filterHandlerIsCompany">
		      <template slot-scope="scope">
		      	<transition>
		      		<el-button size="minism" v-if="scope.row.is_company==null">公共任务</el-button>
		      		<el-button type="primary" size="minism" v-if="scope.row.is_company==1">企业</el-button>
		      		<el-button type="success" size="minism" v-if="scope.row.is_company==0">个人</el-button>
				</transition>
		      </template>
	      </el-table-column>
	      <el-table-column
	        label="库存"
	        width="140">
		      <template slot-scope="scope">
		      	<div class="small text-secondary" v-if="scope.row.stock">
		      		<div>库存：{{scope.row.stock}}</div>
		      		<div>剩余库存：{{scope.row.surplus_stock}}</div>
		      	</div>
		      	<div class="small text-secondary" v-else>不限制</div>
		      </template>
	      </el-table-column>
	      <el-table-column
	        prop="complete_sum"
	        label="已完成"
	        width="100">
	      </el-table-column>
	      <el-table-column
	        label="重复"
	        width="100">
		      <template slot-scope="scope">
		      	<div class="small text-secondary" v-if="scope.row.repeat==0">重复{{scope.row.repeat_sum}}次</div>
			    <div class="small text-success mb-0" v-if="scope.row.repeat==1">每日{{scope.row.repeat_sum}}次</div>
			    <div class="small text-warning mb-0" v-if="scope.row.repeat==2">每周{{scope.row.repeat_sum}}次</div>
			    <div class="small text-primary mb-0" v-if="scope.row.repeat==3">每月{{scope.row.repeat_sum}}次</div>
		      </template>
	      </el-table-column>
	      <el-table-column
	        prop="sort"
	        label="排序"
	        width="100">
	      </el-table-column>
	      <el-table-column
	        label="状态"
	        width="80"
	        :filter-multiple="false"
		    :filters="[{text: '上架', value: 1}, {text: '下架', value: 0}]"
		    :filter-method="filterHandlerStatus">
		      <template slot-scope="scope">
		      	<transition>
		      		<el-button type="success" size="minism"
		      		v-if="scope.row.status==1 || scope.row.status==2" 
		      		@click="setStatus(scope.row);" 
		      		:loading="scope.row.status==2">上架中</el-button>
		      		<el-button type="info" size="minism" 
		      		v-else="scope.row.status==0 || scope.row.status==2" 
		      		@click="setStatus(scope.row);" 
		      		:loading="scope.row.status==2">下架</el-button>
				</transition>
		      </template>
	      </el-table-column>
	      <el-table-column
	        prop="start_time"
	        label="开始时间"
	        width="120">
	      </el-table-column>
	      <el-table-column
	        prop="end_time"
	        label="结束时间"
	        width="120">
	      </el-table-column>
	      <el-table-column
	        label="时间线"
	        width="180">
		      <template slot-scope="scope">
		        <el-popover trigger="hover" placement="top">
		          <p>创建时间: {{ scope.row.create_time }}</p>
		          <p>更新时间: {{ scope.row.update_time }}</p>
		          <p>发布时间: {{ scope.row.start_time }}</p>
		          <p>结束时间: {{ scope.row.end_time }}</p>
		          <div slot="reference" class="name-wrapper">
		            <el-tag size="small" type="info">{{ scope.row.create_time }}</el-tag>
		          </div>
		        </el-popover>
		      </template>
	      </el-table-column>
	      <el-table-column
	        label="/"
	        width="80">
		      <template slot-scope="scope">
		        <el-button
		            type="primary"
		            size="minism"
		            @click="window.open('/score/edit/'+scope.row.id)">
		            编辑
		        </el-button>
		      </template>
	      </el-table-column>
	    </el-table>
	</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}
</script>
<script type="text/javascript">
	var vm=new Vue({
		el:'#app',
		data:{
			view:XYBase.view,
			ScoreTaskEvent:ScoreTaskEvent,
			search:{
				ajax:false,
				total:0
			},
			tableData:[],
		},
		watch:{
		    view:XYBase.viewWatch,
		},
		created(){
			XYBase.created(this);
			this.initHtml();
		},
	    methods: {
	    	setStatus(data){
	    		if (data.status==2) return;
	    		var status=1,oldStatus=data.status;
	    		if (data.status) status=0;
	    		data.status=2;
	    		$.getJSON('/score/setStatus/'+data.id+'/'+status,function(ret){
	    			data.status=oldStatus;
	    			if (ret) {
	    				if (ret.code === 0) {
	    					data.status=status;
	    				}else{
	    					XYBase.msg(ret.msg);
	    				}
	    			}else{
						XYBase.error(XYBase.config.system_network_error);
	    			}
	    		});
	    	},
			resetForm:function(formName) {
				this.$refs[formName].resetFields();
			},
		    filterHandlerStatus(value, row, column) {
        		return row.status === value;
		    },
		    filterHandlerType(value, row, column) {
        		return row.type === value;
		    },
		    filterHandlerIsCompany(value, row, column) {
        		return row.is_company === value;
		    },
	      	initHtml() {
	      		var _this=this;
	      		_this.search.ajax=true;
				$.ajax({
					url:'',
					type:'get',
					data:_this.search,
					dataType:'json',
					error:function(err){
	      				_this.search.ajax=false;
						XYBase.error(XYBase.config.system_network_error);
					},
					success:function(ret){
	      				_this.search.ajax=false;
						if (ret) {
							if (ret.code === 0) {
								_this.search.total=ret.data.total;
								_this.tableData=ret.data.data;
							}else{
								XYBase.msg(ret.msg);
							}
						}else{
							XYBase.error(XYBase.config.system_network_error);
						}
					}
				});
			}
	    }
	});
</script>
{/block}