@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="#">Operation</a></li>
            <li><a href="#">Manage Edit House</a></li>
            <li><a href="#">Create</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 Edit House</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' => 'edit_house.store','method'=>'POST', 'enctype'=>'multipart/form-data')) !!}
                <div class="row">
                    <div class="form-group col-sm-4 col-xs-12">
                        <label class="required">Client</label>
                        {!! Form::select('client_id', $clients,null, array('class' => 'form-control select2','required','id'=>'client-id')) !!}
                    </div>
                    <div class="form-group col-sm-4 col-xs-12">
                        <label class="required">Project</label>
                        {!! Form::select('project_id', $projects,null, array('class' => 'form-control select2','required','id'=>'project-id')) !!}
                    </div>
                    <div class="form-group col-sm-4 col-xs-12">
                        <label class="required">Item</label>
                        {!! Form::select('sale_id', [],null, array('class' => 'form-control select2','required','id'=>'item-loan')) !!}
                    </div>
                    <div class="form-group col-sm-4 col-xs-12">
                        <label class="required">Contract Number</label>
                        {!! Form::text('contract_number', null, array('placeholder' => 'Contract Number','class' => 'form-control','required')) !!}
                    </div>
                    <div class="form-group col-sm-4 col-xs-12">
                        <label class="required">Constract Date</label>
                        {!! Form::text('contract_date', null, array('placeholder'=>'Contract Date','class' => 'form-control date_picker','required')) !!}
                    </div>
                    <div class="form-group col-sm-4 col-xs-12">
                        <label class="required">Date</label>
                        {!! Form::text('date', date('d-m-Y'), array('laceholder'=>'Date','class' => 'form-control date_picker','required')) !!}
                    </div>
                </div>
                <div id="bwi-append">
                    <br/>
                    <div class="row contract-constructor" style="padding: 15px;">
                        <table class="table table-striped table-bordered" width="100%">
                            <thead>
                                <tr>
                                    <th>
                                        ល.រ
                                    </th>
                                    <th>
                                        <label class="required">
                                            បរិយាយនៃការស្នើសុំ
                                        </label>
                                    </th>
                                    <th>
                                        <label class="required">
                                            ខ្នាត
                                        </label>
                                    </th>
                                    <th>
                                        <label class="required">
                                            បរិមាណ
                                        </label>
                                    </th>
                                    <th>
                                        <label class="required">
                                            តម្លៃ
                                        </label>
                                    </th>
                                    <th>
                                        <span class="pull-right button_primary btn-sm plus_ens fa fa-plus" onclick="add_new_request(this)"></span>
                                    </th>
                                </tr>
                            </thead>
                            <tbody class="tb_building_work">
                                <tr>
                                    <td>1</td>
                                    <td>
                                        {!! Form::textarea('description[]', null, array('placeholder' => 'បរិយាយ','class' => 'form-control','rows'=>'3','required')) !!}
                                    </td>
                                    <td>
                                        {!! Form::text('unit[]', null,array('placeholder' => 'ខ្នាត','class' => 'form-control','required')) !!}
                                    </td>
                                    <td>
                                        {!! Form::text('qty[]', null, array('placeholder' => 'បរិមាណ','class' => 'form-control number-only','required')) !!}
                                    </td>
                                    <td>
                                        {!! Form::text('fee_price[]', null, array('placeholder' => 'តម្លៃ','class' => 'form-control number-only','required')) !!}
                                    </td>
                                    <td>
                                        <button class="btn btn-sm btn-danger" disabled>
                                        <span class="fa fa-trash"></span></button>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-12 col-xs-12">
                        <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>
                </div>
                <br/>

                {!! Form::close() !!}
            </div>
        </div>
    </div>
</div>
@endsection @section('js')
<script>
    $(document).on('change','#project-id',function()
    {
        var client_id = $("#client-id").val();
        var project_id = $(this).val();
        
        var route = "{{ route('edit_house.get_sale_info') }}";
        if(client_id=="") {
            $.alert({
                title: '',
                type: 'green ',
                content :'Please Select Client before choosing Project !',
                useBootstrap: false,
                boxWidth: '20%',
                theme: 'modern',
                buttons: {
                    close: {
                        text: 'OK',
                        btnClass: 'button_warning',
                        action:function(){
                            window.location.reload();
                        }
                    }
                }
            });
        }

        $("#main-spinner").show();
        $.ajax({
            url:route,
            type:"POST",
            dataType:"JSON",
            data:{
                "_token":"{{ csrf_token() }}",
                client_id:client_id,
                project_id:project_id
            },
            success:function(data)
            {
                var option = `<option value="">Please Select</option>`;
                $.each(data,function(index,value){
                    option += `<option value="${value.sale_id}">${value.item_names}</option>`;
                });
                $("#item-loan").html(option);
            },
            complete:function()
            {
                $("#main-spinner").hide();
            }
        });
    });
    function remove(obj)
    {
        var index = 0;
        $(obj).closest('tr').remove();
        var tr_class =  $("tbody.tb_building_work").find('tr');
        $(tr_class).each(function(){
            $(this).find('td:eq(0)').html(`<span>${++index}</span>`);
        });
    }
    function add_new_request(obj)
    {
        var tr = `
                            <tr>
                                <td>${$("tbody.tb_building_work").find('tr').length+1}</td>
                                <td>
                                    {!! Form::textarea('description[]', null, array('placeholder' => 'បរិយាយ','class' => 'form-control','rows'=>'3','required')) !!}
                                </td>
                                <td>
                                    {!! Form::text('unit[]',null,array('placeholder' => 'ខ្នាត','class' => 'form-control','required')) !!}
                                </td>
                                <td>
                                    {!! Form::text('qty[]', null, array('placeholder' => 'បរិមាណ','class' => 'form-control number-only','required')) !!}
                                </td>
                                <td>
                                    {!! Form::text('fee_price[]', null, array('placeholder' => 'តម្លៃ','class' => 'form-control number-only','required')) !!}
                                </td>
                                <td>
                                    <button class="btn btn-sm btn-danger" onclick="remove(this)">
                                    <span class="fa fa-trash"></span></button>
                                </td>
                            </tr>
        `;
        $(obj).closest('table').find('tbody').append(tr);
        
    }
</script>
@endsection