<template>
	<view>
		<!-- 状态 -->
		 <!-- && userInfoRoleName.indexOf('admin') == -1 -->
		<view class="label" v-if="(form.status == 2 || form.status == 3 || form.status == 4)">
			<view class="tab" ref="wx" :style="{'color':wxStyle,'height':'100%','border-right': '1rpx solid #f4f4f4'}" @click="tab('wx')">
				<text>报修单</text>
			</view>
			<view class="tab" ref="gz" :style="{'color':gzStyle,'height':'100%','border-left': '1rpx solid #f4f4f4'}" @click="tab('gz')">
				<text>工作单</text>
			</view>
		</view>
		
		<view class="repairdetails" v-if="form.status == 1">
			
			<view class="cardBox">
				<!-- <view class="text">设备信息:</view> -->
				<u-input v-model="form.deviceName" disabledColor="#fff" disabled
				border="none" placeholder="请选择设备信息" >
					<template slot="suffix">
						<u-icon name="scan" color="#bbb" size="32"></u-icon>
					</template>
				</u-input>
			</view>
			
			<view class="cardBox">
				<!-- <view class="text">客户名称:</view> -->
				<u-input v-model="form.corpName" disabled disabledColor="#fff"
				border="none" placeholder="客户名称" ></u-input>
			</view>
			
			<view class="cardBox">
				<!-- <view class="text">地址:</view> -->
				<u-input v-model="form.deviceAddress" disabledColor="#fff" disabled
				 border="none" placeholder="设备地址" ></u-input>
			</view>
			
			<view class="cardBox">
				<!-- <view class="text">故障描述:</view> -->
				<u--textarea v-model="form.faultDescribe" placeholder="故障描述" disabled
					border="none" >
				</u--textarea>
			</view>
			
						
			<view class="kapian">
				<!-- <view style="font-size: 30rpx;color: #999;margin-left: 10rpx;">添加图片</view> -->
				<view style="margin-top: 20rpx;">
					<u-upload
						:fileList="form.maintenanceFiles"
						 :disabled="form.status != 3"
						@afterRead="afterRead"
						@delete="deletePic"
						name="1"
						multiple
						:maxCount="10"
					></u-upload>
				</view>
			</view>
			
		</view>
		
		
		<!-- 维修单 -->
		<view class="repairdetails" v-if="wxStyle != ''">
			
			<view class="cardBox">
				<!-- <view class="text">设备信息:</view> -->
				<u-input v-model="form.deviceName" disabledColor="#fff" disabled
				border="none" placeholder="请选择设备信息" >
					<template slot="suffix">
						<u-icon name="scan" color="#bbb" size="32"></u-icon>
					</template>
				</u-input>
			</view>
			
			<view class="cardBox">
				<!-- <view class="text">客户名称:</view> -->
				<u-input v-model="form.corpName" disabled disabledColor="#fff"
				border="none" placeholder="客户名称" ></u-input>
			</view>
			
			<view class="cardBox">
				<!-- <view class="text">地址:</view> -->
				<u-input v-model="form.deviceAddress" disabledColor="#fff" disabled
				 border="none" placeholder="设备地址" ></u-input>
			</view>
			
			<view class="cardBox">
				<!-- <view class="text">故障描述:</view> -->
				<u--textarea v-model="form.faultDescribe" placeholder="故障描述" disabled
					border="none" >
				</u--textarea>
			</view>
			
						
			<view class="kapian">
				<!-- <view style="font-size: 30rpx;color: #999;margin-left: 10rpx;">添加图片</view> -->
				<view style="margin-top: 20rpx;">
					<u-upload
						:fileList="form.maintenanceFiles"
						 :disabled="form.status != 3"
						@afterRead="afterRead"
						@delete="deletePic"
						name="1"
						multiple
						:maxCount="10"
					></u-upload>
				</view>
			</view>
			
		</view>
		
		<!-- 工作单 -->
		<view class="repairdetails" v-if="gzStyle != ''">
			
			<view class="cardBox">
				<!-- <view class="text">设备信息:</view> -->
				<u-input v-model="form.deviceName" disabledColor="#fff" disabled
				border="none" placeholder="请选择设备信息" >
					<template slot="suffix">
						<u-icon name="scan" color="#bbb" size="32"></u-icon>
					</template>
				</u-input>
			</view>
			
			<view class="cardBox">
				<u--textarea v-model="form.repairDescription" placeholder="维修描述" border="none" :disabled="form.status != 3" >
				</u--textarea>
			</view>
			
			<view class="kapian">
				<!-- <view style="font-size: 30rpx;color: #999;margin-left: 10rpx;">添加图片</view> -->
				<view style="margin-top: 20rpx;">
					<u-upload
						:fileList="form.maintenanceFiles"
						 :disabled="form.status != 3"
						@afterRead="afterRead"
						@delete="deletePic"
						name="1"
						multiple
						:maxCount="10"
					></u-upload>
				</view>
			</view>
			
			<view>
				<view class="kapian" v-for="(item,index) in form.maintenanceFees" :key="item.id">
					<view class="WXflex">
						<view class="wxscfun" style="color: #fff; background-color: #FD4B09;" v-if="form.status == 3" @click="maintenanceDe(item,index)">删除</view>
					</view>
					<view class="repairProject" @click="kapianClick(item)">
						<view class="wxbox">
							{{item.costName}}
						</view>
						
						<view class="wxbox" style="color: #FD4B09;">
							{{item.amount}}元
						</view>
					</view>
				</view>
			</view>
			
			
			<view class="kapian" >
				<view class="wxtitleflex">
					<view class="wxtitle">添加费用</view>
					<view v-if="form.status == 3 && type != 'XQ'" style="color: #fff; background-color: #FD4B09;" class="addwx" @click="kapianShow = true">添加</view>
				</view>
			</view>
		</view>
		<!-- wxaddfun -->
			  
			
			
		
		<u-gap height="60"></u-gap>
		
		<view class="bottombox" v-if="type != 'XQ'">
			<view class="bottombox_name" v-if="form.status != 3 && form.status != 1">
				<text>{{form.maintenanceWorkerName}}</text>
			</view>
			<view class="bottombox_name" @click="pickerShowfun" v-if="form.status == 1 && userInfoRoleName.indexOf('admin') != -1">
				<text>{{form.maintenanceWorkerName == null || form.maintenanceWorkerName == '' ? '请选择维修工' : form.maintenanceWorkerName}}</text>
			</view>
			<view v-if="form.status == 3 && wxStyle == ''" style="color: #FD4B09;">维修费：{{form.maintenanceAmount}}元</view>
			<view class="bottombox_right">
				<view class="buttonClass"
				 v-if="form.status == 1 && userInfoRoleName.indexOf('admin') != -1" 
				 @click="maintenanceDispatchingWorkersfun">确认派工</view>
				<view class="buttonClass" 
					v-if="form.status == 2 && userInfoRoleName.indexOf('admin') != -1"
				 @click="maintenanceRevokeDispatchingWorkersfun">撤销派工</view>
				<view class="buttonClass" v-if="form.status == 2 && userInfoRoleName.indexOf('维修工') != -1" @click="receivingOrdersfun">确认接单</view>
				<view class="buttonClass" v-if="form.status == 3" @click="Confirmexpenses">确认费用</view>
				<view class="buttonClass" v-if="form.status == 4" @click="Confirmexpenses">确认结算</view>
			</view>
		</view>
		
		<u-picker :show="pickerShow" :columns="pickerList" keyName="name"
		 @confirm="pickerShowConfirm" @cancel="pickerShowCancel"></u-picker>
		
		<u-toast ref="uToast"></u-toast>
		<!-- 添加费用弹窗 -->
		
		
		<!-- <u-popup :show="show" mode="top"  @close="close" @open="open">
		        <view>
		            <text>人生若只如初见，何事秋风悲画扇</text>
		        </view>
		</u-popup> -->
		
		<u-modal :show="kapianShow" title="项目" :showCancelButton="true" @cancel="kapianCancel" @confirm="kapianConfirm" confirmColor="#FD4B09">
			<view class="kapianModel">
				<view>
					<text style="color: #FD4B09;">项目：</text>
					<u-input v-model="kapian.costName" disabledColor="#fff" :disabled="form.status != 3"
					border="none" confirm-type="next" placeholder="添加项目" ></u-input>
				</view>
				<view>
					<text style="color: #FD4B09;">金额：</text>
					<u-input v-model="kapian.amount" disabledColor="#fff" :disabled="form.status != 3"
					border="none" type="digit" confirm-type="done" @input="moneyInput" placeholder="金额" ></u-input>
				</view>
			</view>
		</u-modal>
	</view>
</template>

<script>
	import { 
		maintenanceDetail,
		maintenanceReceivingOrders,
		maintenanceConfirm,
		userList,
		maintenanceDispatchingWorkers,
		maintenanceRevokeDispatchingWorkers
	} from '@/api/device/index.js'
	import http from '@/http/api.js'
	import {
		clientId,
		clientSecret
	} from '@/common/setting'
	export default {
		data() {
			return {
				id:null,
				wxStyle: '',
				gzStyle: '',
				form:{
					maintenanceFees:[
						{
							id:null,
							costId:null,
							costName:null,
							number:1,
							price:null,
							amount:null,
							currency:'CNY',
							settlementAmount:null,
							remarks:null,
						}
					]
				},
				// 项目
				kapian: {},
				// 弹窗的开启
				pickerShow:false,
				// 添加费用弹窗
				kapianShow: false,
				pickerList:[],
				userInfoRoleName:[]
			}
		},
		onLoad(e) {
			this.id = e.id
			this.type = e.type
			this.userInfoRoleName = uni.getStorageSync('userInfo').role_name.split(',')
			this.maintenanceDetailfun()
		},
		onShow() {
			
		},
		methods: {
			kapianClick(item) {
				this.kapian = item
				this.kapianShow = true;
			},
			// 添加项目取消
			kapianCancel() {
				this.kapian = {};
				this.kapianShow = false;
			},
			// 添加项目确认
			kapianConfirm() {
				this.form.maintenanceFees.push(this.kapian)
				this.kapianShow = false;
			},
			moneyInput() {
				var money = 0
				
				for (let fees of this.form.maintenanceFees) {
					money += Number(fees.amount)
				}
				
				this.form.maintenanceAmount = money;
			},
			tab(type) {
				if(type == "wx") {
					this.wxStyle = "#fd4b09"
					this.gzStyle = ""
					// this.form.status = 2
					
				} else if (type == "gz") {
					this.gzStyle = "#fd4b09"
					this.wxStyle = ""
					// this.form.status = 3
				}
			},
			// 设备删除
			maintenanceDe(row,index){
				this.form.maintenanceFees.splice(index,1)
				this.moneyInput()
			},
			// 撤销派工
			maintenanceRevokeDispatchingWorkersfun(){
				maintenanceRevokeDispatchingWorkers(this.form).then(res=>{
					if (res.code == 200) {
						this.$refs.uToast.show({
							type: 'success',
							message: "撤销派工成功",
						})
						this.form = res.data
					}else {
						this.$refs.uToast.show({
							type: 'error',
							message: res.msg,
						})
					}
				})
			},
			// 派工
			maintenanceDispatchingWorkersfun(){
				if (!this.form.maintenanceWorkerId) {
					return this.$refs.uToast.show({type: 'warning',message: '请选择维修工'})
				}
				// if (!this.form.maintenanceWorkerTel) {
				// 	return this.$refs.uToast.show({type: 'warning',message: '请输入维修工电话'})
				// }
				maintenanceDispatchingWorkers(this.form).then(res=>{
					if (res.code == 200) {
						this.$refs.uToast.show({
							type: 'success',
							message: "派工成功",
						})
						this.form = res.data
					}else {
						this.$refs.uToast.show({
							type: 'error',
							message: res.msg,
						})
					}
					
				})
			},
			// 维修工弹窗开启
			pickerShowfun(){
				this.pickerShow = true
				this.userListfun()
			},
			// 维修工获取
			userListfun(){
				userList({roleAlias:'维修工'}).then(res=>{
					this.pickerList = [res.data]
				})
			},
			// 维修工弹窗的确认
			pickerShowConfirm(e){
				console.log(e.value[0],181);
				this.form.maintenanceWorkerId = e.value[0].id
				this.form.maintenanceWorkerName = e.value[0].name
				this.form.maintenanceWorkerTel = e.value[0].phone
				this.pickerShow = false
				
			},
			// 维修工弹窗取消
			pickerShowCancel() {
				this.pickerShow = false
			},
			// 维修费用的添加
			wxaddfun(){
				this.form.maintenanceFees.push({
					id:null,
					costId:null,
					costName:null,
					number:1,
					price:null,
					amount:null,
					currency:'CNY',
					settlementAmount:null,
					remarks:null,
				})
			},
			// 确认费用
			Confirmexpenses(){
				let a = true
				if (this.form.maintenanceFees.length <= 0) {
					return this.$refs.uToast.show({type: 'warning',message: '请添加维修费用'})
				}else {
					this.form.maintenanceFees.map((item,index)=>{
						if (!item.costName) {
							this.$refs.uToast.show({type: 'warning',message: `请填写序号${index + 1}的费用名称`})
							a = false
							return
						}
						if (!item.amount) {
							this.$refs.uToast.show({type: 'warning',message: `请填写序号${index + 1}的金额`})
							a = false
							return
						}
					})
					if (a == false) return 
					
					maintenanceConfirm(this.form).then(res=>{
						if (res.code == 200) {
							this.$refs.uToast.show({
								type: 'success',
								message: "接单成功",
							})
							// uni.navigateBack({
							// 	delta: 1
							// });
							this.form = res.data
						}else {
							this.$refs.uToast.show({
								type: 'error',
								message: res.msg,
							})
						}
					})
					
				}
				
			},
			// 确认接单接口
			receivingOrdersfun(){
				maintenanceReceivingOrders(this.form).then(res=>{
					console.log(res,180);
					if (res.code == 200) {
						this.$refs.uToast.show({
							type: 'success',
							message: "接单成功",
						})
						this.form = res.data
					}else {
						this.$refs.uToast.show({
							type: 'error',
							message: res.msg,
						})
					}
				})
			},
			// 维修设备详情
			maintenanceDetailfun(){
				maintenanceDetail({id:this.id}).then(res=>{
					this.form = res.data
					
					
					
					if (this.form.status == 4) {
						this.gzStyle = "#fd4b09"
						this.wxStyle = ""
					} else if (this.form.status == 3) {
						this.gzStyle = "#fd4b09"
						this.wxStyle = ""
					} else if (this.form.status == 2) {
						console.log(this.form.status);
						this.wxStyle = "#fd4b09"
						this.gzStyle = ""
					} else if (this.form.status == 1) {
						userList({roleAlias:'维修工'}).then(res=>{
							this.wxUserList = [res.data]
						})
					}
					// if (this.type == 'JD') {
					// 	this.form.maintenanceFees.push({
					// 		id:null,
					// 		costId:null,
					// 		costName:null,
					// 		number:1,
					// 		price:null,
					// 		amount:null,
					// 		currency:'CNY',
					// 		settlementAmount:null,
					// 		remarks:null,
					// 	})
					// }
				})
			},
		
			// 图片删除
			deletePic(e){
				if(e.file.uploadType == 0) return this.$refs.uToast.show({ type: 'warning', message: '请勿删除客户添加的图片',})
				this.form.maintenanceFiles.splice(e.index,1)
			},
			// 上传图片
			async afterRead(event){
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				console.log(lists);
				let fileListLen = this.form.maintenanceFiles.length
				lists.map((item) => {
					this.form.maintenanceFiles.push({
						uploadType:1,
						...item
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this.form.maintenanceFiles[fileListLen]
					this.form.maintenanceFiles.splice(fileListLen, 1, Object.assign(item, {
						sort: this.form.maintenanceFiles.length,
						fileName: JSON.parse(result).data.originalName,
						url: JSON.parse(result).data.link
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: http.config.baseURL +
							'/blade-resource/oss/endpoint/put-file', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						header: {
							// 客户端认证参数
							'Authorization': 'Basic ' + Base64.encode(clientId + ':' +
								clientSecret),
							'Blade-Auth': 'bearer ' + uni.getStorageSync('accessToken')
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data)
							}, 1000)
						}
					});
				})
			},
			
		}
	}
</script>

<style lang="scss" scoped>
	
	.kapianModel {
		display: flex;
		// justify-content: flex-start;
	}
	
	.label {
		left: 0rpx;
		top: 190rpx;
		width: 100%;
		height: 114rpx;
		line-height: 40rpx;
		background-color: rgba(255, 255, 255, 1);
		text-align: center;
		
		display: flex;
		
		align-items: center;

		
		.tab {
			width: 50%;
			display: flex;
			color: #c4c4c4;
			
			align-items: center;
			justify-content: center;
			font-size: 40rpx;
			text-align: left;
			font-family: PingFangSC-semiBold;
			
		}
	}
	
	.repairdetails {
		padding: 20rpx;
		box-sizing: border-box;
	}
	.cardBox {
		background: #fff;
		border-radius: 12rpx;
		width: 100%;
		padding: 30rpx;
		box-sizing: border-box;
		margin-bottom: 20rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.text {
		font-size: 30rpx;
		color: #999;
		margin-right: 10rpx;
	}
	.kapian {
		background: #fff;
		border-radius: 12rpx;
		width: 100%;
		padding: 30rpx;
		box-sizing: border-box;
		margin-bottom: 20rpx;
		
		.repairProject {
			display: flex;
			justify-content: space-between;
			
			padding: 0 20rpx;
		}
		
		.wxtitleflex {
			display: flex;
			align-items: center;
			justify-content: space-between;
		}
		.wxtitle {
			font-size: 34rpx;
			color: #888;
		}
		.wxbox {
			border-bottom: 2rpx solid #eee;
			display: flex;
			align-items: center;
			justify-content: center;
			padding: 20rpx 0;
			margin: 10rpx 0;
		}
		.addwx {
			color: #dd451b;
			font-size: 30rpx;
			padding: 10rpx 20rpx;
			border: 2rpx solid;
			border-radius: 30rpx;
		}
		.WXflex {
			display: flex;
			justify-content: space-between;
			align-items: center;
			.xuhao {
				font-size: 32rpx;
				color: #101010;
			}
			.wxscfun {
				color: red;
				font-size: 30rpx;
				padding: 10rpx 20rpx;
				border: 2rpx solid;
				border-radius: 28rpx;
			}
		}
	}
	.bottombox {
		position: fixed;
		bottom: 0;
		width: 100%;
		height: 120rpx;
		background: #fff;
		box-shadow: 0rpx 4rpx 4rpx 4rpx rgba(0, 0, 0, 0.5);
		padding: 0 40rpx;
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;
		align-items: center;
		.bottombox_name {
			font-size: 38rpx;
			color: #101010;
		}
		.bottombox_right {
			display: flex;
			.buttonClass {
				// background: #2d4a6a;
				background: #FD4B09;
				padding: 10rpx 60rpx;
				border-radius: 30rpx;
				color: #fff;
				// font-weight: bold;
				// margin: 0 10rpx;
			}
		}
	}
</style>
