<template>
  <div>
	  <FormBox  :isShowHeader="false">
	  	<template #footer>
	  		<el-form ref="form" :inline="true" label-width="70PX">
	  			<el-form-item label="时间">
					<el-date-picker size="medium" v-model="month" :disabled="checked" type="month" :clearable="false" placeholder="请选择月份" value-format="yyyy-MM"></el-date-picker>
	  			</el-form-item>
	  			<el-form-item label="内容搜索">
	  				<el-input size="medium" v-model="keyword" clearable placeholder="内容搜索"  maxlength="20"></el-input>
	  			</el-form-item>
				<el-form-item>
					<el-checkbox v-model="checked" border size="medium">查看所有累计奖票</el-checkbox>
				</el-form-item>
	  		</el-form>
	  	</template>
	  </FormBox>

    <div class="all">
      <div>
        <el-table :data="list" style="width: 100%" v-loading="loading">
			<el-table-column prop="name" min-width="300" label="奖票事件">
			  <template slot-scope="scope">
				 <span>{{scope.row.remark.customize||scope.row.remark.rule}}</span>
			  </template>
			</el-table-column>
			<el-table-column prop="ct" label="事件时间" width="100">
				<template slot-scope="scope">
					<span>{{$moment(scope.row.date+'').format("YYYY-MM-DD")}}</span>
				</template>
			</el-table-column>
			<el-table-column label="操作" align="right">
			  <template slot-scope="scope">
				 <el-link type="primary" :underline="false" @click="openDetail(scope.row)">查看详情</el-link>
			  </template>
			</el-table-column>
			<template slot="empty">
				<noData></noData>
			</template>
        </el-table>
        <center style="padding: 20px 0;">
          <el-pagination background  @size-change="handleSizeChange" @current-change="handleCurrentChange"  :current-page="page" 
		  :page-sizes="[10, 20, 50, 100]"  layout="total, sizes, prev, pager, next" :page-size="page_size"  :total="total"> 
		  </el-pagination>
        </center>
      </div>
    </div>
  
	<!-- 检查单详情 -->
	<BrawerBox :showDrawer.sync="isShowDetail"  drawerTitle="奖票详情">
	  <template slot="main">
	    <div>
	      <div class="flex-box-v">
	        <div class="message-box">
	          <div class="flex-box-ce">
	            <div class="fontColorC">奖票对象</div>
	            <div class="flex-1">{{detail.employee_name}}</div>
	          </div>
			  <div class="flex-box-ce">
				<div class="fontColorC">事件时间</div>
				<div class="flex-1">{{$moment(detail.date+'').format("YYYY-MM-DD")}}</div>
			  </div>
			  <div class="flex-box-ce">
				<div class="fontColorC">发放人</div>
				<div class="flex-1">{{detail.publisher_name||'--'}}</div>
			  </div>
			  <div class="flex-box-ce">
				<div class="fontColorC">发放时间</div>
				<div class="flex-1">{{detail.ct}}</div>
			  </div>
			  <div class="flex-box-ce">
			    <div class="fontColorC">奖票事件</div>
			    <div class="flex-1">{{detail.remark.customize||detail.remark.rule}}</div>
			  </div>
			</div>
	      </div>
	      <div class="message-box" v-if="detail.event">
			 <div style="font-size: 16px;margin-bottom: 20px;">对应积分事件</div>
			 <div class="flex-box" @click="open_detail()" style="cursor: pointer;background-color: rgb(248, 252, 255);margin-bottom: 14px;position: relative;padding: 8px;border-radius: 5px;">
				 <div class="flex-1" style="padding-right: 20px;">
					 {{detail.event.remark.customize||detail.event.remark.rule}}
				 </div>
				 <div class="red" v-if="detail.event.point>0">+{{detail.event.point}} {{detail.event.pt_id==3? 'B分':'A分'}}</div>
				 <div class="green" v-else>{{detail.event.point}} {{detail.event.pt_id==3? 'B分':'A分'}}</div>
			 </div>
	      </div>
	    </div>
	  </template>
	  <template slot="footer">
	      <el-button type="danger" plain @click="deleteItem" v-if="detail.publisher_id==$getUserData().id||($supremeAuthority()!='dept_manager'&&$supremeAuthority()!='employee')">{{detail.publisher_id==$getUserData().id ?'撤回奖票':'删除奖票'}}</el-button>
	      <div class="flex-1"></div>
	      <el-button  plain @click="isShowDetail=false">关 闭</el-button>
	  </template>
	</BrawerBox>
  
	  <el-drawer title="事件详情" :visible.sync="detail_popup" ref="drawer"  :with-header="false" :width="'500px'" direction="rtl">
		<div class="drawer_title flex-box-ce">
			<span class="flex-1">事件详情</span>
			<i  @click="detail_popup=false" style="cursor: pointer;" class="el-icon-close"></i>
		</div>
		<div class="detail_popup" v-loading="detail_loading" v-if="detail_info !== null">
			<el-row style="padding-bottom:10px;border-bottom:1px #f8f8f8 solid;">
				<div class="flex-box flex-v-ce">
					<userImage :user_name="detail_info.employee_name" :img_url="detail_info.img_url" width="50px" height="50px" fontSize="1"></userImage>
					<span style="font-size: 18px;line-height:50px; margin-left:10px;margin-right:4px;">{{ detail_info.employee_name }}</span>
					<span class="red point" v-if="detail_info.point >= 0">+{{ detail_info.point }} {{ point_name(detail_info.pt_id) }}</span>
					<span class="green point" v-if="detail_info.point < 0">{{ detail_info.point }} {{ point_name(detail_info.pt_id) }}</span>
				</div>
			</el-row>
			<el-row >
				<el-col :span="6">事件内容</el-col>
				<el-col :span="18" v-if="detail_info.remark">{{ detail_info.remark && (detail_info.remark.customize || detail_info.remark.rule) }}</el-col>
			</el-row>
			<el-row v-show="detail_info.files && detail_info.files.length > 0">
				<el-col :span="18" :offset="6">
					<el-image
						v-for="(itme, index) in detail_info.files"
						:key="index"
						style="width: 80px; height: 80px;margin: 0 5px;"
						:src="itme"
						:preview-src-list="detail_info.files"
					></el-image>
				</el-col>
			</el-row>
			<el-row v-show="detail_info.rule_list">
				<el-col :span="6">规则分类</el-col>
				<el-col :span="18">{{ detail_info.rule_list }}</el-col>
			</el-row>
			<el-row v-show="detail_info.source_type_mark">
				<el-col :span="6">来源类型</el-col>
				<el-col :span="18">{{ detail_info.source_type_mark }}</el-col>
			</el-row>
			<el-row v-show="detail_info.recorder_name">
				<el-col :span="6">记录人</el-col>
				<el-col :span="18">{{ detail_info.recorder_name }}</el-col>
			</el-row>
			<el-row v-show="detail_info.date">
				<el-col :span="6">事件时间</el-col>
				<el-col :span="18">{{ detail_info.date }}</el-col>
			</el-row>
			<el-row v-show="detail_info.complete_task.time">
				<el-col :span="6">完成时间</el-col>
				<el-col :span="18">{{ detail_info.complete_task.time }}</el-col>
			</el-row>
			<el-row v-show="detail_info.complete_task.remark">
				<el-col :span="6">完成备注</el-col>
				<el-col :span="18">{{ detail_info.complete_task.remark }}</el-col>
			</el-row>
			<el-row v-show="detail_info.complete_task && detail_info.complete_task.files && detail_info.complete_task.files.length > 0">
				<el-col :span="18" :offset="6">
					<el-image
						v-for="(itme, index) in detail_info.complete_task.files"
						:key="index"
						style="width: 80px; height: 80px;margin: 0 5px;"
						:src="itme"
						:preview-src-list="detail_info.complete_task.files"
					></el-image>
				</el-col>
			</el-row>
			<div v-show="detail_info.rule_id">
				<p class="row_title">规则依据</p>
				<el-row>
					<el-col :span="6">规则分类</el-col>
					<el-col :span="18">{{ detail_info.rule_list }}</el-col>
				</el-row>
				<el-row v-show="detail_info.remark.rule">
					<el-col :span="6">积分规则</el-col>
					<el-col :span="18">{{ detail_info.remark.rule }}</el-col>
				</el-row>
				<el-row>
					<el-col :span="6">积分</el-col>
					<el-col :span="18" v-show="detail_info.rule_item.min_point == detail_info.rule_item.max_point">{{ detail_info.rule_item.min_point }} {{ point_name(detail_info.pt_id) }}</el-col>
					<el-col :span="18" v-show="detail_info.rule_item.min_point != detail_info.rule_item.max_point">
						{{ detail_info.rule_item.min_point }} ~ {{ detail_info.rule_item.max_point }} {{ point_name(detail_info.pt_id) }}
					</el-col>
				</el-row>
			</div>
			<div v-show="detail_info.process"><Steps :process="detail_info.process"></Steps></div>
			<div v-show="detail_info.dc_remark.flow"><Review :process="detail_info.dc_remark.flow" :status="detail_info.dc_status"></Review></div>
		</div>
		<div  style="position: absolute; bottom: 20px;right: 20px;left: 20px;" class="flex-box-ce">
<!-- 			<el-button type="primary" v-if="!detail_info.has_ticket" @click="deleteInBatches({id:detail_info.event_id},true)">发放奖票</el-button>
			<el-button type="primary" v-else @click="openDetail({id:detail_info.event_id})">查看奖票</el-button> -->
			<div class="flex-1"></div>
			<el-button @click="detail_popup=false">取消</el-button>
		</div>
	  </el-drawer>
  </div>
</template>
<script>
import {_debounce} from '@/api/auth';
import Steps from '@/components/Steps';
import Review from '@/components/Review';
import toLead from '@/components/toLead';
export default {
 components:{Steps,Review,toLead},
  data() {
    return {
	  checked:false,
      loading: false,
      list: null,
	  month: this.$moment().format("YYYY-MM"),
	  page:1,
	  page_size:10,
      total: 0,
	  keyword:'',
      //部门
      dept_name: [],
      dept_tree: [],
	  
	  isShowDetail:false,
	  detail:{remark:{},event:{remark:{}},date:''},
	  selectItem:{},
	  
	  detail_info: {complete_task:{},remark:{},rule_item:{},dc_remark:{},pt_id:3},
	  detail_popup: false,
	  detail_loading: false,
    };
  },
  watch: {
	'keyword': {
	  deep: true,
	  handler: _debounce(function(val) {
		  this.getUserAwardList();
	  },1000)
	},
    month(val, old_val) {
      this.getUserAwardList();
    },
	checked(){
	  this.getUserAwardList();
	}
  },
  mounted() {
    this.getUserAwardList();
  },
  methods: {
	  point_name(id) {
	    	let point_types=this.$getTyps();
	    	return point_types.find(item => {
	    		if (item.id == id) {
	    			return item.name;
	    		}
	    	}).name;
	  },
	 open_detail() {
	 	this.detail_popup = true;
	 	let data = {event_id:this.detail.event_id};
	 	this.detail_loading = true;
	 	this.$axios('get', '/api/integral/statistics/integral/info',data).then(res => {
	 		this.detail_info = res.data.data;
	 	}).finally(() => {
	 		this.detail_loading = false;
	 	});
	 }, 
	deleteItem(){
		this.$confirm(`确定${this.detail.publisher_id==this.$getUserData().id? '撤回':'删除'}此奖票?`, '提示', {
			confirmButtonText: '确定',
			cancelButtonText: '取消',
			type: 'warning'
		}).then(() => {
			this.$axios("post", "/api/integral/ticket/del",{id:this.detail.id}).then(res => {
				this.$message.success(this.detail.publisher_id==this.$getUserData().id? "已撤回":'已删除');
				this.getUserAwardList(true);
				this.isShowDetail=false;
			})
		}).catch(() => {});
	},
	openDetail(item){
		this.$axios("get", "/api/integral/ticket/info",{id:item.id}).then(res => {
			this.detail=res.data.data;
			this.isShowDetail=true;
		})
	},
	getUserAwardList(is){
		this.loading2 = true;
		!is? this.page=1:'';
		let formData={
			page: this.page,
			page_size: this.page_size,
			employee_id:this.$getUserData().id,
			keyword:this.keyword,
		}
		if(this.checked){
			formData.start_date="2010-01-01";
			formData.end_date=this.$moment().format('YYYY-MM-DD')
		}else{
			formData.month=this.month
		}
		
		this.$axios("get", "/api/integral/ticket", formData,'v2').then(res => {
		  this.list = res.data.data.list;
		  this.total = res.data.data.total;
		}).finally(() => {
			this.loading2 = false;
		});
	},
	// 页面变更
	handleCurrentChange(val) {
	  this.page = val;
	  this.getUserAwardList(true);
	},
	// 页面跳转
	handleSizeChange(val) {
	  this.page_size = val;
	  this.getUserAwardList();
	},
  }
};
</script>

<style scoped lang="scss">
	.all{
	  margin-top:10px;
	  padding:10px 20px 0px 20px;
	  min-height: calc(100vh - 204px);
	  
	}
	.drawer_title {
		font-size: 18px;
		padding: 20px;
		border-bottom: 1px #efefef solid;
	}
	.detail_popup {
		padding: 20px;
		height: calc(100vh - 140px);
		overflow: auto;
		padding-bottom: 100px;
		.row_title {
			position: relative;
			margin: 0 0 20px 0;
			padding-top: 12px;
			font-size: 16px;
			color: #303133;
			line-height: 22px;
		}
		.row_title:before {
			position: absolute;
			top: 0;
			content: ' ';
			width: 100%;
			border-top: 1px #f8f8f8 solid;
		}
		.el-row {
			margin-bottom: 10px;
			font-size: 14px;
			.el-col-4 {
				color: #606266;
			}
		}
	}
  .message-box .label{
    font-size: 18px;
    font-weight: 600;
    margin-bottom: 20px;
  }
  .message-box .flex-box-ce{
    margin-bottom:16px ;
  }
  .message-box .fontColorC{
    width: 100px;
  }
.el-date-editor.el-input {
  width: auto;
}
.date-picker-width {
  width: 145px !important;
}
.search{
	margin: 0 10px;
}
.search ::v-deep .el-input-group__append{
  background: #FFF;
}
.search ::v-deep .el-input-group__append:active {
  background: #26a2ff;
}
.search ::v-deep .el-input-group__append:active .el-icon-search {
  color: #fff;
}
.el-checkbox.is-bordered.el-checkbox--medium {
	padding: 9px 20px 7px 10px;
}
</style>