<template>
	<view class="new_fllow">
		<view class="fllow_form">
			<view><text>*</text>消费日期</view>
			<picker mode="date" @change="changeTime">
				<view class="form_right">
					<view>{{consume_time ? consume_time : '点击选择'}}</view>
					<view class="iconfont icon-arrows_right"></view>
				</view>
			</picker>
		</view>
		<view class="fllow_form">
			<view><text>*</text>消费方式</view>
			<picker :value="index" @change="changeType" :range="typeList">
				<view class="form_right">
					<view>{{consume_type ? consume_type : '点击选择'}}</view>
					<view class="iconfont icon-arrows_right"></view>
				</view>
			</picker>
		</view>
		<view class="fllow_form">
			<view><text>*</text>消费金额</view>
			<view class="form_input">
				<input type="text" v-model="money" placeholder-style="font-size:30rpx;color: #999;" placeholder="消费金额" class="">
			</view>
		</view>
		<view class="fllow_area">
			<view class="area_head">备注</view>
			<textarea v-model="remark" placeholder="请输入备注" placeholder-style="color:#999" />
		</view>
		<view class="fllow_form">
			<view>上传附件</view>
			<view class="form_right" @click="uploadClick">
				<view>选择附件</view>
				<view class="iconfont icon-arrows_right"></view>
			</view>
		</view>
		<u-action-sheet 
			:list="uploadActions"
			v-model="uploadShow"
			@click="uploadType"
			></u-action-sheet>
		<view class="imglist" v-if="imgList.length != 0">
			<view class="imgbox" v-for="(item,index) in imgList" :key="index">
				<image :src="BASE_IMG_URL+'close.png'" class="delimg" @click="delImg(index)" mode="scaleToFill"></image>
				<image :src="item.file_path" class="selfimg" mode="scaleToFill"></image>
			</view>
		</view>
		<!-- 固定审批 -->
		<fixed-approval v-if="approvalPro.status == 1" :list="approvalPro.stepList"></fixed-approval>
		<!-- 审核信息 -->
		<examine-template v-if="approvalPro.status == 0" :remindPeople="remindPeople" @delExamine="delRemind"></examine-template>
		
		<view id="btn" class="btn_g bg-blue button-hover round" @click="saveFollow">
			保存
		</view>
		
	</view>
</template>

<script>
	import {
		netSelect,
		netEditCost,
		netCostDetail
	} from '@/api/kehu.js'
	import {
		BASE_IMG_URL
	} from '@/util/api.js'
	import  {
		netStaffDefaultReviewer,
		uploadUrl,
		netApprovalProcess
	} from '@/api/index.js'
	import { commonF } from '@/common/common'
	import fixedApproval from '@/components/fixedApproval.vue'
	export default {
		mixins:[commonF],
		components:{
			fixedApproval
		},
		data() {
			return {
				id: '', //合同id
				customer_id: '', //客户id
				consume_time: '', //消费日期
				consume_type: '', //消费方式
				money: '', //消费金额
				remark: '', //备注
				file_ids: '', //附件id
				BASE_IMG_URL: BASE_IMG_URL,
				typeList: [], //消费方式 列表
				DefaultRemind: [],
				remindPeople: [], //提醒谁看
				remindType:'contract',
				imgList: [],
				uploadShow: false,
				uploadActions: [{
					id: 1,
					text: '拍照',
					color: '#008EFF'
				}, {
					id: 2,
					text: '从相册中选择',
					color: '#008EFF'
				}, ],
				show: false,
				approvalPro:{},   //审批流程
			}
		},
		onLoad(options) {
			this.id = options.id
			//获取  费用 详情
			this.getDetail()
			//获取 审批流程
			this.getProcess()
		},
		onShow() {
			//获取消费方式
			this.getType()
		},
		methods: {
			//审批流程
			getProcess() {
				netApprovalProcess({type:'consume'}).then(res=>{
					this.approvalPro = res.data
				})
			},
			//费用 详情
			getDetail() {
				netCostDetail({
					id: this.id
				}).then(res => {
					res = res.data
					this.consume_time = res.consume_time
					this.consume_type = res.consume_type
					this.money = res.money
					this.remark = res.remark
					this.imgList = res.files
					this.customer_id = res.customer_id
				})
			},
			//消费方式
			getType() {
				netSelect().then(res => {
					this.typeList = res.data['消费方式']
				})
			},
			//  消费日期
			changeTime(e) {
				this.consume_time = e.detail.value
			},
			//改变  消费方式
			changeType(e) {
				this.consume_type = this.typeList[e.detail.value]
			},
			// 上传附件
			uploadClick() {
				this.uploadShow = true
			},
			onCancel() {
				this.uploadShow = false
			},
			uploadType(index) {
				if (index == 0) {
					//拍照
					uni.chooseImage({
						sourceType: ['camera'],
						sizeType:['compressed'],
						success: (res) => {
							let file = res.tempFilePaths
							file.forEach(ele=>{
								this.uploadFile(ele, 1)
							})
						}
					})
				} else if (index == 1) {
					//相册 选择
					uni.chooseImage({
						sourceType: ['album'],
						sizeType:['compressed'],
						success: (res) => {
							let file = res.tempFilePaths
							file.forEach(ele=>{
								this.uploadFile(ele, 1)
							})
						}
					})
				} else {
					//选择文件
					wx.chooseMessageFile({
						count: 1,
						type: 'file',
						success: (res) => {
							let file = res.tempFiles[0]
							this.uploadFile(file, 2)
						},
						fail: (err) => {
							console.log(err)
						}
					})
				}
			},
			//type  上传图片1  或  文件2
			uploadFile(file, type) {
				uni.uploadFile({
					url: uploadUrl,
					name: 'file',
					header: {
						'token': uni.getStorageSync('token')
					},
					// #ifdef MP-ALIPAY
					fileType:'image',
					// #endif
					filePath: type == 1 ? file : file.path,
					success: (res) => {
						let data = JSON.parse(res.data)
						this.uploadShow = false
						if (data.code == 1) {
							if (type == 1) {
								let obj = {
									id:data.data.id,
									file_path:data.data.url
								}
								this.imgList.push(obj)
							} else {
								let fileObj = {
									id:data.data.id,
									file_path:data.data.url
								}
								file = Object.assign({}, file, fileObj)
								this.fileList.push(file)
							}
						} else {
							uni.showToast({
								title: '上传失败',
								icon: 'none'
							})
						}
					},
					fail: (err) => {
						console.log(err)
					}
				})
			},
			//删除 图片
			delImg(index) {
				this.imgList.splice(index, 1)
			},
			// 选择成员
			moreClick() {
				uni.navigateTo({
					url: '/pagesA/crm/selectMember/selectMember'
				})
			},
			//保存
			saveFollow() {
				let {
					id,
					customer_id, //客户id
					consume_time, //消费日期
					consume_type, //消费方式
					money, //消费金额
					remark, //备注
					imgList,
					remindPeople, //提醒谁看
				} = this

				if (!consume_time) {
					uni.showToast({
						title: '请选择消费日期',
						icon: 'none'
					})
					return
				}
				if (!consume_type) {
					uni.showToast({
						title: '请选择消费方式',
						icon: 'none'
					})
					return
				}
				if (!money) {
					uni.showToast({
						title: '请填写消费金额',
						icon: 'none'
					})
					return
				}
				if ((!remindPeople || remindPeople.length == 0) && this.approvalPro.status == 0) {
					uni.showToast({
						title: '请选择流程审批人',
						icon: 'none'
					})
					return
				}
				let arr = []
				imgList.forEach(ele => {
					arr.push(ele.id)
				})
				let file_ids = arr.join(',')
				//流程审批人
				let remarr = []
				remindPeople.forEach(ele => {
					remarr.push(ele.id)
				})
				let flow_staff_id = remarr.join(',')

				let params = {
					id,
					customer_id, //客户id
					consume_time, //消费日期
					consume_type, //消费方式
					money, //消费金额
					remark, //备注
					file_ids,
					flow_staff_ids:this.approvalPro.status == 0 ? remarr.join(',') : ''
				}
				netEditCost(params).then(res => {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
					setTimeout(() => {
						uni.navigateBack({
							delta: 1
						})
					}, 2000)
				})
			}
		}
	}
</script>

<style lang="scss">
	.new_fllow {
		margin-top: 40rpx;
		font-size: 28rpx;

		.fllow_form {
			display: flex;
			justify-content: space-between;
			background-color: #fff;
			border-bottom: 1rpx solid #EAEAEA;
			padding: 24rpx;

			.form_right {
				display: flex;
				color: #999;

				image {
					width: 40rpx;
					height: 40rpx;
					margin: 28rpx 5rpx 0 0;
				}
			}

			text {
				color: #f00;
				margin-left: 10rpx;
			}

			.form_input {
				input {
					height: 92%;
					// line-height: 100rpx;
					text-align: right;
					margin-right: 10rpx;
				}
			}

			.fllow_button {
				margin-top: 15rpx;
			}
		}
		.imglist {
			padding: 24rpx 34rpx;
			background: #fff;
			border-bottom: 1rpx solid #EAEAEA;
			display: flex;
			justify-content: flex-start;
			align-items: center;
			flex-wrap: wrap;

			.imgbox {
				width: 160rpx;
				height: 160rpx;
				margin-right: 14rpx;
				position: relative;
				margin-bottom: 24rpx;
				border: 1rpx solid #EAEAEA;

				.delimg {
					width: 32rpx;
					height: 32rpx;
					border-radius: 50%;
					position: absolute;
					right: -16rpx;
					top: -16rpx;
					background: rgba(0, 0, 0, 0.5);
					z-index: 2;
				}

				.selfimg {
					width: 160rpx;
					height: 160rpx;
				}
			}
		}

		.fllow_area {
			background-color: #fff;
			// padding: 0 70rpx;
			margin-bottom: 30rpx;
			padding-bottom: 30rpx;
			color: #999;
			.area_head {
				padding:24rpx;
				font-size: 32rpx;
				text {
					color: #f00;
				}
			}

			textarea {
				width: 80%;
				height: 300rpx;
				padding: 30rpx;
				background: #f8f8f8;
				margin: 0 auto;
			}
		}
		.follow_task {
			height: 80rpx;
			line-height: 80rpx;
		}
	}

	.imglist {
		padding: 24rpx 34rpx;
		background: #fff;
		border-bottom: 1rpx solid #EAEAEA;
		display: flex;
		justify-content: flex-start;
		align-items: center;
		flex-wrap: wrap;

		.imgbox {
			width: 160rpx;
			height: 160rpx;
			margin-right: 14rpx;
			position: relative;
			margin-bottom: 24rpx;
			border: 1rpx solid #EAEAEA;

			.delimg {
				width: 32rpx;
				height: 32rpx;
				border-radius: 50%;
				position: absolute;
				right: -16rpx;
				top: -16rpx;
				background: rgba(0, 0, 0, 0.5);
				z-index: 2;
			}

			.selfimg {
				width: 160rpx;
				height: 160rpx;
			}
		}
	}

	#btn {
		width: 500rpx;
		height: 88rpx;
		background: $uni-text-color;
		border-radius: 29rpx;
		font-size: 34rpx;
		color: #fff;
		line-height: 88rpx;
	}
</style>
