<template>
  <div>
	  <FormBox   style="margin: 0px;background-color: #fff;">
		<div class="form-item">
			<div class="form-label">姓名搜索</div>
			<div class="form-search">
				  <el-select style="margin: 0 10px;" v-model="formData.employee_id" size="medium" filterable placeholder="请输入或选择人员" clearable>
					<el-option v-for="item in employee_map" :key="item.id" :label="item.name" :value="item.id"></el-option>
				  </el-select>
			</div>
		</div>
		<div class="form-item">
			<div class="form-label">事件时间</div>
			<div class="form-search">
				<el-date-picker size="medium" value-format="yyyy-MM-dd" :picker-options="pickerOptions" v-model="date" type="daterange" range-separator="至" start-placeholder="开始日期"	end-placeholder="结束日期"> </el-date-picker>
			</div>
		</div>
		<div class="form-item">
			<div class="form-label">内容搜索</div>
			<div class="form-search">
				<el-input size="medium" v-model="formData.keyword" clearable placeholder="请输入" maxlength="20"></el-input>
			</div>
		</div>
	  </FormBox>

    <div class="all">
      <div>
        <el-table :data="list" style="width: 100%" v-loading="loading">
			<el-table-column prop="employee_name" label="姓名" align="left" width="200">
				  <template slot-scope="scope">
					<div class="flex-box">
						<userImage :img_url="scope.row.employee_img_url" :user_name="scope.row.employee_name" width="50px" height="50px"></userImage>
						<div style="height: 50px;padding-left: 10px;" class="flex-box-v flex-h-zhu">
							<span>{{scope.row.employee_name}}</span>
						</div>
					</div>
				  </template>
			</el-table-column>
			<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 moment from 'moment' // 时间库
import Steps from '@/components/Steps';
import Review from '@/components/Review';
import toLead from '@/components/toLead';
export default {
  components:{Steps,Review,toLead},
  data() {
    return {
	  pickerOptions: {
		shortcuts: [
			{
				text: '今天',
				onClick(picker) {
					const now = new Date(new Date().toLocaleDateString());
					const start = now.getTime();
					picker.$emit('pick', [moment(start).format('YYYY-MM-DD'), moment(start).format('YYYY-MM-DD')]);
				}
			},
			{
				text: '昨天',
				onClick(picker) {
					const now = new Date(new Date().toLocaleDateString());
					const start = now.getTime() - 60 * 60 * 24 * 1000;
					picker.$emit('pick', [moment(start).format('YYYY-MM-DD'),moment(start).format('YYYY-MM-DD')]);
				}
			},
			{
				text: '本周',
				onClick(picker) {
					picker.$emit('pick', [moment().startOf('isoWeek').format('YYYY-MM-DD'),moment().endOf('isoWeek').format('YYYY-MM-DD')]);
				}
			},
			{
				text: '上周',
				onClick(picker) {
					picker.$emit('pick', [moment().subtract(1, 'w').startOf('isoWeek').format('YYYY-MM-DD'),moment().subtract(1, 'w').endOf('isoWeek').format('YYYY-MM-DD')]);
				}
			},
			{
				text: '本月',
				onClick(picker) {
				const now = new Date();
					const startDate = new Date(now.getFullYear(), now.getMonth(), 1);
					const endDate = new Date(now.getFullYear(), now.getMonth() + 1, 0);
					picker.$emit('pick', [moment(startDate.getTime()).format('YYYY-MM-DD'), moment(endDate.getTime()).format('YYYY-MM-DD')]);
				}
			},
			{
				text: '上月',
				onClick(picker) {
					const now = new Date();
					const startDate = new Date(now.getFullYear() - (now.getMonth() > 0 ? 0 : 1), (now.getMonth() + 11) % 12, 1);
					const endDate = new Date(now.getFullYear(), now.getMonth(), 0);
					picker.$emit('pick', [moment(startDate.getTime()).format('YYYY-MM-DD'), moment(endDate.getTime()).format('YYYY-MM-DD')]);
				}
			},
			{
				text: '本季',
				onClick(picker) {
					const quarter=Math.ceil((new Date().getMonth()+1)/3)
					let str=moment().format(`YYYY-${(quarter-1)*3+1}-01`);
					let ent=moment(moment().format(`YYYY-${(quarter-1)*3+1}-01`)).add(2,`month`).endOf('month').format("YYYY-MM-DD");
					console.log(str,ent)
					picker.$emit('pick',[str,ent]);
				}
			},
			{
				text: '本年',
				onClick(picker) {
					let year=moment().format('YYYY');
					picker.$emit('pick', [year+'-01-01',year+'-12-31']);
				}
			},
		],
	  },		
      loading: false,
      list: null,
	  formData: {
	    keyword: "",
		employee_id:'',
	    publisher_id: this.$getUserData().id,
	  },
	  page:1,
	  page_size:10,
      total: 0,
      employee_map: [],
	  date:'',
	  
	  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: {
	'formData.keyword': {
	  deep: true,
	  handler: _debounce(function(val) {
		  this.getUserAwardList();
	  },1000)
	},
    date(val) {
      this.getUserAwardList();
    },
	'formData.employee_id'(){
	  this.getUserAwardList();
	}
  },
  mounted() {
	this.employee_map = this.$getCache('userList');
    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();
				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.formData.employee_id||0,
			publisher_id:this.formData.publisher_id,
			keyword:this.formData.keyword,
		}
		if(this.date){
			formData.start_date=moment(this.date[0]).format('YYYY-MM-DD')
			formData.end_date=this.date[1]
		}
		
		this.$axios("get", "/api/integral/ticket", formData,'v2').then(res => {
		  let list = res.data.data.list;
		  this.list = 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">
	.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 ::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;
}
  .all{
    margin-top:10px;
    padding:10px 20px 0px 20px;
    min-height: calc(100vh - 204px);
    
  }
</style>

