<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="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 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">
                    <!-- 全部的按钮 -->
                    <button class="cu-btn bg-green shadow-blur" @tap="search">搜索</button>
                </view>
			</view>
			<view class="cu-form-group">
                <view class="title">
                    <text class="padding-right-sm"><text class="cuIcon-filter"></text>筛选更多</text>
                    <view class="cu-tag bg-orange light">测试</view></view>
                <view>
                    <view class="text-bold text-df">
                        <text class="cuIcon-close"></text>
                        重置</view>
                </view>
<!--				<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>
		</view>
		<!-- 列表部分 -->
		<view class="padding-sm quick-list">
			<view class="cu-list menu margin-top-sm 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="radius text-lg text-bold" :class="item.jcjg ? 'text-green':'text-red'">
							{{ item.jcjg ? '合格' : '不合格' }}
						</view>
					</view>
				</view>
				<view class="content padding-sm detail solid-bottom">
					<view>检测任务编号：{{ item.rwbh }}</view>
					<view>检测站名称：{{ item.jczmc }}</view>
					<view>被检企业名称：{{ item.enterprisesName }}</view>
					<view>样本名称：{{ item.ybmc }}</view>
					<view>产品批次号：{{ item.productBatchId }}</view>
					<view>检测时间：{{ item.jcTime }}</view>
					<view>检测值：{{ item.jcz }}{{ item.jczUnit }}</view>
					<view>检测类型：{{ typeMap[item.type] }}</view>
					<view>创建时间：{{ item.createTime }}</view>
				</view>
			</view>
		</view>
        <view class="cu-modal" :class="modalName==='filterDlg'?'show':''">
            <view class="cu-dialog">
<!--                <view class="cu-bar bg-white justify-end">
                    <view class="content" style="width: 100%">添加抽样单</view>
                    <view class="action" @tap="hideModal">
                        <text class="cuIcon-close text-red"></text>
                    </view>
                </view>-->
                <view class="padding-lr-lg bg-white padding-bottom">
                    <view class="cu-list menu">
                        <view class="cu-form-group ">
                            <view class="title">样品名称</view>
                            <input name="input" v-model="productName" placeholder="请输入抽样人员1">
                        </view>
                        <view class="cu-form-group ">
                            <view class="title">检测结果</view>
                            <input name="input" v-model="options.samplingPeopleSecond" placeholder="请输入抽样人员2">
                        </view>
                    </view>
                    <view class="padding flex flex-direction">
                        <button class="cu-btn bg-blue lg" @tap="submitSampleSheet">提交</button>
                    </view>
                </view>
            </view>
        </view>
	</view>
</template>

<script>
	import {
		_sampleDetail
	} from "../sample-detail/sample-detail-model.js";
	export default {
		data() {
			return {
				listData: [],
				regionArray: [],
				regionIndex: [],
				xzqObjList: [],
				queryXzq:"全部",
				queryXzqdm: null,
                modalName: null,
				pageNum: 1,
				pageSize: 10,
				pageTotal: 1,
				unitId: '',
				enterprisesName: '',
				productName: '',
				inSearching: false,
				typeMap: {
					1: '农残',
					2: '畜禽',
					3: '水产'
				}
			};
		},
		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.getQuickList({
					xzqCode: this.queryXzqdm.substring(4, 13),
					enterprisesName: this.enterprisesName,
					ybmc: 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;
	}

	.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>
