

$("#select_allb").change(function(){
	$(".checkbox").prop('checked', $(this).prop("checked"));
});
$('#select_allb').change(function(){
	if(false == $(this).prop("checked")){
		$(".checkbox").prop('checked', false);
	}
	if ($('.checkbox:checked').length == $('.checkbox').length ){
		$(".checkbox").prop('checked', true);
	}
});
$('.change_group_house_code').change(function(){
    $('.group_id').empty();$(".zone_id").empty();
    var project_code = $("select[name='item_projectid']").val();
    var category_code = $("select[name='item_categoryid']").val();

    $.ajax({
        type: 'GET',
        url: '/realty/items-get-group-house-code',
        dataType:'json',
        data: {
            'project_code': project_code,
            'category_code': category_code
        },
        success: function(data){

            $('.group_id').append($('<option>').text("please select").attr('value',""));
            $.each(data['item_group'], function(index, value) {
                $('.group_id').append($('<option>').text(value.item_group_name).attr('value', value.id));

            });
            $(".zone_id").append($('<option>').text("please select").attr("value",""));
            $.each(data['item_zone'],function(index, el) {
            	$('.zone_id').append($('<option>').text(el.item_zone_name).attr("value",el.id));
            });

        }
    });

});
$('.change_prefix_code').change(function(){

	var project_code = $("select[name='item_projectid']").val();
	var category_code = $("select[name='item_categoryid']").val();
	$.ajax({
        type: 'GET',
        url: '/realty/items-prefix-code',
        dataType:'json',
        data: {
			'project_code': project_code,
			'category_code': category_code
		},
        success: function (data) {
			var prefix_project = data.project[0].project_code;
			var prefix_category = data.category[0].category_code;
			var  prefix_append = prefix_project +''+ prefix_category+'-';
			$("input[name='item_name']").val(prefix_append);
			$('.code_start_from').val(prefix_append);
			prefix_code_start();
        }
    });
     
});

function prefix_code_start()
{
	var project_code = $("select[name='item_projectid']").val();
	var category_code = $("select[name='item_categoryid']").val();
	var code_start = $('.code_start_from').val();
	$.ajax({
        type: 'GET',
        url: '/realty/items-prefix-code-start',
        dataType:'json',
        data: {
			'project_code': project_code,
			'category_code': category_code
		},
        success: function (data) {

        	if(data.items_count == ''){
        		$('.code_start_from').val(code_start+1);
        		$('.price').val('');
				$('.sale_price').val('');
				$('.width_house').val('');
				$('.length_house').val('');
				$('.size_house').val('');
				$('.width_land').val('');
				$('.length_land').val('');
				$('.size_land').val('');
				$('.item_title').val('');
				$('.item_bedroom').val('');
				$('.item_bathroom').val('');
				$('.item_sq_fp').val('');
				$('.item_parking').val('');
				$('.item_floor').val('');
				$('.item_develop_by').val('');
				$('.item_completed_year').val('');
				$('.item_des').val('');
				$('.item_bordering_north').val('');
				$('.item_bordering_south').val('');
				$('.item_bordering_west').val('');
				$('.item_bordering_east').val('');
				$('.commission_con').val('');
				$('.commission_amount_con').val('');
        	}else{
        		$('.code_start_from').val(code_start+(parseInt(data.items_count)+1));
				$('.price').val(data.item_price);
				$('.sale_price').val(data.item_cost);
                $('.width_house').val(data.item_width_house);
                $('.length_house').val(data.item_length_house);
				$('.size_house').val(data.item_size_house);
                $('.width_land').val(data.item_width_land);
                $('.length_land').val(data.item_length_land);
				$('.size_land').val(data.item_size_land);
				$('.item_title').val(data.item_title);
				$('.item_bedroom').val(data.item_bedroom);
				$('.item_bathroom').val(data.item_bathroom);
				$('.item_sq_fp').val(data.item_sq_fp);
				$('.item_parking').val(data.item_parking);
				$('.item_floor').val(data.item_floor);
				$('.item_develop_by').val(data.item_develop_by);
				$('.item_completed_year').val(data.item_completed_year);
                $('.item_bordering_north').val(data.item_bordering_north);
                $('.item_bordering_south').val(data.item_bordering_south);
                $('.item_bordering_west').val(data.item_bordering_west);
                $('.item_bordering_east').val(data.item_bordering_east);
                $('.commission_con').val(data.commission_percentage);
                $('.commission_amount_con').val(data.commission_amount);
        	}
        }
    });
}
function generate(i,code,price,sale_price,comm_amount,comm_rate,comm_con,size_house,size_land,prefix_house,item_house,house_no,item_group_select,item_group_option,start_house_num_type){
		var option = ``;
		$.each(item_group_option,function(index,value){
			if(value.id==item_group_select){
				option += `<option value="${value.id}" selected>${value.item_group_name}</option>`;
			}
			else{
				option += `<option value="${value.id}">${value.item_group_name}</option>`;
			}
		});
		var comm_ag='<div class="col-sm-1 col-xs-2">'+
			'<input type="text" name="commission_amount_g[]" value="'+comm_amount+'" class="effect-1 number-only">'+
		'</div>';
		var comm_rg='<div class="col-sm-1 col-xs-2">'+
			'<input type="text" name="commission_percentage_g[]" value="'+comm_rate+'" class="effect-1 number-only">'+
		'</div>';
		var comm_cg='<div class="col-sm-1 col-xs-2" style="display:none;">'+
			'<input type="text" name="commission_condition_g[]" value="'+comm_con+'" class="effect-1 number-only">'+
		'</div>';
		if(isNaN(comm_con)){comm_cg='';$("#com_co").css("display","none");}
		if(isNaN(comm_rate)){comm_rg='';$("#com_ra").css("display","none");}
		if(isNaN(comm_amount)){comm_ag='';$("#com_am").css("display","none");}
	var generate = '<div class="row remove">'+
		'<div class="col-sm-1 col-xs-2">'+ 
			'<input type="text" name="item_name[]" class="effect-1" value="'+code+i+'">'+
		'</div>'+
		'<div class="col-sm-1 col-xs-2">'+ 
			'<input type="text" name="item_house[]" class="effect-1" value="'+prefix_house+house_no+'">'+
		'</div>'+
		'<div class="col-sm-1 col-xs-2">'+
			'<input type="text" name="item_cost[]" value="'+sale_price+'" class="effect-1 number-only">'+
		'</div>'+
		'<div class="col-sm-1 col-xs-2">'+
			'<input type="text" name="item_price[]" value="'+price+'" class="effect-1 number-only">'+
		'</div>'+comm_ag+comm_rg+comm_cg+
		'<div class="col-sm-1 col-xs-2">'+
			'<input type="text" name="size_house[]" value="'+size_house+'" class="effect-1">'+
		'</div>'+
		'<div class="col-sm-1 col-xs-2">'+
			'<input type="text" name="size_land[]" value="'+size_land+'" class="effect-1">'+
		'</div>'+
		'<div class="col-sm-1 col-xs-2">'+
        	'<input type="text" name="title_number[]" value="" class="effect-1">'+
		'</div>'+
		'<div class="col-sm-1 col-xs-2">'+
        	'<select name="group_item[]">'+option+'</select>'+
		'</div>'+
		'<div class="col-sm-1 col-xs-2">'+
			'<select name="available[]" class="form-control chosen-select">'+
				'<option value="1">YES</option>'+
				'<option value="0">NO</option>'+
			'</select>'+
		'</div>'+
		'<div class="col-sm-1 col-xs-1">'+
			'<span class="badge badge-danger btn_remove"><i class="fa fa-minus"></i></span>'+
		'</div>'+
		'</div>';
	return generate;

}
