<template>
	<el-dialog :title="!dataForm.id ? '新增清洗商' : '编辑清洗商'" :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-row :gutter="15">
				<el-col :span="24">
					<el-form-item label="清洗商名称" prop="supplierName">
						<el-input v-model="dataForm.supplierName" placeholder="请输入清洗商名称" clearable
							:style='{"width":"100%"}' />
					</el-form-item>
				</el-col>
			<el-col :span="24">
				<el-form-item label="产品类型" prop="productType">
					<el-select v-model="dataForm.productType" placeholder="请选择产品类型" clearable filterable
						:style='{"width":"100%"}'>
						<el-option v-for="item in productTypeOptions" :key="item.Value" :label="item.Name" :value="item.Name" />
					</el-select>
				</el-form-item>
			</el-col>
				<el-col :span="24">
					<el-form-item label="清洗价格" prop="laundryPrice">
						<el-input-number v-model="dataForm.laundryPrice" placeholder="请输入清洗价格" :min="0" :precision="2"
							:style='{"width":"100%"}' />
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="备注" prop="remark">
						<el-input v-model="dataForm.remark" type="textarea" :rows="4" placeholder="请输入备注信息" clearable
							:style='{"width":"100%"}' />
					</el-form-item>
				</el-col>
			</el-row>
		</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 request from '@/utils/request'

export default {
	data() {
		return {
			visible: false,
			btnLoading: false,
			productTypeOptions: [],
			dataForm: {
				id: undefined,
				supplierName: '',
				productType: '',
				laundryPrice: 0,
				remark: ''
			},
			rules: {
				supplierName: [
					{ required: true, message: '请输入清洗商名称', trigger: 'blur' }
				],
				productType: [
					{ required: true, message: '请选择产品类型', trigger: 'change' }
				],
				laundryPrice: [
					{ required: true, message: '请输入清洗价格', trigger: 'blur' },
					{ type: 'number', min: 0, message: '清洗价格不能小于0', trigger: 'blur' }
				]
			}
		}
	},
	methods: {
		init(id) {
			this.visible = true
			this.dataForm = {
				id: undefined,
				supplierName: '',
				productType: '',
				laundryPrice: 0,
				remark: ''
			}
			this.loadProductTypeOptions()
			if (id) {
				// 编辑模式，获取详情
				this.getDetail(id)
			}
		},
		// 加载产品类型选项
		loadProductTypeOptions() {
			request({
				url: '/api/Extend/LqStoreConsumableInventory/consumable-product-type',
				method: 'GET'
			}).then(res => {
				if (res.code == 200 && res.data && Array.isArray(res.data)) {
					this.productTypeOptions = res.data
				} else {
					this.productTypeOptions = []
				}
			}).catch(() => {
				this.productTypeOptions = []
			})
		},
		// 获取详情
		getDetail(id) {
			request({
				url: `/api/Extend/LqLaundrySupplier/${id}`,
				method: 'GET'
			}).then(res => {
				if (res.code == 200 && res.data) {
					const data = res.data
					this.dataForm = {
						id: data.id,
						supplierName: data.supplierName || '',
						productType: data.productType || '',
						laundryPrice: data.laundryPrice || 0,
						remark: data.remark || ''
					}
				}
			}).catch(() => {
				this.$message({
					type: 'error',
					message: '获取详情失败'
				})
			})
		},
		// 提交表单
		dataFormSubmit() {
			this.$refs.elForm.validate((valid) => {
				if (!valid) {
					return false
				}
				this.btnLoading = true
				const url = this.dataForm.id ? '/api/Extend/LqLaundrySupplier/Update' : '/api/Extend/LqLaundrySupplier/Create'
				const method = this.dataForm.id ? 'PUT' : 'POST'
				const data = this.dataForm.id
					? {
						id: this.dataForm.id,
						supplierName: this.dataForm.supplierName,
						productType: this.dataForm.productType,
						laundryPrice: this.dataForm.laundryPrice,
						remark: this.dataForm.remark
					}
					: {
						supplierName: this.dataForm.supplierName,
						productType: this.dataForm.productType,
						laundryPrice: this.dataForm.laundryPrice,
						remark: this.dataForm.remark
					}
				request({
					url: url,
					method: method,
					data: data
				}).then(res => {
					this.btnLoading = false
					this.$message({
						type: 'success',
						message: res.msg || (this.dataForm.id ? '编辑成功' : '创建成功'),
						onClose: () => {
							this.visible = false
							this.$emit('refresh')
						}
					})
				}).catch(() => {
					this.btnLoading = false
				})
			})
		}
	}
}
</script>

<style lang="scss" scoped>
</style>

