{extend name="main"}
{block name="body"}
<div class="bg-white p-3">
	<div class="pb-3">
        <el-button
            type="primary"
            size="mini"
            @click="window.open('/vip/addVipRule')">
            添加规则
        </el-button>
	</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
	        label="名称">
		      <template slot-scope="scope">
		          <p class="mb-1 font-weight-bold">{{ scope.row.title }}</p>
		          <p class="small text-secondary mb-0">{{ scope.row.desc }}</p>
		          <p class="small text-secondary mb-0">{{ scope.row.tips }}</p>
		      </template>
	      </el-table-column>
	      <el-table-column
	        prop="alias"
	        label="别名"
	        width="180">
	      </el-table-column>
	      <el-table-column
	        label="默认值"
	        width="100">
		      <template slot-scope="scope">
		          <p class="mb-0" v-if="scope.row.type=='input'">{{scope.row.value}}{{scope.row.unit}}</p>
		          <p class="mb-0" v-if="scope.row.type=='switch'">{{scope.row.value?'开启':'关闭'}}</p>
		      </template>
	      </el-table-column>
	      <el-table-column
	        prop="type"
	        label="类型"
	        width="100">
		      <template slot-scope="scope">
		          <p class="mb-0" v-if="scope.row.type=='input'">输入框</p>
		          <p class="mb-0" v-if="scope.row.type=='switch'">开关</p>
		      </template>
	      </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="setVipRuleStatus(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="setVipRuleStatus(scope.row);" 
		      		:loading="scope.row.status==2">禁止</el-button>
				</transition>
		      </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/editVipRule/'+scope.row.id)">
		            编辑
		        </el-button>
		      </template>
	      </el-table-column>
	    </el-table>
	</div>
</div>
{/block}
{block name="script"}
<script type="text/javascript">
	var vm=new Vue({
		el:'#app',
		data:{
			view:XYBase.view,
			search:{
				ajax:false,
			},
			tableData:[],
		},
		watch:{
		    view:XYBase.viewWatch,
		},
		created(){
			XYBase.created(this);
			this.initHtml();
		},
	    methods: {
	    	setVipRuleStatus(data){
	    		if (data.status==2) return;
	    		var status=1,oldStatus=data.status;
	    		if (data.status) status=0;
	    		data.status=2;
	    		$.getJSON('/vip/setVipRuleStatus/'+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);
	    			}
	    		});
	    	},
		    filterHandlerStatus(value, row, column) {
        		return row.status === 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.tableData=ret.data;
							}else{
								XYBase.msg(ret.msg);
							}
						}else{
							XYBase.error(XYBase.config.system_network_error);
						}
					}
				});
			}
	    }
	});
</script>
{/block}