<template>
	<view class="quick-task" style="padding-bottom: 150upx">
		<cu-custom class="home-custom" bgColor=".bg-sancolor"
			bgImage="https://ahwgh.oss-cn-hangzhou.aliyuncs.com/enterprise/6525f808df760.png" :isBack="true">
			<block slot="content">胶体金检测</block>
		</cu-custom>
		<!-- 搜索部分 -->
		<view class="search-Top"
			style="position: fixed;z-index: 999; width: 100%;box-shadow: 0 5px 10px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);">
			<view class="cu-bar search bg-white">
				<view class="search-form radius">
					<text class="cuIcon-search"></text>
					<input type="text" placeholder="输入企业名称搜索" confirm-type="search" v-model="enterprisesName" />
					<text class="cuIcon cuIcon-close endClass text-xxl text-bold" @tap="resetEnterprisesQuery"
						v-show="enterprisesName !== ''"></text>
				</view>
				<view class="action">
					<!-- 全部的按钮 -->
					<picker mode="multiSelector" :range="regionArray" @change="RegionChange" :value="regionIndex"
						@columnchange="columnchange">
						<view class="picker">
							{{queryXzq}}
						</view>
					</picker>
					<text class="cuIcon-triangledownfill"></text>
				</view>
			</view>
			<view class="cu-bar search bg-white">
				<view class="search-form radius">
					<text class="cuIcon-search"></text>
					<input type="text" placeholder="输入样本名称搜索" confirm-type="search" v-model="productName" />
					<text class="cuIcon cuIcon-close endClass text-xxl text-bold" @tap="resetProductQuery"
						v-show="productName !== ''"></text>
				</view>
				<view class="action">
					<!-- 全部的按钮 -->
					<button class="cu-btn bg-green shadow-blur" style="margin-right: 22px;" @tap="search">搜索</button>
				</view>
			</view>
		</view>
		<!-- 列表部分 -->
		<view class="padding-sm quick-list">
			<view class="cu-list menu margin-top taskDetail bg-white" v-for="(item, index) in listData" :key="index">
				<view class="cu-item">
					<view class="title text-black text-bold text-lg">
						<text class="cuIcon-titles text-green"></text>
						{{ item.jcxm }}
					</view>
					<view class="action">
						<view class="text-gray">
							{{ [1,2].includes(item.jcjg) ? '阴性' : '阳性' }}
						</view>

					</view>
				</view>
				<view class="content padding-sm detail solid-bottom">
					<view>检测编号：{{ item.jcbh }}</view>
					<view>检测站名称：{{ item.jczmc }}</view>
					<view>被检单位：{{ item.enterprisesName }}</view>
					<view>样本名称：{{ item.productName }}</view>
					<view>产品批次号：{{ item.batchId }}</view>
				</view>
			</view>
		</view>
		<view class="padding flex flex-direction padding-bottom-xl fixedFootBtn padding-top-xs">
			<button class="cu-btn bg-green lg margin-top-sm shadow-blur" type="" @tap="nextSetp">
				新增检测
			</button>
		</view>
	</view>
</template>

<script>
	import {
		_sampleDetail
	} from "../sample-detail/sample-detail-model.js";
	export default {
		data() {
			return {
				listData: [],
				regionArray: [],
				regionIndex: [],
				xzqObjList: [],
				queryXzq:"全部",
				queryXzqdm: null,
				pageNum: 1,
				pageSize: 10,
				pageTotal: 1,
				unitId: '',
				enterprisesName: '',
				productName: '',
				inSearching: false,
			};
		},
		onLoad() {
			const { unitId } = uni.getStorageSync("userInfo");
			this.unitId = unitId;
			this.queryXzqdm = unitId;
			this.getList()
			this.initRange();
		},
		methods: {
			search() {
				this.pageNum = 1;
				this.getList()
			},
			resetEnterprisesQuery(){
			    this.enterprisesName = "";
			},
			resetProductQuery() {
				this.productName = "";
			},
			getList() {
				this.inSearching = true
				_sampleDetail.getJTJList({
					xzqCode: this.queryXzqdm,
					enterprisesName: this.enterprisesName,
					productName: this.productName,
					pageNum: this.pageNum,
					pageSize: this.pageSize
				}, res => {
					const { records, total } = res.data
					if (this.pageNum === 1) {
						this.listData = records
					} else {
						this.listData = this.listData.concat(records) 
					}
					this.pageTotal = total
					this.inSearching = false
					if (records.length > 0) {
						this.pageNum++
					}
				})
			},
			onReachBottom() {
			    this.getList();
			},
			getSubXzqList(xzqdm) {
			    return new Promise(resolve => {
			        _sampleDetail.getSubXzqList(xzqdm, res => {
			            resolve(res);
			        });
			    });
			},
			async initRange() {
				const {
					unitId,
					name
				} = uni.getStorageSync("userInfo");
				let role = uni.getStorageSync("role");
				let xzqdm;
				if (role == "f35551e4a06647dea5c2c43aaba22c2a") {
					xzqdm = unitId.substring(4, 13);
				}
				if (role == "a1cbf841b0514b47bbdd3f50c80b729f") {
					xzqdm = unitId

				}

				let level = 0;
				if (xzqdm.endsWith("0000000")) { //省
					level = 3;
				} else if (xzqdm.endsWith("00000")) { //市
					level = 2;
				} else if (xzqdm.endsWith("000")) { //县
					level = 1;
				}
				const xzqlist = await this.getSubXzqList(xzqdm);
				if (level !== 0) {

					xzqlist.unshift({
						xzqdm: "",
						xzqmc: "请选择"
					});

					this.regionArray.splice(0, this.regionArray.length, xzqlist.map(v => v.xzqmc));
					this.xzqObjList.splice(0, this.xzqObjList.length, xzqlist);
					this.regionIndex.splice(0, this.regionIndex.length, 0);

					for (let i = 0; i < level - 1; i++) {
						this.regionArray.push(["请选择"]);
						this.xzqObjList.push({
							xzqdm: "",
							xzqmc: "请选择"
						});
						this.regionIndex.push(0);
					}
				} else {
			 	xzqlist.unshift({
						xzqdm: unitId,
						xzqmc: name
					});
					this.regionArray.push([name]);
					this.xzqObjList.push({
						xzqdm: unitId,
						xzqmc: name
					});
					this.regionIndex.push(0);
				}


			},
			async columnchange(e) {
			    const { column, value } = e.detail;
			    const xzqdm = this.xzqObjList[column][value]["xzqdm"];
			    if (xzqdm) {
			        if (column <= this.regionIndex.length - 2) {
			            const subList = await this.getSubXzqList(xzqdm);
			            subList.unshift({
			                xzqdm: "",
			                xzqmc: "请选择"
			            });
			            this.xzqObjList.splice(column + 1, 1, subList); //逐级push-乡镇
			            this.regionIndex.splice(column + 1, 1, 0);
			            this.regionArray.splice(column + 1, 1, subList.map(v => v.xzqmc));
			            for (let i = column + 2; i < this.regionIndex.length; i++) {
			                this.xzqObjList.splice(i, 1, [{ xzqdm: "", xzqmc: "请选择" }]); //逐级push-乡镇
			                this.regionIndex.splice(i, 1, 0);
			                this.regionArray.splice(i, 1, ["请选择"]);
			            }
			        }
			    } else {
			        for (let i = column + 1; i < this.regionIndex.length; i++) {
			            this.xzqObjList.splice(i, 1, [{ xzqdm: "", xzqmc: "请选择" }]); //逐级push-乡镇
			            this.regionIndex.splice(i, 1, 0);
			            this.regionArray.splice(i, 1, ["请选择"]);
			        }
			    }
			
			},
			RegionChange(e){
			    this.regionIndex = e.detail.value;
			    let xzqdm = "", xzqmc = [];
			    for (let i = 0; i <= this.regionIndex.length - 1; i++) {
			        if (this.regionArray[i][this.regionIndex[i]] !== "请选择") {
			            xzqmc.push(this.regionArray[i][this.regionIndex[i]]);
			            xzqdm = this.xzqObjList[i][this.regionIndex[i]].xzqdm;
			        }
			    }
			    this.queryXzqdm = xzqdm || this.unitId;
			    this.queryXzq = xzqmc.join("/") || '全部';
				this.pageNum = 1
			    this.getList();//查询
			},
			nextSetp() {
				uni.navigateTo({
					url: "/pages/quick-task/form"
				});
			}
		}
	}
</script>

<style scoped>
	.quick-task {
		background: #fff;
		height: 100%;
		width: 100%;
	}

	.first-step {
		height: 100%;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		padding-top: 20vh;
	}

	.first-step button {
		width: 90%;
		margin-top: 20vh;
	}

	.first-step image {
		width: 60vw;
		margin-left: 10vw;
	}

	.quick-list {
		padding-top: 100px;
		padding-bottom: 100px;
	}

	.detail view {
		height: 1.8em;
		line-height: 1.8em !important;
		border: none;
		color: gray;
	}

	.taskDetail {
		/*border: 1rpx solid #8799a3;*/
		box-shadow: 0 0 25upx #aaa;
		border-radius: 10rpx;
		overflow: hidden;
	}
</style>
