<template>
	<view class="collector">
		<u-navbar ref="uNavbar" :border-bottom="false" :title-width="340" :back-icon-color="setNav.backColor" :is-back="setNav.isdisPlayNavTitle"
		 :title-size="setNav.size" :title-bold="setNav.bold" :title="setNav.navTitle" :title-color="setNav.color" :background="setNav"></u-navbar>
		<view class="title">
			当前还 <text>{{notRead}}人</text> 未读
		</view>
		<view class="table">
			<view class="thead">
				<view class="tr">
					<view class="th check-th"></view>
					<view class="th">负责人</view>
					<view class="th">单位</view>
					<view class="th">开始时间</view>
					<view class="th">结束时间</view>
				</view>
			</view>
			<!-- <checkbox-group @change="checkboxChange"> -->
				<view class="tbody" v-if="data.length">
					<view class="tr" v-for="(item, index) in data" :key="index">
						<!-- <view class="">
							<checkbox :value="String(item.id)" :checked="item.checked" color="#fff" />
						</view> -->
						<view class="td check-td check">
							<image v-if="!item.checked" @click="selected(item)" class="normal" src="../../../static/img/icon_qx_normal.png"></image>
							<image v-else class="selected" @click="selected(item)" src="../../../static/img/icon_qx_selected.png"></image>
						</view>
						<view class="td">
							{{item.receive_user_name}}
						</view>
						<view class="td">
							{{item.receive_dept_name}}
						</view>
						<view class="td">
							{{item.send_message_time}}
						</view>
						<view class="td">
							{{item.receive_time == null ? '未结束' : item.receive_time}}
						</view>
					</view>
				</view>
			<!-- </checkbox-group> -->
			<view class="no-data" v-else>暂无数据</view>
		</view>
		<view class="btns" v-if="data.length">
			<view class="left" @click="allchange()">
				<view class="check">
					<image v-if="!checked"  class="normal" src="../../../static/img/icon_qx_normal.png"></image>
					<image v-else class="selected"  src="../../../static/img/icon_qx_selected.png"></image>
				</view>
				<view>全选</view>
				<!-- <checkbox-group @change=""> -->
					<!-- <label class="check-td">
						<checkbox value="all" :checked="" color="#fff" />全选
					</label> -->
				<!-- </checkbox-group> -->
			</view>
			<view class="right" @click="insertList">
				催收文
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				checked: false,
				setNav: {
					backgroundColor: '#fff', //背景色
					color: '#333', //字体颜色
					bold: true,
					size: '36',
					backColor: '#333',
					isdisPlayNavTitle: true, //是否显示返回按钮
					navTitle: '选择催收文人员' //导航标题
				},
				data: [],
				total: 0,
				notRead: 0,
				pageNo: 1
			}
		},
		onReachBottom() {
			if (this.data.length >= this.total) {
				return
			}
			this.pageNo += 1
			this.statistic()
		},
		onLoad(options) {
			this.id = options.id
			this.statistic()
			this.init()
		},
		methods: {
			selected(item) {
				item.checked = !item.checked
			},
			async insertList() {
				let data = []
			
				this.data.forEach(item => {
					if (item.checked) {
						data.push({
							"message_id": item.message_id,
							"message_title": item.message_title,
							"send_user_name": item.send_user_name,
							"send_user_id": item.send_user_id,
							"message_time": item.send_message_time,
							"receive_user_id": item.receive_user_id,
							"receive_user_name": item.receive_user_name
						})
					}
				})
				
				if (!data.length) {
					return uni.showToast({
						title: '请勾选催收人员'
					})
				}
				 
				let res = await this.$u.post('boman-web-core/p/cs/insertList', {
					 "tableName": "urge_read_message",
					 "dataList":  data
				})
				if (res.code == 200) {
					uni.showToast({
						title: '催收成功',
						duration: 2500
					})
					this.pageNo = 1
					this.statistic()
					this.init()
					this.checked = false
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none',
						duration: 2000
					})
				}
			},
			async statistic() {
				// 
				let res = await this.$u.post('boman-web-core/p/cs/queryList', {
					 "table": "boman_message_receive",
					 "pageNo": this.pageNo,
					 "isUi": false,
					 "pageSize": 10,
					 "orderBy": "status desc",
					 "fixedData": {
						 "condition": {
							 "visible": "Y",
							 "message_id": this.id,
							 "status":"N",
							 "is_del":"N"
						 }
					 }
				})
				if (res.data && res.data.rows) {
					res.data.rows = res.data.rows.map(item => {
						return Object.assign({}, item, {
							checked: false
						})
					})
					if (this.pageNo == 1) {
						this.data = res.data.rows
						this.total = res.data.total
					} else {
						this.data = this.data.concat(res.data.rows)
					}
				}
			},
			async init() {
				// 
				let res = await this.$u.get('boman-web-core/messageReceive/recieve/statistic/'+ this.id)
				if (res.code == 200) {
					this.notRead =  res.data.notRead
				}
			},
			checkboxChange: function(e) {
				var items = this.data,
					values = e.detail.value;
				for (var i = 0, lenI = items.length; i < lenI; ++i) {
					const item = items[i]
					if (values.includes(String(item.id))) {
						this.$set(item, 'checked', true)
					} else {
						this.$set(item, 'checked', false)
					}
				}
			},
			allchange(e) {
				var items = this.data,
					values = this.checked;
					if (!values) {
						this.checked = true
						for (var i = 0, lenI = items.length; i < lenI; ++i) {
							const item = items[i]
							this.$set(item, 'checked', true)
						}
					} else {
						this.checked = false
						for (var i = 0, lenI = items.length; i < lenI; ++i) {
							const item = items[i]
							this.$set(item, 'checked', false)
						}
					}
				
			}
		}
	}
</script>

<style lang="scss" scoped>
	.collector {
		width: 100%;
		height: 100%;
		background: #fff;

		/deep/ .u-navbar-inner {
			box-shadow: none;
		}

		.title {
			height: 56rpx;
			background: #FFFFFF;
			line-height: 56rpx;
			box-shadow: 0px 6rpx 4rpx 0px rgba(218, 218, 218, 0.35);
			font-size: 24rpx;
			color: #343434;
			padding-left: 32rpx;

			text {
				color: #FF3131;
				margin: 0 12rpx;
			}
		}

		.btns {
			position: fixed;
			bottom: 0;
			height: 84rpx;
			display: flex;
			width: 100%;
			border-top: 1px solid #E5E5E5;
			/deep/ .left {
				width: 190rpx;
				background: #FFFFFF;
				text-align: center;
				font-size: 28rpx;
				font-weight: 500;
				color: #343434;
				line-height: 84rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				image {
					margin-right: 10rpx;
				}
				uni-checkbox {
					margin-right: 20rpx;
				}
			}
			.right {
				flex: 1;
				background: #009FE8;
				color: #fff;
				text-align: center;
				line-height: 84rpx;
			}
		}

		.table {
			.tr {
				display: flex;

				.th,
				.td {
					flex: 1;
					text-align: center;
					display: flex;
					justify-content: center;
					align-items: center;
				}

				.td {
					padding: 20rpx 4rpx;
					font-size: 20rpx;
					color: #333333;
					border-bottom: 1px solid #E5E5E5;
				}
			
			}

			.thead {
				padding: 33rpx 0;
				font-size: 24rpx;
				font-weight: bold;
				color: #333333;
			}
		}
		/deep/ .check-td,
		.check-th {
			max-width: 8%;
		
			.uni-checkbox-input {
				margin: 0 auto;
				width: 0rpx;
				height: 0rpx;
				padding: 12rpx;
		
				&.uni-checkbox-input-checked {
					background-color: #009FE8;
					border: 1px solid #009FE8;
				}
		
				&.uni-checkbox-input-checked:before {
					font-size: 20rpx;
				}
			}
		}
		.check {
		
			.normal,
			.selected {
				width: 30rpx;
				height: 30rpx;
			}
		}
	}
</style>
