﻿<template>
	<el-dialog :title="!dataForm.id ? '新建人员分润设置' : isDetail ? '详情' : '编辑'" :close-on-click-modal="false" 
		:visible.sync="visible" class="NCC-dialog NCC-dialog_center" lock-scroll width="700px">
		<el-row :gutter="20" class="">
			<el-form ref="elForm" :model="dataForm" size="small" label-width="120px" label-position="right" 
				:disabled="!!isDetail" :rules="dynamicRules">
				
				<!-- 分润人员账号信息 - 仅在新建时显示 -->
				<template v-if="!dataForm.id">
					<el-col :span="24">
						<div class="form-section-title">
							<i class="el-icon-user"></i>
							<span>分润人员账号信息</span>
						</div>
					</el-col>
					
					<el-col :span="12">
						<el-form-item label="分润人员账号" prop="profitUserAccount">
							<el-input v-model="dataForm.profitUserAccount" placeholder="请输入分润人员账号" 
								:style='{"width":"100%"}' clearable>
							</el-input>
						</el-form-item>
					</el-col>
					
					<el-col :span="12">
						<el-form-item label="分润人员密码" prop="profitUserPassword">
							<el-input v-model="dataForm.profitUserPassword" placeholder="请输入分润人员密码" 
								type="password" :style='{"width":"100%"}' show-password clearable>
							</el-input>
						</el-form-item>
					</el-col>
					
					<el-col :span="12">
						<el-form-item label="分润人员姓名" prop="profitUserName">
							<el-input v-model="dataForm.profitUserName" placeholder="请输入分润人员姓名" 
								:style='{"width":"100%"}' clearable>
							</el-input>
						</el-form-item>
					</el-col>
					
					<el-col :span="12">
						<el-form-item label="分润人员手机号" prop="profitUserPhone">
							<el-input v-model="dataForm.profitUserPhone" placeholder="请输入分润人员手机号" 
								:style='{"width":"100%"}' clearable>
							</el-input>
						</el-form-item>
					</el-col>
				</template>
				
				<!-- 分润设置信息 -->
				<el-col :span="24">
					<div class="form-section-title">
						<i class="el-icon-setting"></i>
						<span>分润设置信息</span>
					</div>
				</el-col>
				
				<el-col :span="12">
					<el-form-item label="用户角色名称" prop="userRoleName">
						<el-select v-model="dataForm.userRoleName" placeholder="请选择角色" 
							:style='{"width":"100%"}' clearable>
							<el-option label="补货员" value="补货员"></el-option>
							<el-option label="合伙人" value="合伙人"></el-option>
							<el-option label="推广员" value="推广员"></el-option>
							<el-option label="分润人员" value="分润人员"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				
				<el-col :span="12">
					<el-form-item label="代理商ID" prop="agentId">
						<el-select v-model="dataForm.agentId" placeholder="请选择代理商" 
							:style='{"width":"100%"}' clearable filterable>
							<el-option v-for="(item, index) in agentIdOptions" :key="index" 
								:label="item.RealName" :value="item.Id">
							</el-option>
						</el-select>
					</el-form-item>
				</el-col>
				
				<el-col :span="12">
					<el-form-item label="分润比例" prop="profitRatio">
						<div class="input-with-unit">
							<el-input-number v-model="dataForm.profitRatio" placeholder="请输入分润比例" 
								:step="0.01" :precision="2" :min="0" :max="100" 
								:style='{"width":"100%"}'>
							</el-input-number>
							<span class="unit-text">%</span>
						</div>
					</el-form-item>
				</el-col>
				
				<el-col :span="12">
					<el-form-item label="是否启用" prop="enabledMark">
						<el-switch v-model="dataForm.enabledMark" :active-value="1" :inactive-value="0">
						</el-switch>
					</el-form-item>
				</el-col>
				
				<el-col :span="24">
					<el-form-item label="备注信息" prop="description">
						<el-input v-model="dataForm.description" placeholder="请输入备注信息" 
							show-word-limit :style='{"width":"100%"}' type='textarea' 
							:autosize='{"minRows":3,"maxRows":5}'>
						</el-input>
					</el-form-item>
				</el-col>
				
			</el-form>
		</el-row>
		<span slot="footer" class="dialog-footer">
			<el-button @click="visible = false">取 消</el-button>
			<el-button type="primary" @click="dataFormSubmit()" v-if="!isDetail" :loading="submitLoading">确 定</el-button>
		</span>
	</el-dialog>
</template>
<script>
	import request from '@/utils/request'
	import { getDictionaryDataSelector } from '@/api/systemData/dictionary'
	import { previewDataInterface } from '@/api/systemData/dataInterface'
	export default {
		components: {},
		props: [],
		data() {
			return {
				loading: false,
				submitLoading: false,
				visible: false,
				isDetail: false,
				dataForm: {
					id:'',
					profitUserAccount: '',
					profitUserName: '',
					profitUserPhone: '',
					profitUserPassword: '',
					userRoleName: '',
					profitRatio: 0,
					enabledMark: 1,
					agentId: '',
					description: '',
				},
				rules: {
					userRoleName: [
						{ required: true, message: '请选择用户角色', trigger: 'change' }
					],
					profitRatio: [
						{ required: true, message: '请输入分润比例', trigger: 'blur' },
						{ type: 'number', min: 0, max: 100, message: '分润比例必须在0-100之间', trigger: 'blur' }
					],
					agentId: [
						{ required: true, message: '请选择代理商', trigger: 'change' }
					]
				},
				agentIdOptions: [],
			}
		},
		computed: {
			// 动态验证规则
			dynamicRules() {
				const baseRules = {
					userRoleName: [
						{ required: true, message: '请选择用户角色', trigger: 'change' }
					],
					profitRatio: [
						{ required: true, message: '请输入分润比例', trigger: 'blur' },
						{ type: 'number', min: 0, max: 100, message: '分润比例必须在0-100之间', trigger: 'blur' }
					],
					agentId: [
						{ required: true, message: '请选择代理商', trigger: 'change' }
					]
				};
				
				// 仅在新建时添加账号相关验证
				if (!this.dataForm.id) {
					baseRules.profitUserAccount = [
						{ required: true, message: '请输入分润人员账号', trigger: 'blur' },
						{ min: 3, max: 20, message: '账号长度在 3 到 20 个字符', trigger: 'blur' }
					];
					baseRules.profitUserPassword = [
						{ required: true, message: '请输入分润人员密码', trigger: 'blur' },
						{ min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
					];
					baseRules.profitUserName = [
						{ required: true, message: '请输入分润人员姓名', trigger: 'blur' }
					];
					baseRules.profitUserPhone = [
						{ required: true, message: '请输入分润人员手机号', trigger: 'blur' },
						{ pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号格式', trigger: 'blur' }
					];
				}
				
				return baseRules;
			}
		},
        watch: {},
        created() {
			this.getAgentIdOptions();
		},
		mounted() {
        },
		methods: {
			// 获取代理商选项
			getAgentIdOptions() {
				request({
					url: '/api/Extend/UavAgentProfitConfig/GetAllAgent',
					method: 'get'
				}).then(res => {
					this.agentIdOptions = res.data || [];
				}).catch(() => {
					this.agentIdOptions = [];
				});
			},
			
			goBack() {
                this.$emit('refresh')
            },
			init(id, isDetail) {
				this.dataForm.id = id || 0;
                this.visible = true;
                this.isDetail = isDetail || false;
				this.$nextTick(() => {
					this.$refs['elForm'].resetFields();
					if (this.dataForm.id) {
						request({
							url: '/api/Extend/UavProfitUserSetting/' + this.dataForm.id,
							method: 'get'
						}).then(res =>{
							this.dataForm = res.data;
						})
					}
				})
			},
			dataFormSubmit() {
				this.$refs['elForm'].validate((valid) => {
                    if (valid) {
						this.submitLoading = true;
                        if (!this.dataForm.id) {
                            request({
                                url: `/api/Extend/UavProfitUserSetting`,
                                method: 'post',
                                data: this.dataForm,
                            }).then((res) => {
								this.submitLoading = false;
                                this.$message({
                                    message: res.msg,
                                    type: 'success',
                                    duration: 1000,
                                    onClose: () => {
                                        this.visible = false,
                                            this.$emit('refresh', true)
                                    }
                                })
                            }).catch(() => {
								this.submitLoading = false;
							})
                        } else {
                            request({
                                url: '/api/Extend/UavProfitUserSetting/' + this.dataForm.id,
                                method: 'PUT',
                                data: this.dataForm
                            }).then((res) => {
								this.submitLoading = false;
                                this.$message({
                                    message: res.msg,
                                    type: 'success',
                                    duration: 1000,
                                    onClose: () => {
                                        this.visible = false
                                        this.$emit('refresh', true)
                                    }
                                })
                            }).catch(() => {
								this.submitLoading = false;
							})
                        }
                    }
                })
			},
		}
	}
</script>

<style scoped>
.form-section-title {
	font-size: 16px;
	font-weight: 600;
	color: #303133;
	margin: 20px 0 15px 0;
	padding-bottom: 8px;
	border-bottom: 2px solid #409EFF;
	display: flex;
	align-items: center;
}

.form-section-title i {
	margin-right: 8px;
	color: #409EFF;
}

.input-with-unit {
	display: flex;
	align-items: center;
	width: 100%;
	position: relative;
}

.input-with-unit .el-input-number {
	flex: 1;
}

.unit-text {
	position: absolute;
	right: 8px;
	top: 50%;
	transform: translateY(-50%);
	color: #606266;
	font-size: 14px;
	font-weight: 500;
	white-space: nowrap;
	pointer-events: none;
	z-index: 1;
}

.el-input-number {
	width: 100%;
}

.el-select {
	width: 100%;
}
</style>
