<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="formData.month" :clearable="false" :disabled="checked" type="month" placeholder="请选择月份" value-format="yyyy-MM"></el-date-picker>
				</el-form-item>
				<el-form-item label="部门">
					<el-cascader
					size="medium"
					  v-model="dept_name"
					  :options="dept_tree"
					  :props="{ multiple: true, value: 'id', label: 'name', children: '_child'}"
					  ref="deptRef"
					  filterable
					  collapse-tags
					  placeholder="全公司"
					  clearable
					></el-cascader>
				</el-form-item>
				<el-form-item label="人员搜索">
					<el-input size="medium" v-model="formData.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="all_integral_list" style="width: 100%" v-loading="loading">
          <el-table-column prop="employee_name" label="姓名" align="left">
			  <template slot-scope="scope">
			  	<div class="flex-box">
			  		<userImage :user_name="scope.row.name" :img_url="scope.row.img_url" width="50px" height="50px"></userImage>
			  		<div style="height: 50px;padding-left: 10px;" class="flex-box-v flex-h-zhu">
			  			<span>{{ scope.row.name }}</span>
			  		</div>
			  	</div>
			  </template>
          </el-table-column>
		  <el-table-column prop="count" label="部门">
		  	<template slot-scope="scope">
		  		<span style="font-size: 12px;max-width: 200px;" class="fontColorC font-flex-word">{{ scope.row.dept||'--' }}</span>
		  	</template>
		  </el-table-column>
          <el-table-column prop="count" label="奖票数量" align="left">
            <template slot-scope="scope">
              <span>{{ scope.row.count }}</span>
            </template>
          </el-table-column>
		  <el-table-column>
		  	<template slot-scope="scope">
		  		<span class="blue" style="cursor: pointer;" @click="openTable(scope.row)">查看奖票明细</span>
		  	</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="formData.page" 
		  :page-sizes="[10, 20, 50, 100]"  layout="total, sizes, prev, pager, next" :page-size="pageLimit1"   :total="total"> 
		  </el-pagination>
        </center>
      </div>
    </div>
  
	<!-- 对齐我的目标详情 -->
	<el-dialog title="奖票明细" :visible.sync="isShowTable" :append-to-body="true" width="900px" top="5%">
	  <div class="flex-box-ce" style="margin-bottom: 20px;">
		  <h1 class="flex-1" style="font-size: 18px;">{{selectItem.name}}的奖票明细-{{checked? '所有奖票':formData.month}}</h1>
		  <el-input v-model="keywordTabel" size="small" clearable placeholder="搜索内容" style="width: 250px;" maxlength="20"></el-input>
	  </div>
	  <div>
	    <el-table :data="tableList" style="width: 100%;" v-loading="loading2">
			<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>
				 <el-link v-if="scope.row.publisher_id==$getUserData().id||($supremeAuthority()!='dept_manager'&&$supremeAuthority()!='employee')" type="danger" :underline="false" @click="deleteItem(scope.row.id,scope.row.publisher_id)" style="padding-left: 10px;">{{scope.row.publisher_id==$getUserData().id ?'撤回奖票':'删除奖票'}}</el-link>
			  </template>
			</el-table-column>
			<template slot="empty">
				<noData></noData>
			</template>
	    </el-table>
	    <el-pagination style="text-align: center;margin-top: 20px;"  :current-page.sync="page" :page-sizes="[5,10,15]"  layout="total,prev,pager,next,sizes" :total="total2" @size-change="handleSizeChange2"  @current-change="handleCurrentChange2" :page-size="page_size"></el-pagination>
	  </div>
	  <div class="flex-box-end" style="margin-top: 20px;">
	  	<el-button @click="isShowTable=false">关 闭</el-button>
	  </div>
	</el-dialog>
	
	<!-- 检查单详情 -->
	<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,
      all_integral_list: null,
      formData: {
        month: this.$moment().format("YYYY-MM"),
        keyword: "",
        dept_ids: "",
        page: 1,
        page_size: 10
      },
      total: 0,
      //部门
      dept_name: [],
      dept_tree: [],
      pageLimit1: 10,
	  
	  page: 1,
	  total2: 0,
	  page_size: 10,
	  keywordTabel:'',
	  isShowTable:false,
	  tableList:[],
	  loading2:false,
	  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.month"(val, old_val) {
      this.formData.page = 1;
      this.get_all_integral();
    },
	'formData.keyword': {
	  deep: true,
	  handler: _debounce(function(val) {
		  this.formData.page = 1;
		  this.get_all_integral();
	  },1000)
	},
	'keywordTabel':{
	  deep: true,
	  handler: _debounce(function(val) {
		  this.getUserAwardList();
	  },1000)
	},
	checked(){
		this.formData.page = 1;
		this.get_all_integral();
	},
	dept_name(val, old_val) {
		 this.formData.page = 1;
		if (val.length !== 0) {
			let dept_ids=[];
			val.forEach((item)=>{
				dept_ids.push(...item);
			})
			let set =new Set(dept_ids);
			this.formData.dept_ids = [...set].toString();
		} else {
			this.formData.dept_ids = 0;
		}
		this.$nextTick(() => {
		  this.$refs.deptRef.dropDownVisible = false;
		  this.get_all_integral();
		});
	},
  },
  created() {
  	this.getDepartment(); //部门
  },
  mounted() {
    this.get_all_integral();
  },
  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(id,is){
		
		let str='';
		let num=1;
		if(is==this.$getUserData().id||this.detail.publisher_id==this.$getUserData().id){
			str='确定撤回此奖票?';
		}else {
			str='确定删除此奖票?';
			num=2;
		}
		
		this.$confirm(str, '提示', {
			confirmButtonText: '确定',
			cancelButtonText: '取消',
			type: 'warning'
		}).then(() => {
			this.$axios("post", "/api/integral/ticket/del",{id:id||this.detail.id}).then(res => {
				
				this.$message.success(num==1? "已撤回":'已删除');
				this.getUserAwardList();
				this.isShowDetail=false;
			})
		}).catch(() => {});
	},
	openTable(item){
		this.isShowTable=true;
		this.selectItem=item;
		this.page_size=10;
		this.keywordTabel="";
		this.getUserAwardList();
	},
	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.selectItem.id,
			keyword:this.keywordTabel,
		}
		if(this.checked){
			// formData.start_date="2010-01-01";
			// formData.end_date=this.$moment().format('YYYY-MM-DD')
		}else{
			formData.month=this.formData.month
		}
		
		this.$axios("get", "/api/integral/ticket", formData,'v2').then(res => {
		  this.tableList = res.data.data.list;
		  this.total2 = res.data.data.total;
		}).finally(() => {
			this.loading2 = false;
		});
	},
	// 页面变更
	handleCurrentChange2(val) {
	  this.page = val;
	  this.getUserAwardList(true);
	},
	// 页面跳转
	handleSizeChange2(val) {
	  this.page_size = val;
	  this.getUserAwardList();
	},
    //分页
    handleSizeChange(val) {
      this.pageLimit1 = val;
      this.formData.page_size = this.pageLimit1;
      this.get_all_integral();
    },
    handleCurrentChange(val) {
      this.formData.page = val;
      this.get_all_integral();
    },
    //请求数据
    get_all_integral() {
      this.loading = true;
	  let formData=JSON.parse(JSON.stringify(this.formData));
	  if(this.checked){
		delete formData.month
	  }
      this.$axios("get", "/api/integral/ticket/statistics", formData).then(res => {
          if (res.data.code == 1) {
            this.all_integral_list = res.data.data.list;
            this.total = res.data.data.total;
          } else {
            this.$message.error(res.data.data.msg);
          }
        }).finally(() => {
          this.loading = false;
        });
    },
	returnArr(child,arr){
	  if(child.length>0){
		child.forEach(item=>{
		  arr.push(item.value)
		  this.returnArr(item.children,arr)
		})
	  }
	},
    //获取部门
    getDepartment() {
      this.$axios("get", "/api/department/tree").then((res) => {
        this.dept_tree = this.getTreeData(res.data.data.list);
      });
    },
    // 递归判断列表，把最后的children设为undefined
    getTreeData(data) {
      for (var i = 0; i < data.length; i++) {
        if (data[i]._child.length < 1) {
          // children若为空数组，则将children设为undefined
          data[i]._child = undefined;
        } else {
          // children若不为空数组，则继续 递归调用 本方法
          this.getTreeData(data[i]._child);
        }
      }
      return data;
    },
  }
};
</script>

<style scoped lang="scss">
	.drawer_title {
		font-size: 18px;
		padding: 20px;
		border-bottom: 1px #efefef solid;
	}
  .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;
}
.el-checkbox.is-bordered.el-checkbox--medium {
	padding: 9px 20px 7px 10px;
}
.search ::v-deep .el-input-group__append:active .el-icon-search {
  color: #fff;
}
.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;
		}
	}
}
  .all{
    margin-top:10px;
    padding:10px 20px 0px 20px;
    min-height: calc(100vh - 204px);
    
  }
</style>
