<template>
	<view>

		<cu-custom bgColor="bg-gradual-pink" :isBack="true" >
			<block slot="backText">返回</block>
			<block slot="content">检测委托申请</block>
		</cu-custom>

		<form>

			<uni-group mode="card">
				<view class="cu-form-group" style="justify-content: flex-start;">
					<view class="title">委托单号</view>
					<view style="text-align: left;"  >{{weituodanhao}}</view>
				</view>
				<!-- 			<view class="cu-form-group">
						<view class="title">类型</view>
						<input placeholder="常规" name="input" v-model="myFormData.username" disabled></input>
					</view> -->
				<view class="cu-form-group">
					<my-date style="padding: 0rpx;" v-model="beginTime" placeholder="请选择" fields="day"
						label="期望完成时间"></my-date>
				</view>
			</uni-group>

<!-- 			<uni-link
				href="https://www.szjyxt.com/demo/reportJsp/pdfReport.jsp?access_token%20=%20999f19ea64f947ab83c13b0ef5e6677e&rpx=%E4%B8%AD%E6%B1%87%E7%91%9E%E5%BE%B7%E6%A3%80%E6%B5%8B%E4%B8%AD%E5%BF%83/08%E4%BB%AA%E5%99%A8%E8%AE%BE%E5%A4%87%E7%9A%84%E4%BD%BF%E7%94%A8%E3%80%81%E7%BB%B4%E6%8A%A4%E5%92%8C%E7%AE%A1%E7%90%86%E7%A8%8B%E5%BA%8F/GDYR-CX06-R06A%20%20%E4%BB%AA%E5%99%A8%E8%AE%BE%E5%A4%87%E7%BB%B4%E6%8A%A4%E4%BF%9D%E5%85%BB%E8%AE%B0%E5%BD%95%E8%A1%A8(%E6%9C%88).rpx&id_=948538515816185856"
				text="查阅报告"></uni-link>
			<uni-link
				href="
				https://www.szjyxt.com/demo/reportJsp/pdfReport.jsp?access_token%20=%20999f19ea64f947ab83c13b0ef5e6677e&rpx=%E4%B8%AD%E6%B1%87%E7%91%9E%E5%BE%B7%E6%A3%80%E6%B5%8B%E4%B8%AD%E5%BF%83/08%E4%BB%AA%E5%99%A8%E8%AE%BE%E5%A4%87%E7%9A%84%E4%BD%BF%E7%94%A8%E3%80%81%E7%BB%B4%E6%8A%A4%E5%92%8C%E7%AE%A1%E7%90%86%E7%A8%8B%E5%BA%8F/GDYR-CX06-R06A%20%20%E4%BB%AA%E5%99%A8%E8%AE%BE%E5%A4%87%E7%BB%B4%E6%8A%A4%E4%BF%9D%E5%85%BB%E8%AE%B0%E5%BD%95%E8%A1%A8(%E6%9C%88).rpx&id_=948538515816185856
				"
				text="超链"></uni-link> -->

<!-- 		<uni-section title="基本用法" type="line" padding>
			<uni-rate v-model="rateValue" @change="onChange" />
		</uni-section>
		<uni-section title="半星" subTitle="使用 allow-half 属性设置是否显示半星" type="line" padding>
			<uni-rate allow-half :value="3.5" />
		</uni-section> -->
			
			

			<uni-group mode="card" title="样品信息">
				<view class="cu-form-group margin-top">
					<view class="title" style="color: #888888">样品名称</view>
					<input placeholder="请输入样品名称" v-model="ypname" name="input"></input>
				</view>
				<view class="cu-form-group ">
					<view class="title" style="color: #888888">规格型号</view>
					<input placeholder="请输入规格型号" v-model="ypguige" name="input"></input>
				</view>
				<view class="cu-form-group ">
					<view class="title" style="color: #888888">数量</view>
					<input placeholder="请输入数量" v-model="ypshuliang" type="number" name="input"></input>
				</view>
				<view class="cu-form-group ">
					<view class="title" style="color: #888888">生产日期/批号</view>
					<input placeholder="请输入" v-model="yppihao" name="input"></input>
				</view>
				<view class="cu-form-group ">


					<view class="title" style="color: #888888">样品处置</view>
					<app-select v-model="type2" placeholder="请选择处置方式" :dict="plan_type" space></app-select>
				</view>
				<view class="cu-form-group">
					<view class="title" style="color: #888888">检测类别</view>
					<app-select v-model="type1" placeholder="请选择检测类别" :dict="plan_type1" space ></app-select>
				</view>
			</uni-group>

			<uni-group mode="card" >
				<uni-section v-bind:title="'检测对象/'+sex[radio1].leixing" type="line">
							<view class="example-body2 uni-px-5 uni-pb-5">
								<uni-data-checkbox :multiple="false" v-model="radio1" :localdata="sex"   @change="change(sex[radio1].text)">
								</uni-data-checkbox>
						</view>

						<view class="uni-px-5 uni-pb-5">			
							<uni-data-checkbox multiple v-model="checkbox2"  
							:localdata="sex[radio1].canshu"  icon="right" @change="calc" ></uni-data-checkbox>
						</view>						
						<view>
							
						    <view style="margin-bottom: 25rpx; float: right;">总价:{{total}}￥</view>
						</view>
						</uni-section>
			
					<uni-collapse-item title="检测方法" :open="false">
						
						
						<uni-card :is-shadow="false" >
			                  {{fangfaText}}
						</uni-card>
					</uni-collapse-item>
				</uni-group>


			<view class="">
				<uni-section title="" type="line" padding>


					<view class="cu-form-group">
						<view class="title">报告份数</view>
						<uni-number-box v-model="vModelValue" @blur="blur" @focus="focus" @change="changeValue" />
					</view>


					<view class="cu-form-group">
						<view class="title">是否出具意见和解释</view>
						<uni-data-checkbox v-model="radio2" :localdata="yijian" @change="change(yijian[radio2].text)">
						</uni-data-checkbox>

					</view>

					<view class="cu-form-group">
						<view class="title">检测结论</view>
						<uni-data-checkbox v-model="radio3" :localdata="jielun" @change="change(jielun[radio3].text)">
						</uni-data-checkbox>

					</view>
					
	

					<view class="cu-form-group">
						<view class="title">取报告方式</view>
						<uni-data-checkbox style="flex-wrap: nowrap;" v-model="radio4" :localdata="baogao"
							@change="change(baogao[radio4].text)"></uni-data-checkbox>

					</view>
					<view class="cu-form-group" style="justify-content: flex-start;">
						<view class="title">其它要求</view>
						<view><input class="uni-input"  v-model="remark"  /></view>
					</view>





		<uni-collapse-item title="声明" :open="open">
			<uni-card :is-shadow="false">
				<text class="uni-body" style="color: #DD514C;">1.送样单位须保证样品的真实性、代表性.
					2.本检验中心保证检测结果的公正性，对来样的检测数据负责；并对其样品及相关资料保密。
					3.委托方需对送检样品的安全性和缺陷负责，一旦因样品自身问题发生事故损失的，委托方需承担相应责任。</text>
			</uni-card>
		</uni-collapse-item>

                    

				</uni-section>

			</view>




			<view class="padding flex flex-direction" style="margin-bottom: 100rpx;">
				<button class="cu-btn bg-blue lg" @click="onSubmit">提交</button>
			</view>
		</form>
	</view>
</template>

<script>
	const plan_type = [{
		text: '返回给委托人',
		value: '1'
	}, {
		text: '自取',
		value: '2'
	}, {
		text: '不取回',
		value: '3'
	}];
	const plan_type1 = [{
		text: '委托检测',
		value: '1'
	}, {
		text: '抽样检测',
		value: '2'
	}];
	import myDate from '@/components/my-componets/my-date.vue'
	import appSelect from '@/components/my-componets/appSelect.vue'
	import md5 from "@/common/util/md5.js"
	import { ACCESS_TOKEN,USER_NAME,USER_INFO } from "@/common/util/constants"
	export default {
		components: {
			myDate,
			appSelect
		},

		data() {
			return {
				remark:'无特殊要求',
				jiance:[],
				ypname:'',
				ypguige:'',
				ypshuliang:'',
				yppihao:'',
				ypchuzhi:'',
				ypleibie:'',
				
				fangfa:[],
				fangfaText:'',
				total:0,
				open:true,
				rateValue: 0,
				vModelValue: 1,
				radio1: 0,
				radio2: 1,
				radio3: 1,
				radio4: 0,
				checkbox2: [],
				sex: [{
					text: '碳素钢和中低合金钢',
					value: 0,
					canshu:[]
				}, {
					text: '金属零件',
					value: 1,
					canshu:[]
				}],

				yijian: [{
					text: '要',
					value: 0
				}, {
					text: '不要',
					value: 1
				}],
				jielun: [{
					text: '要',
					value: 0
				}, {
					text: '不要',
					value: 1
				}],
				baogao: [{
					text: '自取',
					value: 0
				}, {
					text: '邮件',
					value: 1
				}, {
					text: '其它',
					value: 2
				}],
				value: 0,
				type: "default",
				inverted: false,
				leibie: "",
				weituodanhao: '',
				plan_type1,
				plan_type,
				type2: '',
				type1: '',
				beginTime: '',
				index: -1,
				leixing: '',
				switchC: true,
				imgList: [],
				uploadUrl: "/sys/common/upload",
				myFormData: {
					weituodanhao: '',
					avatar: '',
					realname: '',
					username: '',
					sex: 1,
					birthday: '',
					orgCode: '',
					workNo: '',
					phone: '',
					email: '',
					id: '',
				},

			};
		},
		onLoad: function(option) {
        let pagepara={
			"parameters": [],
			"requestPage": {
				"pageNo": 1,
				"limit": 100,
				"offset": 0,
				"totalCount": 12
			}
		}
		let this_=this;
		//"ibps/business/v3/entrust/sysjtsjpz/query"
        this.$http.post("/ibps/business/v3/entrust/sysjtsjpz/query", pagepara).then(res => {
        	if (res.data.state == 200) {
				console.log(res.data.variables.data)
				let data=res.data.variables.data;		
                 //console.log(filterJson(data,"jianCeDuiXiang"))
				let jianceduix=filterJson(data,"jianCeDuiXiang")
				let datajiance=[]
				for(let i in jianceduix){
					let yy={};
					    yy.jianceduix=jianceduix[i].jianCeDuiXiang;
						yy.jianCeLeiBie=jianceduix[i].jianCeLeiBie;
						yy.jianCeFangFa=jianceduix[i].jianCeFangFa;
						yy.canshu=data.filter(function(item){
						    return item.jianCeDuiXiang==jianceduix[i].jianCeDuiXiang
					})
					datajiance.push(yy);
					
				}
				//let jiance=[];
				for(let i in datajiance){
					let entity={};
					entity.value=i;
					entity.text=datajiance[i].jianceduix;
					entity.leixing=datajiance[i].jianCeLeiBie;
					entity.canshu=datajiance[i].canshu;
					entity.fangfa=datajiance[i].jianCeFangFa;
					for(let j in datajiance[i].canshu){
						datajiance[i].canshu[j].text=datajiance[i].canshu[j].xiangMuCanShu+"  单价:"+datajiance[i].canshu[j].price +"￥"
						datajiance[i].canshu[j].value=j;
					//	datajiance[i].canshu[j].selected=true;						
					}
					if(i<2){
						this_.sex[i]=entity;//
					}else{
						this_.sex.push(entity)
					}
					
					
					//jiance.push(entity)
				}
				
				
		
				this_.radio1=0;

				console.log(this_.sex)
				//this_.radio1=0;
        	}
        });
        
		this.radio1=0;
			//this.defaultdata=[0,1];



			//委托单号
			let smsParams = {
				alias: "gzyrwtbh"
			};

			this.$http.post("/ibps/business/v3/jc/sys/getNextIdByAlias", smsParams).then(res => {
				if (res.data.state == 200) {
					console.log(res.data.data)
					this_.weituodanhao = res.data.data
				}
			});
			//检测对象 https://www.szjyxt.com/ibps/business/v3/data/template/queryDataTable
/* 			let param1 = {
				"parameters": [{
						"key": "response_data",
						"value": "{\"template_id\":\"958692267806162944\",\"template_type\":\"\",\"attrs\":\"\",\"result_columns\":[{\"label\":\"检测对象\",\"name\":\"jian_ce_dui_xiang\"}],\"sort_columns\":\"\",\"buttons\":\"\",\"export_columns\":\"\",\"datasetKey\":\"jianceduixiangv\",\"unique\":\"jian_ce_dui_xiang\",\"key\":\"jcdxzly\",\"dynamic_params\":{}}"
					},
					{
						"key": "filter_condition_key",
						"value": ""
					}
				]
			} */
/* 			this.$http.post("ibps/business/v3/data/template/queryDataTable", param1).then(res => {
				if (res.data.state == 200) {
					console.log(res.data.data)
					let data = res.data.data.dataResult
					console.log(data)
				}
			}); */
			//检测类型、检测
			//请求系统接口demo
			
			//查询方法名称
			let data = 'select * FROM t_ffgl';
            data = '{"sql":"' + data + '"}'
			let md5 = this.sig(data) //加密， 获取md5密文
			let requestData = data.slice(0, 1) + '"sig":"' + md5 + '",' + data.slice(1) //结果拼接
			this.$http.post("/ibps/business/v3/sys/universal/inputSqlSelectData", requestData).then(res => {
					if (res.data.state == 200) {
						this.fangfa = res.data.variables.data
					}				
				});
			let query = this.$Route.query
		},
		methods: {
            calc(e){
				console.log(e)
				//计算价格
				let total=0;
				let data=e.detail.data;
				let canshu=[];
		
				
				let text='';
				
				let jiance=[];
				
				for(let i in data){
					//计算价格
					total=total+data[i].price;
					canshu.push(data[i].xiangMuCanShu)
					//生成检测项目数据
					
					let entity={};
					entity.jianCeLeiXing=data[i].jianCeDuiXiang;
					entity.xiangMuCanShu=data[i].xiangMuCanShu;
					entity.jianCeFangFa=data[i].jianCeFangFa;
					entity.danJia=data[i].price;
					
					entity.shuLiang="1";
					entity.zheKou="0";
					entity.zheHouJia=data[i].price;
					
					//方法text拼接
					for(let j in this.fangfa){
						
						let fangfaid=data[i].jianCeFangFa.split(",")
						//console.log(fangfaid)
						
/* 						for( let k in fangfaid){
							let id=fangfaid[k].replace(",","")
							console.log(data[i].jianCeFangFa==id)
							if(data[i].jianCeFangFa==id){							
								text=text+ i+"."+this.fangfa[j].fang_fa_ming_chen+"<br/>"
							}	
						} */
						
				
					}
					
					jiance.push(entity);
				}
				this.fangfaText=text;
				console.log(this.fangfaText)
				this.jiance=jiance;
				this.canshu=canshu;
				this.total=total
				
				
			},
			onChange(e) {
					//console.log('rate发生改变:' + JSON.stringify(e))
					console.log(e)
					// console.log(this.rateValue);
				},
			onSubmit() {
				
				let data={
						crmZhuangTai: "crm",
					    weiTuoZhuangTai: "待审核",
						id: "",
						weiTuoDanHaoZhuan: "",
						yangPinBianHaoZhu: "",
						shouLiBuMen: "检测室",
						weiTuoFang: "外部委托",
						jianCeWeiZhi: "",
						shouLiShiJian: "2022-06-07",
						dingDanHao: "",
						dingDanLaiYuan: "业务人员下单",
						baoJiaDan: "",
						quDao: "手机下单",
						xiaoShouRenYuan: "",
						zongJinE: this.total,
						weiTuoDanHao: "",
						lianXiRen: "",
						weiTuoDanWei: "",
						lianXiFangShi: "",
						qiWangWanChengShi: "",
						tongXunDiZhi: "质量部",
						gdyrqcyp: [],
						yangPinChuZhi: "",
						jianCeLeiBie: "",
						keHuYaoQiu: "无特殊要求",
						jcxmcrm: [], //检测项目
						quBaoGaoFangShiJi: "自取",
						baoGaoFenShu: 1,
						qiTaYaoQiu: this.remark,
						jianCeJieGuoZuoCh: "不要",
						jianCeJieLun: "不要",
						jianCeBaoGaoZhong: "不要",
						hkfab: [],
						dingDanShenHeYiJi: "",
						dingDanShenPiYiJi: "",
						bianZhiRen: "",
						yangPinMingCheng: "样品名称",
						guiGeXingHao: "",
						shengChanRiQiPiHa: "",
						shouJiKeHuId:""
                    };
					
					
					
	
			
			//组装数据
			
			//获取下单用户信息
			let info= uni.getStorageSync(USER_INFO);
            console.log(info)
			
			if(info==undefined){
				return;
			}
			
			data.lianXiRen=info.username;
			data.weiTuoDanWei=info.companyid;
			data.lianXiFangShi=info.number;
			data.dingDanLaiYuan="手机下单";
			data.shouLiBuMen="检测室";
			data.shouJiKeHuId=info.id;
			data.weiTuoDanHao=this.weituodanhao;
			data.qiWangWanChengShi=this.beginTime;
			
			data.jcxmcrm=this.jiance;
			

			//样品处置
			
			
			if(this.type2==""||this.type1==""){
				this.$tip.alert("请选择检测类别或处置方式");
				return false;
			}
 			data.yangPinChuZhi=this.plan_type[this.type2-1].text;
			//检测类别
		    data.jianCeLeiBie=this.plan_type1[this.type1-1].text;

			//样品名称、规格、数量、批号
			let yangpin= {
				yangPinMingCheng: this.ypname,
				guiGeXingHao:this.ypguige,
				shuLiang: this.ypshuliang,
				shengChanRiQiPiHa: this.yppihao
			}
		    
			data.gdyrqcyp.push(yangpin);
		    //报告份数
		    data.baoGaoFenShu=this.vModelValue;
		   
		   
		   
			//检测结论
			data.jianCeJieLun=this.jielun[this.radio3].text;
			
			data.quBaoGaoFangShiJi=this.baogao[this.radio4].text
			
			
			
			
			
			
			let para={boCode: "gdyrqcwt",
					version: 0,
					formKey: "ddfq",
					pk: "",
					data:JSON.stringify(data)}
		//	console.log(para)
			
			let that=this;
			this.$http.post("ibps/business/v3/data/template/saveFormData/vo",para).then(res =>{
				if (res.data.state == 200) {
					this.$tip.success("下单成功!")
					this.$Router.replace({name:'index'})
				}else{
					this.$tip.error("请求错误: "+res.data.message)
				}			
			    }).catch((err) => {
				  let msg = ((err.response || {}).data || {}).message || err.data.message || "请求出现错误，请稍后再试"
				  this.$tip.error(msg);
			    }); 
			
	
			},
			DateChange(e) {
				this.myFormData.birthday = e.detail.value
			},
			SwitchC(e) {
				this.switchC = e.detail.value
			},
			sig(sql) {
				let rul = (sql.length + 9) * 12 * 3 + 168
				let salt = "JinYuanXinTong"
				return md5(rul + '' + salt)
			},
			ChooseImage() {
				var that = this;
				uni.chooseImage({
					count: 4, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						that.$http.upload(that.$config.apiUrl + that.uploadUrl, {
								filePath: res.tempFilePaths[0],
								name: 'file'
							})
							.then(res => {
								that.myFormData.avatar = res.data.message;
							})
							.catch(err => {
								that.$tip.error(err.data.message)
							});
						this.imgList = res.tempFilePaths
					}
				});
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					title: '召唤师',
					content: '确定要删除这段回忆吗？',
					cancelText: '再看看',
					confirmText: '再见',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			},
			change(text) {
                  this.checkbox2=[]; //检测项目重新选择
/* 				if (text == '金属零件' || text == '碳素钢和中低合金钢') {
					let data = '';
					if (text == '金属零件') {
						data = "select * from t_sysjtsjpz where jian_ce_dui_xiang = '金属零件' and shi_fou_shan_chu_ = '0'";
						this.leixing = "尺寸和形位公差测量"
					}
					if (text == '碳素钢和中低合金钢') {
						data =
							"select * from t_sysjtsjpz where jian_ce_dui_xiang = '碳素钢和中低合金钢' and shi_fou_shan_chu_ = '0'";
						this.leixing = "元素成分测定"
					}

					data = '{"sql":"' + data + '"}'
					let md5 = this.sig(data) //加密， 获取md5密文
					let requestData = data.slice(0, 1) + '"sig":"' + md5 + '",' + data.slice(1) //结果拼接
					this.$http.post("/ibps/business/v3/sys/universal/inputSqlSelectData", requestData).then(res => {
						if (res.data.state == 200) {
							let result = res.data.variables.data;
							console.log(result)
							let canshu = []
							for (let index in result) {
								let en = {}
								en.show = false,
									en.text = result[index].xiang_mu_can_shu_;
								canshu.push(en);
							}
							this.canshu = canshu;

							console.log(this.canshu)
						}				
						});
						
						
				}
 */
			},			
			changeValue(value) {
				console.log('返回数值：', value);
			},
			focus(e) {
				console.log('focus:', e);
			},
			blur(e) {
				console.log('blur:', e);
			},
			setType() {
				let types = ["default", "primary", "success", "warning", "error"];
				let index = types.indexOf(this.type);
				types.splice(index, 1);
				let randomIndex = Math.floor(Math.random() * 4);
				this.type = types[randomIndex];
			},
			setInverted(index) {
				this.canshu[index].show = !this.canshu[index].show;
			},


		}
	}
	//过滤数据
	function filterJson(data, Name) {   //data是json对象，Name是根据什么字段去重
	    var map = {},
	      dest = [];
	    for (var i = 0; i < data.length; i++) {
	        var ai = data[i];
	        if (i == 0) {
	            dest.push(ai);
	        } else {
	            var filterData = dest.filter(function (e) {
	                return e[Name] == ai[Name];
	            })
	            if (filterData.length == 0) {
	                dest.push(ai);
	            }
	        }
	    }
	    return dest;
	}

	
	
</script>

<style>
	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}

	.example-body {
		/* #ifndef APP-PLUS-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: flex-start;
		align-items: flex-end;
		flex-wrap: wrap;
	}

	.tag-view {
		margin-right: 10px;
	}

	.example-body2 {
		padding: 12px;
		background-color: #FFFFFF;
	}

	.result-box {
		text-align: center;
		padding: 20px 0px;
		font-size: 16px;
	}
		.uni-list-item {
			border-bottom-style: solid;
			border-bottom-width: 1px;
			border-bottom-color: #eee;
			font-size: 14px;
		}
	
		.uni-list-item__container {
			/* #ifndef APP-NVUE */
			display: flex;
			width: 100%;
			box-sizing: border-box;
			/* #endif */
			padding: 12px 15px;
			flex: 1;
			position: relative;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;
		}
	
		.uni-list-item__content-title {
			font-size: 14px;
			color: #666;
		}
</style>
