@extends('client.layouts.app')
@section('title', 'تعديل منتج ')
@section('content')
 <!-- BEGIN: Content-->
 <div class="app-content content ">
    <div class="content-overlay"></div>
    <div class="header-navbar-shadow"></div>
    <div class="content-wrapper container-xxl p-0">
      <div class="content-header row">
        <div class="content-header-left col-md-9 col-12 mb-2">
          <div class="row breadcrumbs-top">
            <div class="col-12">
              <h2 class="content-header-title float-start mb-0">تعديل @if($product->is_product==1) منتج @else خدمة @endif </h2>
              <div class="breadcrumb-wrapper">
                <ol class="breadcrumb">
                  <li class="breadcrumb-item"><a href="{{route('client.home')}}">لوحة التحكم</a>
                  </li>
                  <li class="breadcrumb-item"><a href="{{route('client.products.index')}}">المنتجات</a>
                  </li>
                  <li class="breadcrumb-item active">{{$product->product_name}}
                </li>
                  <li class="breadcrumb-item active">تعديل
                  </li>
                </ol>
              </div>
            </div>
          </div>
        </div>
        <div class="content-header-right text-md-end col-md-3 col-12 d-md-block d-none">
          <div class="mb-1 breadcrumb-right">
            <div class="dropdown">
              @can('قائمة المنتجات')
              <a  href="{{ route('client.products.index') }}">
                <button class="btn-icon btn btn-primary" type="button"><i data-feather='eye'> </i></button>
                </a>
                @endcan
            </div>
          </div>
        </div>
      </div>
      <div class="content-body">
        <!-- Basic Horizontal form layout section start -->
        <!-- Basic multiple Column Form section start -->
<section id="multiple-column-form">
<div class="row">
  <div class="col-12">
    <div class="card">
      <div class="card-header">
        <h4 class="card-title">البيانات الاساسية</h4>
      </div>
      <div class="card-body">

        <form class="form" id="addForm" action="#">
            <input type="hidden" name="company_id" value="{{$company_id}}">
            <input type="hidden" name="id" value="{{$product->id}}">
            <input type="hidden" name="is_product" id="is_product" value="{{$product->is_product}}">


            @csrf
            <div class="row" >


              <div class="col-xl-6 col-md-6 col-12">
                  <div class="row">

                <div class="col-md-6 col-12" >
                  <div class="mb-1">
                <b class="form-label" for="store_id"> الفئة</b>
                   <select name="category_id" id="category" class="form-control" >
                    @foreach($categories as $category)
                    <option value="{{$category->id}}"
                            @if($category->id == $product->category_id)
                            selected
                            @endif
                            type="{{$category->category_type}}">{{$category->category_name}}</option>
                   @endforeach

                </select>
                </div>
                </div>

                <div class="col-md-6 col-12 " >
                  <div class="mb-1">
                <b class="form-label" for="store_id"> المخزن</b>
                    <select   name="store_id" id="store" class="form-control">
                      <option value="">اختر المخزن</option>
                          <?php $i = 0; ?>
                          @foreach($stores as $store)
                          <option
                              @if($store->id == $product->store_id)
                              selected
                              @endif
                              value="{{$store->id}}">{{$store->store_name}}</option>
                         @endforeach
                      </select>
                    </div>
                </div>

                <div class="col-md-6 col-12 " >
                  <div class="mb-1">
                    <b class="form-label" for="store_id">اسم المنتج</b>
                    <input type="text" name="product_name" id="order_name" class="form-control" placeholder="اسم المنتج"  autofocus value="{{$product->product_name}}">

                    </div>
                </div>
                <div class="col-md-6 col-12 " >
                  <div class="mb-1">
                    <b class="form-label" for="unit_id">الوحدة الأساسية</b>
                    <select  name="unit_id" class="form-control" id="unit_id">
                    <option value="">اختر وحدة</option>
                    @foreach($units as $unit)
                    <option
                        @if($product->unit_id == $unit->id)
                        selected
                        @endif
                        value="{{$unit->id}}">{{$unit->unit_name}}</option>
                     @endforeach
                </select>
                    </div>
                </div>

            </div>
          </div>

            <div class="col-xl-6 col-md-6 col-12 " >
              <div class="row">
  
                  <div class="col-md-6 col-12">
                    <div class="mb-1">
                    <b for="balance">رصيد البداية</b>
                    <input   type="text" name="balance_start" id="balance_start" value="{{$product->balance_start}}"
                           class="form-control " placeholder="رصيد البداية" >
                    </div>
                </div>
                <div class="col-xl-6 col-md-6 col-12">
                    <div class="mb-1">
                    <b for="purchasing_price">سعر التكلفة</b>
                    <input  type="text" name="purchasing_price" id='purchasing_price' value="{{$product->purchasing_price}}"
                           class="form-control "  placeholder="سعر التكلفة" >
                    </div>
                </div>
                <div class="clearfix"></div>
                <div class="col-md-4 col-12">
                    <div class="mb-1">
                    <b for="wholesale_price">سعر الجملة</b>
                    <input  type="text" name="wholesale_price" id="wholesale_price" value="{{$product->wholesale_price}}"
                           class="form-control " placeholder="سعر الجملة" 
                           >
                    </div>
                </div>
                <div class="col-md-4 col-12">
                    <div class="mb-1">
                    <b for="sector_price">سعر القطاعى</b>
                    <input  type="text"  name="sector_price" id="sector_price" placeholder="سعر القطاعى" value="{{$product->sector_price}}"
                           class="form-control ">
                    </div>
                </div>
                <div class="col-md-4 col-12">
                    <div class="mb-1">
                    <b for="min_balance"> حد أدنى المخازن</b>
                    <input   type="text" name="min_balance" id="min_balance" placeholder="رصيد حد أدنى المخازن" value="{{$product->min_balance}}"
                           class="form-control "
                           />
                    </div>
                </div>
          </div>


            </div>
            <div class="clearfix"></div>
            <a href="javascript:;" class="btn btn-link open_extras">
                خيارات إضافية
            </a>
            <div class="extras row" style="display: none;">
                
                <div class="col-md-3 col-12">
                  <div class="mb-1">
                    <label for="model"> موديل المنتج</label>
                    <input type="text" name="product_model" value="{{$product->product_model}}" placeholder="موديل المنتج" class="form-control" id='model'>
                  </div>
                  </div>
                  
                <div class="col-md-3 col-12">
                  <div class="mb-1">
                    <label for="size"> مقاس </label>
                    <select  name="sizes[]"
                    class="form-control " id="basic" multiple
                    title="اختر مقاس">
                     @foreach($all_sizes as $size)
                    <option value="{{$size->id}}"
                            @if(in_array($size->id,$sizes))
                            selected
                        @endif
                    >
                        {{$size->size}}
                    </option>
                @endforeach
            </select>
                </div>
                </div>

                <div class="col-md-3 col-12">
                  <div class="mb-1">
                    <label for="color"> لون </label>
                    <select  name="colors[]"
                            class="form-control" id="basic"
                            multiple
                            title="اختر لون">
                            @foreach($all_colors as $color)
                            <option value="{{$color->id}}"
                                    @if(in_array($color->id,$colors))
                                    selected
                                @endif
                            >
                                {{$color->color}}
                            </option>
                        @endforeach
                    </select>
                </div>
                </div>
                <div class="col-md-3 col-12">
                  <div class="mb-1">
                  <b for="balance_start">رصيد البداية</b>
                  <input   type="text" name="balance_start" id="balance_start" value="{{$product->balance_start}}"
                         class="form-control " placeholder="رصيد البداية" >
                  </div>
              </div>
                <div class="clearfix"></div>

                <div class="col-md-3 col-12">
                  <div class="mb-1">
                    <label for="description">وصف المنتج</label>
                    <input type="text" name="description" id="description" class="form-control text-right" value="{{$product->description}}" placeholder="وصف المنتج">
                  </div>
                </div>
                <div class="col-md-3 col-12">
                  <div class="mb-1">
                    <label for=""> اختر صورة لرفعها </label>
                    <input accept=".jpg,.png,.jpeg" type="file"
                           oninput="pic.src=window.URL.createObjectURL(this.files[0])" id="file"
                           name="product_pic" class="form-control">
                  </div>
                  <div class="mb-1">
                    <label for="" class="d-block mt-2"> معاينة الصورة </label>
                    <img id="pic" src="{{asset($product->product_pic)}}"
                                     style="width: 100px; height:100px;"/>
                  </div>
                </div>
                <div class="clearfix"></div>


            </div>
            </div>
            
            <div class="clearfix"></div>

          

            <div class="col-12" align="center">
            <br>
              <button type="submit" class="btn btn-success me-1">تحديث</button>
              <button type="button" class="btn btn-outline-danger" onclick="window.history.back()">رجوع</button>
            </div>
          </div>
          
        </form>

                
                </div>
            </div>
            </div>
        </div>

        </section>
@if($product->is_product==1)
<section id="multiple-column-form">
<div class="row">
  <div class="col-12">
    <div class="card">
      <div class="card-header">
        <h4 class="card-title">الوحدات</h4>
      </div>
      <div class="card-body">

        <form class="form" id="unitsForm" action="#" method="post">
          <input class="form-control" type="hidden" name="product_id" value="{{$product->id}}">

            @csrf
            <div class="row" >
              <div class="table-responsive">

              <table class="table table-striped">
                <thead>
                  <tr>
                    <th>الوحدة</th>
                    <th>الكمية بالنسبة للوحدة الأساسية </th>
                    <th>السعر</th>
                    <th>الكود العالمى</th>
                  </tr>
                </thead>
                <tbody>
                  @foreach($product_units as $unit)
                  <input type="hidden" name="secondary_unit_id[]" value="{{$unit->secondary_unit_id}}" >
                  <tr>
                    <td>{{$unit->secondary_unit->unit_name}}</td>
                    <td><div class="row"><input class="form-control" style="width: 20%" name="quantity[]" value="{{$unit->quantity}}" @if($unit->secondary_unit_id == $unit->product_primary_unit_id) readonly @endif> &nbsp; {{$unit->primary_unit->unit_name}} </div></td>
                    <td><div class="row"><input class="form-control" style="width: 30%" name="price[]" value="{{$unit->price}}" @if($unit->secondary_unit_id == $unit->product_primary_unit_id) readonly @endif> &nbsp; {{$company->currancy}} </div></td>
                    <td><div class="row"><input class="form-control" style="width: 55%" name="barcode[]" id="barcode" value="{{$unit->barcode}}">
                        <button type="button" id="generate_code_universal" data-unit="{{$unit->secondary_unit_id}}" class="btn btn-md btn-primary barcode_universal"
                            style="width:15%;height: 45px;display: inline; float: left;padding: 5px ;margin-right: 5px;">
                            <i class="las la-magic" style="font-size : 20px"></i>
                            توليد
                        </button></div></td>
                  </tr>
                  @endforeach
                  
                </tbody>
              </table>
            </div>

            </div>

            <div class="clearfix"></div>

            <div class="col-12" align="center">
            <br>
              <button type="submit" class="btn btn-success me-1">تحديث</button>
              <button type="button" class="btn btn-outline-danger" onclick="window.history.back()">رجوع</button>
            </div>
          </div>
          
        </form>

                
                </div>
            </div>
            </div>
          </div>
        </div>

        </section>
@endif
</div>
</div>


@endsection
@section('script')
<script>
    $('.barcode_universal').on('click',function(){
         var code = Math.floor(100000000 + Math.random() * 9999999999);
          // $('#order_universal').val(code);
          // set result in closest input
          $(this).closest('tr').find('input[name="barcode[]"]').val(code);
    });
        checkCategory();

      $('#category').on('change', function () {
        checkCategory();
    });


    function checkCategory(){
      var category_name = $("#category").val();
        var category_type = $("#category").children("option:selected").attr('type');
      if (category_type == 'خدمية') {
        $('#balance').val("").attr('disabled', true);
            $('#model').val("").attr('disabled', true);
            // $('#order_universal').val("").attr('readonly', true);
            $('#min_balance').attr('disabled', true);
            $('#store').attr('disabled', true);
            $('#unit_id').attr('disabled', true);
            $('select[name="colors[]"]').attr('disabled', true);
            $('select[name="sizes[]"]').attr('disabled', true);
            $('#is_product').val("0");
            $('#purchasing_price').val("0");
            $('#wholesale_price').val("0");
            $('#purchasing_price').attr("readonly", true);
            $('#wholesale_price').attr("readonly", true);

        } else {
          $('#unit_id').attr('disabled', false);
            $('#balance').attr('disabled', false);
            $('#model').attr('disabled', false);
            // $('#order_universal').attr('readonly', false);
            $('#min_balance').attr('disabled', false);
            $('#store').attr('disabled', false);
            $('select[name="colors[]"]').attr('disabled', false);
            $('select[name="sizes[]"]').attr('disabled', false);
            $('#is_product').val(1);
            $('#purchasing_price').attr("readonly", false);
            $('#wholesale_price').attr("readonly", false);

        }
    }

    $('.open_extras').on('click',function () {
        $('.extras').fadeIn(300);
        $(this).hide(300);
    });
    
var selectForm = $('#addForm');

if (selectForm) {
    selectForm.validate({

    rules: {
      'store_id': {
        required: true,
      },
      'product_name': {
        required: true
      },
      'unit_id': {
        required: true
      },
      'balance': {
        required: true,
        number: true

      },
      'purchasing_price': {
        required: true,
        number: true

      },
      'wholesale_price': {
        required: true,
        number: true

      },
      'sector_price': {
        required: true,
        number: true

      },
      'min_balance': {
        required: true,
        number: true

      },
      'category_id': {
        required: true
      }

      
    }
  });
}

$("#addForm").on('submit',(function(e) {

e.preventDefault();

$.ajax({

  url: "{{route('client.products.update')}}",

  type: "POST",

  data:  new FormData(this),

  dataType:  'json',

  contentType: false,

  cache: false,

  processData:false,

  beforeSend : function (){var ifConnected = window.navigator.onLine;if (!ifConnected) {toastr.info("يتم الآن إعادة الإتصال","لا يوجد إتصال بالإنترنت",{positionClass: "toast-top-right",tapToDismiss: !1,timeOut: 2e3,rtl: $("html").attr("data-textdirection")});}},

  
  success: function(response) {

    if(response.status){

            toastr.success("",response.message,{
			positionClass: "toast-top-right",
			tapToDismiss: !1,
			timeOut: 2e3,
			rtl: $("html").attr("data-textdirection")
		    });      

    } 

  },
  error: function(response){
          $.each(response.responseJSON.errors, function (key, item) 
          {
            toastr.error("",item,{
			positionClass: "toast-top-right",
			tapToDismiss: !1,
			timeOut: 2e3,
			rtl: $("html").attr("data-textdirection")
		    });

          });
  },

 });

}));


$("#unitsForm").on('submit',(function(e) {

e.preventDefault();

$.ajax({

  url: "{{route('client.products.units.update')}}",

  type: "POST",

  data:  new FormData(this),

  dataType:  'json',

  contentType: false,

  cache: false,

  processData:false,

  beforeSend : function (){var ifConnected = window.navigator.onLine;if (!ifConnected) {toastr.info("يتم الآن إعادة الإتصال","لا يوجد إتصال بالإنترنت",{positionClass: "toast-top-right",tapToDismiss: !1,timeOut: 2e3,rtl: $("html").attr("data-textdirection")});}},

  
  success: function(response) {

    if(response.status){

            toastr.success("",response.message,{
			positionClass: "toast-top-right",
			tapToDismiss: !1,
			timeOut: 2e3,
			rtl: $("html").attr("data-textdirection")
		    });      

    } 

  },
  error: function(response){
          $.each(response.responseJSON.errors, function (key, item) 
          {
            toastr.error("",item,{
			positionClass: "toast-top-right",
			tapToDismiss: !1,
			timeOut: 2e3,
			rtl: $("html").attr("data-textdirection")
		    });

          });
  },

 });

}));

</script>
@endsection

@section('style')
<style>
    .btn.dropdown-toggle.bs-placeholder {
        height: 40px;
    }
    .bootstrap-select{
        width: 80% !important;
    }
    .form-control{
        height: 45px !important;
        padding: 10px !important;
    }

</style>
@endsection
