<template>
	<view class="mainBox">
		<uni-nav-bar fixed="true" statusBar="true" left-icon="back" title="我的请样记录" @clickLeft="back">
		</uni-nav-bar>
		<view class="top-wrapper">
			<uni-section>
				<uni-easyinput prefixIcon="search" style="width: 460rpx" v-model="qualityWorkOrderCode"
					placeholder="质检工单编码">
				</uni-easyinput>
			</uni-section>
			<button class="search_btn" @click="doSearch">搜索</button>
		</view>
		<view class="wrapper">
			<u-list @scrolltolower="scrolltolower" class="listContent">
				<view v-for="(item, index) in tableList" :key="index" style="position: relative">
					<myCard :item="item" :index="index + 1" :btnList="btnList" :columns="columns" @submit="submit(item)"
						@view="open('view',item)" @edit="open('edit',item)" @cancel="cancel(item.id)">
					</myCard>
				</view>
			</u-list>
		</view>
		<view style="margin-top: 20vh" v-if="tableList.length == 0">
			<u-empty iconSize="150" textSize="32" text="暂无数据"> </u-empty>
		</view>
		<!-- 		<view class="add" @click="open('add')">
			<u-icon name="plus" color="#fff"></u-icon>
		</view> -->
		<u-toast ref="uToast"></u-toast>
		<addSample ref='addSampleRef' @reload="doSearch"></addSample>
	</view>
</template>

<script>
	import {
		samplingRecordsPage,
		listCancel
	} from "@/api/samplingRecords";
	import myCard from "@/pages/saleManage/components/myCard.vue";
	import {
		recordingMethodList
	} from "@/utils/utils.js";
	import addSample from './addSample.vue'
	import {
		processInstanceCreateAPI,
		processInstancePage
	} from '@/api/wt/index.js'
	export default {
		components: {
			myCard,
			addSample
		},
		data() {
			return {
				btnList: [{
						name: "详情",
						btnType: "primary",
						apiName: 'view',
					},
					{
						name: "编辑",
						btnType: "primary",
						apiName: 'edit',
						judge: [{
								key: "approvalStatus",
								value: [0, 3],
							},
							{
								key: "status",
								value: [0, 1, 2, 4],
							},
						],
					},
					{
						name: "提交",
						btnType: "success",
						apiName: 'submit',
						judge: [{
								key: "approvalStatus",
								value: [0, 3],
							},
							{
								key: "status",
								value: [0, 1, 2, 4],
							},
						],
					},
					{
						name: "作废",
						btnType: "error",
						apiName: 'cancel',
						judge: [{
								key: "approvalStatus",
								value: [0, 3],
							},
							{
								key: "status",
								value: [0, 1, 2, 4],
							},
						],
					},
				],
				columns: [
					[{
						label: "请样记录编码：",
						prop: "code",
						type: "title",
						className: "perce100",
					}, ],
					[{
							label: "质检工单编码：",
							prop: "qualityWorkOrderCode",
						},
						{
							label: "质检工单名称：",
							prop: "qualityWorkOrderName",
						},
					],
					[{
							label: "质检方式：",
							prop: "qualityMode",
							formatter: (row) => {
								return row.qualityMode == 1 ? "全检" : "抽检";
							},
						},
						{
							label: "请样类型：",
							prop: "conditionType",
							formatter: (row) => {
								return row.conditionType == 1 ? "整样" : "小样";
							},
						},
					],
					[{
							label: "编码：",
							prop: "productCode",
						},
						{
							label: "名称：",
							prop: "productName",
						},
					],
					[{
							label: "批次号：",
							prop: "batchNo",
						},
						{
							label: "规格：",
							prop: "specification",
						},
					],
					[{
							label: "请样数量：",
							prop: "sampleQuantity",
						},
						{
							label: "单位：",
							prop: "measureUnit",
						},
					],
					[{
							label: "状态：",
							prop: "status",
							formatter: (row) => {
								return row.status == 1 ?
									"审核中" :
									row.status == 2 ?
									"已请样" :
									row.status == 3 ?
									"已作废" :
									row.status == 4 ?
									"不通过" :
									"未提交";
							},
						},
						{
							label: "审核状态：",
							prop: "approvalStatus",
							formatter: (row) => {
								const reviewStatus = {
									0: "未提交",
									1: "审核中",
									2: "已审核",
									3: "审核不通过",
								};
								return reviewStatus[row.approvalStatus];
							},
						},
					],
					[{
							label: "创建人：",
							prop: "createUserName",
						},
						{
							label: "创建时间：",
							prop: "createTime",
						},
					],
					[{
						label: "操作：",
						prop: "action",
						type: "action",
						className: "perce100",
					}, ],
				],
				tableList: [],
				page: 1,
				size: 10,
				isEnd: false,
				qualityWorkOrderCode: "",
			};
		},
		computed: {},
		onShow() {
			this.isEnd = false;
			this.page = 1;
			this.getList();
		},
		methods: {
			async listSubmit(item) {
				try {
					//后台不提供接口
					let list = await processInstancePage({
						pageNo: 1,
						pageSize: 1,
						reset: true,
						key: 'sampling_approval_process'
					})
					let params = {
						businessId: item.id,
						businessKey: 'sampling_approval_process',
						formCreateUserId: item.createUserId,
						processDefinitionId: list?.list[0]?.processDefinition.id,
						variables: {
							businessCode: item.code,
							businessName: '请样',
							businessType: item.conditionType == 1 ? '整样' : '小样'
						},
					}

					await processInstanceCreateAPI(params)
					uni.showModal({
						title: `提交成功`,
						content: '',
						confirmText: '确认',
						showCancel: false, // 是否显示取消按钮，默认为 true
						success: () => {
							this.doSearch()
						}
					})
				} catch {

				}
			},
			open(type, row) {
				console.log(1212)

				this.$refs.addSampleRef.open(type, row);
			},
			doSearch() {
				this.isEnd = false;
				this.page = 1;
				this.getList();
			},
			getList() {
				let userInfo = uni.getStorageSync('userInfo')
				if (this.isEnd) {
					return;
				}
				uni.showLoading({
					title: "加载中",
				});

				let data = {
					pageNum: this.page,
					size: this.size,
					currentLoginUserId: userInfo.userId,
					qualityWorkOrderCode: this.qualityWorkOrderCode,
				};
				samplingRecordsPage(data)
					.then((res) => {
						if (this.page === 1) {
							this.tableList = res.list;
						} else {
							this.tableList.push(...res.list);
						}
						this.page += 1;
						this.isEnd = this.tableList.length >= res.count;
					})
					.then(() => {
						uni.hideLoading();
					});
			},
			cancel(id) {
				uni.showModal({
					title: "提示",
					content: "确定要作废此信息吗？",
					success: (res) => {
						if (res.confirm) {
							listCancel({
								id
							}).then((res) => {
								this.isEnd = false;
								this.page = 1;
								this.$refs.uToast.show({
									type: "success",
									message: "操作成功",
								});
								this.getList();
							});
						}
					},
				});
			},
			add() {
				uni.navigateTo({
					url: "/pages/qms/inspectionWork/list",
				});
			},
			scrolltolower() {
				if (this.isEnd) {
					return;
				}
				this.getList();
			},
			back() {
				uni.navigateBack();
			},
			submit(row) {
				// 提交逻辑，需要根据实际业务实现
				uni.showToast({
					title: "提交功能待实现",
					icon: "none",
				});
			},
		},
	};
</script>

<style lang="scss" scoped>
	.add {
		width: 96rpx;
		height: 96rpx;
		border-radius: 48rpx;
		background: #3c9cff;
		position: fixed;
		bottom: 100rpx;
		right: 24rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.top-wrapper {
		background-color: #fff;
		display: flex;
		width: 750rpx;
		height: 88rpx;
		padding: 16rpx 32rpx;
		align-items: center;
		gap: 16rpx;

		/deep/.uni-section {
			margin-top: 0px;
		}

		/deep/.uni-section-header {
			padding: 0px;
		}

		.search_btn {
			width: 120rpx;
			height: 70rpx;
			line-height: 70rpx;
			padding: 0 24rpx;
			background: $theme-color;
			font-size: 32rpx;
			color: #fff;
			margin: 0;
			margin-left: 26rpx;
		}
	}
</style>