<template xmlns:v-slot="http://www.w3.org/1999/XSL/Transform">
	<div>
		<el-dialog
			:title="title"
			:close-on-click-modal="false"
			draggable
			width="1400px"
			height="500px"
			@close="close"
			append-to-body
			v-model="visible">
			<el-row>
				<el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="status === 'audit' || status === 'taskFormDetail'"
						 label-width="135px" @submit.native.prevent>

					<el-divider content-position="left"><i class="el-icon-document"></i> 项目信息</el-divider>
					<el-row  :gutter="0">
						<el-col :span="12">
							<el-form-item label="项目名称" prop="projectName"
										  :rules="[
                              {required: true, message:'项目名称不能为空', trigger:'blur'},
                              {required: true, message:'项目名称不能为空', trigger:'change'}
                   ]">
								<el-input :readonly="true" @focus="openContractForm()" v-model="inputForm.projectName" placeholder="请选择项目名称">
									<el-button slot="append" icon="el-icon-search" @click="openContractForm()"></el-button>
								</el-input>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="项目编号" prop="projectNumber"
										  :rules="[
                   ]">
								<el-input :disabled="true" v-model="inputForm.projectNumber" placeholder="请填写项目编号" clearable></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="项目经理1" prop="projectMasterName"
										  :rules="[
                   ]">
								<el-input :disabled="true" v-model="inputForm.projectMasterName" placeholder="请填写项目经理1" clearable></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="项目经理2" prop="realHeaderName"
										  :rules="[
                   ]">
								<el-input :disabled="true" v-model="inputForm.realHeaderName" placeholder="请填写项目经理2" clearable></el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<el-divider content-position="left"><i class="el-icon-document"></i> 质量复核信息</el-divider>
					<el-row  :gutter="0">
						<el-col :span="12">
							<el-form-item label="送审价(元)" prop="reviewFee"
										  :rules="[

                   ]">
								<el-input v-model="inputForm.reviewFee" placeholder="请输入送审价" clearable></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="审定价(元)" prop="approvalFee"
										  :rules="[

			   ]">
								<el-input v-model="inputForm.approvalFee" placeholder="请输入审定价" clearable></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="合同类别" prop="contractCategory" :rules="[]">
								<el-radio-group v-model="inputForm.contractCategory">
									<el-radio v-for="item in $dictUtils.getDictList('contract_amount_type')" :key="item.value" :label="item.value">{{item.label}}</el-radio>
								</el-radio-group>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="合同价(元)" prop="contractFee"
										  :rules="[

			   ]">
								<el-input v-model="inputForm.contractFee" placeholder="请输入合同价" clearable></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="下一节点审核人" prop="reviewBy"
										  :rules="[
                   ]">
								<el-select :disabled="true" v-model="inputForm.reviewBy" placeholder="请选择下一节点审核人" style="width:100%;">
									<el-option
										v-for="item in reviewBys"
										:key="item.index"
										:label="item.label"
										:value="item.value">
									</el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="核增核减额(元)" prop="verifyFee"
										  :rules="[

			   ]">
								<el-input v-model="inputForm.verifyFee" placeholder="请输入核增核减额" clearable></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="核增核减率(元)" prop="verifyRate"
										  :rules="[

			   ]">
								<el-input v-model="inputForm.verifyRate" placeholder="请输入核增核减率" clearable></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="咨询标的额(元)" prop="consultFee"
										  :rules="[

			   ]">
								<el-input v-model="inputForm.consultFee" placeholder="请输入咨询标的额" clearable></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="土建造价(元)" prop="buildingFee"
										  :rules="[

			   ]">
								<el-input v-model="inputForm.buildingFee" placeholder="请输入土建造价" clearable></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="安装造价(元)" prop="installFee"
										  :rules="[

			   ]">
								<el-input v-model="inputForm.installFee" placeholder="请输入安装造价" clearable></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="土建比例(%)" prop="buildingRate"
										  :rules="[

			   ]">
								<el-input v-model="inputForm.buildingRate" placeholder="请输入土建比例" clearable></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="安装比例(%)" prop="installRate"
										  :rules="[

			   ]">
								<el-input v-model="inputForm.installRate" placeholder="请输入土建比例" clearable></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="工作开始时间" prop="startDate" :rules="[
               ]">
								<el-date-picker
									style="width: 100%"
									v-model="inputForm.startDate"
									value-format="YYYY-MM-DD"
									placeholder="选择日期"
								>
								</el-date-picker>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="工作结束时间" prop="endDate" :rules="[
               ]">
								<el-date-picker
									style="width: 100%"
									v-model="inputForm.endDate"
									value-format="YYYY-MM-DD"
									placeholder="选择日期"
								>
								</el-date-picker>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="造价咨询营业收入（万元）" prop="ziXunShouRu"
										  :rules="[

			   ]">
								<el-input v-model="inputForm.ziXunShouRu" placeholder="请输入造价咨询营业收入" clearable></el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<div>
						<el-divider content-position="left"><i class="el-icon-document"></i>
							<span style="color:#f5222d;">*</span> 项目组成员(质量控制流程)
							<el-button size="default" style="margin-left: 20px" type="primary" @click="insertEvent('detail')" plain>
								新增
							</el-button>
						</el-divider>
						<el-row  :gutter="15" >
							<el-col :span="24">
								<vxe-table
									border
									show-footer
									show-overflow
									ref="detailTable"
									:key="detailKey"
									class="vxe-table-element"
									:data="inputForm.detailInfos"
									style="margin-left: 5em"
									highlight-current-row
									:edit-config="{trigger: 'click', mode: 'row', showStatus: true, autoClear: true, icon:'_'}"
									:edit-rules="validRules"
								>
									<vxe-table-column field="userName" title="成员名称" :edit-render="{}" show-overflow="title" :rules="[{required: true, message:'请选择用户', trigger:'blur'}]">
										<template v-slot:edit="scope">
											<el-input v-model="scope.row.userName" @focus="userPullListForm(scope.$rowIndex)"></el-input>
										</template>
									</vxe-table-column>
									<vxe-table-column field="userRole" title="角色" :edit-render="{}" show-overflow="title">
										<template v-slot:edit="scope">
											<el-input v-model="scope.row.userRole" ></el-input>
										</template>
									</vxe-table-column>
									<vxe-table-column field="zgNumber" title="注册/资格证号" :edit-render="{}" show-overflow="title">
										<template v-slot:edit="scope">
											<el-input v-model="scope.row.zgNumber" ></el-input>
										</template>
									</vxe-table-column>
									<vxe-table-column field="speciality" title="专业" :edit-render="{}" show-overflow="title">
										<template v-slot:edit="scope">
											<el-input v-model="scope.row.speciality" ></el-input>
										</template>
									</vxe-table-column>
									<vxe-table-column field="content" title="本人负责内容" :edit-render="{}" show-overflow="title">
										<template v-slot:edit="scope">
											<el-input v-model="scope.row.content" ></el-input>
										</template>
									</vxe-table-column>
									<vxe-table-column field="opinion" title="审批意见" :edit-render="{}" show-overflow="title">
										<template v-slot:edit="scope">
											<el-input v-model="scope.row.opinion" ></el-input>
										</template>
									</vxe-table-column>
									<vxe-table-column title="操作" width="100">
										<template v-slot="scope">
											<el-button size="default" type="danger" @click="removeEvent(scope.row,scope.$rowIndex,'detail')">删除</el-button>
										</template>
									</vxe-table-column>
								</vxe-table>
							</el-col>
						</el-row>
					</div>
				</el-form >
				<el-tabs v-model="activeName" type="border-card" style="width: 100%; margin-top: 20px">
					<el-tab-pane label="附件" name="enclosure">
						<!--        附件-->
						<UpLoadComponent ref="enclosure"></UpLoadComponent>
					</el-tab-pane>
				</el-tabs>
			</el-row>
			<template #footer>
			<span class="dialog-footer">
			  <el-button  @click="close()" icon="el-icon-circle-close">关闭</el-button>
			  <el-button  v-if="method !== 'view'" type="primary" icon="el-icon-circle-check" @click="doSubmit('save')">确定</el-button>
			  </span>
			</template>
			<ProjectRecoredChooseCom  ref="projectRecoredChooseCom" @getProject="getContract"></ProjectRecoredChooseCom>
		</el-dialog>
	</div>
</template>

<script>
	import ReportReviewService from '@/api/cw/reportManagement/ReportReviewService'
	import UpLoadComponent from '@/views/common/UpLoadComponent'
	import projectReportService from '@/api/cw/reportManagement/ProjectReportService'
	import EnterpriseSearchService from '@/api/cw/common/EnterpriseSearchService'
	import ProjectRecoredChooseCom from '../projectRecords/ProjectRecordsChooseCom'
	export default {
		props: {
			businessId: {
				type: String,
				default: ''
			},
			formReadOnly: {
				type: Boolean,
				default: false
			},
			status: {
				type: String,
				default: ''
			}
		},
		data () {
			return {
				validRules: {
					userName: [
						{required: true, message: '成员名称不能为空'}
					],
					zgNumber: [
						{required: true, message: '注册/资格证号不能为空'}
					],
					speciality: [
						{required: true, message: '专业不能为空'}
					],
					content: [
						{required: true, message: '本人负责内容不能为空'}
					],
					opinion: [
						{required: true, message: '审核意见不能为空'}
					],
				},
				detailKey: '',
				reviewBys: [],
				title: '',
				method: '',
				loading: false,
				inputForm: {
					projectName: '',
					projectId: '',
					projectNumber: '',
					projectMasterName: '',
					projectMasterId1: '',
					projectMasterId2: '',
					realHeaderName: '',
					reviewFee: '',
					approvalFee: '',
					contractCategory: '',
					contractFee: '',
					reviewBy: '',
					verifyFee: '',
					verifyRate: '',
					consultFee: '',
					buildingFee: '',
					installFee: '',
					buildingRate: '',
					installRate: '',
					startDate: '',
					endDate: '',
					ziXunShouRu: '',
					detailInfos: [],
					createBy: {
						id: '',
						name: this.$store.state.user.name
					},
				},
				keyWatch: '',
				activeName: 'enclosure',
				tableKey: '',
				tableKeyClient: '1',
				visible: false
			}
		},
		ReportReviewService: null,
		enterpriseSearchService: null,
		created () {
			this.reportReviewService = new ReportReviewService()
			this.enterpriseSearchService = new EnterpriseSearchService()
		},
		computed: {
			bus: {
				get () {
					this.$refs.enclosure.setDividerName('附件', false)
					// this.$refs.archiveFile.setDividerName('签章文件', false)
					return this.businessId
				},
				set (val) {
					this.businessId = val
				}
			}
		},
		watch: {
			'keyWatch': {
				handler (newVal) {
					if (this.commonJS.isNotEmpty(this.bus)) {
						this.init('', this.bus)
					} else {
						this.$nextTick(() => {
							this.$refs.inputForm.resetFields()
						})
					}
				}
			}
		},
		components: {
			UpLoadComponent,
			ProjectRecoredChooseCom,
		},
		methods: {
			getKeyWatch (keyWatch) {
				this.keyWatch = keyWatch
			},
			init (method, id) {
				this.visible = true
				this.activeName = 'enclosure'
				this.reportReviewService = new ReportReviewService()
				this.method = method
				if (method === 'add') {
					this.title = '报告信息添加'
					this.method = method
				}
				this.reviewBys = []
				this.inputForm = {
					projectName: '',
					projectId: '',
					projectNumber: '',
					projectMasterName: '',
					projectMasterId1: '',
					projectMasterId2: '',
					realHeaderName: '',
					reviewFee: '',
					approvalFee: '',
					contractCategory: '',
					contractFee: '',
					reviewBy: '',
					verifyFee: '',
					verifyRate: '',
					consultFee: '',
					buildingFee: '',
					installFee: '',
					buildingRate: '',
					installRate: '',
					startDate: '',
					endDate: '',
					ziXunShouRu: '',
					detailInfos: [],
				}
				this.inputForm.id = id
				this.loading = false
				this.$nextTick(() => {
					if (this.commonJS.isEmpty(this.inputForm.createDate)) {
						this.inputForm.createDate = this.moment(new Date()).format('YYYY-MM-DD')
					}
					if (this.commonJS.isEmpty(this.inputForm.officeId)) {
						this.inputForm.officeId = this.$store.state.user.office.id
					}
					if (this.commonJS.isEmpty(this.inputForm.realCreate)) {
						this.inputForm.realCreate = this.$store.state.user.id
					}
					if (this.commonJS.isEmpty(this.inputForm.signatureType) && this.method !== 'view') {
						this.inputForm.signatureType = '1'
					}
					this.$refs.enclosure.clearUpload()
					this.$refs.enclosure.newUpload(this.method, [], 'cwProjectReport', null, null, null, null, false)
				})
			},
			openContractForm () {
				if (!this.commonJS.isEmpty(this.inputForm.contractId)) {
					this.$refs.projectRecoredChooseCom.init(this.inputForm.contractId)
				} else {
					this.$refs.projectRecoredChooseCom.init()
				}
			},
			getContract (row) {
				console.log('row', row)
				this.inputForm.projectName = row.projectName // 项目名称
				this.inputForm.projectId = row.id // 项目id
				this.inputForm.projectNumber = row.projectNumber // 项目编号
				this.inputForm.projectMasterName = row.projectMasterName // 项目经理
				this.inputForm.projectMasterId1 = row.projectMasterId // 项目经理1id
				this.inputForm.projectMasterId2 = row.realHeader // 项目经理2id
				this.inputForm.realHeaderName = row.realHeaderName // 项目经理2名称
				this.clearClientList()
				this.$forceUpdate()
			},
			clearClientList () {
				// 项目直接对接联系人列表清除
				this.inputForm.clientList = []
				this.$forceUpdate()
			},
			// 表单提交
			async doSubmit (status, callback) {
				if (this.$refs.enclosure.checkProgress()) {
					this.loading = false
					throw new Error()
				} else {
					if (status === 'save') {
						console.log('save111111')
						// 暂存
						this.inputForm.status = '5'
						this.loading = true
						this.$refs['inputForm'].validate((valid) => {
							if (valid) {
								this.loading = true
								if (this.commonJS.isEmpty(this.inputForm.createDate)) {
									this.inputForm.createDate = this.moment(new Date()).format('YYYY-MM-DD')
								}
								projectReportService.saveForm(this.inputForm).then((data) => {
									this.$emit('refreshList')
									callback(data.businessTable, data.businessId, this.inputForm)
									this.close()
									this.loading = false
								}).catch(() => {
									this.close()
									this.loading = false
								})
							} else {
								this.loading = false
							}
						})
						return
					} else if (status === 'start') {
						// 送审  待审核
						this.inputForm.status = '2'
					} else if (status === 'agree') {
						// 审核同意
						this.inputForm.agreeDate = this.moment(new Date()).format('YYYY-MM-DD')
						this.inputForm.agreeUserId = this.$store.state.user.id
						this.inputForm.status = '5'
					} else if (status === 'reapply') {
						this.inputForm.status = '2'
					}
					this.$refs['inputForm'].validate((valid) => {
						if (valid) {
							this.loading = true
							// eslint-disable-next-line no-unused-vars
							let _this = this
							let _num = 0
							const wait = function () {
								return new Promise((resolve, reject) => {
									resolve()
								})
							}
							wait().then(() => {
								if (_num !== 1) {
									if (this.commonJS.isEmpty(this.inputForm.createDate)) {
										this.inputForm.createDate = this.moment(new Date()).format('YYYY-MM-DD')
									}
									projectReportService.saveForm(this.inputForm).then((data) => {
										callback(data.businessTable, data.businessId, this.inputForm)
										this.$emit('refreshList')
										this.loading = false
									}).catch(() => {
										this.loading = false
									})
								}
							})
						}
					})
				}
			},
			close () {
				this.$refs.enclosure.clearUpload()
				// this.$refs.archiveFile.clearUpload()
				this.$refs.inputForm.resetFields()
				this.visible = false
			},
			// 两位小数数字验证
			twoDecimalPlaces (num) {
				let str = num.toString()
				var len1 = str.substr(0, 1)
				var len2 = str.substr(1, 1)
				// eslint-disable-next-line eqeqeq
				if (str.length > 1 && len1 == 0 && len2 != '.') {
					str = str.substr(1, 1)
				}
				// eslint-disable-next-line eqeqeq
				if (len1 == '.') {
					str = ''
				}
				// eslint-disable-next-line eqeqeq
				if (str.indexOf('.') != -1) {
					var str_ = str.substr(str.indexOf('.') + 1)
					// eslint-disable-next-line eqeqeq
					if (str_.indexOf('.') != -1) {
						str = str.substr(0, str.indexOf('.') + str_.indexOf('.') + 1)
					}
					if (str_.length > 2) {
						this.$message.warning(`金额小数点后只能输入两位，请正确输入！`)
						return (str = '')
					}
				}
				// eslint-disable-next-line no-useless-escape
				str = str.replace(/[^\d^\.]+/g, '') // 保留数字和小数点
				return str
			},
			// 新增
			async insertEvent (type) {
				if (type === 'detail') {
					let d = {
						userName: this.$store.state.user.name,
						deptName: this.$store.state.user.office.name,
						userId: this.$store.state.user.id,
						deptId: this.$store.state.user.office.id
					}
					if (this.commonJS.isEmpty(this.inputForm.detailInfos)) {
						this.inputForm.detailInfos = []
					}
					this.$refs.detailTable.insertAt(d)
					this.inputForm.detailInfos.push(d)
					this.detailKey = Math.random()
				}
			},
			// 删除
			removeEvent (row, rowIndex, type) {
				if (type === 'detail') {
					this.$refs.detailTable.remove(row)
					this.inputForm.detailInfos.splice(rowIndex, 1)
				}
			},
		}
	}
</script>
<style scoped>
	/deep/ .el-input-number .el-input__inner {
		text-align: left;
	}
</style>
