@extends('adminlte::page') @section('content')
<?php Fun::lang() ?>
<div class="title-page">
    <div class="page-bar">
        <ul class="page-breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> Dashboard</a></li>
            <li><a href="#">Manage Stock</a></li>
            <li><a href="#">Create Item</a></li>
        </ul>
    </div>
</div>

<div class="page">
    <div class="top-content">
        <div class="row">
            <div class="col-sm-12 col-xs-12">
                <div class="title-page">
                    <div class="pull-left">
                        <h3>Create Item (Lands)</h3>
                    </div>
                </div>

            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-12 col-xs-12">

            <div class="form-wrapper">
                {!! Form::open(array('route' => 'items-land.store','method'=>'POST','enctype'=>'multipart/form-data')) !!}
                <div class="row">
                    <div class="form-group col-sm-3 col-xs-12">

                        <label class="required">Project</label>
                        {!! Form::select('item_projectid', $projects, null, ['class' => 'form-control project_id select2 change_prefix_code change_group_code','required']) !!}

                    </div>
                    <div class="form-group col-sm-3 col-xs-12">

                        <label class="required">Category</label>
                        {!! Form::select('item_categoryid', $categories, null, ['class' => 'form-control category_id select2 change_prefix_code change_group_code','required']) !!}

                    </div>
                    <div class="form-group col-sm-3 col-xs-12">

                        <label class="required">Group Item</label>
                        {!! Form::select('item_groupid', $item_group, null, ['class' => 'form-control group_id select2 ','required']) !!}

                    </div>
                    <div class="form-group col-sm-3 col-xs-12">

                        <label>Item Zone</label>
                        {!! Form::select('item_zone_id', $item_zone, null, ['class' => 'form-control zone_id select2 change_zone_item']) !!}

                    </div>
                    {!! Form::hidden('item_name', null, array('placeholder' => 'Prefix Code','class' => 'form-control profix_code','id'=>'profix_code')) !!}
                    <div class="form-group col-sm-3 col-xs-12">

                        <label class="required">Prefix Code Start From</label>
                        {!! Form::text('code_start_from', null, array('placeholder' => 'Prefix Code Start From','class' => 'form-control code_start_from','readonly')) !!}
                    </div>
                    <div class="form-group col-sm-3 col-xs-12">

                        <label class="required">Title</label>
                        {!! Form::text('item_title', null, array('placeholder' => 'Title','class' => 'form-control item_title','required')) !!}

                    </div>
                    <div class="form-group col-sm-3 col-xs-12">

                        <label>Item Cost</label>
                        {!! Form::text('item_cost', null, array('placeholder' => 'Item Cost','class' => 'form-control sale_price number-only','required')) !!}

                    </div>


                    <div class="form-group col-sm-3 col-xs-12">

                        <label>Width</label>
                        {!! Form::text('item_width_land', null, array('placeholder' => 'Width','class' => 'form-control width_land','id'=>'width_land')) !!}

                    </div>
                    <div class="form-group col-sm-3 col-xs-12">

                        <label>Length</label>
                        {!! Form::text('item_length_land', null, array('placeholder' => 'Length','class' => 'form-control length_land','id'=>'length_land')) !!}

                    </div>
                    <div class="form-group col-sm-3 col-xs-12">

                        <label class="required">Size</label>
                        {!! Form::text('item_size_land', null, array('placeholder' => 'Size','class' => 'form-control size_land','required','id'=>'size_land')) !!}

                    </div>

                    <div class="form-group col-sm-3 col-xs-12">

                        <label class="required">Sale Price</label>
                        {!! Form::text('item_price', null, array('placeholder' => 'Sale Price','class' => 'form-control price number-only','required')) !!}

                    </div>

                    <div class="form-group col-sm-3 col-xs-12">

                        <label class="">Commission type</label>
                        {!! Form::select('commission_type', array('1'=>'Amount($)','2'=>" Percentage(%)"), null, ['class' => 'form-control comm_type select2','']) !!}

                    </div>
                    <div class="form-group col-sm-3 col-xs-12 commission_amount_con">

                        <label class="commission_amount_con">Commission Amount($)</label>
                        {!! Form::text('commission_amount', 0, array('placeholder' => 'commission amount','class' => 'form-control commission_amount commission_amount_con number-only','')) !!}

                    </div>
                    <div class="form-group col-sm-3 col-xs-12 commission_con">

                        <label class="commission_con">Commission(%)(/sale price)</label>
                        {!! Form::text('commission_percentage', 0, array('placeholder' => 'commission percentage','class' => 'form-control commission commission_con number-only','')) !!}

                    </div>
                    {{--<div class="form-group col-sm-3 col-xs-12">--}}

                        {{--<label class="">Commission Condition</label>--}}
                        {{--{!! Form::text('commission_condition', 0, array('placeholder' => 'commission condition','class' => 'form-control commission_condition number-only','')) !!}--}}

                    {{--</div>--}}
                    <div class="form-group col-sm-3 col-xs-12">

                        <label class="required">Develop By</label>
                        {!! Form::text('item_develop_by', null, array('placeholder' => 'Develop By','class' => 'form-control item_develop_by')) !!}

                    </div>
                    <div class="form-group col-sm-3 col-xs-12">

                        <label class="required">Block Time</label>
                        {!! Form::text('item_block_time', null, array('placeholder' => 'Block Time : 4...','class' => 'form-control number-only')) !!}

                    </div>
                    <div class="form-group col-sm-3 col-xs-12">

                        <label class="required">Count Item</label>
                        {!! Form::text('items_count', null, array('placeholder' => 'Count Item : 1','class' => 'form-control number-only','id'=>'item_count','required')) !!}

                    </div>
                    <div class="form-group col-sm-3 col-xs-12">

                        <label class="required">Type</label>
                        {{ Form::select('item_type', ['Sales' => 'Sales', 'Rent' => 'Rent'],null, ['class' => 'form-control select2']) }}

                    </div>

                    <div class="form-group col-sm-3 col-xs-12">

                        <label class="">House Bordering North</label>
                        {!! Form::text('item_bordering_north', null, array('placeholder' => 'House Bordering North','class' => 'form-control item_bordering_north','')) !!}

                    </div>
                    <div class="form-group col-sm-3 col-xs-12">

                        <label class="">House Bordering South</label>
                        {!! Form::text('item_bordering_south', null, array('placeholder' => 'House Bordering South','class' => 'form-control item_bordering_south','')) !!}

                    </div>
                    <div class="form-group col-sm-3 col-xs-12">

                        <label class="">House Bordering West</label>
                        {!! Form::text('item_bordering_west', null, array('placeholder' => 'House Bordering West','class' => 'form-control item_bordering_west','')) !!}

                    </div>
                    <div class="form-group col-sm-3 col-xs-12">

                        <label class="">House Bordering East</label>
                        {!! Form::text('item_bordering_east', null, array('placeholder' => 'House Bordering East','class' => 'form-control item_bordering_east','')) !!}

                    </div>

                    <div class="form-group col-sm-3 col-xs-12">

                        <label class="required">Prefix Land No </label><span class="text-danger">(A letter placed before another.)</span>
                        {!! Form::text('prefix_house', null, array('placeholder' => 'House Prefix Number','class' => 'form-control prefix_house','readonly')) !!}

                    </div>

                    <div class="form-group col-sm-3 col-xs-12">

                        <label class="required">Start Land No </label><span class="text-danger">(Odd or Even)</span>
                        {!! Form::text('item_house', null, array('placeholder' => 'Start Land Number','class' => 'form-control item_house number-only')) !!}

                    </div>

                    <div class="form-group col-sm-3 col-xs-12">

                        <label class="required">Number of Item </label><span class="text-danger">(we are allow create small than 161 Items)</span>
                        {!! Form::text('number_of_properties', null, array('placeholder' => 'Number of item','class' => 'form-control number_of_properties number-only')) !!}

                    </div>

                    <!--<div class="col-sm-12 col-xs-12">
                        <strong for="">Image (525x300px)</strong>
                        <p class="text-danger">Maximum width and height to upload is 525x300 pixcel</p>
                        <div class="row">
                            @for($i=0; $i
                            <8; $i++)
                                <div class="col-sm-1">
                                    <a href="#" class="thumbnail">
                                        <input class="file-upload-image" name="image{{$i}}" type="file" id="A{{$i}}">
                                        <span class="close" id="A{{$i}}">x</span>
                                        <img src="{{asset('assets/images/default.png')}}" id="A{{$i}}" class="img-thumbnail img-responsive" width="100%" alt="loading...">
                                        <?php echo '<small style="color:red">' . $errors->first('image' . $i) . '</small>'; ?>
                                    </a>
                                </div>
                            @endfor
                        </div>
                    </div>-->

                <!--<div class="form-group col-sm-2 col-xs-12">
                    <div class="form-group">
                        <label for="required" class="required">Slide Image </label>
                        <p class="text-danger">Maximum width and height to upload is 1920x680 pixcel</p>
                        <a href="#" class="thumbnail">
                            <input class="file-upload-image" name="item_slide" type="file" id="A">
                            <span class="close" id="A">x</span>
                            <img src="{{asset('assets/images/default.png')}}" id="A" class="img-thumbnail img-responsive" width="100%" alt="loading...">
                            <?php echo '<small style="color:red">' . $errors->first('item_slide') . '</small>'; ?>
                        </a>
                    </div>
                </div>-->

                <div class="col-sm-12 form-group">

                    <label for="required">Description</label>
                    {!! Form::textarea('item_des', null, array('placeholder' => 'Description','class' => 'form-control summernote','rows'=>'2')) !!}

                </div>

                <div class="col-sm-12 col-xs-12 form-group">
                    <div class="form-group">
                        <input type="button" value="Generate" class="button_warning pull-right" onclick="generate()">
                    </div>
                </div>
                <div class="col-sm-12 col-xs-12 generate_item">
                    <div class="row">
                        <div class="col-sm-1">
                            <label>Item Code</label>
                        </div>
                        <div class="col-sm-1">
                            <label>Land No</label>
                        </div>
                        <div class="col-sm-1">
                            <label>Item Cost</label>
                        </div>
                        <div class="col-sm-1">
                            <label>Sale Price</label>
                        </div>
                         <div class="col-sm-1" id="com_am">
                            <label>commission($)</label>
                        </div>
                        <div class="col-sm-1" id="com_ra">
                            <label>commission(%)</label>
                        </div>
                        <div class="col-sm-1" id="com_co">
                            <label>condition</label>
                        </div>
                        <div class="col-sm-1">
                            <label>Size</label>
                        </div>
                        <div class="col-sm-1">
                            <label>Item Count</label>
                        </div>
                        <div class="col-sm-1">
                            <label>Available</label>
                        </div>
                        <div class="col-sm-1">
                            <label>Remove</label>
                        </div>
                    </div>
                </div>
            </div>

            {!! Form::close() !!}
        </div>
    </div>
</div>
</div>
@endsection
@section('js')
{{--<script type="text/javascript" src="{{asset('assets/controller/items.js')}}"></script>--}}
<script type="text/javascript">
    $(document).ready(function(){
        $(".commission_con").hide();
        $("#length_land").keyup(function(){
            var length_land = Number($(this).val());
            var width_land = Number($('#width_land').val());
            var total=width_land*length_land;
            $('#size_land').val(total);

        });
        $("#width_land").keyup(function(){
            var width_land = Number($(this).val());
            var length_land = Number($('#length_land').val());
            var total=width_land*length_land;
            $('#size_land').val(total);

        });
    });
        $(document).on("change",".comm_type",function(){
            var type=1;
            type=$(this).val();
            if(type==1){
                $(".commission_amount_con").show();
                $(".commission_con").hide();
            }
            else{
                $(".commission_amount_con").hide();
                $(".commission_con").show();
            }
        });

    $(document).on("keyup",".commission_amount",function(){
        $(".commission").val(0);
    });
    $(document).on("keyup",".commission",function(){
        $(".commission_amount").val(0);
    });
    $('.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();
            }
        });

    });
    $('.change_group_code').change(function(){
        $('.group_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-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, function(index, value) {
                    $('.group_id').append($('<option>').text(value.item_group_name).attr('value', value.id));

                });

            }
        });

    });
    $('.change_zone_item').change(function(){
        var zone_id = $("select[name='item_zone_id']").val();
        $.ajax({
            type: 'GET',
            url: '/realty/items-get-zone-item-name',
            dataType:'json',
            data: {
                'zone_id': zone_id
            },
            success: function(data){
                $('.prefix_house').val(data.item_zone_name);
            }
        });

    });
    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('');
                    $('.commission_condition').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);
                    $('.commission_condition').val(data.condition);
                    $('.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);
                }
            }
        });
    }
</script>
<script>
    function generate() {
        $('.button').remove();
	$('.remove').remove();
	if($('.price').val() == ''){
		$('.price').focus();
	}else if($('.size').val() == ''){
		$('.size').focus();
	}else if($('.sale_price').val() == ''){
		$('.sale_price').focus();
	}else if($('.profix_code').val() == ''){
		$('.profix_code').focus();
	}else if($('.number_of_properties').val() == ''){
		$('.number_of_properties').focus()
	}else{
		var num_of_pro 		= $('.number_of_properties').val();
		var profix_code 	= $('.profix_code').val();
		var price 			= $('.price').val();
		var sale_price 		= $('.sale_price').val();
		var size 			= $('.size_land').val();
		var code_start_from = $('.code_start_from').val();
		var length_code 	= profix_code.length;
		var code_from 		= code_start_from.substring(length_code);
		var result 			= parseInt(code_from);
		var num_of_item 	= (parseInt(num_of_pro)+result);
		var prefix_house	= $('.prefix_house').val();
		var item_house  	= $('.item_house').val();
		var item_street 	= $('.item_street').val();
		var house_no    	= parseInt(item_house);
        var comm_amount=parseFloat($(".commission_amount").val());
        var comm_rate=parseFloat($(".commission").val());
        var comm_con=parseFloat($(".commission_condition").val());
        var count_item = parseInt(!$('#item_count').val()?1:$('#item_count').val());
		for (result; result < num_of_item ; result++) {
			var fun_generate = generate_data(result,profix_code,price,sale_price,comm_amount,comm_rate,comm_con,size,prefix_house,item_house,house_no);
			house_no += 1;
			$('.generate_item').append(fun_generate);

		}
		$('.generate_item').delegate('.btn_remove', 'click', function () {
			$(this).parent().parent().remove();
		});
		var button = `<div class="col-sm-12 col-xs-12 button">
							<div class="form-group">
								<button type="submit" class="button_sm button_primary pull-right"><span class="fa fa-save"></span> Save</button>
                                <button type="button"  onClick="javascript:history.go(-1)" class="button_sm button_warning pull-right">
                                        <span class=" fa fa-arrow-circle-left"></span> Back</button>
							</div>
						</div>`;
		$('.generate_item').append(button);
	}
    }

    function generate_data(i,code,price,sale_price,comm_amount,comm_rate,comm_con,size,prefix_house,item_house,house_no){
        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">'+
            '<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="item_size[]" value="'+size+'" class="effect-1">'+
		'</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;

}
</script>

<style>
    .form-group {
        margin-bottom: 32px;
    }
</style>
@endsection
