<template>
	<view class="fullHeight">
		<headers :showBack="true">
			<view class="headerName">
				{{pageTitle}}
			</view>
		</headers>
		<view class="formBox2" :class="isAdd?'':'editForm'">
			<form @submit="formSubmit">
				<view class="uni-form-item">
					<view class="uni-form-label">
						<text class="star">*</text>单位工程名称
					</view>
					<view class="uni-form-input">
						<input class="uni-input" placeholder-class="cl" name="buildName" :value="form.buildName" placeholder="请输入" />
					</view>
				</view>
				<view class="uni-form-item">
					<view class="uni-form-label">
						<text class="star">*</text>施工项目部门
					</view>
					<view class="uni-form-input">
						<input class="uni-input" placeholder-class="cl" name="buildProjectDept" :value="form.buildProjectDept" placeholder="请输入" />
					</view>
				</view>
				<view class="uni-form-item">
					<view class="uni-form-label">
						<text class="star">*</text>项目负责人
					</view>
					<view class="uni-form-input">
						<input class="uni-input" placeholder-class="cl" name="projectDirector" :value="form.projectDirector" placeholder="请输入" />
					</view>
				</view>
				<view class="uni-form-item">
					<view class="uni-form-label">
						<text class="star">*</text>单位工程施工员
					</view>
					<view class="uni-form-input">
						<input class="uni-input" placeholder-class="cl" name="constructionCrew" :value="form.constructionCrew" placeholder="请输入" />
					</view>
				</view>
				
				<view class="uni-form-item">
					<view class="uni-form-label">
						星期几
					</view>
					<view class="uni-form-input">
						<picker @change="bindPickerChange" :value="selectIndex" :range="colorList">
							<view class="uni-input uni-select cl" v-if="selectIndex==null">
								请选择 <image class="icon-down" src="/static/icon-down-black.png" mode=""></image>
							</view>
							<view class="uni-input uni-select" v-else>{{colorList[selectIndex]}}
								<image class="icon-down" src="/static/icon-down-black.png" mode=""></image>
							</view>
						</picker>
					</view>
				</view>
				<view class="uni-form-item">
					<view class="uni-form-label">
						<text class="star">*</text>施工日期
					</view>
					<view class="uni-form-input">
						<picker mode="date" :value="form.buildTime" @change="bindDateChange">
							<view class="uni-input uni-select cl" v-if="form.buildTime==''">
								请选择 <image class="icon-down" src="/static/icon-down-black.png" mode=""></image>
							</view>
							<view class="uni-input uni-select" v-else>{{form.buildTime}}
								<image class="icon-down" src="/static/icon-down-black.png" mode=""></image>
							</view>
						</picker>
					</view>
				</view>
				
				<view class="uni-form-item">
					<view class="uni-form-label">
						上午天气
					</view>
					<view class="uni-form-input">
						<input class="uni-input" placeholder-class="cl" name="morningWeather" :value="form.morningWeather" placeholder="请输入" />
					</view>
				</view>
				<view class="uni-form-item">
					<view class="uni-form-label">
						下午天气
					</view>
					<view class="uni-form-input">
						<input class="uni-input" placeholder-class="cl" name="afternoonWeather" :value="form.afternoonWeather" placeholder="请输入" />
					</view>
				</view>
				<view class="uni-form-item">
					<view class="uni-form-label">
						气温
					</view>
					<view class="uni-form-input">
						<input class="uni-input" placeholder-class="cl" name="temperature" :value="form.temperature" placeholder="请输入" />
					</view>
				</view>
				
				<view class="uni-form-item">
					<view class="uni-form-label">
						<text class="star">*</text>当日施工作业面情况
					</view>
					<view class="uni-form-input">
						<input class="uni-input" placeholder-class="cl" name="buildPlace" :value="form.buildPlace" placeholder="请输入施工部位" />
					</view>
				</view>
				<view class="uni-form-item">
					<view class="uni-form-label">
						上午作业人数
					</view>
					<view class="uni-form-input">
						<input class="uni-input" placeholder-class="cl" name="morningPersonNum" :value="form.morningPersonNum" placeholder="请输入" />
					</view>
				</view>
				<view class="uni-form-item">
					<view class="uni-form-label">
						下午作业人数
					</view>
					<view class="uni-form-input">
						<input class="uni-input" placeholder-class="cl" name="afternoonPersonNum" :value="form.afternoonPersonNum" placeholder="请输入" />
					</view>
				</view>
				<view class="uni-form-item">
					<view class="uni-form-label">
						作业班组类型
					</view>
					<view class="uni-form-input">
						<input class="uni-input" placeholder-class="cl" name="taskTeamType" :value="form.taskTeamType" placeholder="请输入" />
					</view>
				</view>
				<view class="uni-form-item">
					<view class="uni-form-label">
						作业单位班组名称
					</view>
					<view class="uni-form-input">
						<input class="uni-input" placeholder-class="cl" name="taskTeam" :value="form.taskTeam" placeholder="请输入" />
					</view>
				</view>
				<view class="uni-form-item">
					<view class="uni-form-label">
						<text class="star">*</text>作业情况
					</view>
					<view class="uni-form-input">
						<input class="uni-input" placeholder-class="cl" name="taskCondition" :value="form.taskCondition" placeholder="请输入" />
					</view>
				</view>
				<view class="uni-form-item">
					<view class="uni-form-label">
						1、现场分部分项施工进度情况(交底情况、劳动力配置情况、机械运行情况、进度计划情况、质量特殊过程关键部位施工旁站情况):
					</view>
					<view class="uni-form-input">
						<input class="uni-input" placeholder-class="cl" name="buildProgress" :value="form.buildProgress" placeholder="请输入" />
					</view>
				</view>
				<view class="uni-form-item">
					<view class="uni-form-label">
						2、作业面安全文明施工情况(工完场清、材料堆码) 、安全自检情况:
					</view>
					<view class="uni-form-input">
						<input class="uni-input" placeholder-class="cl" name="safeBuildCondition" :value="form.safeBuildCondition" placeholder="请输入" />
					</view>
				</view>
				<view class="uni-form-item">
					<view class="uni-form-label">
						3、材料进场情况(规格、数量、使用部位、验收情况、堆放位置):
					</view>
					<view class="uni-form-input">
						<input class="uni-input" placeholder-class="cl" name="materialEnter" :value="form.materialEnter" placeholder="请输入" />
					</view>
				</view>
				<view class="uni-form-item">
					<view class="uni-form-label">
						4、设计变更、洽商、指令单,技术交底情况:
					</view>
					<view class="uni-form-input">
						<input class="uni-input" placeholder-class="cl" name="technicalInterpretation" :value="form.technicalInterpretation" placeholder="请输入" />
					</view>
				</view>
				<view class="uni-form-item">
					<view class="uni-form-label">
						5、现场验收情况(部位、时间、参与人、验收情况及结果)、工程资料填写交接归档情况:
					</view>
					<view class="uni-form-input">
						<input class="uni-input" placeholder-class="cl" name="sceneInspected" :value="form.sceneInspected" placeholder="请输入" />
					</view>
				</view>
				<view class="uni-form-item">
					<view class="uni-form-label">
						6、上级单位外部单位人员到项目检查指导情况(对工程提出的决定、建议,要求解决处理的问题):
					</view>
					<view class="uni-form-input">
						<input class="uni-input" placeholder-class="cl" name="inspectGuidance" :value="form.inspectGuidance" placeholder="请输入" />
					</view>
				</view>
				<view class="uni-form-item">
					<view class="uni-form-label">
						7、现场协调情况、工作面移交情况:
					</view>
					<view class="uni-form-input">
						<input class="uni-input" placeholder-class="cl" name="sceneCoordinate" :value="form.sceneCoordinate" placeholder="请输入" />
					</view>
				</view>
				<view class="uni-form-item">
					<view class="uni-form-label">
						8、其它特殊情况记录(停电、水,停窝工等,原材料,劳务力等其它影响施工情况):
					</view>
					<view class="uni-form-input">
						<input class="uni-input" placeholder-class="cl" name="otherSpecial" :value="form.otherSpecial" placeholder="请输入" />
					</view>
				</view>
				<view class="uni-form-item">
					<view class="uni-form-label" style="position: relative;">
						附件
						<view class="addImgBox" @click="selectImg">
							<image src="/static/icon-add-blue.png" class="icon-add"></image>
						</view>
					</view>
					<view class="uni-form-input" style="margin-top:30rpx">
						<view class="imgBox" v-for="(item,index) in fileList" :key="index">
							<!-- <image :src="url_config+'image/'+item.url" class="img"></image> -->
							<text @click="handlePreview(item.url)">{{item.name}}</text>
							<uni-icons2 @click.stop="deleteImg(index)" class="deleteImg" type="clear" size="18"></uni-icons2>
						</view>
						<!-- <view class="addImgBox" @click="selectImg">
							<image src="/static/icon-add.png" class="icon-add"></image>
						</view> -->
					</view>
				</view>
				<view class="fixdBox">
					<button form-type="submit" type="primary" class="btn submitBtn big">保存</button>
				<button class="btn deleteBtn" v-show="!isAdd" @click="deleteFn">删除</button>
				</view>
				
			</form>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pageTitle: '新增日志',
				selectIndex: 0,
				colorList: ['星期一', '星期二', '星期三', '星期四','星期五','星期六','星期天'],
				carTypeList:[],
				carTypeIndex:null,
				form: {
					afternoonPersonNum: "",
					afternoonWeather: "",
					buildName: "",
					buildPlace: "",
					buildProgress: "",
					buildProjectDept: "",
					buildTime: "",
					buildWeek: "",
					constructionCrew: "",
					imgUrl: "",
					inspectGuidance: "",
					materialEnter: "",
					morningPersonNum: "",
					morningWeather: "",
					otherSpecial: "",
					projectDirector: "",
					safeBuildCondition: "",
					sceneCoordinate: "",
					sceneInspected: "",
					taskCondition: "",
					taskTeam: "",
					taskTeamType: "",
					technicalInterpretation: "",
					temperature: "",
					projectSn: ''
				},
				personList: [],
				projectDetail: {},
				isAdd: true,
				fileList:[]
			}
		},
		onLoad(options) {
			this.projectDetail = JSON.parse(uni.getStorageSync('projectDetail'))
			
			if (options.id) {
				this.isAdd = false
				this.pageTitle='编辑日志'
				this.getDetails(options.id)
			}
		},
		
		methods: {
			handlePreview(url) {
				let type = url.split('.')[1];
				if (type == 'doc' || type == 'xls' || type == 'ppt' || type == 'pdf' || type == 'docx' || type == 'xlsx' || type ==
					'pptx') {
					uni.openDocument({
						filePath: this.url_config + 'image/' + url,
					})
				}else{
					let fileList = [];
					fileList = [this.url_config + 'image/' + url];
					uni.previewImage({
						current: 0,
						urls: fileList
					})
				}
				
			},
			deleteFn(){
				var that = this
				uni.showModal({
					title:'提示',
					content:'确定删除该日志吗？',
					success(res) {
						if(res.confirm){
							that.sendRequest({
								url: 'xmgl/projectBuildLog/delete',
								data: {
									id: that.form.id
								},
								method: "POST",
								success(res) {
									uni.showToast({
										title:'删除成功！'
									})
									setTimeout(()=>{
										uni.navigateBack({})
									},1000)
									
								}
							})
						}
					}
				})
			},
			selectImg(){
				var that =this
				uni.chooseImage({
					count:1,
					success(res) {
						const tempFilePaths = res.tempFilePaths;
						for (var i = 0; i < tempFilePaths.length; i++) {
							uni.uploadFile({
								url: that.url_config+'upload/image', //仅为示例，非真实的接口地址
								filePath: tempFilePaths[i],
								name: 'files',
								success: (uploadFileRes) => {
									console.log(uploadFileRes.data);
									var data=JSON.parse(uploadFileRes.data).data[0]
									that.fileList.push({
										url:data.imageUrl,
										name:data.filename
									})
								}
							});
						}
						
					}
				})
			},
			deleteImg(index){
				this.fileList.splice(index,1)
			},
			getDetails(id) {
				var that = this
				this.sendRequest({
					url: 'xmgl/projectBuildLog/queryById',
					data: {
						id: id
					},
					method: "post",
					success(res) {
						that.form = res.result
						that.selectIndex = that.colorList.indexOf(that.form.buildWeek)
						that.fileList=JSON.parse(that.form.imgUrl)
					}
				})
			},
			bindPickerChange: function(e) {
				this.selectIndex = e.target.value
			},
			bindDateChange: function(e) {
				this.form.buildTime = e.target.value
			},
			formSubmit(e) {
				var that = this
				console.log(e)
				var params = e.detail.value
				params.projectSn = this.projectDetail.projectSn
				
				if (params.buildName == '') {
					uni.showToast({
						title: '请输入单位工程名称',
						icon: 'none'
					})
					return false;
				}
				if (params.buildProjectDept == '') {
					uni.showToast({
						title: '请输入施工项目部门',
						icon: 'none'
					})
					return false;
				}
				if (params.projectDirector == '') {
					uni.showToast({
						title: '请输入项目负责人',
						icon: 'none'
					})
					return false;
				}
				if (params.constructionCrew == '') {
					uni.showToast({
						title: '请输入单位工程施工员',
						icon: 'none'
					})
					return false;
				}
				if (this.form.buildTime == '') {
					uni.showToast({
						title: '请选择施工日期',
						icon: 'none'
					})
					return false;
				}
				if (params.buildPlace == '') {
					uni.showToast({
						title: '请输入施工部位',
						icon: 'none'
					})
					return false;
				}
				if (params.taskCondition == '') {
					uni.showToast({
						title: '请输入作业情况',
						icon: 'none'
					})
					return false;
				}
				params.imgUrl=JSON.stringify(this.fileList)
				params.buildWeek=this.colorList[this.selectIndex]
				params.buildTime=this.form.buildTime

				var url = 'xmgl/projectBuildLog/edit'
				if (this.isAdd) {
					url = 'xmgl/projectBuildLog/add'

				} else {
					params.id = this.form.id
				}
				this.sendRequest({
					url: url,
					data: params,
					method: "POST",
					success(res) {
						uni.showToast({
							title: '保存成功！'
						})
						uni.navigateBack({
							
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.addImgBox{
		// border: 1px solid rgba(42, 43, 91, 0.1);
		// background-color: #f6f5f8;
		// width: 120rpx;
		// height: 120rpx;
		// display: inline-flex;
		// align-items: center;
		// justify-content: center;
		// border-radius: 20rpx;
		// font-size: 24rpx;
		position: absolute;
		top: 0;
		right: 0;
		z-index: 1;
		.icon-add{
			width: 36rpx;
			height: 36rpx;
		}
	}
	.imgBox{
		// width: 120rpx;
		// height: 120rpx;
		// display: inline-flex;
		position: relative;
		// margin-right: 30rpx;
		.img{
			width: 100%;
			height: 100%;
			border-radius: 20rpx;
		}
		.deleteImg{
			position: absolute;
			right: 0px;
			top: 0;
		}
	}
	.fixdBox{
		position: fixed;
		bottom: 30rpx;
		left: 30rpx;
		width: calc(100% - 60rpx);
		.submitBtn{
			margin-top: 0;
		}
	}
	.formBox2{
		padding-bottom: 80px;
		margin-top: 30rpx;
	}
	.editForm{
		padding-bottom: 130px;
	}
</style>
