<template>
	<view class="">
		<u--form style="margin: 0 20px;" labelPosition="left" :model="form" :rules="rules" ref="uForm"
			labelWidth='140rpx'>
			<u-form-item v-if="['productionSupervisorApprove1'].includes(taskDefinitionKey)" label="技术员"
				prop="technicalAnswerName" required>
				<u--input clearable placeholder="请选择" border="surround" v-model="form.technicalAnswerName"
					@click.native="showTechnicianPicker"></u--input>
			</u-form-item>

			<u-form-item label="审批建议" prop="reason" required>
				<u--textarea style="width: 100%;" height='120' border='surround' placeholder="请输入审批建议"
					v-model="form.reason"></u--textarea>
			</u-form-item>
		</u--form>
		<view>
			<u-button style="width: 100%;margin-bottom: 10rpx;" icon="edit-pen" :loading='loading' type="success"
				text="通过" @click="handleAudit(1)">
			</u-button>
			<u-button style="width: 100%;" :loading='loading' type="error" icon="close" text="驳回"
				@click="handleAudit(0)" v-if="!['starter'].includes(taskDefinitionKey)"></u-button>
		</view>
		<u-picker itemHeight='60' :show="technicianShow" visibleItemCount='10' :columns="userOptions" keyName="name"
			@confirm='selectTechnicianInfo' @cancel='technicianShow = false' title='选择技术员'></u-picker>
	</view>
</template>

<script>
	import {
		approveTaskWithVariables,
		businessOpportunityUpdateAPI
	} from '@/api/wt/index.js'
	import {
		listAllUserBind
	} from '@/api/common.js'
	export default {
		name: 'taskSubmit',
		props: {
			businessId: {
				default: ''
			},
			taskId: {
				default: ''
			},
			id: {
				default: ''
			},
			taskDefinitionKey: {
				default: ''
			}
		},

		data() {
			return {
				loading: false,
				technicianShow: false,
				userOptions: [],
				form: {
					technicalAnswerId: '',
					technicalAnswerName: '',
					reason: '',
				},
				rules: {
					reason: {
						type: 'string',
						required: true,
						message: '请输入审批建议',
						trigger: 'blur'
					},
					technicalAnswerName: {
						type: 'string',
						required: true,
						message: '请选择技术员',
						trigger: ['blur', 'change']
					},
				},
				tabOptions: [{
					key: 'productionSupervisorApprove1',
					permissionType: 'update',
					name: '生产主管审批',
				}, {
					key: 'techLeaderApprove',
					permissionType: 'view',
					name: '技术部主管审批',
				}, {
					key: 'productionSupervisorApprove2',
					permissionType: 'view',
					name: '生产主管审批',
				}, {
					key: 'salesManagerApprove',
					permissionType: 'view',
					name: '销售主管审批',
				}],
			}
		},
		mounted() {
			this.$refs.uForm.setRules(this.rules)
			this.getUserOptions()
		},
		methods: {
			async getUserOptions() {
				const data = await listAllUserBind()
				this.userOptions.push(data)
			},
			showTechnicianPicker(val) {
				if (val.target.nodeName == 'I') {
					this.form.technicalAnswerId = ''
					this.form.technicalAnswerName = ''
					return
				}
				this.technicianShow = true
			},
			selectTechnicianInfo(e) {
				this.form.technicalAnswerId = e.value[0]?.id
				this.form.technicalAnswerName = e.value[0]?.name
				this.technicianShow = false
			},

			async handleAudit(status) {
				if (!!status) await this.$refs.uForm.validate()
				let permissionType = this.tabOptions.find(item => item.key == this.taskDefinitionKey)?.permissionType
				if (!!status && permissionType === 'update') {
					let date = await this.getTableValue();
					let arr = []
					if (this.taskDefinitionKey == 'productionSupervisorApprove1') {
						arr = date.productList.map(item => {
							return {
								...item,
								technicalAnswerId: this.form.technicalAnswerId,
								technicalAnswerName: this.form.technicalAnswerName
							}
						})
					}
					if (this.taskDefinitionKey == 'productionSupervisorApprove2') {
						arr = date.productList.map(item => {
							return {
								...item
							}
						})
					}
					await businessOpportunityUpdateAPI(arr)
				}
				this.loading = true
				await this._approveTaskWithVariables(status);
			},
			async _approveTaskWithVariables(status) {
				let variables = {
					pass: !!status,
				};
				if (!!status) {
					variables.technicianId = this.form.technicalAnswerId
				}
				let res = await approveTaskWithVariables({
					id: this.taskId,
					reason: this.form.reason,
					variables
				})

				if (res.code != '-1') {
					this.$emit('handleAudit', {
						status,
						title: status === 0 ? '驳回' : ''
					});
				}
				this.loading = false
			},

			getTableValue() {
				return new Promise((resolve, reject) => {
					this.$emit('getTableValue', async (data) => {
						resolve(await data);
					});
				});
			}
		}
	}
</script>

<style>
</style>