﻿<template>
<transition name="el-zoom-in-center">
	<div class="NCC-preview-main">
			<div class="NCC-common-page-header">
				<el-page-header @back="goBack" :content="!dataForm.id ? '新建' : isDetail ? '详情' : '编辑'"/>
				<div class="options">
					<el-button type="primary" @click="dataFormSubmit()" v-if="!isDetail">确 定</el-button>
					<el-button @click="goBack">取 消</el-button>
				</div>
		  </div>
		<el-row :gutter="15"  class="main" :style="{margin: '0 auto',width:'100%'}">
				<el-form ref="elForm" :model="dataForm" size="small" label-width="100px" label-position="right" :disabled="!!isDetail" :rules="rules">
					<el-col :span="24" >
						<el-form-item label="自动编号" prop="enCode">
							<el-input v-model="dataForm.enCode" placeholder="系统自动生成" readonly >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12" >
						<el-form-item label="项目名称" prop="project">
							<el-input v-model="dataForm.project" placeholder="请输入" clearable required :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12" >
						<el-form-item label="客户名称" prop="customer">
							<el-input v-model="dataForm.customer" placeholder="请输入" clearable required :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24" >
						<el-form-item label="备注说明" prop="remark">
							<el-input v-model="dataForm.remark" placeholder="请输入报价有效期、是否含税等说明" required 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="appendix">
							<NCC-UploadFz v-model="dataForm.appendix" :fileSize="1" sizeUnit="GB" :limit="9" buttonText="点击上传" >
							</NCC-UploadFz>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label-width="0" >
							<el-table :data="dataForm.zyOaPricelistList" size='mini' >
								<el-table-column type="index" width="50" label="序号" align="center" />
								<el-table-column prop="sortCode" label="序号">
									<template slot-scope="scope">
										<el-input-number v-model="scope.row.sortCode" placeholder="数字文本" required ></el-input-number>
									</template>
								</el-table-column>
								<el-table-column prop="name" label="品名">
									<template slot-scope="scope">
										<el-input v-model="scope.row.name" placeholder="请输入" clearable required ></el-input>
									</template>
								</el-table-column>
								<el-table-column prop="specs" label="规格">
									<template slot-scope="scope">
										<el-input v-model="scope.row.specs" placeholder="请输入" clearable required></el-input>
									</template>
								</el-table-column>
								<el-table-column prop="unit" label="单位">
									<template slot-scope="scope">
										<el-select v-model="scope.row.unit" placeholder="请选择" clearable required>
											<el-option v-for="(item, index) in unitOptions" :key="index" :label="item.fullName" :value="item.id" :disabled="item.disabled"></el-option>
										</el-select>
									</template>
								</el-table-column>
								<el-table-column prop="amount" label="单价" required>
									<template slot-scope="scope">
										<el-input-number v-model="scope.row.amount" placeholder="数字文本" precision="2"  :step="100" ></el-input-number>
									</template>
								</el-table-column>
								<el-table-column prop="num" label="数量" required>
									<template slot-scope="scope">
										<el-input-number v-model="scope.row.num" placeholder="数字文本" :step="1" ></el-input-number>
									</template>
								</el-table-column>
								<el-table-column prop="description" label="备注">
									<template slot-scope="scope">
										<el-input v-model="scope.row.description" 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="handleDelZyOaPricelistEntityList(scope.$index)">删除</el-button>
									</template>
								</el-table-column>
							</el-table>
							<div class="table-actions" @click="addHandleZyOaPricelistEntityList()">
								<el-button type="text" icon="el-icon-plus">新增</el-button>
							</div>
						</el-form-item>
					</el-col>
					<el-col :span="6" v-if="false" >
						<el-form-item label="创建用户" prop="creatorUserId">
							<el-input v-model="dataForm.creatorUserId" placeholder="系统自动生成" readonly >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="6" v-if="false" >
						<el-form-item label="创建时间" prop="creatorTime">
							<el-input v-model="dataForm.creatorTime" placeholder="系统自动生成" readonly >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="6" v-if="false" >
						<el-form-item label="修改用户" prop="lastModifyUserId">
							<el-input v-model="dataForm.lastModifyUserId" placeholder="系统自动生成" readonly >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="6" v-if="false" >
						<el-form-item label="修改时间" prop="lastModifyTime">
							<el-input v-model="dataForm.lastModifyTime" placeholder="系统自动生成" readonly >
							</el-input>
						</el-form-item>
					</el-col>
				</el-form>
		</el-row>
	</div>
</transition>
</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,
				visible: false,
				isDetail: false,
				dataForm: {
					enCode:undefined,
					project:undefined,
					customer:undefined,
					remark:undefined,
					appendix:[],
					zyOaPricelistList:[],
					creatorUserId:undefined,
					creatorTime:undefined,
					lastModifyUserId:undefined,
					lastModifyTime:undefined,
				},
				rules: {
					project:[
						{
							required:true,
							message:'请输入项目名称',
							trigger:'blur'
						},
					],
					customer:[
						{
							required:true,
							message:'请输入客户名称',
							trigger:'blur'
						},
					],
					remark:[
						{
							required:true,
							message:'请输入备注说明',
							trigger:'blur'
						},
					],
				     sortCode:[
						{
							required:true,
							message:'请输入序号',
							trigger:'blur'
						},
					],
					name:[
						{
							required:true,
							message:'请输入品名',
							trigger:'blur'
						},
					],
				},
				unitOptions : [],
			}
		},
		computed: {},
        watch: {},
        created() {
			this.getunitOptions();
		},
		mounted() {
        },
		methods: {
			getunitOptions(){
			  getDictionaryDataSelector('d4d4cc4401064c34a81099a76f778dc3').then(res => {
					this.unitOptions = res.data.list
				});
			},
			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/SubDev/ZyOaPrice/' + this.dataForm.id,
							method: 'get'
						}).then(res =>{
							this.dataForm = res.data;
						})
					}
				})
			},
			dataFormSubmit() {
				this.$refs['elForm'].validate((valid) => {
                    if (valid) {
                        if (!this.dataForm.id) {
                            request({
                                url: `/api/SubDev/ZyOaPrice`,
                                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/SubDev/ZyOaPrice/' + 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)
                                    }
                                })
                            })
                        }
                    }
                })
			},
			addHandleZyOaPricelistEntityList() {
				let item = {
					sortCode:1,
					name:undefined,
					specs:undefined,
					unit:undefined,
					amount:0,
					num:1,
					description:undefined,
				}
				this.dataForm.zyOaPricelistList.push(item)
			},
			handleDelZyOaPricelistEntityList(index) {
				this.dataForm.zyOaPricelistList.splice(index, 1);
			},
	}
}
</script>
