<template>
	<view class="checkSubmit">
		<view class="commons-title">
			面料信息
		</view>
		<view class="submit-warp">
			<view class="form-text">
				面料
			</view>
			<view class="form-box">
				<view>
					<view class="" style="text-align: left;display: flex;justify-content: space-between;">
						<view class="" style="color:#999;width:90%"  @click="openSelectQd('mianliao')">
							{{formData.materialName || '请选择面料 '}}
						</view>
						
						<uni-icons  @click="openSelectQd(index)" type="forward" size="20"></uni-icons>
					</view>
				</view>
			</view>
		</view>
		<view class="submit-warp" style="margin-top:40rpx">
			<view class="form-text">
				供应商
			</view>
			<view class="form-box">
				<view>
					<view class="" style="text-align: left;display: flex;justify-content: space-between;">
						<view class="" style="color:#999;width:90%"  @click="openSelectQd('gongyingshang')">
							{{formData.supplierName || '请选择供应商 '}}
						</view>
						
						<uni-icons  @click="openSelectQd(index)" type="forward" size="20"></uni-icons>
					</view>
				</view>
			</view>
		</view>
		<view class="commons-title">
			拍照上传
		</view>
		<view class="form-box" style="margin:0 30rpx">
			<view class="imglist">
				<view class="item" v-for="(item,index) in imgs" :key="item" v-if="!item.loadingType">
					<image :src="item" alt=""></image>
					<view class='delete' bindtap='deleteImg' @click="imgDelete(index)">
						x
					</view>
				</view>
				<view class="item" v-else>
					<image src="../../static/loding.gif"></image>
				</view>
				<view class="last-item" @click="bindUpload">
					<text class="sign">+</text>
				</view>
			</view>
		</view>
		<view class="commons-title">
			问题选择
		</view>
		<view class="wenti-list">
			<view class="list-box" :class="i.active ? 'active' : ''" v-for="(i,index) in wentiList" :key="i.type" @click="wentiSelect(i,index)">
				{{i.name}}
			</view>
		</view>
		<view class="" style="margin: 0 20rpx 20rpx;border:1px solid #dcdcdc;">
			<textarea style="width:100%;padding:20rpx" v-model="formData.remark" placeholder="备注信息"/>
		</view>
		
		<view class="submit-btn-warp">
			<button @click="checkSubmit(2)" style="background:#EF0000;color:#fff;margin-bottom: 30rpx;">取消</button>
			<button @click="checkSubmit(1)" style="background:#1A3AF0; color:#fff;margin-bottom: 30rpx;">提交</button>
		</view>
		<uni-popup ref="popup2" type="top">
			<view class="search-box">
				<uni-search-bar @confirm="selectList" v-model="req.search" @cancel="reload">
				</uni-search-bar>
				<ul>
					<li @click="selectMaterialList(i)" v-for="i in range2" :key="i.id">
						{{i.name}}
					</li>
				</ul>
			</view>
		</uni-popup>
		<uni-popup ref="popup" type="top">
			<view class="search-box">
				<uni-search-bar @confirm="selectList" v-model="req.search" @cancel="reload">
				</uni-search-bar>
				<ul>
					<li @click="selectMaterialList(i)" v-for="i in range" :key="i.id">
						{{i.name}}
					</li>
				</ul>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		getInfo
	} from "@/util/api.js";
	import Vue from 'vue'
	// import htzImageUpload from '@/components/htz-image-upload/htz-image-upload.vue'
	import {
		dateFormat,
		getTime
	} from '../../util/uitl.js'
	export default {
		components: {
			// htzImageUpload,
		},
		data() {
			return {
				wentiList:[
					{type:1,name:"短少",active:false},
					{type:2,name:"抽纱",active:false},
					{type:3,name:"褶皱",active:false},
					{type:4,name:"脏污",active:false},
					{type:5,name:"破洞",active:false},
					{type:6,name:"缩水",active:false}
				],
				value: 0,
				range: [],
				range2:[],
				formData: {
					supplierId:null,
					materialId:null,
					problemType:'',
					remark:null,
					attr:[],
					supplierName:null,
				},
				imgs: [],
				option: null,
				imgUrl: null,
				checkListData: [],
				optionCopy: null,
				materialIndex:null,
				req:{
					pageNum:1,
					pageSize:10,
					search:null,
					materialCode:null,
				},
				selectModalType:'gongyingshang',
			}
		},
		onLoad(option) {
			const v = this
			v.selectList()
		},
		methods: {
			selectMaterialList(i){
				const v = this
				if(v.selectModalType == 'gongyingshang'){
					v.formData.supplierId = i.id
					v.formData.supplierName = i.name
				}else{
					v.formData.materialId = i.id
					v.formData.materialName = i.name
					v.req.materialCode = i.id
					
				}
				console.log(i)
				this.$refs.popup2.close()
				this.$refs.popup.close()
			},
			openSelectQd(modalType){
				const v = this
				if(modalType == 'gongyingshang' && v.formData.materialId == null){
					wx.showToast({
						title: "请先选择面料！",
						icon: "none",
						duration: 2000
					})
					return
				}
				
				v.selectModalType = modalType
				v.req.search = null
				v.selectList()
				if(modalType == 'gongyingshang'){
					this.$refs.popup2.open('top')
				}else{
					this.$refs.popup.open('top')
				}
				
			},
			bindPickerChange(e){
				console.log(e)
				const v = this
				v.formData.supplierName = v.range[e.detail.value].bankAccoutName
				v.formData.supplierId = v.range[e.detail.value].id
			},
			selectList(){
				const v = this
				if(v.selectModalType == 'gongyingshang'){
					v.$post('/supplier/selectList',v.req).then(res=>{
						console.log(res)
						this.range2 = res.data
						
					})
				}else{
					v.$post('/material/selectList',v.req).then(res=>{
						console.log(res)
						this.range = res.data
						
					})
				}
				
				
				
			},
			wentiSelect(item,index){
				this.wentiList[index].active = !this.wentiList[index].active
			},
			imgDelete(_index) {
				const v = this
				v.imgs.splice(_index, 1)
			},
			bindUpload() {
				const v = this
				wx.chooseImage({
					count: 1,
					sizeType: ["original", "compressed"], // 可以指定是原图还是压缩图，默认二者都有
					sourceType: ["album", "camera"], // 可以指定来源是相册还是相机，默认二者都有
					success: function(res) {
						// 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
						var tempFilePaths = res.tempFilePaths
						for (var i = 0; i < tempFilePaths.length; i++) {
							v.imgs.push({
								loadingType: true
							})
							wx.uploadFile({
								url: 'https://cfm.bytesail.cn/api/storage/cloudApi/applet/uploadFile',
								filePath: tempFilePaths[i],
								name: "file",
								header: {
									'Blade-Auth': 'bearer ' + Vue.prototype.$token.access_token,
									'Authorization':'Basic c2FiZXI6c2FiZXJfc2VjcmV0',
									'Content-Type':'application/json;charset=UTF-8',
								},
								success: function(res) {
									console.log(res.data)
									res.data = JSON.parse(res.data)
									if (res.data.code == 200) {
										wx.showToast({
											title: "上传成功",
											icon: "none",
											duration: 1500
										})
										v.imgs.splice(-1)
										v.imgs.push(res.data.msg)
										console.log(v.imgs)
									}

								},
								fail: function(err) {
									wx.showToast({
										title: "上传失败",
										icon: "none",
										duration: 2000
									})
								},
								complete: function(result) {
									console.log(result.errMsg)
								}
							})
						}
					}
				})

			},
			zidingyiSuccess() {

			},
			checkSubmit(_type) {
				const v = this
				if(_type == 2){
					wx.reLaunch({
						url: '/pages/home'
					})
				}
				
				v.formData.problemType = ''
				v.formData.attr = []
				
				for (var i = 0; i < v.imgs.length; i++) {
					v.formData.attr.push({
						realName:"xxx",
						path:v.imgs[i]
					})
					
				}
				for (var i = 0; i < v.wentiList.length; i++) {
					if(v.wentiList[i].active){
						v.formData.problemType = v.formData.problemType + v.wentiList[i].type + ','
					}
				}
				if(v.formData.problemType == ''){
					uni.showToast({
						title: '请选择问题类型！'
					})
					return
				}
				v.formData.problemType = v.formData.problemType.slice(0,v.formData.problemType.length - 1)
				v.$post('/supplierProblem/add', v.formData).then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: '提交成功，跳转中！'
						})
						setTimeout(() => {

							wx.reLaunch({
								url: '/pages/home'
							})
						}, 2000)
					}
				})


			},
			select(e) {
				console.log(e)
				const v = this
				v.$post('/wx/common/uploadFile', {
					file: e.tempFilePaths[0]
				}).then(res => {

				})
			},
		},
	}
</script>

<style lang="less" scoped>
	.search-box{
		background-color: #fff;
		ul{
			li{
				padding: 25rpx 30rpx;
				border-bottom: 1rpx solid #dcdcdc;
			}
		}
	}
	.submit-btn-warp{
		display: flex;
		justify-content: space-between;
		button{
			width: 47%;
		}
	}
	.wenti-list{
		overflow: hidden;
		.list-box{
			
			width: 28%;
			margin: 0 2% 20rpx;
			float: left;
			height: 60rpx;
			line-height: 60rpx;
			text-align: center;
			border:1px solid #DDDCDC;
			border-radius: 10rpx;
		}
		.list-box.active{
			border:1px solid #1A3AF0;
			background-color: #1A3AF0;
			color: #fff;
		}
	}
	.submit-warp{
		display: flex;
		.form-text{
			width: 20%;
			text-align: center;
		}
		.form-box{
			width: 80%;
			padding: 0 20rpx;
		}
	}
	.imglist {
		display: flex;
		flex-wrap: wrap;
	}
	
	.imglist .item {
		width: 150rpx;
		height: 150rpx;
		margin-right: 22rpx;
		margin-bottom: 10rpx;
		position: relative;
	}
	
	.imglist .last-item {
		width: 150rpx;
		height: 150rpx;
		text-align: center;
		line-height: 146rpx;
		border: 2rpx dashed #8B97A9;
		box-sizing: border-box;
	}
	
	.imglist .item image {
		width: 100%;
		height: 100%;
	}
	
	.imglist .item .delete {
		width: 30rpx;
		height: 30rpx;
		position: absolute;
		top: -25rpx;
		right: -12rpx;
		font-size: 40rpx;
		font-weight: bold;
	}
</style>
