<template>
	<view class="mainBox">
		<uni-nav-bar background-color="#157A2C" color="#fff" 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="searchVal" placeholder="需求单编码/需求单名称/产品名称">
				</uni-easyinput>
			</uni-section>

			<button class="search_btn" @click="doSearch">搜索</button>

			<image class="menu_icon" src="~@/static/pda/menu.svg"></image>

		</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" :columns="columns" :btnList="btnList" @del="del(item)" @handleAudit="handleAudit(item)">
					</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="add">
			<u-icon name="plus" color="#fff"></u-icon>
		</view>

		
		<u-toast ref="uToast"></u-toast>

	</view>
</template>

<script>
    import {
		getTableList,
        deleteInformation
	} from '@/api/purchasingManage/purchaseNeedManage.js'
	import myCard from '../components/myCard.vue'
    import { reviewStatusEnum } from "@/enum/dict";
	import {
		processInstanceCreateAPI,
		processInstancePage
	} from '@/api/wt/index.js'
	export default {
		components: {
			myCard,
		},

		data() {
			return {
				tableList: [],
				searchVal: '',
				page: 1,
				size: 10,
				isEnd: false,
				modalShow: false,
				current: {},
				addForm: {
					planExecuteUserId: '',
					planExecuteUserName: '',
					planExecuteGroupId: '',
					planExecuteGroupName: '',
					id: ''
				},
				btnList: [{
					name: '详情',
					apiName: '',
					btnType: 'primary',
					type: '1',
					pageUrl: '/pages/purchasingManage/purchaseNeedManage/components/drawer',

				}, 
                {
					name: '修改',
					apiName: '',
					btnType: 'primary',
					type: '1',
					pageUrl: '/pages/purchasingManage/purchaseNeedManage/add',
					judge: [{
						authorities: '',
					}, {
						key: 'status',
						value: [0, 3],

					}],
				}, 
				{
					name: '提交',
					apiName: 'handleAudit',
					btnType: 'primary',
					type: '2',
					pageUrl: '',
					judge: [{
						authorities: '',
					}, {
						key: 'status',
						value: [0, 3],

					}],
				},
                {
					name: '删除',
					apiName: 'del',
					btnType: 'error',
					type: '2',
					pageUrl: '',
					judge: [{
						
					}, {
						key: 'status',
						value: [0, 3]
					}],
				}],
				columns: [
					[{
						label: '名称：',
						prop: 'requirementName',
						type: 'title',
						className: 'perce100',
					}],
					[{
						label: '编码：',
						prop: 'requirementCode'
					}, {
						label: '产品名称：',
						prop: 'productNames'
					}],
					[{
						label: '需求类型：',
						prop: 'sourceTypeName'
					}, {
						label: '需求部门：',
						prop: 'requireDeptName'
					}],
					[{
						label: '需求人：',
						prop: 'requireUserName'
					}, {
						label: '创建时间：',
						prop: 'createTime'
					}],
					[{
						label: '状态：',
						prop: 'status',
						formatter: (row) => {
							return reviewStatusEnum.find(item => item.value === row.status)?.label || '';
						}
					}],
					[{
						label: '操作：',
						prop: 'action',
						type: 'action',
						className: 'perce100',
					}],

				]
			}
		},
		computed: {

		},

		onShow() {
			this.isEnd = false
			this.page = 1
			this.getList()
		},

		methods: {
			doSearch() {
				this.isEnd = false
				this.page = 1
				this.getList()
			},
			//获取列表信息
			getList() {
				if (this.isEnd) {
					return
				}
				uni.showLoading({
					title: '加载中'
				})

				let data = {
					pageNum: this.page,
					size: this.size,
					type: 1,
					searchName: this.searchVal

				}
				getTableList(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()
				})
			},
			handleAudit(row) {
				this.current = row
				this.listSubmit()
				console.log(this.modalShow, 'dsds')
			},
			async listSubmit() {
				try {
					//后台不提供接口
					let list = await processInstancePage({
						pageNo: 1,
						pageSize: 1,
						reset: true,
						key: 'purchase_requirement_approve_tg'
					})
					let params = {
						businessId: this.current.id,
						businessKey: 'purchase_requirement_approve_tg',
						formCreateUserId: this.current.createUserId,
						processDefinitionId: list?.list[0]?.processDefinition.id,
						variables: {
							businessCode: this.current.requirementCode,
							businessName: this.current.requirementName,
							businessType: this.current.sourceTypeName,
						},
					}

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

				}
			},
			add() {
				uni.navigateTo({
					url: '/pages/purchasingManage/purchaseNeedManage/add'
				})

			},
			del(item) {
                uni.showModal({
                    title: '提示',
                    content: '确定删除吗？',
                    success: (res) => {
                        if (res.confirm) {
                            deleteInformation([item.id]).then(res => {
                                this.isEnd = false
                                this.page = 1
                                this.$refs.uToast.show({
                                    type: "success",
                                    message: "操作成功",
                                })
                                this.getList(this.where)
                            }).catch(() => {
                                // this.$refs.uToast.show({
                                //     type: "error",
                                //     message: "操作失败",
                                // })
                            })
                        }
                    }
                })
			},
			scrolltolower() {
				if (this.isEnd) {
					return
				}
				this.getList();
			},
		}
	}
</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;
		}

		.menu_icon {
			width: 44rpx;
			height: 44rpx;
			margin-left: 14rpx;
		}
	}
</style>