<template xmlns:v-slot="http://www.w3.org/1999/XSL/Transform">
	<div>
		<el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method === 'view' ? 'readonly' : ''"
			:disabled="status === 'audit' || status === 'taskFormDetail'" label-width="100px" @submit.native.prevent>
			<el-divider content-position="left"><i class="el-icon-document"></i> 基础信息</el-divider>
			<el-row :gutter="26">
				<el-col :span="12">
					<el-form-item label="入库编号" prop="wareHouseNumber">
						<el-input placeholder="自动生成" v-model="inputForm.wareHouseNumber" :disabled="true"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="入库名称" prop="wareHouseName" :rules="[{ required: true, message: '入库名称不能为空', trigger: 'blur' }
					]">
						<el-input v-model="inputForm.wareHouseName" placeholder="请填写入库名称"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="经办人" prop="handledBy">
						<el-input v-model="inputForm.handledBy" :disabled="true"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="经办人部门" prop="handledByOffice">
						<SelectTree :disabled="true" ref="officeTree" :props="{
							value: 'id',             // ID字段名
							label: 'name',         // 显示名称
							children: 'children'    // 子级字段名
						}" :url="`/system-server/sys/office/treeData?type=2`" :value="inputForm.handledByOffice" :accordion="true"
							size="default" @getValue="(value) => { inputForm.handledByOffice = value }" />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="入库时间" prop="wareHouseDate"
						:rules="[{ required: true, message: '请选择入库时间', trigger: 'blur' }]">
						<el-date-picker v-model="inputForm.wareHouseDate" type="date" value-format="YYYY-MM-DD"
							style="width: 100%" placement="bottom-start" placeholder="选择日期">
						</el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="采购编号" prop="purchaseNo" :rules="[]">
						<el-input :readonly="true" @focus="openContractForm()" v-model="inputForm.purchaseNo"
							placeholder="请选择采购编号">
							<el-button slot="append" icon="el-icon-search" @click="openContractForm()"></el-button>
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="备注" prop="remarks">
						<el-input v-model="inputForm.remarks" type="textarea" :rows="5" maxlength="500"
							placeholder="请输入简介" show-word-limit>
						</el-input>
					</el-form-item>
				</el-col>
			</el-row>

			<div v-if="commonJS.isNotEmpty(inputForm.purchaseNo)">
				<el-divider content-position="left"><i class="el-icon-document"
						style="font-weight: bold;font-size: 16px;color:#409EFF"></i>
					<el-link @click="viewContract(inputForm.purchaseNo)"><span
							style="font-weight: bold;font-size: 16px;color:#409EFF">&nbsp;采购详情</span></el-link>
				</el-divider>
				<el-row :gutter="15">
					<el-col :span="24">
						<vxe-table border show-footer show-overflow :footer-method="footerMethod" ref="detailTable"
							class="vxe-table-element" :data="inputForm.detailInfos" style="margin-left: 3em">
							<vxe-column min-width="100" align="center" field="purchaserAgent" title="采购人"></vxe-column>
							<vxe-column min-width="100" align="center" field="procurementOffice"
								title="采购部门"></vxe-column>
							<vxe-column min-width="100" align="center" field="procurementType"
								title="采购类型"></vxe-column>
							<vxe-column min-width="220" align="center" field="tradeName" title="商品名称"></vxe-column>
							<vxe-column min-width="100" align="center" field="supplierName" title="供应商"></vxe-column>
							<vxe-column min-width="100" align="center" field="tradePrice" title="商品单价(元)"></vxe-column>
							<vxe-column min-width="80" align="center" field="tradeNumber" title="商品数量"></vxe-column>
							<vxe-column min-width="100" align="center" field="priceSum" title="商品总价"></vxe-column>
							<vxe-column min-width="100" align="center" field="company" title="单位"></vxe-column>
							<vxe-column min-width="100" align="center" field="remarks" title="备注"></vxe-column>
							<vxe-column align="center" title="操作" fixed="right" width="80">
								<template v-slot="scope">
									<vxe-button status="primary" size="small" :disabled="false"
										@click="seeFileInfo(scope.$rowIndex)" plain>附件</vxe-button>
								</template>
							</vxe-column>
						</vxe-table>
					</el-col>
				</el-row>
			</div>

			<el-divider content-position="left"><i class="el-icon-document"></i>
				入库详情
			</el-divider>
			<el-row v-if="status === 'audit' || status === 'taskFormDetail'" :gutter="15">
				<el-col :span="24">
					<vxe-table border show-footer show-overflow :footer-method="footerMethod2" ref="detailTableWare"
						class="vxe-table-element" :data="inputForm.wareHouse" style="margin-left: 3em"
						:row-class-name="rowClassName" :key="tableKey">
						<vxe-column min-width="100px" align="center" field="wareHouseMan" title="入库人">
							<template v-slot="scope">
								<span v-if="scope.row.upFlag === '1'"> {{ scope.row.wareHouseMan }}(修改前) </span>
								<span v-else-if="scope.row.upFlag === '2'"> {{ scope.row.wareHouseMan }}(已删除) </span>
								<span v-else-if="scope.row.upFlag === '0' && commonJS.isNotEmpty(scope.row.sourceId)">
									{{ scope.row.wareHouseMan }}(修改后) </span>
								<span v-else-if="scope.row.upFlag === '0' && commonJS.isEmpty(scope.row.sourceId)">
									{{ scope.row.wareHouseMan }} </span>
							</template>
						</vxe-column>
						<vxe-column min-width="100px" align="center" field="wareHouseManOffice"
							title="入库部门"></vxe-column>
						<vxe-column min-width="100px" align="center" field="wareHouseType" title="入库类型"></vxe-column>
						<vxe-column min-width="180px" align="center" field="tradeName" title="商品名称"></vxe-column>
						<vxe-column min-width="150px" align="center" field="supplierName" title="供应商"></vxe-column>
						<vxe-column min-width="80px" align="center" field="tradeNumber" title="商品数量"></vxe-column>
						<vxe-column min-width="80px" align="center" field="company" title="单位"></vxe-column>
						<vxe-column min-width="80px" align="center" field="spec" title="包装规格"></vxe-column>
						<vxe-column min-width="80px" align="center" field="tradePrice" title="商品单价(元)"></vxe-column>
						<vxe-column min-width="80px" align="center" field="priceSum" title="商品总价"></vxe-column>
						<vxe-column min-width="180px" align="center" field="produceDate" title="生产日期"></vxe-column>
						<vxe-column min-width="80px" align="center" field="shelfLife" title="保质期"></vxe-column>
						<vxe-column min-width="80px" align="center" field="actualPrice" title="实际价格"></vxe-column>
						<vxe-column min-width="110px" align="center" field="remarks" title="备注"></vxe-column>
						<vxe-column align="center" fixed="right" title="操作" width="80px">
							<template v-slot="scope">
								<vxe-button status="primary" size="small" v-if="scope.row.upFlag !== '1'"
									:disabled="false" @click="seeFileInfo2(scope.$rowIndex)" plain>附件</vxe-button>
							</template>
						</vxe-column>
					</vxe-table>
				</el-col>
			</el-row>
			<el-row :gutter="15" v-else>
				<el-col :span="24">
					<vxe-table border show-footer show-overflow :footer-method="footerMethod2" ref="detailTableWare"
						class="vxe-table-element" :data="inputForm.wareHouse" style="margin-left: 3em"
						highlight-current-row
						:edit-config="{ trigger: 'click', mode: 'row', showStatus: false, autoClear: !shelfLifeUnitSelectVisible, icon: '-' }"
						:edit-rules="validRules">
						<vxe-table-column min-width="100px" align="center" field="wareHouseMan" title="入库人"
							:edit-render="{}">
							<template v-slot:edit="scope">
								<el-input v-model="scope.row.wareHouseMan"
									@focus="userPullListForm(scope.$rowIndex)"></el-input>
							</template>
						</vxe-table-column>
						<vxe-table-column min-width="100px" align="center" field="wareHouseManOffice" title="入库部门"
							:edit-render="{}">
							<template v-slot:edit="scope">
								<el-input :disabled='true' v-model="scope.row.wareHouseManOffice"></el-input>
							</template>
						</vxe-table-column>
						<vxe-table-column min-width="100px" align="center" field="wareHouseType" title="入库类型"
							:edit-render="{}">
							<template v-slot:edit="scope">
								<el-input v-model="scope.row.wareHouseType"
									@focus="typePullForm(scope.$rowIndex)"></el-input>
							</template>
						</vxe-table-column>

						<vxe-table-column min-width="180px" align="center" field="tradeName" title="商品名称"
							:edit-render="{}">
							<template v-slot:edit="scope">
								<!--              <el-input @change="ifSameValue" maxlength="64" v-model="scope.row.tradeName"></el-input>-->
								<vxe-pulldown ref="xDown1" transfer>
									<template #default>
										<vxe-input v-model="scope.row.tradeName" placeholder="请输入商品名称" maxlength="64"
											@focus="focusEvent1(scope.row)" @input="keyupEvent1(scope.row)"></vxe-input>
									</template>
									<template #dropdown>
										<div class="my-dropdown1">
											<div class="list-item1" v-for="item in scope.row.tradeNameData"
												:key="item.id" @click="selectEvent1(scope.row, item)">
												<span>{{ item.tradeName }}</span>
											</div>
										</div>
									</template>
								</vxe-pulldown>
							</template>
						</vxe-table-column>
						<vxe-table-column min-width="100px" align="center" field="supplierName" title="供应商"
							:edit-render="{}">
							<template v-slot:edit="scope">
								<vxe-pulldown ref="xDown4" transfer>
									<template #default>
										<vxe-input v-model="scope.row.supplierName" :readonly="true"
											:disabled="status === 'audit' || status === 'taskFormDetail'"
											placeholder="请填写供应商" @focus="focusEvent4(scope.$rowIndex)"></vxe-input>
									</template>
									<template #dropdown>
										<div style="width: 600px;
                              height: 300px;
                              background-color: #fff;
                              box-shadow: 0 0 6px 2px rgba(0, 0, 0, 0.1);">
											<vxe-input v-model="suppName" type="search" @search-click="searchClick"
												placeholder="请输入供应商名称搜索" style="width: 100%" clearable></vxe-input>
											<vxe-grid border show-overflow auto-resize height="auto"
												:row-config="{ isHover: true }" :loading="loading4"
												:pager-config="tablePage4" :data="tableData4" :columns="tableColumn4"
												@cell-click="cellClickEvent4" @page-change="pageChangeEvent4">
											</vxe-grid>
										</div>
									</template>
								</vxe-pulldown>
							</template>
						</vxe-table-column>
						<vxe-table-column min-width="80px" align="center" field="tradeNumber" title="商品数量"
							:edit-render="{}">
							<template v-slot:edit="scope">
								<el-input @change="changeValue2" v-model="scope.row.tradeNumber"
									@blur="scope.row.tradeNumber = twoDecimalPlaces2(scope.row.tradeNumber)"
									maxlength="10"></el-input>
							</template>
						</vxe-table-column>
						<vxe-table-column min-width="80px" align="center" field="company" title="单位" :edit-render="{}">
							<template #edit="scope">
								<el-input maxlength="64" v-model="scope.row.company"></el-input>
							</template>
						</vxe-table-column>
						<vxe-table-column min-width="80px" align="center" field="spec" title="包装规格" :edit-render="{}">
							<template v-slot:edit="scope">
								<!-- 只能输入数字 -->
								<el-input maxlength="64" v-model="scope.row.spec"
									@blur="scope.row.spec = handleCheckNumber(scope.row.spec)"></el-input>

							</template>
						</vxe-table-column>
						<vxe-table-column min-width="80px" align="center" field="tradePrice" title="商品单价(元)"
							:edit-render="{}">
							<template v-slot:edit="scope">
								<el-input @change="changeValue2" maxlength="15" v-model="scope.row.tradePrice"
									@blur="scope.row.tradePrice = twoDecimalPlaces2(scope.row.tradePrice)"></el-input>
							</template>
						</vxe-table-column>
						<vxe-table-column min-width="80px" align="center" field="priceSum" title="商品总价"
							:edit-render="{}">
							<template v-slot:edit="scope">
								<el-input :disabled='true' v-model="scope.row.priceSum"
									@blur="scope.row.priceSum = twoDecimalPlaces(scope.row.priceSum)"
									maxlength="15"></el-input>
							</template>
						</vxe-table-column>
						<vxe-table-column min-width="180px" align="center" field="produceDate" title="生产日期"
							:edit-render="{}">
							<template v-slot:edit="scope">
								<el-date-picker v-model="scope.row.produceDate" type="date" placement="bottom-start"
									value-format="YYYY-MM-DD" style="width: 100%" placeholder="选择生产日期">
								</el-date-picker>
							</template>
						</vxe-table-column>
						<vxe-table-column min-width="80px" align="center" field="shelfLife" title="保质期"
							:edit-render="{}">
							<template v-slot="scope">
								<span v-if="scope.row.shelfLife && scope.row.shelfLifeUnit">
									{{ scope.row.shelfLife }} {{ scope.row.shelfLifeUnit }}
								</span>
							</template>
							<template v-slot:edit="scope">
								<el-input v-model="scope.row.shelfLife" maxlength="64">
									<template #append>
										<el-select v-model="scope.row.shelfLifeUnit" style="width: 70px;"
											@visible-change="handleShelfLifeUnitVisibleChange">
											<el-option label="年" value="年" />
											<el-option label="月" value="月" />
											<el-option label="天" value="天" />
										</el-select>
									</template>
								</el-input>
							</template>
						</vxe-table-column>
						<vxe-table-column min-width="80px" align="center" field="actualPrice" title="实际价格"
							:edit-render="{}">
							<template #edit="scope">
								<el-input v-model="scope.row.actualPrice"
									@blur="scope.row.actualPrice = twoDecimalPlaces(scope.row.actualPrice)"
									maxlength="15"></el-input>
							</template>
						</vxe-table-column>

						<vxe-table-column min-width="80px" align="center" field="remarks" title="备注" :edit-render="{}">
							<template #edit="scope">
								<el-input maxlength="255" v-model="scope.row.remarks"></el-input>
							</template>
						</vxe-table-column>
						<vxe-table-column align="center" fixed="right" title="操作" width="250px">
							<template v-slot="scope">
								<el-button size="small" type="primary"
									@click="updateWareHouse(scope.$rowIndex)">修改</el-button>
								<el-button size="small" type="primary" @click="sss2(scope.$rowIndex)">上传附件</el-button>
								<el-button size="small" type="danger"
									@click="removeEvent(scope.row, scope.$rowIndex, 'detail')">删除</el-button>
							</template>
						</vxe-table-column>
					</vxe-table>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="24" style="margin-top: 20px">
					<el-form-item label="修改原因" prop="updateCause"
						:rules="[{ required: true, message: '修改原因不能为空', trigger: 'blur' }]">
						<el-input type="textarea" :rows="5" maxlength="500" placeholder="请填写修改原因" show-word-limit
							v-model="inputForm.updateCause"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<!-- 附件 -->
		<MaterialManagementDialog ref="materialManagementDialog" @getUpload="getUpload"></MaterialManagementDialog>
		<UpdateInfoForm ref="updateInfoForm"></UpdateInfoForm>
		<PurchaseChooseForm ref="purchaseChooseForm" @getProject="getContract"></PurchaseChooseForm>
		<UpLoadComponent ref="uploadComponent"></UpLoadComponent>
		<CwProgramPageForm ref="cwProgramPageForm" @getProgram="getProgram"></CwProgramPageForm>
		<MaterialTypePullForm ref="materialTypePullForm" @getProgramForType="getProgramForType"></MaterialTypePullForm>
		<UserPullForm ref="userPullForm" @getProgramForUser="getProgramForUser"></UserPullForm>
	</div>
</template>

<script>
import MaterialManagementDialog from '../file/MaterialManagementDialog'
import WareHouseService from '@/api/psi/WareHouseService'
import UpdateInfoForm from '../info/UpdateInfoForm'
import PurchaseChooseForm from './PurchaseChooseForm'

import CommonApi from '@/api/cw/common/CommonApi'
import MaterialManagementService from '@/api/psi/MaterialManagementService'
import UpLoadComponent from '@/views/common/UpLoadComponent'
import SelectTree from '@/components/treeSelect/treeSelect.vue'
import XEUtils from 'xe-utils'
import CwProgramPageForm from '@/views/cw/reimbursementApproval/info/CwProgramPageForm'
import MaterialTypePullForm from '../info/MaterialTypePullForm'
import UserPullForm from '@/views/psiManagement/components/UserPullForm'
import SupplierService from '@/api/psi/SupplierService'
export default {
	data() {
		return {
			validRules: {
				wareHouseMan: [
					{ required: true, message: '请选择入库人' }
				],
				wareHouseType: [
					{ required: true, message: '请选择入库类型' }
				],
				supplierName: [
					{ required: true, message: '请选择供应商' }
				],
				tradeName: [
					{ required: true, message: '请填写商品名称' }
				],
				tradeNumber: [
					{ required: true, message: '商品数量不能为空' }
				],
				spec: [
					{ required: true, message: '包装规格不能为空' }
				]
			},
			title: '',
			method: '',
			visible: false,
			loading: false,
			indexRow: '',
			inputForm: {
				procInsId: '',
				wareHouseTotalPrice: '', // 入库商品总价
				wareHouseActualPrice: '', // 入库商品实际价格
				tradeTotalPrice: '',  // 商品总价
				fileInfoLost: [],
				purchaseNo: '',
				handledBy: '',
				handledById: '',
				handledByOffice: '',
				wareHouseName: '',
				wareHouseNumber: '', // 入库编号
				userId: '',
				wareHouseDate: '',
				remarks: '',
				detailInfos: [],
				wareHouse: [],
				// amountInfos: [],
				files: [], // 附件信息
				functionFlag: '',
				updateCause: ''
			},
			shelfLifeUnitSelectVisible: false,
			keyWatch: '',
			tableKey: '',
			loading4: false,
			tablePage4: {
				total: 0,
				currentPage: 1,
				pageSize: 10
			},
			tableColumn4: [
				{ field: 'name', title: '名称', align: 'center' },
				{ field: 'address', title: '地址', align: 'center' },
				{ field: 'telPhone', title: '联系电话', align: 'center' }
			],
			tableData4: [],
			suppName: '',
			suppIndex: ''
		}
	},
	wareHouseService: null,
	MaterialManagementService: null,
	commonApi: null,
	supplierService: null,
	created() {
		this.materialManagementService = new MaterialManagementService()
		this.commonApi = new CommonApi()
		this.wareHouseService = new WareHouseService()
		this.supplierService = new SupplierService()
	},
	props: {
		businessId: {
			type: String,
			default: ''
		},
		formReadOnly: {
			type: Boolean,
			default: false
		},
		status: {
			type: String,
			default: ''
		}
	},
	components: {
		MaterialManagementDialog,
		PurchaseChooseForm,
		UpLoadComponent,
		SelectTree,
		CwProgramPageForm,
		MaterialTypePullForm,
		UserPullForm,
		UpdateInfoForm
	},
	computed: {
		bus: {
			get() {
				return this.businessId
			},
			set(val) {
				this.businessId = val
			}
		},
		name() {
			return this.$store.state.user.name
		},
		officeName() {
			return this.$store.state.user.office.name
		},
		purchaserAgentId() {
			return this.$store.state.user.id
		},
		procurementTypeId() {
			return ''
		}
	},
	watch: {
		'keyWatch': {
			handler(newVal) {
				if (this.commonJS.isNotEmpty(this.bus)) {
					this.init('', this.bus)
				} else {
					this.$nextTick(() => {
						this.$refs.inputForm.resetFields()
					})
				}
			}
		},
		'loading': {
			handler(newVal) {
				this.$emit('changeLoading', newVal)
				this.$refs.uploadComponent.changeLoading(newVal)
			}
		}
	},
	methods: {
		getKeyWatch(keyWatch) {
			this.keyWatch = keyWatch
		},
		init(method, id) {
			this.method = method
			this.inputForm = {
				procInsId: '',
				wareHouseTotalPrice: '', // 入库商品总价
				wareHouseActualPrice: '', // 入库商品实际价格
				tradeTotalPrice: '',  // 商品总价
				fileInfoLost: [],
				purchaseNo: '',
				handledBy: this.$store.state.user.name,
				handledById: this.$store.state.user.id,
				handledByOffice: this.$store.state.user.office.id,
				wareHouseName: '',
				wareHouseNumber: '', // 入库编号
				userId: this.$store.state.user.id,
				wareHouseDate: new Date(),
				remarks: '',
				detailInfos: [],
				wareHouse: [],
				// amountInfos: [],
				files: [], // 附件信息
				functionFlag: '',
				updateCause: ''
			}
			if (method === 'add') {
				this.title = `新建采购类型`
			} else if (method === 'edit') {
				this.title = '修改采购类型'
			}
			this.inputForm.id = id
			this.visible = true
			this.loading = false
			this.$nextTick(() => {
				console.log('this.status', this.status)
				if (this.status === 'audit' || this.status === 'taskFormDetail') {
					method = 'view'
				}
				if (this.commonJS.isEmpty(this.inputForm.handledByOffice)) {
					this.inputForm.handledByOffice = this.$store.state.user.office.id
				}
				this.loading = true
				this.$refs.inputForm.resetFields()
				this.wareHouseService.findById(this.inputForm.id, this.status).then((data) => {
					this.$refs.uploadComponent.clearUpload()
					if (data.status === '5' && this.status === 'taskFormDetail') { // 当前正在查询已通过的流程详情页
						this.wareHouseService.findLastHiByid(this.inputForm.id).then((data) => {
							if (this.commonJS.isNotEmpty(data.wareHouseManId)) {
								this.wareHouseManId = data.wareHouseManId
							}
							if (this.commonJS.isNotEmpty(data.wareHouseTypeId)) {
								this.wareHouseTypeId = data.wareHouseTypeId
							}
							this.inputForm = this.recover(this.inputForm, data)
							this.inputForm = JSON.parse(JSON.stringify(this.inputForm))
							if (this.commonJS.isNotEmpty(this.inputForm.detailInfos)) {
								let i = this.inputForm.detailInfos.length
								for (let j = 0; j < i; j++) {
									if (this.commonJS.isNotEmpty(this.inputForm.detailInfos[j].tradePrice)) {
										if (this.commonJS.isNotEmpty(this.inputForm.detailInfos[j].tradeNumber)) {
											this.inputForm.detailInfos[j].priceSum = this.inputForm.detailInfos[j].tradePrice * this.inputForm.detailInfos[j].tradeNumber
										}
									}
								}
							}
							if (this.commonJS.isNotEmpty(this.inputForm.wareHouse)) {
								let k = this.inputForm.wareHouse.length
								for (let j = 0; j < k; j++) {
									if (this.commonJS.isNotEmpty(this.inputForm.wareHouse[j].tradePrice)) {
										if (this.commonJS.isNotEmpty(this.inputForm.wareHouse[j].tradeNumber)) {
											this.inputForm.wareHouse[j].priceSum = this.inputForm.wareHouse[j].tradePrice * this.inputForm.wareHouse[j].tradeNumber
										}
									}
								}
							}
							this.$refs.uploadComponent.newUpload('view', this.inputForm.files, 'psi')
							this.loading = false
							this.tableKey = Math.random()
						})
					} else {
						if (this.commonJS.isNotEmpty(data.wareHouseManId)) {
							this.wareHouseManId = data.wareHouseManId
						}
						if (this.commonJS.isNotEmpty(data.wareHouseTypeId)) {
							this.wareHouseTypeId = data.wareHouseTypeId
						}
						this.inputForm = this.recover(this.inputForm, data)
						if (this.commonJS.isEmpty(this.inputForm.handledByOffice)) {
							this.inputForm.handledByOffice = this.$store.state.user.office.id
						}
						let i = this.inputForm.detailInfos.length
						for (let j = 0; j < i; j++) {
							if (this.commonJS.isNotEmpty(this.inputForm.detailInfos[j].tradePrice)) {
								if (this.commonJS.isNotEmpty(this.inputForm.detailInfos[j].tradeNumber)) {
									this.inputForm.detailInfos[j].priceSum = this.inputForm.detailInfos[j].tradePrice * this.inputForm.detailInfos[j].tradeNumber
								}
							}
						}
						let k = this.inputForm.wareHouse.length
						for (let j = 0; j < k; j++) {
							if (this.commonJS.isNotEmpty(this.inputForm.wareHouse[j].tradePrice)) {
								if (this.commonJS.isNotEmpty(this.inputForm.wareHouse[j].tradeNumber)) {
									this.inputForm.wareHouse[j].priceSum = this.inputForm.wareHouse[j].tradePrice * this.inputForm.wareHouse[j].tradeNumber
								}
							}
						}
						this.$refs.uploadComponent.newUpload(method, this.inputForm.files, 'psi')
						this.inputForm = JSON.parse(JSON.stringify(this.inputForm))
						this.loading = false
						this.tableKey = Math.random()
					}
				})
				if (method !== 'edit' && method !== 'view') {
					this.$refs.uploadComponent.newUpload(method, [], 'psi')
				}
			})
		},
		getUpload(p, index) {
			p.then((list) => {
				// list为返回数据
				this.inputForm.wareHouse[index].fileInfoLost = list
				this.inputForm.wareHouse[index].fileNumber = list.length
				this.tableKeyClient = Math.random()
			})
		},
		close() {
			this.inputForm = {
				procInsId: '',
				wareHouseTotalPrice: '', // 入库商品总价
				wareHouseActualPrice: '', // 入库商品实际价格
				tradeTotalPrice: '',  // 商品总价
				fileInfoLost: [],
				purchaseNo: '',
				handledBy: '',
				handledById: '',
				handledByOffice: '',
				wareHouseName: '',
				wareHouseNumber: '', // 入库编号
				userId: '',
				wareHouseDate: '',
				remarks: '',
				detailInfos: [],
				wareHouse: [],
				// amountInfos: [],
				files: [], // 附件信息
				functionFlag: '',
				updateCause: ''
			}
			this.method = ''
			this.inputForm.functionFlag = ''
			this.inputForm.detailInfos = []
			this.inputForm.wareHouse = []
			this.inputForm.amountInfos = []
			this.$refs.uploadComponent.clearUpload()
			this.$refs.inputForm.resetFields()
			this.visible = false
		},
		// 新增
		async insertEvent(type) {
			if (type === 'detail') {
				await this.$refs.detailTableWare.insert().then((data) => {
					data.wareHouseManId = this.wareHouseManId
					data.wareHouseMan = this.name
					data.wareHouseManOffice = this.officeName
					// this.inputForm.detailInfos.push(data)
					this.inputForm.wareHouse.push(data)
				})
			}
		},
		startForm(callback) {
			this.loading = true
			this.inputForm.functionFlag = 'start'
			if (this.commonJS.isNotEmpty(this.inputForm.id)) {
				this.wareHouseService.findById(this.inputForm.id).then((data) => {
					// 审核状态不是“未发起”或“暂存”或“撤回”或“已完成”，就弹出提示
					if (this.commonJS.isNotEmpty(data.status) && data.status !== '0' && data.status !== '1' && data.status !== '3' && data.status !== '5') {
						this.loading = false
						this.$message.error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
						throw new Error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
					} else {
						this.startFormTrue(callback)
					}
				})
			} else {
				this.startFormTrue(callback)
			}
		},
		reapplyForm(callback) {
			this.loading = true
			this.wareHouseService.findById(this.inputForm.id).then((data) => {
				if (data.status !== '4') { // 审核状态不是“驳回”，就弹出提示
					this.loading = false
					this.$message.error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
					throw new Error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
				} else {
					this.startFormTrue(callback)
				}
			})
		},
		// 暂存
		async saveForm(callback) {
			this.loading = true
			if (this.$refs.uploadComponent.checkProgress()) {
				this.loading = false
				return
			}
			if (this.commonJS.isEmpty(this.inputForm.files)) {
				this.inputForm.files = []
			}
			this.inputForm.files = this.$refs.uploadComponent.getDataList()
			this.inputForm.status = '1'
			this.wareHouseService.save(this.inputForm).then((data) => {
				callback()
				this.$refs.inputForm.resetFields()
				this.loading = false
			}).catch(() => {
				this.$refs.inputForm.resetFields()
				this.loading = false
			})
		},
		// 送审
		async startFormTrue(callback) {
			this.loading = true
			this.$refs['inputForm'].validate(async (valid) => {
				if (valid) {
					this.loading = true
					if (this.$refs.uploadComponent.checkProgress()) {
						this.loading = false
						this.inputForm.functionFlag = ''
						return
					}
					if (this.commonJS.isEmpty(this.inputForm.files)) {
						this.inputForm.files = []
					}
					let i = this.inputForm.wareHouse.length
					for (let j = 0; j < i; j++) {
						let k = j + 1
						if (this.commonJS.isEmpty(this.inputForm.wareHouse[j].wareHouseMan)) {
							this.$message.error('入库详情第' + k + '行请选择入库人')
							this.loading = false
							this.inputForm.functionFlag = ''
							return
						}
						if (this.commonJS.isEmpty(this.inputForm.wareHouse[j].wareHouseType)) {
							this.$message.error('入库详情第' + k + '行请选择入库类型')
							this.loading = false
							this.inputForm.functionFlag = ''
							return
						}
						if (this.commonJS.isEmpty(this.inputForm.wareHouse[j].supplierName) || this.commonJS.isEmpty(this.inputForm.wareHouse[j].supplierId)) {
							this.$message.error('入库详情第' + k + '行请选择供应商')
							this.loading = false
							this.inputForm.functionFlag = ''
							return
						}
						if (this.commonJS.isEmpty(this.inputForm.wareHouse[j].tradeName)) {
							this.$message.error('入库详情第' + k + '行请填写商品名称')
							this.loading = false
							this.inputForm.functionFlag = ''
							return
						}
						if (this.commonJS.isEmpty(this.inputForm.wareHouse[j].tradeNumber)) {
							this.$message.error('入库详情第' + k + '行请填写商品数量')
							this.loading = false
							this.inputForm.functionFlag = ''
							return
						}
						if (this.commonJS.isEmpty(this.inputForm.wareHouse[j].spec)) {
							this.$message.error('入库详情第' + k + '行请填写包装规格')
							this.loading = false
							this.inputForm.functionFlag = ''
							return
						}
					}
					for (let j = 0; j < i; j++) {
						for (let k = j + 1; k < i; k++) {
							if (this.inputForm.wareHouse[j].wareHouseTypeId === this.inputForm.wareHouse[k].wareHouseTypeId) { // 如果入库类型相同
								if (this.inputForm.wareHouse[j].tradeName === this.inputForm.wareHouse[k].tradeName) { // 如果商品名称相同
									// 这里直接通过判断供应商的id是否相等，因为入库只允许选择供应商，不允许手填
									if (this.inputForm.wareHouse[j].supplierId === this.inputForm.wareHouse[k].supplierId) {
										this.$message.warning(`入库详情中，同种入库类型、同一供应商的商品名称只能输入一条`)
										this.loading = false
										throw new Error('入库详情中，同种入库类型、同一供应商的商品名称只能输入一条')
									}
								}
							}
						}
					}
					this.inputForm.files = this.$refs.uploadComponent.getDataList()
					this.inputForm.status = '2'
					this.wareHouseService.save(this.inputForm).then((data) => {
						this.inputForm.id = data.businessId
						this.inputForm.functionFlag = ''
						callback(data.businessTable, data.businessId, this.inputForm)
						this.$refs.inputForm.resetFields()
						this.loading = false
					}).catch(() => {
						this.inputForm.functionFlag = ''
						this.$refs.inputForm.resetFields()
						this.loading = false
					})
				} else {
					this.loading = false
				}
			})
		},
		// 通过
		async agreeForm(callback) {
			this.loading = true
			this.$refs['inputForm'].validate(async (valid) => {
				if (valid) {
					this.loading = true
					if (this.$refs.uploadComponent.checkProgress()) {
						this.loading = false
						return
					}
					if (this.commonJS.isEmpty(this.inputForm.files)) {
						this.inputForm.files = []
					}
					this.inputForm.files = this.$refs.uploadComponent.getDataList()
					this.inputForm.status = '5'
					this.inputForm.functionFlag = 'agree'
					this.wareHouseService.save(this.inputForm).then((data) => {
						callback(data.businessTable, data.businessId, this.inputForm)
						this.loading = false
						this.inputForm.functionFlag = ''
					}).catch(() => {
						this.loading = false
						this.inputForm.functionFlag = ''
					})
				} else {
					this.loading = false
				}
			})
		},
		async updateStatusById(type, callback) {
			this.loading = true
			if (await this.$refs.uploadComponent.checkProgress()) {
				this.loading = false
				throw new Error()
			} else {
				if (type === 'reject' || type === 'reback') {
					this.wareHouseService.findById(this.inputForm.id).then((data) => {
						if (data.status !== '2') { // status的值不等于“审核中”，就弹出提示
							this.loading = false
							this.$message.error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
							throw new Error()
						} else {
							if (type === 'reject') {
								// 驳回
								this.inputForm.status = '4'
							}
							if (type === 'reback') {
								// 撤回
								this.inputForm.status = '3'
							}
							if (type === 'reject' || type === 'reback') {
								let param = { status: this.inputForm.status, id: this.inputForm.id }
								this.wareHouseService.updateStatusById(param).then(() => {
									this.loading = false
									callback()
								})
							}
						}
					})
				} else if (type === 'hold') {
					this.wareHouseService.findById(this.inputForm.id).then((data) => {
						if (data.status !== '4') { // status的值不等于“驳回”就弹出提示
							this.loading = false
							this.$message.error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
							throw new Error()
						} else {
							// 终止
							this.wareHouseService.backSourceData(this.inputForm.id).then(() => {
								this.loading = false
								callback()
							})
						}
					})
				}
			}
		},
		twoDecimalPlaces(num, index) {
			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
			this.inputForm.detailInfos[index].priceSum = str.replace(/[^\d^\.]+/g, '') // 保留数字和小数点
		},
		countAmount(row) {
			let amount
			let taxAmount
			let count = 0
			if (!this.commonJS.isEmpty(row.amount)) {
				amount = parseFloat(row.amount)
				count = count + amount
			}
			if (!this.commonJS.isEmpty(row.taxAmount)) {
				taxAmount = parseFloat(row.taxAmount)
				count = count + taxAmount
			}
			row.count = parseFloat(count).toFixed(2)
		},
		getProgram(rows) {
			this.inputForm.detailInfos[this.indexRow].projectId = rows[0].id
			this.inputForm.detailInfos[this.indexRow].projectName = rows[0].name
			this.inputForm.detailInfos[this.indexRow].reportNumber = rows[0].reportNo
			this.indexRow = ''
			this.$forceUpdate()
		},
		getProgramForType(rows) {
			this.inputForm.wareHouse[this.indexRow].wareHouseTypeId = rows.id
			this.inputForm.wareHouse[this.indexRow].wareHouseType = rows.name
			// this.inputForm.detailInfos[this.indexRow].procurementType = rows.name
			this.indexRow = ''
			this.$forceUpdate()
		},
		getProgramForUser(rows) {
			this.inputForm.wareHouse[this.indexRow].wareHouseManId = rows[0].id
			this.inputForm.wareHouse[this.indexRow].wareHouseMan = rows[0].name
			this.inputForm.wareHouse[this.indexRow].deptId = rows[0].parentId
			this.inputForm.wareHouse[this.indexRow].wareHouseManOffice = rows[0].officeName
			this.indexRow = ''
			this.$forceUpdate()
		},
		getUserInfo(rows) {
			console.log('rows', rows)
		},
		openContractForm() {
			this.$refs.purchaseChooseForm.init()
		},
		getContract(row) {
			this.materialManagementService.findById(row.id).then((data) => {
				this.inputForm.detailInfos = data.detailInfos
				this.inputForm.purchaseNo = data.purchaseNo
				let i = this.inputForm.detailInfos.length
				for (let j = 0; j < i; j++) {
					if (this.commonJS.isNotEmpty(this.inputForm.detailInfos[j].tradePrice)) {
						if (this.commonJS.isNotEmpty(this.inputForm.detailInfos[j].tradeNumber)) {
							this.inputForm.detailInfos[j].priceSum = this.inputForm.detailInfos[j].tradePrice * this.inputForm.detailInfos[j].tradeNumber
						}
					}
				}
			})
			this.$forceUpdate()
		},
		// 查看采购详情
		async viewContract(purchaseNo) {
			this.wareHouseService.findRequestId(purchaseNo).then((data) => {
				this.$refs.updateInfoForm.init('view', data)
			}).catch(() => {
			})
		},
		footerMethod({ columns, data }) {
			const footerData = [
				columns.map((column, columnIndex) => {
					if (columnIndex === 0) {
						return '商品总价'
					}
					if (['priceSum'].includes(column.property)) {
						// eslint-disable-next-line no-undef
						this.inputForm.tradeTotalPrice = XEUtils.sum(data, column.property)
						return XEUtils.sum(data, column.property)
					}
					return null
				})
			]
			return footerData
		},
		footerMethod2({ columns, data }) {
			const footerData = [
				columns.map((column, columnIndex) => {
					if (columnIndex === 0) {
						return '商品总价'
					}
					if (['priceSum'].includes(column.property)) {
						// eslint-disable-next-line no-undef
						this.inputForm.wareHouseTotalPrice = XEUtils.sum(data, column.property)
						return XEUtils.sum(data, column.property)
					}
					if (['actualPrice'].includes(column.property)) {
						// eslint-disable-next-line no-undef
						this.inputForm.wareHouseActualPrice = XEUtils.sum(data, column.property)
						return XEUtils.sum(data, column.property)
					}
					return null
				})
			]
			return footerData
		},
		// 值改变事件
		changeValue() {
			let i = this.inputForm.detailInfos.length
			for (let j = 0; j < i; j++) {
				if (this.commonJS.isNotEmpty(this.inputForm.detailInfos[j].tradePrice)) {
					if (this.commonJS.isNotEmpty(this.inputForm.detailInfos[j].tradeNumber)) {
						// parseFloat(item.account).toFixed(2)
						this.inputForm.detailInfos[j].priceSum = parseFloat(parseFloat(this.inputForm.detailInfos[j].tradePrice) * parseFloat(this.inputForm.detailInfos[j].tradeNumber)).toFixed(2)
					}
				}
			}
		},
		// 值改变事件
		changeValue2() {
			let i = this.inputForm.wareHouse.length
			for (let j = 0; j < i; j++) {
				if (this.commonJS.isNotEmpty(this.inputForm.wareHouse[j].tradePrice)) {
					if (this.commonJS.isNotEmpty(this.inputForm.wareHouse[j].tradeNumber)) {
						// parseFloat(item.account).toFixed(2)
						this.inputForm.wareHouse[j].priceSum = parseFloat(parseFloat(this.inputForm.wareHouse[j].tradePrice) * parseFloat(this.inputForm.wareHouse[j].tradeNumber)).toFixed(2)
					}
				}
			}
		},
		seeFileInfo(index) {
			if (this.commonJS.isEmpty(this.inputForm.detailInfos[index].fileInfoLost)) {
				this.inputForm.detailInfos[index].fileInfoLost = []
			}
			this.$refs.materialManagementDialog.newUpload('view', this.inputForm.detailInfos[index].fileInfoLost, null, null, null, null, null, false, index)
		},
		seeFileInfo2(index) {
			if (this.commonJS.isEmpty(this.inputForm.wareHouse[index].fileInfoLost)) {
				this.inputForm.wareHouse[index].fileInfoLost = []
			}
			this.$refs.materialManagementDialog.newUpload('view', this.inputForm.wareHouse[index].fileInfoLost, null, null, null, null, null, false, index)
		},
		sss2(index) {
			if (this.commonJS.isEmpty(this.inputForm.wareHouse[index].fileInfoLost)) {
				this.inputForm.wareHouse[index].fileInfoLost = []
			}
			this.$refs.materialManagementDialog.newUpload(null, this.inputForm.wareHouse[index].fileInfoLost, null, null, null, null, null, false, index)
		},
		// 采购人下拉弹窗
		userPullListForm(rowIndex) {
			this.indexRow = rowIndex
			this.$refs.userPullForm.init()
		},
		// 采购类型下拉弹窗
		typePullForm(rowIndex) {
			this.indexRow = rowIndex
			this.$refs.materialTypePullForm.init()
		},
		// 删除
		removeEvent(row, rowIndex, type) {
			if (type === 'detail') {
				this.$message.success('所选行标记为已删除，保存后生效')
				this.$refs.detailTableWare.remove(row)
				this.inputForm.wareHouse.splice(rowIndex, 1)
			}
		},
		ifSameValue() {
			let i = this.inputForm.wareHouse.length
			for (let j = 0; j < i; j++) {
				if (j + 1 < i) {
					if (this.inputForm.wareHouse[j].wareHouseTypeId === this.inputForm.wareHouse[j + 1].wareHouseTypeId) {
						if (this.commonJS.isNotEmpty(this.inputForm.wareHouse[j].tradeName) &&
							this.commonJS.isNotEmpty(this.inputForm.wareHouse[j + 1].tradeName) && this.inputForm.wareHouse[j].tradeName === this.inputForm.wareHouse[j + 1].tradeName) {
							// parseFloat(item.account).toFixed(2)
							this.inputForm.wareHouse.splice(j + 1, 1)
							this.$message.warning(`同种入库类型的商品名称只能输入一条信息！`)
						}
					}
				}
			}
		},
		// 入库详情修改
		updateWareHouse(index) {
			this.$message.success('所选行标记为已修改，保存后生效')
			this.inputForm.wareHouse[index].upFlag = '1'
		},
		// 列表行颜色
		rowClassName({ row, rowIndex }) {
			if (row.upFlag === '1') { // 修改前
				return 'row-grey'
			} else if (row.upFlag === '2') { // 已删除
				return 'row-red'
			} else if (row.upFlag === '0' && this.commonJS.isNotEmpty(row.sourceId)) { // 修改后
				return 'row-green'
			} else if (row.upFlag === '0' && this.commonJS.isEmpty(row.sourceId)) { // 未被修改过的数据
				return ''
			}
		},
		focusEvent4(rowIndex) {
			this.$refs.xDown4.showPanel()
			this.loading4 = true
			this.suppName = '' // 搜索框的值 清空
			this.suppIndex = rowIndex // 获取当前行下标
			this.tablePage4.currentPage = 1
			this.tablePage4.pageSize = 10
			this.refreshSupplierList()
		},
		suffixClick4() {
			this.$refs.xDown4.togglePanel()
		},
		searchClick() {
			this.refreshSupplierList()
		},
		cellClickEvent4({ row }) {
			this.inputForm.wareHouse[this.suppIndex].supplierName = row.name
			this.inputForm.wareHouse[this.suppIndex].supplierId = row.id
			this.inputForm.wareHouse[this.suppIndex].isSupplier = '2' // isSupplier：手动填写 ‘1’ 选择填写 ‘2’
			this.suppIndex = ''
			this.$refs.xDown4.hidePanel()
		},
		pageChangeEvent4({ currentPage, pageSize }) {
			this.tablePage4.currentPage = currentPage
			this.tablePage4.pageSize = pageSize
			this.refreshSupplierList()
		},
		// 刷新供应商
		refreshSupplierList() {
			this.supplierService.list({
				'current': this.tablePage4.currentPage,
				'size': this.tablePage4.pageSize,
				'name': this.suppName
			}).then((data) => {
				this.tableData4 = data.records
				this.tablePage4.total = data.total
				this.loading4 = false
			})
		},
		handleShelfLifeUnitVisibleChange(visible) {
			this.shelfLifeUnitSelectVisible = visible
		},
		focusEvent1(row) {
			row.tradeNameList = []
			row.tradeNameData = []
			if (this.commonJS.isNotEmpty(row.wareHouseTypeId)) {
				this.wareHouseService.findTradeByTypeId(row.wareHouseTypeId).then((data) => {
					row.tradeNameList = JSON.parse(JSON.stringify(data))
					row.tradeNameData = JSON.parse(JSON.stringify(data))
					this.$refs.xDown1.showPanel()
				}).catch(() => {
					this.$refs.xDown1.showPanel()
				})
			} else {
				this.$refs.xDown1.showPanel()
			}
		},
		keyupEvent1(row) {
			row.tradeNameData = this.commonJS.isNotEmpty(row.tradeName) ? row.tradeNameList.filter(item => item.tradeName.indexOf(row.tradeName) > -1) : row.tradeNameList
		},
		selectEvent1(row, item) {
			row.tradeName = item.tradeName
			this.$refs.xDown1.hidePanel()
		},
		// 校验是否为数字（禁止0，只允许正整数）
		handleCheckNumber(value) {
			// 正则：必须是1-9开头，后面跟0-9，不允许小数、不允许0
			const reg = /^[1-9]\d*$/
			if (!reg.test(value)) {
				this.$message.warning('请输入除0以外的正整数')
				return ''
			}
			return value
		},
	}
}
</script>
<style scoped>
.el-divider__text {
	font-size: 16px;
	font-weight: bold;
}

/deep/ .row-green {
	/*background-color: #67c23ad1;*/
	/*color: #fff;*/
	font-weight: bold;
}

/deep/ .row-grey {
	background-color: #b8b7b7;
	color: #fff;
}

/deep/ .row-red {
	background-color: #f56c6c66;
	/*color: #fff;*/
	font-weight: bold;
}

/*.my-dropdown1、.list-item1、.list-item1:hover  为商品名称下拉选择框样式*/
.my-dropdown1 {
	height: 155px;
	overflow: auto;
	border-radius: 4px;
	border: 1px solid #dcdfe6;
	background-color: #fff;
}

.list-item1 {
	line-height: 30px;
	text-align: center;
	border-bottom: #c4c7cc73 1px solid;
}

.list-item1:hover {
	background-color: #f5f7fa;
}
</style>
