<template>
	<el-dialog title="审批申请" :close-on-click-modal="false" :visible.sync="visible"
		class="NCC-dialog NCC-dialog_center" lock-scroll width="600px">
		<el-form ref="elForm" :model="dataForm" size="small" label-width="120px" label-position="right" :rules="rules">
			<el-form-item label="申请编号">
				<span>{{ dataForm.id || '无' }}</span>
			</el-form-item>
			<el-form-item label="申请人">
				<span>{{ dataForm.applicationUserName || '无' }}</span>
			</el-form-item>
			<el-form-item label="申请门店">
				<span>{{ dataForm.applicationStoreName || '无' }}</span>
			</el-form-item>
			<el-form-item label="申请时间">
				<span>{{ formatDateTime(dataForm.applicationTime) }}</span>
			</el-form-item>
			<el-form-item label="审批状态">
				<el-tag :type="getStatusType(dataForm.approvalStatus)" size="small">
					{{ dataForm.approvalStatus || '无' }}
				</el-tag>
			</el-form-item>
			<el-form-item label="备注">
				<span>{{ dataForm.remarks || '无' }}</span>
			</el-form-item>
			<el-divider></el-divider>
			<el-form-item label="审批结果" prop="result">
				<el-radio-group v-model="dataForm.result">
					<el-radio label="通过">通过</el-radio>
					<el-radio label="不通过">不通过</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="审批意见" prop="opinion">
				<el-input v-model="dataForm.opinion" type="textarea" :rows="4" placeholder="请输入审批意见（可选）"
					maxlength="500" show-word-limit />
			</el-form-item>
		</el-form>
		<span slot="footer" class="dialog-footer">
			<el-button @click="visible = false">取 消</el-button>
			<el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading">确 定</el-button>
		</span>
	</el-dialog>
</template>

<script>
import { approveApplication } from '@/api/extend/lqInventory'

export default {
	data() {
		return {
			visible: false,
			btnLoading: false,
			dataForm: {
				id: '',
				applicationUserName: '',
				applicationStoreName: '',
				applicationTime: null,
				approvalStatus: '',
				remarks: '',
				result: '通过',
				opinion: ''
			},
			rules: {
				result: [
					{ required: true, message: '请选择审批结果', trigger: 'change' }
				]
			}
		}
	},
	methods: {
		init(row) {
			this.visible = true
			this.dataForm = {
				id: row.id || '',
				applicationUserName: row.applicationUserName || '',
				applicationStoreName: row.applicationStoreName || '',
				applicationTime: row.applicationTime || null,
				approvalStatus: row.approvalStatus || '',
				remarks: row.remarks || '',
				result: '通过',
				opinion: ''
			}
		},
		dataFormSubmit() {
			this.$refs.elForm.validate((valid) => {
				if (!valid) {
					return false
				}
				if (this.dataForm.approvalStatus !== '审批中') {
					this.$message({
						type: 'warning',
						message: '只有审批中状态的申请才能进行审批操作'
					})
					return false
				}
				this.btnLoading = true
				approveApplication(this.dataForm.id, this.dataForm.result, this.dataForm.opinion).then(res => {
					this.btnLoading = false
					this.$message({
						type: 'success',
						message: res.msg || '审批成功',
						onClose: () => {
							this.visible = false
							this.$emit('refresh')
						}
					})
				}).catch(() => {
					this.btnLoading = false
				})
			})
		},
		getStatusType(status) {
			const statusMap = {
				'待审批': 'info',
				'审批中': 'warning',
				'已通过': 'success',
				'未通过': 'danger',
				'已退回': 'info'
			}
			return statusMap[status] || 'info'
		},
		formatDateTime(timestamp) {
			if (!timestamp) return '无'
			const date = new Date(timestamp)
			const year = date.getFullYear()
			const month = String(date.getMonth() + 1).padStart(2, '0')
			const day = String(date.getDate()).padStart(2, '0')
			const hours = String(date.getHours()).padStart(2, '0')
			const minutes = String(date.getMinutes()).padStart(2, '0')
			const seconds = String(date.getSeconds()).padStart(2, '0')
			return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
		}
	}
}
</script>

<style lang="scss" scoped>
::v-deep .el-form-item {
	margin-bottom: 18px;
}
</style>

