﻿<template>
	<el-dialog :title="!dataForm.id ? '新建' :  isDetail ? '详情':'编辑'" :close-on-click-modal="false" :visible.sync="visible" class="NCC-dialog NCC-dialog_center" lock-scroll width="90%">
		<el-row :gutter="15" class="" >
				<el-form ref="elForm" :model="dataForm" size="small" label-width="100px" label-position="right" :disabled="!!isDetail" :rules="rules">
					<el-col :span="8">
						<el-form-item label="开单编号" prop="id">
							<el-input v-model="dataForm.id" placeholder="请输入" clearable readonly :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="开单会员" prop="kdhy">
							<el-select v-model="dataForm.kdhy" placeholder="请选择" clearable :style='{"width":"100%"}' filterable >
								<el-option v-for="(item, index) in kdhyOptions" :key="index" :label="item.fullName" :value="item.id" ></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="8" v-if="false" >
						<el-form-item label="开单会员名称" prop="kdhyc">
							<el-input v-model="dataForm.kdhyc" placeholder="请输入" clearable :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="8" v-if="false" >
						<el-form-item label="会员手机号" prop="kdhysjh">
							<el-input v-model="dataForm.kdhysjh" placeholder="请输入" clearable :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="单据门店" prop="djmd">
							<el-select v-model="dataForm.djmd" placeholder="请选择" clearable :style='{"width":"100%"}' filterable >
								<el-option v-for="(item, index) in djmdOptions" :key="index" :label="item.fullName" :value="item.id" ></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="金三角" prop="jsj">
							<el-select v-model="dataForm.jsj" placeholder="请选择" clearable :style='{"width":"100%"}' filterable >
								<el-option v-for="(item, index) in jsjOptions" :key="index" :label="item.fullName" :value="item.id" ></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="8" v-if="false">
						<el-form-item label="开单日期" prop="kdrq">
							<el-date-picker v-model="dataForm.kdrq" placeholder="请选择" clearable :style='{"width":"100%"}' type='date' format="yyyy-MM-dd" value-format="timestamp" >
							</el-date-picker>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="顾客类型" prop="gjlx">
							<el-select v-model="dataForm.gjlx" placeholder="请选择" clearable :style='{"width":"100%"}' >
								<el-option v-for="(item, index) in gjlxOptions" :key="index" :label="item.fullName" :value="item.id" ></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="整单业绩" prop="zdyj">
							<el-input v-model="dataForm.zdyj" placeholder="请输入" clearable :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="实付业绩" prop="sfyj">
							<el-input v-model="dataForm.sfyj" placeholder="请输入" clearable :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="欠款" prop="qk">
							<el-input v-model="dataForm.qk" placeholder="请输入" clearable :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="是否储扣" prop="sfck">
							<el-radio-group v-model="dataForm.sfck" :style='{}' >
								<el-radio v-for="(item, index) in sfckOptions" :key="index" :label="item.id"  >{{item.fullName}}</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :span="8" v-if="dataForm.sfck === '是'">
						<el-form-item label="储扣方式" prop="ckfs">
							<el-select v-model="dataForm.ckfs" placeholder="请选择" clearable :style='{"width":"100%"}' >
								<el-option v-for="(item, index) in ckfsOptions" :key="index" :label="item.fullName" :value="item.id" ></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="8" v-if="dataForm.sfck === '是'">
						<el-form-item label="储扣明细" prop="ckmx">
							<el-input v-model="dataForm.ckmx" placeholder="请输入" clearable :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="付款方式" prop="fkfs">
							<el-select v-model="dataForm.fkfs" placeholder="请选择" clearable :style='{"width":"100%"}' >
								<el-option v-for="(item, index) in fkfsOptions" :key="index" :label="item.fullName" :value="item.id" ></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="结算机构" prop="fkyy">
							<el-select v-model="dataForm.fkyy" placeholder="请选择" clearable :style='{"width":"100%"}' filterable >
								<el-option v-for="(item, index) in fkyyOptions" :key="index" :label="item.fullName" :value="item.id" ></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="合作机构" prop="hgjg">
							<el-select v-model="dataForm.hgjg" placeholder="请选择" clearable :style='{"width":"100%"}' >
								<el-option v-for="(item, index) in hgjgOptions" :key="index" :label="item.fullName" :value="item.id" ></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="8" v-if="false">
						<el-form-item label="付款判断" prop="fkpd">
							<el-input v-model="dataForm.fkpd" placeholder="请输入" clearable :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="8" v-if="false">
						<el-form-item label="客户来源" prop="khly">
							<el-select v-model="dataForm.khly" placeholder="请选择" clearable :style='{"width":"100%"}' >
								<el-option v-for="(item, index) in khlyOptions" :key="index" :label="item.fullName" :value="item.id" ></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="8" v-if="false">
						<el-form-item label="推荐人" prop="tjr">
							<el-input v-model="dataForm.tjr" placeholder="请输入" clearable :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="是否首开订单" prop="sfskdd">
							<el-radio-group v-model="dataForm.sfskdd" :style='{}' >
								<el-radio v-for="(item, index) in sfskddOptions" :key="index" :label="item.id"  >{{item.fullName}}</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
				
					<el-col :span="24">
						<el-form-item label-width="0">
							<el-table :data="dataForm.lqKdJksyjList" size='mini'>
								<el-table-column type="index" width="50" label="序号" align="center" />
								<el-table-column prop="id" label="业绩编号" v-if="false">
									<template slot-scope="scope">
										<el-input v-model="scope.row.id" placeholder="请输入" clearable ></el-input>
									</template>
								</el-table-column>
								<el-table-column prop="glkdbh" label="关联开单编号" v-if="false">
									<template slot-scope="scope">
										<el-input v-model="scope.row.glkdbh" placeholder="请输入" clearable ></el-input>
									</template>
								</el-table-column>
								<el-table-column prop="jks" label="健康师">
									<template slot-scope="scope">
										<user-select v-model="scope.row.jks" placeholder="请选择" clearable ></user-select>
									</template>
								</el-table-column>
								<el-table-column prop="jksxm" label="健康师姓名">
									<template slot-scope="scope">
										<el-input v-model="scope.row.jksxm" placeholder="请输入" clearable ></el-input>
									</template>
								</el-table-column>
								<el-table-column prop="jkszh" label="健康师账号">
									<template slot-scope="scope">
										<el-input v-model="scope.row.jkszh" placeholder="请输入" clearable ></el-input>
									</template>
								</el-table-column>
								<el-table-column prop="jksyj" label="健康师业绩">
									<template slot-scope="scope">
										<el-input v-model="scope.row.jksyj" placeholder="请输入" clearable ></el-input>
									</template>
								</el-table-column>
								<el-table-column prop="yjsj" label="业绩时间" v-if="false">
									<template slot-scope="scope">
										<el-input v-model="scope.row.yjsj" placeholder="请输入" clearable ></el-input>
									</template>
								</el-table-column>
								<el-table-column label="操作" width="50">
									<template slot-scope="scope">
										<el-button size="mini" type="text" class="NCC-table-delBtn" @click="handleDelLqKdJksyjEntityList(scope.$index)">删除</el-button>
									</template>
								</el-table-column>
							</el-table>
							<div class="table-actions" @click="addHandleLqKdJksyjEntityList()">
								<el-button type="text" icon="el-icon-plus">新增</el-button>
							</div>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label-width="0">
							<el-table :data="dataForm.lqKdKjbsyjList" size='mini'>
								<el-table-column type="index" width="50" label="序号" align="center" />
								<el-table-column prop="id" label="业绩编号" v-if="false">
									<template slot-scope="scope">
										<el-input v-model="scope.row.id" placeholder="请输入" clearable ></el-input>
									</template>
								</el-table-column>
								<el-table-column prop="glkdbh" label="关联开单编号" v-if="false">
									<template slot-scope="scope">
										<el-input v-model="scope.row.glkdbh" placeholder="请输入" clearable ></el-input>
									</template>
								</el-table-column>
								<el-table-column prop="kjbls" label="科技部老师">
									<template slot-scope="scope">
										<user-select v-model="scope.row.kjbls" placeholder="请选择" clearable ></user-select>
									</template>
								</el-table-column>
								<el-table-column prop="kjblsxm" label="科技部老师姓名">
									<template slot-scope="scope">
										<el-input v-model="scope.row.kjblsxm" placeholder="请输入" clearable ></el-input>
									</template>
								</el-table-column>
								<el-table-column prop="kjblszh" label="科技部老师账号">
									<template slot-scope="scope">
										<el-input v-model="scope.row.kjblszh" placeholder="请输入" clearable ></el-input>
									</template>
								</el-table-column>
								<el-table-column prop="kjblsyj" label="科技部老师业绩">
									<template slot-scope="scope">
										<el-input v-model="scope.row.kjblsyj" placeholder="请输入" clearable ></el-input>
									</template>
								</el-table-column>
								<el-table-column prop="yjsj" label="业绩时间" v-if="false">
									<template slot-scope="scope">
										<el-input v-model="scope.row.yjsj" placeholder="请输入" clearable ></el-input>
									</template>
								</el-table-column>
								<el-table-column label="操作" width="50">
									<template slot-scope="scope">
										<el-button size="mini" type="text" class="NCC-table-delBtn" @click="handleDelLqKdKjbsyjEntityList(scope.$index)">删除</el-button>
									</template>
								</el-table-column>
							</el-table>
							<div class="table-actions" @click="addHandleLqKdKjbsyjEntityList()">
								<el-button type="text" icon="el-icon-plus">新增</el-button>
							</div>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label-width="0">
							<el-table :data="dataForm.lqKdPxmxList" size='mini'>
								<el-table-column type="index" width="50" label="序号" align="center" />
								<el-table-column prop="id" label="明细编号" v-if="false">
									<template slot-scope="scope">
										<el-input v-model="scope.row.id" placeholder="请输入" clearable ></el-input>
									</template>
								</el-table-column>
								<el-table-column prop="glkdbh" label="关联开单编号" v-if="false">
									<template slot-scope="scope">
										<el-input v-model="scope.row.glkdbh" placeholder="请输入" clearable ></el-input>
									</template>
								</el-table-column>
								<el-table-column prop="px" label="品项">
									<template slot-scope="scope">
										<el-select v-model="scope.row.px" placeholder="请选择" clearable >
											<el-option v-for="(item, index) in pxOptions" :key="index" :label="item.fullName" :value="item.id" :disabled="item.disabled"></el-option>
										</el-select>
									</template>
								</el-table-column>
								<el-table-column prop="pxmc" label="品项名称">
									<template slot-scope="scope">
										<el-input v-model="scope.row.pxmc" placeholder="请输入" clearable ></el-input>
									</template>
								</el-table-column>
								<el-table-column prop="pxjg" label="品项价格">
									<template slot-scope="scope">
										<el-input v-model="scope.row.pxjg" placeholder="请输入" clearable ></el-input>
									</template>
								</el-table-column>
								<el-table-column label="操作" width="50">
									<template slot-scope="scope">
										<el-button size="mini" type="text" class="NCC-table-delBtn" @click="handleDelLqKdPxmxEntityList(scope.$index)">删除</el-button>
									</template>
								</el-table-column>
							</el-table>
							<div class="table-actions" @click="addHandleLqKdPxmxEntityList()">
								<el-button type="text" icon="el-icon-plus">新增</el-button>
							</div>
						</el-form-item>
					</el-col>

						<el-col :span="24">
						<el-form-item label="简介" prop="jj">
							<el-input v-model="dataForm.jj" placeholder="请输入" show-word-limit :style='{"width":"100%"}' type='textarea' :autosize='{"minRows":4,"maxRows":4}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="上传文件" prop="scwj">
							<NCC-UploadImg v-model="dataForm.scwj" :fileSize="30" sizeUnit="MB" :limit="9" >
							</NCC-UploadImg>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="会员签字" prop="hyqz">
							<NCC-UploadImg v-model="dataForm.hyqz" :fileSize="5" sizeUnit="MB" :limit="9" >
							</NCC-UploadImg>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="备注" prop="bz">
							<el-input v-model="dataForm.bz" placeholder="请输入" show-word-limit :style='{"width":"100%"}' type='textarea' :autosize='{"minRows":4,"maxRows":4}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="健康师业绩" prop="jksyj" v-if="false">
							<el-input v-model="dataForm.jksyj" placeholder="请输入" clearable :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="科技部老师业绩" prop="kjblsyj" v-if="false">
							<el-input v-model="dataForm.kjblsyj" placeholder="请输入" clearable :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="品项信息" prop="pxxx" v-if="false">
							<el-input v-model="dataForm.pxxx" placeholder="请输入" clearable :style='{"width":"100%"}' >
							</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">确 定</el-button>
		</span>
	</el-dialog>
</template>
<script>
	import request from '@/utils/request'
	import { getDictionaryDataSelector } from '@/api/systemData/dictionary'
	import { previewDataInterface } from '@/api/systemData/dataInterface'
	import { getUserInfo } from '@/api/permission/user'
	export default {
		components: {},
		props: [],
		data() {
			return {
				loading: false,
				visible: false,
				isDetail: false,
				dataForm: {
					id:'',
					id:undefined,
					kdhy:undefined,
					kdhyc:undefined,
					kdhysjh:undefined,
					djmd:undefined,
					jsj:undefined,
					kdrq:undefined,
					gjlx:undefined,
					hgjg:undefined,
					zdyj:undefined,
					sfyj:undefined,
					qk:undefined,
					ckfs:undefined,
					ckmx:undefined,
					fkfs:undefined,
					fkyy:undefined,
					fkpd:undefined,
					khly:undefined,
					tjr:undefined,
					sfskdd:undefined,
					sfck:undefined,
					jj:undefined,
					scwj:[],
					hyqz:[],
					bz:undefined,
					lqKdJksyjList:[],
					lqKdKjbsyjList:[],
					lqKdPxmxList:[],
					jksyj:undefined,
					kjblsyj:undefined,
					pxxx:undefined,
				},
				rules: {
				},
				kdhyOptions : [],
				djmdOptions : [],
				jsjOptions : [],
				gjlxOptions:[{"fullName":"会员","id":"会员"},{"fullName":"非会员","id":"非会员"}],
				hgjgOptions : [],
				ckfsOptions:[{"fullName":"储值卡","id":"储值卡"},{"fullName":"扣项","id":"扣项"},{"fullName":"套餐","id":"套餐"}],
				fkfsOptions:[{"fullName":"现金","id":"现金"},{"fullName":"微信","id":"微信"},{"fullName":"支付宝","id":"支付宝"},{"fullName":"银行卡","id":"银行卡"},{"fullName":"医院","id":"医院"}],
				fkyyOptions : [],
				khlyOptions:[{"fullName":"自然到店","id":"自然到店"},{"fullName":"会员推广","id":"会员推广"},{"fullName":"网络推广","id":"网络推广"}],
				sfskddOptions:[{"fullName":"是","id":"是"},{"fullName":"否","id":"否"}],
				sfckOptions:[{"fullName":"是","id":"是"},{"fullName":"否","id":"否"}],
				pxOptions : [],
			}
		},
		computed: {},
        watch: {
			'dataForm.zdyj': {
				handler(newVal) {
					this.calculateQk();
				}
			},
			'dataForm.sfyj': {
				handler(newVal) {
					this.calculateQk();
				}
			}
		},
        created() {
			this.getkdhyOptions();
			this.getdjmdOptions();
			this.getjsjOptions();
			this.gethgjgOptions();
			this.getfkyyOptions();
			this.getpxOptions();
		},
		mounted() {
        },
		methods: {
			getkdhyOptions(){
				previewDataInterface('730998109110273285').then(res => {
					this.kdhyOptions = res.data
				});
			},
			getdjmdOptions(){
				previewDataInterface('730960205902251269').then(res => {
					this.djmdOptions = res.data
				});
			},
			getjsjOptions(){
				previewDataInterface('733894897408410885').then(res => {
					this.jsjOptions = res.data
				});
			},
			gethgjgOptions(){
				previewDataInterface('733896629660157189').then(res => {
					this.hgjgOptions = res.data
				});
			},
			getfkyyOptions(){
				previewDataInterface('733898797075137797').then(res => {
					this.fkyyOptions = res.data
				});
			},
			getpxOptions(){
				previewDataInterface('730971133356016901').then(res => {
					this.pxOptions = res.data
				});
			},
			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/LqKdKdjlb/' + this.dataForm.id,
							method: 'get'
						}).then(res =>{
							this.dataForm = res.data;
							if(!this.dataForm.scwj)this.dataForm.scwj=[];
							if(!this.dataForm.hyqz)this.dataForm.hyqz=[];
						})
					} else {
						// 新增时获取当前用户门店
						this.getCurrentUserStore();
					}
				})
			},
			getCurrentUserStore() {
				getUserInfo().then(res => {
					if (res.data && res.data.organizeId) {
						this.dataForm.djmd = res.data.organizeId;
					}
				}).catch(() => {
					// 获取失败时忽略
				});
			},
			calculateQk() {
				const zdyj = parseFloat(this.dataForm.zdyj) || 0;
				const sfyj = parseFloat(this.dataForm.sfyj) || 0;
				this.dataForm.qk = (zdyj - sfyj).toFixed(2);
			},
			dataFormSubmit() {
				this.$refs['elForm'].validate((valid) => {
                    if (valid) {
                        if (!this.dataForm.id) {
                            request({
                                url: `/api/Extend/LqKdKdjlb`,
                                method: 'post',
                                data: this.dataForm,
                            }).then((res) => {
                                this.$message({
                                    message: res.msg,
                                    type: 'success',
                                    duration: 1000,
                                    onClose: () => {
                                        this.visible = false,
                                            this.$emit('refresh', true)
                                    }
                                })
                            })
                        } else {
                            request({
                                url: '/api/Extend/LqKdKdjlb/' + this.dataForm.id,
                                method: 'PUT',
                                data: this.dataForm
                            }).then((res) => {
                                this.$message({
                                    message: res.msg,
                                    type: 'success',
                                    duration: 1000,
                                    onClose: () => {
                                        this.visible = false
                                        this.$emit('refresh', true)
                                    }
                                })
                            })
                        }
                    }
                })
			},
			addHandleLqKdJksyjEntityList() {
				let item = {
					id:undefined,
					glkdbh:undefined,
					jks:undefined,
					jksxm:undefined,
					jkszh:undefined,
					jksyj:undefined,
					yjsj:undefined,
				}
				this.dataForm.lqKdJksyjList.push(item)
			},
			handleDelLqKdJksyjEntityList(index) {
				this.dataForm.lqKdJksyjList.splice(index, 1);
			},
			addHandleLqKdKjbsyjEntityList() {
				let item = {
					id:undefined,
					glkdbh:undefined,
					kjbls:undefined,
					kjblsxm:undefined,
					kjblszh:undefined,
					kjblsyj:undefined,
					yjsj:undefined,
				}
				this.dataForm.lqKdKjbsyjList.push(item)
			},
			handleDelLqKdKjbsyjEntityList(index) {
				this.dataForm.lqKdKjbsyjList.splice(index, 1);
			},
			addHandleLqKdPxmxEntityList() {
				let item = {
					id:undefined,
					glkdbh:undefined,
					px:undefined,
					pxmc:undefined,
					pxjg:undefined,
				}
				this.dataForm.lqKdPxmxList.push(item)
			},
			handleDelLqKdPxmxEntityList(index) {
				this.dataForm.lqKdPxmxList.splice(index, 1);
			},
		}
	}
</script>
