{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('/vip/addValueAdded')">
	            创建增值服务
	        </el-button>
		</div>
		<div class="small text-secondary">当前共有：{{search.total}}个增值服务</div>
	</div>
	<div class="table-responsive">
		<el-table
			ref="tableList"
	      	:data="tableData"
    		v-loading="search.ajax"
    		style="width: 100%;"
    		header-cell-class-name="table-header-DDE6FB">
	      <el-table-column
	        prop="id"
	        label="ID"
	        width="60">
	      </el-table-column>
	      <el-table-column
	        prop="title"
	        label="名称">
	      </el-table-column>
	      <el-table-column
	        prop="value"
	        label="数量"
	        width="100">
		      <template slot-scope="scope">
		      	<div class="small text-secondary">
		      		{{scope.row.value}}{{scope.row.unit}}
		      	</div>
		      </template>
	      </el-table-column>
	      <el-table-column
	      	prop="type_text"
	        label="类型"
	        width="120">
	      </el-table-column>
	      <el-table-column
	        label="服务人群"
	        width="100">
		      <template slot-scope="scope">
		      	<transition>
		      		<span class="text-primary" v-if="scope.row.is_company==1">企业</span>
		      		<span class="text-success" v-if="scope.row.is_company==0">个人</span>
				</transition>
		      </template>
	      </el-table-column>
	      <el-table-column
	        prop="price"
	        label="价格"
	        width="100">
	      </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="sold"
	        label="已售"
	        width="80">
	      </el-table-column>
	      <el-table-column
	        prop="level"
	        label="优先级"
	        width="80">
	      </el-table-column>
	      <el-table-column
	        prop="effective_day"
	        label="有效期(天)"
	        width="100">
	      </el-table-column>
	      <el-table-column
	        label="状态"
	        width="80">
		      <template slot-scope="scope">
		      	<transition>
		      		<el-button type="success" size="minism"
		      		v-if="scope.row.status==1 || scope.row.status==2" 
		      		@click="setValueAddedStatus(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="setValueAddedStatus(scope.row);" 
		      		:loading="scope.row.status==2">下架</el-button>
				</transition>
		      </template>
	      </el-table-column>
	      <el-table-column
	        prop="start_time"
	        label="开始~结束时间"
	        width="200">
		      <template slot-scope="scope">
		      	<div class="flex small">
		          <div>{{ scope.row.start_time }}</div>
		          <div class="px-1" v-if="scope.row.end_time">~</div>
		          <div>{{ scope.row.end_time }}</div>
		      	</div>
		      </template>
	      </el-table-column>
	      <el-table-column
	        label="创建/更新时间"
	        width="180">
		      <template slot-scope="scope">
		      	<div class="small">
		          <div>{{ scope.row.create_time }}</div>
		          <div>{{ scope.row.update_time }}</div>
		      	</div>
		      </template>
	      </el-table-column>
	      <el-table-column
	        label="/"
	        width="80">
		      <template slot-scope="scope">
		        <el-button
		            type="primary"
		            plain
		            round
		            size="minism"
		            @click="window.open('/vip/editValueAdded/'+scope.row.id)">
		            编辑
		        </el-button>
		      </template>
	      </el-table-column>
	    </el-table>
	</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}
</script>
<script type="text/javascript">
	var vm=new Vue({
		el:'#app',
		data:{
			view:XYBase.view,
			ValueAddedType:ValueAddedType,
			search:{
				ajax:false,
				total:0
			},
			tableData:[],
		},
		watch:{
		    view:XYBase.viewWatch,
		},
		created(){
			XYBase.created(this);
			this.initHtml();
		},
	    methods: {
	    	setValueAddedStatus(data){
	    		if (data.status==2) return;
	    		var status=1,oldStatus=data.status;
	    		if (data.status) status=0;
	    		data.status=2;
	    		$.getJSON('/vip/setValueAddedStatus/'+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}