 @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 Product</a></li>
            <li><a href="#">Item</a></li>
            <li><a href="#">Edit Multiple 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="pull-left">
                     <h3>Edit Multiple Item</h3>
                 </div>
                 <div class="pull-right">
                     <a class="button_sm button_primary" href="{{ route('items.index') }}"><span class="fa fa-reply"></span> Back</a>
                 </div>
             </div>
         </div>
     </div>
     <div class="row">
         <div class="col-sm-12 col-xs-12">
             <div class="form-wrapper">
                 {!! Form::open(array('route' => 'items.update-multiple','method'=>'POST','enctype'=>'multipart/form-data')) !!}
                 <div class="row">
                     <div class="form-group col-sm-4 col-xs-12">
                         <label class="required">Project</label>
                         {!! Form::select('item_projectid', $projects, null, ['class' => 'form-control project_id select2 change_group_house_code append-item selectBox','id'=>'project_id']) !!}
                     </div>
                     <div class="form-group col-sm-4 col-xs-12">
                         <label class="required">Category</label>
                         {!! Form::select('item_categoryid', $categories, null, ['class' => 'form-control category_id select2 change_group_house_code append-item selectBox','id'=>'category_id']) !!}
                     </div>
                     <div class="form-group col-sm-4 col-xs-12">
                         <label>Group Item</label>
                         {!! Form::select('item_groupid', $item_group, null, ['class' => 'form-control group_id project_id_comm select2 selectBox append-item','id'=>'group_id']) !!}
                     </div>
                     <div class="form-group col-sm-4 col-xs-12">
                         <label class="">Item Zone</label>
                         {!! Form::select('item_zone_id', $item_zone, null, ['class' => 'form-control zone_id select2 project_id_comm selectBox append-item','required','id'=>'zone_id']) !!}
                     </div>
                     <div class="form-group col-sm-4 col-xs-12">
                         <label class="">item</label>
                         {!! Form::select('item_name[]', $items, null, array('class' => 'form-control item_id input-sm select2 append-item','id'=>'item_id','multiple'=>'multiple', 'autocomplete' => 'off','' )) !!}
                     </div>
                     <div class="form-group col-sm-4 col-xs-12">
                         <label class="required">Amount $</label>
                         {!! Form::text('raise_amount', null, array('placeholder' => 'Increase amount','class' => 'form-control sale_price number-only','id'=>'new_price')) !!}
                     </div>
                     <div class="form-group col-sm-12 col-xs-12">
                         <label for="required">Remark</label>
                         {!! Form::textarea('remark', null, array('placeholder' => 'Typing note for this action...','class' => 'form-control summernote','rows'=>'2')) !!}
                     </div>
                     {{-- <div class="form-group col-sm-12 col-xs-12">
                        <button type="button" class="selectBox btn btn-warning btn-sm fa fa-arrow-right pull-right"> Generate</button>
                     </div> --}}
                 </div>
                 <table id="datatable_item_price" class="table table-striped table-bordered table-hover nowrap">
                     <thead>
                         <tr>
                             <th style="width: 60px !important;">NO</th>
                             <th>Project Name</th>
                             <th>Category Name</th>
                             <th style="width: 200px !important;">Group</th>
                             <th style="width: 150px !important;">Block</th>
                             <th>Item Name</th>
                             <th>Real Estate Number</th>
                             <th>Old Price($)</th>
                             <th>Price Update</th>
                         </tr>
                     </thead>
                     <tbody class="tbody">

                     </tbody>
                 </table>
                 <div class="row">
                     <div class="col-sm-12 col-xs-12">
                         <div class="form-group">
                             <button type="submit" class="button_primary pull-right"><i class="fa fa-save"></i> Submit</button>
                         </div>
                     </div>
                 </div>
                 {!! Form::close() !!}
             </div>
             <br/>
         </div>
         
     </div>
 </div>
 </div>
 @endsection
 @section('js')
 <script type="text/javascript">
     $(document).on("keyup","#new_price",function()
     {
         $(".tbody").find("tr").find("td:eq(8)").find('input').val($(this).val());
     });
     $(document).ready(function() {
         $(".comm_amount").attr("readonly", "true");
         $(".comm_condition").attr("readonly", "true");
         $(".comm_rate").attr("readonly", "true");
     });
     $(document).on('change', '.selectBox', function() {
         var projectID = $('#project_id').find(':selected').val();
         var categoryID = $('#category_id').find(':selected').val();
         var zoneID = $('#zone_id').find(':selected').val();
         var groupID = $('#group_id').find(':selected').val();
         var newAmount = $('#new_price').val();
         var id = $(this).attr('id');
         var tr = '';

         if (projectID != "" && categoryID != "") {
             $.ajax({
                 type: "POST",
                 dataType: "JSON",
                 url: "{{route('filter-item-properties')}}",
                 data: {
                     projectID: projectID,
                     categoryID: categoryID,
                     groupID: groupID,
                     zoneID: zoneID,
                     _token: "{{csrf_token()}}"
                 },
                 success: function(response) {
                     var zone = response['zone'];
                     var group = response['group'];
                     var item = response['item'];
                     if (id != "zone_id" && id != "group_id") {
                         $('#zone_id,#group_id').empty().append(`<option value="">Please Select</option>`);
                         $.each(zone, function(i, z) {
                             $('#zone_id').append(`<option value="${z.id}">${z.item_zone_name}</option>`);
                         });
                         $.each(group, function(i, g) {
                             $('#group_id').append(`<option value="${g.id}">${g.item_group_name}</option>`);
                         });
                     }
                     $('#item_id').empty();
                     $.each(item, function(i, it) {
                         $('#item_id').append(`<option value="${it.id}">${it.item_house}</option>`);
                     });
                 }
             });
         }
     });
     $(document).on('change', '.append-item', function() {
         var projectID = $('#project_id').find(':selected').val();
         var categoryID = $('#category_id').find(':selected').val();
         var zoneID = $('#zone_id').find(':selected').val();
         var groupID = $('#group_id').find(':selected').val();
         var newAmount = $('#new_price').val();
         var itemID = $('#item_id').val();
         var id = $(this).attr('id');
         var tr = '';
         if (projectID == "" && categoryID == "") {
             alert("Please Select Any Required Fields");
         } else {
             $.ajax({
                 type: "POST",
                 dataType: "JSON",
                 url: "{{route('filter-item-properties')}}",
                 data: {
                     projectID: projectID,
                     categoryID: categoryID,
                     groupID: groupID,
                     zoneID: zoneID,
                     itemID: itemID,
                     _token: "{{csrf_token()}}"
                 },
                 success: function(response) {
                     var zone = response['zone'];
                     var group = response['group'];
                     var item = response['item'];
                     $.each(item, function(i, it) {

                        var opt_zone = `<option value = "" >Please Select</option>`;

                        var opt_group = `<option value = "" >Please Select</option>`;

                        $.each(zone,function(index,value)
                        {
                            if(value.id==it.item_zone_id)
                            {
                                opt_zone += `
                                    <option selected value = "${value.id}">
                                        ${value.item_zone_name}
                                    </option>`;
                            }
                            else{
                                opt_zone += `
                                    <option value = "${value.id}">
                                        ${value.item_zone_name}
                                    </option>`;
                            }
                        });
                        $.each(group,function(index,value){
                            
                            if(value.id == it.item_groupid)
                            {
                                opt_group += `
                                        <option selected value = "${value.id}">
                                            ${value.item_group_name}
                                        </option>`;
                            }else{
                                opt_group += `
                                        <option value = "${value.id}">
                                            ${value.item_group_name}
                                        </option>`;
                            }
                        });

                         tr += `<tr>
                                    <td><input class="form-control" type="text" value="${(i+1)}" readonly></td>
                                    <td><input class="form-control" type="text" value="${it.project_name}" readonly></td>
                                    <td><input class="form-control" type="text" value="${it.category_name}" readonly></td>
                                    <td>
                                        <select name = "item_group_new[]" class = "form-control select2" required>
                                            ${opt_group}
                                        </select>
                                    </td>
                                    <td>
                                        <select name = "item_zone_new[]" class = "form-control select2" required>
                                            ${opt_zone}
                                        </select>
                                    </td>
                                    <td>
                                        <input type="hidden" name="item_id[]"   value="${it.id}">
                                        <input name ="item_house_new[]" class="form-control" type="text" value="${it.item_house}">
                                    </td>
                                    <td>
                                        <input class="form-control" name="item_title_number_new[]" type="text" value="${it.item_title_number==null?"":it.item_title_number}">
                                    </td>
                                    <td>
                                        <input class="form-control" type="text" value="${it.item_price}" readonly>
                                    </td>
                                    <td><input class="form-control number-only" type="text" value="${newAmount}" name="new_price[]"></td>
                                </tr>`;
                     });
                     $('.tbody').empty().append(tr);
                     $(".select2").select2();
                 }
             });
         }
     });
 </script>
 @endsection