// 手工选择商品选中的商品列表
$(function(){
    $('.dialog-close-shops').click(function(){
        close_shops();
    });    
});
//打开选择
function choose_shops(goods_id,attr_key,k){
	$('.choose_shops').show();
    $('.choose_shops_maks').show();
	$('.choose_shops_main').html('');
	$('.pages_hanchao_shops').html('');
	
	get_shops_list(goods_id,attr_key,k);//显示门店并且显示库存
}
//关闭选择
function close_shops(){
    $('.choose_shops').hide();
    $('.choose_shops_maks').hide();
}
//显示门店并且显示库存
function get_shops_list(goods_id,attr_key,k){
    $('#loading_goods_shops').show();
    var querydata= new Object();
	querydata.goods_id = goods_id;
	querydata.is_edit = is_edit;
	if(querydata.is_edit != 1){
		querydata.attr_key = attr_key;
	}
    $.ajax({
        type: "POST",
        url: APP + "Shop/Goods/choose_shops",
        data: querydata,
        dataType: "JSON",
        success: function(ret){           
            $('#loading_goods_shops').hide();
            if(ret.status == 1){
                $('.choose_shops_main').html(ret.data);
				if(querydata.is_edit == 1){
					if(goods_id){
						$('.pages_hanchao_shops').html('<span style="margin-left:15px;">备注：</span><input type="text" name="remarks" class="remark_input" placeholder="请填写备注" value="" /><button class="btn btn-primary f_r sureChoose mt15" onclick="sub_save_data()" type="button">确认</button>');
					}else{
						$('.pages_hanchao_shops').html('<button class="btn btn-primary f_r sureChoose mt15" onclick="close_shops()" type="button">确认</button>');
					}
				}else{
					// $('.pages_hanchao_shops').html(ret.page+'<button class="btn btn-primary f_r sureChoose mt15" onclick="save_data('+k+')" type="button">确认</button>');
					if(goods_id && attr_key){
						var html_ff = "sub_save_data_attr('"+attr_key+"')";

						$('.pages_hanchao_shops').html('<span style="margin-left:15px;">备注：</span><input type="text" name="remarks" class="remark_input" placeholder="请填写备注" value="" /><button class="btn btn-primary f_r sureChoose mt15" onclick="'+html_ff+'" type="button">确认</button>');
					}
				}
			}else{
                $.showErr(ret.info);
            }
        }
    });
}
function save_data(k){
	var type_num = '';
	$("select[name='type_num[]']").each(function(){
		type_num += $(this).val()+',';
	});
	var shop_ids = '';
	$("input[name='shop_ids[]']").each(function(){
		shop_ids += $(this).val()+',';
	});
	var goods_num = '';
	$("input[name='goods_num[]']").each(function(){
		if($(this).val()){
			goods_num += $(this).val()+',';
		}else{
			goods_num += '0,';
		}
	});
	
	$("#showdiv_"+k+" input[name='type_num_arr[]']").val(type_num);
	$("#showdiv_"+k+" input[name='shop_ids_arr[]']").val(shop_ids);
	$("#showdiv_"+k+" input[name='goods_num_arr[]']").val(goods_num);
	close_shops();
}

//单独设置初始库存
function sub_save_data(){
	var querydata= new Object();
	querydata.id = goods_id;
	
	var shop_ids_arr = new Array();
	var type_num_arr = new Array();
	var goods_num_arr = new Array();
	var shop_ids = $("input[name='shop_ids[]']");
	var type_num = $("select[name='type_num[]']");
	var goods_num = $("input[name='goods_num[]']");
	for(var i=0;i<goods_num.length;i++){
		var goods_num_k = $(goods_num[i]).val();
		if(goods_num_k){
			if(goods_num_k > 0){
				shop_ids_arr[i] = $(shop_ids[i]).val();
				type_num_arr[i] = $(type_num[i]).val();
				goods_num_arr[i] = goods_num_k;
			}else{
				$.showErr('变更数量必须大于0');return false;
			}
		}
	}
    var remarks = $("input[name='remarks']").val();
	querydata.shop_ids = shop_ids_arr;
	querydata.type_num = type_num_arr;
	querydata.goods_num = goods_num_arr;
    querydata.remarks = remarks;

	$.ajax({
        type: "POST",
        url: APP + "Shop/Goods/sub_shops_stock",
        data: querydata,
        dataType: "JSON",
        success: function(ret){
            if(ret.status == 1){
                $.showSuccess('门店库存设置成功！');location.reload();
			}else{
                $.showErr(ret.msg);
            }
        }
    });
}

//货品单独设置初始库存
function sub_save_data_attr(attr_key){
	var querydata= new Object();
	querydata.id = goods_id;
	querydata.is_attr = 1;
	querydata.attr_key = attr_key;
	
	var shop_ids_arr = new Array();
	var type_num_arr = new Array();
	var goods_num_arr = new Array();
	var shop_ids = $("input[name='shop_ids[]']");
	var type_num = $("select[name='type_num[]']");
	var goods_num = $("input[name='goods_num[]']");
	for(var i=0;i<goods_num.length;i++){
		var goods_num_k = $(goods_num[i]).val();
		if(goods_num_k){
			if(goods_num_k > 0){
				shop_ids_arr[i] = $(shop_ids[i]).val();
				type_num_arr[i] = $(type_num[i]).val();
				goods_num_arr[i] = goods_num_k;
			}else{
				$.showErr('变更数量必须大于0');return false;
			}
		}
	}
    var remarks = $("input[name='remarks']").val();
	querydata.shop_ids = shop_ids_arr;
	querydata.type_num = type_num_arr;
	querydata.goods_num = goods_num_arr;
	querydata.remarks = remarks;
	// alert(querydata.remarks);return false;
	$.ajax({
        type: "POST",
        url: APP + "Shop/Goods/sub_shops_stock",
        data: querydata,
        dataType: "JSON",
        success: function(ret){
            if(ret.status == 1){
                $.showSuccess('门店库存设置成功！');location.reload();
			}else{
                $.showErr(ret.msg);
            }
        }
    });
}
