mui.plusReady(function() {
	window.addEventListener('reload', function(e) { //执行刷新
		location.reload();
	});

	var comName = "山东省全亮共信息科技有限公司";
	var cardNum = "810102701421013395";
	var cardBank = "日照银行股份有限公司泰安路支行";
	$('.comName ').val(comName)
	$('.comNameBtn').attr("data-value", comName)
	$('.cardNum ').val(cardNum)
	$('.cardNumBtn').attr("data-value", cardNum)
	$('.cardBank ').val(cardBank)
	$('.cardBankBtn').attr("data-value", cardBank)
	var self = plus.webview.currentWebview();
	//console.log(self);
	var shopId = self.id;
	var type = 0; //0 huozhe 1

	if ("undefined" != typeof(self.type)) {
		type = self.type
	}
	if (0 == type) {
		getuncommitted();
	} else if (1 == type) {
		getcommitted();
	}
	$('.con_con_com').eq(type).show().siblings().hide();
	$('.con_nav').on("tap", ".nav_header", function() {
		type = $(this).index();
		$(this).addClass('active').siblings().removeClass('active');
		$('.con_con_com').eq(type).show().siblings().hide();
		if (0 == type) {
			getuncommitted();
		} else if (1 == type) {
			getcommitted();
		}
	})
	$('.committed').on("tap", ".row_title", function() {
		var idx = $(this).index();
		// //console.log(idx);
		$('.row_cont').eq(idx).toggle()
	})
	$('.committed').on("tap", ".oper", function(e) {
		e.preventDefault();
		e.stopPropagation()
		id = $(this).parents().parents().attr("data-id")
		id = parseInt(id);
		//console.log(id);
		JZL.openWindow("shoperUploadVoucher.html", "shoperUploadVoucher.html", {
			orderId: id,
			shopId: shopId
		})
	})
	// 	$('.btns').on('tap', '.bc_btn', function() {
	// 		var idx = $(this).index()
	// 		JZL.openWindow("uploadVoucher.html", "uploadVoucher.html")
	// 	})
	// 获取未提交凭证
	getuncommitted();

	function getuncommitted() {
		JZL.ajax(qlgUrl('app/Shoporders/getCertificate'), {
			shopId: shopId,
			type: 0
		}, function(data) {
			//console.log(data)
			if ("undefined" != typeof data.data) {

				var html = '';
				var youhui = 0;
				var payable = 0;
				mui.each(data.data, function() {
					payable = Math.abs(this.payable);
					youhui = Math.abs(youhui) + payable;

					html += '<div class="row shadown_wai" data-orderNo="' + this.orderNo + '" data-id="' + this.orderId +
						'"><div class="row_title"><div class="row_title_"><input type="checkbox" name="check1" class="check1" data-payable=' +
						payable + ' value="" checked/><label class="store_name">订单编号:' +
						this.orderNo + ' </label></div><div class="indent_status"><span>' + this.status +
						'</span> </div><div class="indent_youhui">优惠款<span>' + payable +
						'</span> </div></div><div class="row_con clearfix" data-id=""><div class="row_block clearfix">'

					mui.each(this.list, function() {

						html += '<div class="row_block_list"><div class="row_block_img"><img src="' + hyhImgUrl(this.goodsImg) +
							'" /></div><div class="rcr clearfix"><div class="rcrc"><p>' + this.goodsName + '</p><p class="leibie">' +
							this.goodsSpecNames + '</p></div></div><div class="rcrr"><p>￥' + this.goodsPrice + '</p></div></div>'
					});
					youhui = youhui.toFixed(2);
					html += '</div><div class="combination"> 共' + this.list.length + '件商品 合计<o>￥' + this.realTotalMoney +
						'</o>（含运费￥' + this.deliverMoney + '）</div><div class="combination create_time shadown_wai">订单完成时间：' + this.receiveTime +
						'</div></div></div></div>'

				})
				html +=
					'<div class="btns clearfix"><input type="checkbox" name="" id="checkall" value="" checked /><label for="checkall">全选</label><div class="combination_totle">共<span class= "orderNo">' +
					data.data.length + '</span>笔订单 优惠款<span class="youhuiNo">' + youhui +
					'</span></div><div class="bc_btn">提交凭证</div></div>'
			} else {

				html = '<p style="float: left;width: 100%;text-align: center;padding: 10px;">没有更多订单了</p>';
				// html = '<div class="row_title_ shadown_wai clearfix"><label>没有需要提交的订单</label></div>'
			}
			$('.uncommitted .con_con_com_').html(html);
		})
	}
	var i = 0;
	//全选或者全不选
	$('body').on('change', '#checkall', function() {
		checkAll('.check1', $(this).prop("checked"));
		if (false == $(this).prop("checked")) {
			$('.orderNo').html("0")
			$('.youhuiNo').html("0")
		} else {
			var youhui = 0;
			$('.orderNo').html($('.check1').length)
			mui.each($('.indent_youhui span'), function() {
				youhui += Number($(this).text())
			})
			$('.youhuiNo').html(youhui)
		}
		// 				if (0 == i) {
		// 					$('input[name="check1"]').prop('checked', true);
		// 					i = 1;
		// 				} else {
		// 	/				$('input[name="check1"]').prop('checked', false);
		// 					i = 0;
		// 				}
	})
	// checkAll('.check1',$('#checkall').checked);
	function checkAll(domName, isChecked) {
		//console.log(isChecked);
		$(domName).prop("checked", isChecked);
	}

	function checkNum() {
		var length = $('input[name=check1]:checked').length;
		var len = $('input[name=check1]').length;

		if (length == len) {
			$('#checkall').prop('checked', true);
		} else {
			$('#checkall').prop('checked', false);
		}
		var youhui = 0;
		$.each($('input[name="check1"]:checked'), function() {
			youhui += Number($(this).attr('data-payable'));

		})
		$('.orderNo').html(length);
		$('.youhuiNo').html(youhui);

	}
	$('body').on('change', '.check1', function() {
		checkNum();
	})
	var click = 0
	$('body').on('tap', ".bc_btn", function() {
		if (0 == $('input[name="check1"]:checked').length) {
			mui.alert("请选择至少一个订单")
			return;
		}
		if (1 == click) {
			return;
		}
		click = 1;
		var orderIdsArr = [];
		$.each($('input[name="check1"]:checked'), function() {
			orderIdsArr.push($(this).parents().parents().parents().attr('data-id'))
		})
		orderIds = orderIdsArr.join(',')


		JZL.openWindow("shoperUploadVoucher.html", "shoperUploadVoucher.html", {
			shopId: shopId,
			orderIds: orderIds
		})
	})
	var isjiazai = false;

	function getcommitted(page, PerPage) {
		var recommenddata = {
			page: page ? page : 1,
			PerPage: PerPage ? PerPage : 10
		}
		if (isjiazai == true) {
			return;
		}
		isjiazai = true;


		JZL.ajax(qlgUrl('app/Shoporders/getCertificate'), {
			shopId: shopId,
			type: 1
		}, function(data) {
			//console.log(data);
			if (1 == data.status) {
				var data = data.data;
				var html = '';
				var totPayable = 0;
				if (data.Rows == '') {
					$('.committed ').append(
						'<p style="float: left;width: 100%;text-align: center;padding-bottom: 9px;">没有更多订单了</p>');
					return;
				}
				$.each(data.Rows, function() {
					totPayable = 0;
					if ("已通过" == this.statusStr) {
						html += '<div class="row shadown_wai" data-id="' + this.id +
							'"><div class="row_title "><span class="created_time">提交时间：' + this.createTime +
							'</span><span class="indent_status">' + this.statusStr + '</span></div><div class="row_cont">'
					} else {
						html += '<div class="row shadown_wai" data-id="' + this.id +
							'"><div class="row_title "><span class="created_time">提交时间：' + this.createTime +
							'</span><span class="indent_status">' + this.statusStr +
							'</span><span class="oper"><img src="../img/bianji9.png"></span></div><div class="row_cont">'
					}

					$.each(this.list, function() {
						totPayable += Number(this.payable)
						html += '  <div class="row_block_list"><div class="order_num">订单编号：' + this.orderNo +
							'</div><div class="discount_amount">优惠金额:' + this.payable + '</div></div>'
					})
					totPayable = totPayable.toFixed(2)

					html += '<div class="combination">总计优惠款:' + totPayable + '</div></div></div>'
				})
				if (page == 1) {
					$('.committed').html(html);
				} else {
					$('.committed').append(html);
				}
			} else {
				mui.alert(data.msg)
			}
		})
	}

	mui('body').on('tap', '.btn', function() {
		// 每次触发事件就会使用 innerText 获取纯文本。
		var copy_content = $(this).attr("data-value");

		// var copy_content = this.innerText;
		// 加了一个确认框 让用户选择是否复制
		// mui.confirm('您要复制内容吗？', '小禾CRM', ['取消', '复制内容'], function (e) {
		//     if (e.index == 1) {
		//判断是安卓还是ios
		if (mui.os.ios) {
			// ios 的方法 这个我没具体研究过 直接拿来用了
			var UIPasteboard = plus.ios.importClass("UIPasteboard");
			var generalPasteboard = UIPasteboard.generalPasteboard();
			//设置 复制的内容也就是 触发事件 innerText 获取的内容
			generalPasteboard.plusCallMethod({
				setValue: copy_content,
				forPasteboardType: "public.utf8-plain-text"
			});
			generalPasteboard.plusCallMethod({
				valueForPasteboardType: "public.utf8-plain-text"
			});
			// 在上边都走完 给用户一个提示
			mui.toast('复制成功')
		} else {
			//安卓 的方法 这个我没具体研究过 直接拿来用了
			var context = plus.android.importClass("android.content.Context");
			var main = plus.android.runtimeMainActivity();
			var clip = main.getSystemService(context.CLIPBOARD_SERVICE);
			plus.android.invoke(clip, "setText", copy_content);
			// 在上边都走完 给用户一个提示
			mui.toast('复制成功')
		}
		// }
	})



})
