<template>
	<view v-if="isShow">
		<view class="buttons-father">
			<view class="boxtwo">
				<view class="ipt">
					<input type="text" placeholder="物料名称/物料简拼" v-model="form.wlParamInfo" @confirm="search" />
					<image src="/static/index/procurement/x.png" mode="aspectFill" v-if="form.wlParamInfo"
						@click="search(0)"></image>
				</view>
				<view class="scr" @click="search">
					检索
				</view>
				<view class="shx" @click="ification = true">
					<image src="/static/index/requestform/shaixuan.png" mode="aspectFill"></image>
					<view class="shx-font">
						筛选
					</view>
				</view>
				<view class="shx" @click="chongzhi" v-if="form.categoryId!=''||form.wlParamInfo!='' ">
					<image src="/static/index/requestform/chongzhi.png" mode="aspectFill"></image>
					<view class="shx-font">
						重置
					</view>
				</view>
			</view>
			<view class="boxtwo flexend">
				<view class="qlthbtn">
					<image src="/static/index/requestform/ql0.png" mode="aspectFill"></image>
					<view class="shx-font" @click="changePage(1)">
						请领单
					</view>
					<view class="qlthbtn-red" v-if="isQingling"></view>
				</view>
				<view class="qlthbtn">
					<image src="/static/index/requestform/ql1.png" mode="aspectFill"></image>
					<view class="shx-font" @click="changePage(2)">
						回退单
					</view>
					<view class="qlthbtn-red" v-if="isRead"></view>
				</view>
				<view class="qlthbtn" style="width: 8vw;" @click="thshow = true">
					<image src="/static/index/requestform/ql2.png" mode="aspectFill"></image>
					<view class="shx-font">
						退货
					</view>
				</view>
			</view>
		</view>
		<addwl :Material="addMaterial" @delall="delall" @del="adddel" @submit="submit"></addwl>
		<carditem :InvoicingList="listarr" @scrolltolower="scrolltolower" :status="status" @comfig="comfig"></carditem>
		<takeing :show="takeshow" :objtake="objtake" @fill="takeshow = false" @right="right"></takeing>
		<cgdclass @confirm="confirm" :show="ification" :typenum="0" ref="classication" @fill="ification = false">
		</cgdclass>
		<medetails :show="detaishow" @fill="detaishow = false" @confirm="confirm" :objtake="objtake"></medetails>
		<tanchuang @back="tanshow = false" :show="tanshow" font="确定将该物料从本次请领清单中移除吗？" @right="del"></tanchuang>
		<takerecord :objtake="objtake" :show="recordshow" @fill="recordshow = false"></takerecord>
		<submits :show="subshow" :list="addMaterial" @fill="subshow = false" @config="tijiao"></submits>
		<tanchuang @back="thshow = false" :show="thshow" font="是否发起退货流程？" @right="thclk"></tanchuang>
		<errorshow :show="openerror" :font="errmsg" @close="openerror=false" />
	</view>
</template>

<script setup lang="ts">
	import { ref, onMounted, onBeforeUnmount, computed, nextTick, watch, reactive } from 'vue';
	import { queryInvoicingList, wlzd, queryGwcInfo, addGwc, deleteGwcWl, removeAllGwc, submitQld, requireTH, queryQld } from '../api/api.js'
	import addwl from '../common/addwl.vue';
	import carditem from '../common/carditem.vue';
	import takeing from '../common/takeing.vue';
	import cgdclass from '../common/cgdclass.vue';
	import medetails from '../common/medetails.vue';
	import tanchuang from '../common/tanchuang.vue';
	import takerecord from '../common/takerecord.vue';
	import submits from '../common/submits.vue'
	const form = reactive({
		pageNo: 1,
		pageSize: 10,
		nuId: uni.getStorageSync('nuId'),
		elderId: uni.getStorageSync('elderId'),
		wlParamInfo: '',
		categoryId: '',
		typeId: '',
		medicationId: ''
	})
	const props = defineProps({
		isShow: {
			type: Boolean
		},
	});
	const transition = ref(false);
	watch(
		() => props.isShow,
		(newVal, oldVal) => {
			listarr.value = [];
			form.categoryId = '';
			form.typeId = '';
			form.medicationId = '';
			form.pageNo = 1;
			form.wlParamInfo = '',
				getreddian()
			config()
			wuliao()
			if (!oldVal && newVal) {
				transition.value = false;
				setTimeout(() => {
					transition.value = true;
				}, 50)
			} else {
				transition.value = false;
			}

		}
	)
	const takeshow = ref(false)
	const ification = ref(false)
	const detaishow = ref(false)
	const tanshow = ref(false)
	const recordshow = ref(false)
	const subshow = ref(false)
	const thshow = ref(false)
	const openerror = ref(false)
	const errmsg = ref('')
	const classication = ref<InstanceType<typeof ChildComponent>>()
	const addMaterial = ref([])

	const isRead = ref(false)
	const isQingling = ref(false)
	const getreddian = () => {
		let plzinfo0 = {
			pageNo: 1,
			pageSize: 1,
			searchContent: "",
			status: "3",
			elderId: uni.getStorageSync('elderId'),
			nuId: uni.getStorageSync('nuId')
		}
		queryQld(plzinfo0).then((res : any) => {
			if (res.result.records.length) {
				isRead.value = true;
			} else {
				isRead.value = false;
			}
		})
		let plzinfo1 = {
			pageNo: 1,
			pageSize: 1,
			searchContent: "",
			status: "4",
			elderId: uni.getStorageSync('elderId'),
			nuId: uni.getStorageSync('nuId')
		}
		queryQld(plzinfo1).then((res : any) => {
			// console.log("22",res.result)
			if (res.result.records.length) {
				isQingling.value = true;
			} else {
				isQingling.value = false;
			}
		})
	}
	const wuliao = () => {
		let obj = {
			nuId: form.nuId,
			elderId: form.elderId
		}
		queryGwcInfo(obj).then(res => {
			// console.log(res)
			addMaterial.value = res.result
		})
	}
	const listarr = ref([])
	const status = ref('loadmore')
	const config = () => {

		queryInvoicingList(form).then(res => {
			res.result.records.forEach(item => {
				item.zk = false;
				item.scrollleft = 0;
			})
			// console.log("?????",form,res.result.records)
			listarr.value.push(...res.result.records)
			status.value = (res.result.total == listarr.value.length ? 'nomore' : 'loadmore')
		})
	}
	const thclk = () => {
		
		let data = {
			nuId: uni.getStorageSync('nuId'),
			elderId: uni.getStorageSync('elderId'),
			nuName: uni.getStorageSync('nuName'),
		}
		console.log("?????",data)
		requireTH(data).then(res => {
			console.log("?????",res)
			if (res.success) {
				thshow.value = false;
				uni.showToast({
					icon: 'success',
					title: res.message
				})
			} else {
				thshow.value = false;
				errmsg.value = res.result?.message ? res.result?.message : res.message
				openerror.value = true;
			}
		})
	}
	const search = (x : number) => {
		if (x === 0) { form.wlParamInfo = '' }
		form.pageNo = 1;
		listarr.value = [];
		config();
	}
	const scrolltolower = () => {
		if (status.value == 'loading' || status.value == 'nomore') { return }
		status.value = 'loading';
		form.pageNo++;
		config()
	}
	const submit = () => {
		wuliao();
		subshow.value = true;
	}
	const adddel = (v) => {
		listarr.value.forEach(item => {
			if (item.wlId == v.wlId) {
				objtake.value = item
			}
		})
		if (!objtake.value.wlId) {
			objtake.value = v;
		}
		tanshow.value = true;
		console.log(objtake.value)
	}
	const del = () => {
		let obj = {
			nuId: form.nuId,
			elderId: form.elderId,
			wlId: objtake.value.wlId
		}
		console.log(obj)
		deleteGwcWl(obj).then(res => {
			if (res.success) {
				wuliao();
				tanshow.value = false;
				objtake.value.isAdd = null;
				objtake.value.qlNum = null;
			} else {
				errmsg.value = res.result?.message ? res.result?.message : res.message
				openerror.value = true;
			}
		})
	}
	const delall = () => {
		console.log({ nuId: form.nuId, elderId: form.elderId })
		removeAllGwc({ nuId: form.nuId, elderId: form.elderId }).then(res => {
			if (res.success) {
				wuliao();
				listarr.value.forEach(item => {
					item.isAdd = null;
					item.qlNum = null;
					item.gwcId = null;
				})
			} else {
				errmsg.value = res.result?.message ? res.result?.message : res.message
				openerror.value = true;
			}
		})
	}
	const objtake = ref({})
	const comfig = (v, i, t) => {
		console.log(`看看数据`, v, i, t)
		switch (t) {
			case 0:
				objtake.value = v;
				if (v.isAdd == 1) {
					tanshow.value = true;
				} else {
					takeshow.value = true;
				}

				break;

			case 1:
				objtake.value = v;
				recordshow.value = true;

				break;

			case 2:
				objtake.value = v;
				detaishow.value = true;
				break;
			case 3:
				let obj = {
					nuId: form.nuId,
					elderId: form.elderId,
					wlId: v.wlId,
					izZd: v.zhiDingId ? false : true
				}
				wlzd(obj).then(res => {
					console.log(obj, res)
					if (res.success) {
						// objtake.value = v;
						// objtake.value.zk = false;
						// objtake.value.scrollleft = 0;
						// objtake.value = v.zhiDingId?false:true;
						// listarr.value.splice(i,1);
						// listarr.value.unshift(v)
						listarr.value = [];
						form.pageNo = 1;

						config()
					} else {
						errmsg.value = res.result?.message ? res.result?.message : res.message
						openerror.value = true;
					}
				})

				break;

			default:
				break;
		}
	}
	const right = (e, v) => {
		console.log(v)
		let obj = {
			kfId: v.nuId,
			wlId: v.wlId,
			qlNum: e,
			nuId: form.nuId,
			elderId: form.elderId
		}
		addGwc(obj).then(res => {
			if (res.success) {
				takeshow.value = false;
				v.isAdd = 1;
				v.qlNum = e;
				wuliao();
			} else {
				errmsg.value = res.result?.message ? res.result?.message : res.message
				openerror.value = true;
			}
		})
	}
	const tijiao = () => {
		let obj = {
			nuId: form.nuId,
			elderId: form.elderId,
			nuName: uni.getStorageSync('nuName'),
			elderName: uni.getStorageSync(`NUall`).elderInfo.name
		}
		submitQld(obj).then(res => {
			console.log("?????111", obj, res)
			if (res.success && res.result.status == 'success') {
				uni.showToast({
					icon: 'success',
					title: '提交成功'
				})
				setTimeout(() => {
					subshow.value = false;
					wuliao();
					changePage(1)
				}, 800)
			} else {
				// console.log("!!!!!",res)
				errmsg.value = res.result?.message ? res.result?.message : res.message
				openerror.value = true;
			}
		})
	}
	const confirm = (e) => {
		form.categoryId = e.categoryId;
		form.typeId = e.typeId;
		form.medicationId = e.medicationId;
		form.pageNo = 1;
		listarr.value = [];
		config();
		ification.value = false;
		console.log(e)
	}
	const chongzhi = () => {
		form.categoryId = '';
		form.typeId = '';
		form.medicationId = '';
		form.pageNo = 1;
		form.wlParamInfo = '',
			listarr.value = [];
		classication.value.typescroll(1, -1, {});
		classication.value.qingkong();
		classication.value.config();
	}
	const emit = defineEmits(['changePageNumber']) // 也可以用类型签名（见下面）
	const changePage = (index : number) => {
		emit('changePageNumber', index)
	}
</script>

<style lang="less" scoped>
	@import "../css/take.less";
</style>