<?php

namespace App\Services;
use App\Models\Measuerments;
use App\Models\ProductUnits;
use App\Models\Stores;
use App\Services\Average;
use App\Traits\GeneralTrait;
use DB;

class ManfacturingService
{
    use GeneralTrait;


    //Filter Income
    public function ItemsFilter($Filter)
    {

        $output = '';

        $units=ProductUnits::where('Product',$Filter['product'])->get();
        $search=$Filter['search'];
        if(is_numeric($search)){


            $rr = ProductUnits::where('Product', $Filter['product'])->where('Barcode', $search)->first();
        }else {
            $rr = ProductUnits::where('Product', $Filter['product'])->where('Def', 1)->first();
        }


        $Stores=Stores::all();

        $PrrroName=$Filter['PrrroName'];
        $UniiName=$Filter['UniiName'];
        $StoreNemo=$Filter['StoreNemo'];
        $pr=$Filter['price'];
        $store=$Filter['store'];

        if(auth()->guard('admin')->user()->can('سعر التكلفة في التصنيع')){

            $SowPrice='block';
        }else{

            $SowPrice='none';

        }


        $output .= '

       <tr id="Row'.$Filter['id'].'">
        <td>
        '.$PrrroName.'
 <input type="hidden"  id="P_Ar_Name'.$Filter['id'].'" value="'.$Filter['name_ar'].'">
        <input type="hidden"  id="P_En_Name'.$Filter['id'].'" value="'.$Filter['name_en'].'">
        <input type="hidden"  id="Product'.$Filter['id'].'" value="'.$Filter['product'].'">
        <input type="hidden"  id="VOne'.$Filter['id'].'" value="'.$Filter['v1'].'">
        <input type="hidden"  id="VTwo'.$Filter['id'].'" value="'.$Filter['v2'].'">
        <input type="hidden"  id="V_Name'.$Filter['id'].'" value="">
        <input type="hidden"  id="VV_Name'.$Filter['id'].'" value="">
        </td>

        <td>
             <select class="select2 form-control w-100"   id="UnitPurch'.$Filter['id'].'" onchange="UnitCodePurchh('.$Filter['id'].')">
                <option value="">    '.trans('admin.Choice_Unit').'</option>
              ';

        foreach($units as $uni){
            $nam=Measuerments::find($uni->Unit);
            if(app()->getLocale() == 'ar' ){
                $UnitNamme=$nam->Name;
            }else{

                $UnitNamme=$nam->NameEn;
            }
            $output .= '
            <option value="'.$uni->Unit.'"       '; if ($rr->Unit == $uni->Unit) {    $output .= '  selected  ';  }   $output .= ' > '.$UnitNamme.'</option>
                 ';


        }

        $output .= '

                        </select>
        </td>
        <td>
         <input type="text" id="CodePurch'.$Filter['id'].'" class="form-control" value="'.$Filter['P_Code'].'"  disabled>
         <input type="hidden" id="UnitPurchName'.$Filter['id'].'" value="'.$UniiName.'">
         <input type="hidden" id="TaxRate'.$Filter['id'].'" value="'.$rr->Product()->first()->Tax()->first()->Rate.'">
         <input type="hidden" id="TaxType'.$Filter['id'].'" value="'.$rr->Product()->first()->Tax()->first()->Type.'">
         <input type="hidden" id="PurchTax'.$Filter['id'].'" value="'.$rr->Product()->first()->Tax.'">
        </td>


        <td>
        <input type="number" id="Qty'.$Filter['id'].'" step="any"   class="form-control" onkeyup="PurchTotal('.$Filter['id'].')" onclick="PurchTotal('.$Filter['id'].')" >

        </td>

              <td style="display: '.$SowPrice.'">

 <input type="number" id="Price'.$Filter['id'].'" step="any"  class="form-control" onkeyup="PurchTotal('.$Filter['id'].')" onclick="PurchTotal('.$Filter['id'].')" value="'.$pr.'" disabled>
 <input type="hidden" id="Discount'.$Filter['id'].'" step="any"  class="form-control" onkeyup="PurchTotal('.$Filter['id'].')" onclick="PurchTotal('.$Filter['id'].')" value="0" >
<input type="hidden" id="TotalBFTax'.$Filter['id'].'"   class="form-control" >
<input type="hidden" id="Tax'.$Filter['id'].'"   class="form-control" >
  <input type="hidden" id="Total'.$Filter['id'].'"   class="form-control" >
        </td>



            <td>
   <select class="select2 form-control w-100"   id="StorePurch'.$Filter['id'].'" onchange="StoreNamePurch('.$Filter['id'].')">
              ';

        foreach($Stores as $stor){
            if(app()->getLocale() == 'ar' ){
                $StorNamme=$stor->Name;
            }else{

                $StorNamme=$stor->NameEn;
            }

            $output .= '
      <option value="'.$stor->id.'"       ';  if($stor->id == $store){    $output .= '  selected  ';  }   $output .= ' > '.$StorNamme.'</option>
                 ';
        }

        $output .= '

                        </select>
        </td>


             <td>
      <input type="number" step="any" id="DeprecPrecent'.$Filter['id'].'" onkeyup="PurchTotal('.$Filter['id'].')" onclick="PurchTotal('.$Filter['id'].')"   class="form-control" value="0">

          </td>

          <td>
      <input type="number" step="any" id="Deprec'.$Filter['id'].'"   class="form-control" value="0" disabled >
      <input type="hidden" id="StorePurchName'.$Filter['id'].'"   class="form-control"  value="'.$StoreNemo.'" >
          </td>

        <td>
 <button type="button" class="btn btn-default waves-effect waves-themed" style="display:none" id="AddBtnPur'.$Filter['id'].'" onclick="Fun('.$Filter['id'].')">
          <i class="fal fa-plus"></i>
          </button>
          </td>
        </tr>


            ';


        return $output;

    }

   //Filter Income Precent
    public function ItemsFilterPrecent($Filter)
    {

        $output = '';

        $units=ProductUnits::where('Product',$Filter['product'])->get();
        $search=$Filter['search'];
        if(is_numeric($search)){


            $rr = ProductUnits::where('Product', $Filter['product'])->where('Barcode', $search)->first();
        }else {
            $rr = ProductUnits::where('Product', $Filter['product'])->where('Def', 1)->first();
        }


        $Stores=Stores::all();

        $PrrroName=$Filter['PrrroName'];
        $UniiName=$Filter['UniiName'];
        $StoreNemo=$Filter['StoreNemo'];
        $pr=$Filter['price'];
        $store=$Filter['store'];

        if(auth()->guard('admin')->user()->can('سعر التكلفة في التصنيع')){

            $SowPrice='block';
        }else{

            $SowPrice='none';

        }

        $output .= '

       <tr id="Row'.$Filter['id'].'">
        <td>
        '.$PrrroName.'
 <input type="hidden"  id="P_Ar_Name'.$Filter['id'].'" value="'.$Filter['name_ar'].'">
        <input type="hidden"  id="P_En_Name'.$Filter['id'].'" value="'.$Filter['name_en'].'">
        <input type="hidden"  id="Product'.$Filter['id'].'" value="'.$Filter['product'].'">
        <input type="hidden"  id="VOne'.$Filter['id'].'" value="'.$Filter['v1'].'">
        <input type="hidden"  id="VTwo'.$Filter['id'].'" value="'.$Filter['v2'].'">
        <input type="hidden"  id="V_Name'.$Filter['id'].'" value="">
        <input type="hidden"  id="VV_Name'.$Filter['id'].'" value="">
        </td>

        <td>
             <select class="select2 form-control w-100"   id="UnitPurch'.$Filter['id'].'" onchange="UnitCodePurchh('.$Filter['id'].')">
                <option value="">    '.trans('admin.Choice_Unit').'</option>
              ';

        foreach($units as $uni){
            $nam=Measuerments::find($uni->Unit);
            if(app()->getLocale() == 'ar' ){
                $UnitNamme=$nam->Name;
            }else{

                $UnitNamme=$nam->NameEn;
            }
            $output .= '
            <option value="'.$uni->Unit.'"       ';  if ($rr->Unit == $uni->Unit) {   $output .= '  selected  ';  }   $output .= ' > '.$UnitNamme.'</option>
                 ';


        }

        $output .= '

                        </select>
        </td>
        <td>
         <input type="text" id="CodePurch'.$Filter['id'].'" class="form-control" value="'.$Filter['P_Code'].'"  disabled>
         <input type="hidden" id="UnitPurchName'.$Filter['id'].'" value="'.$UniiName.'">
         <input type="hidden" id="TaxRate'.$Filter['id'].'" value="'.$rr->Product()->first()->Tax()->first()->Rate.'">
         <input type="hidden" id="TaxType'.$Filter['id'].'" value="'.$rr->Product()->first()->Tax()->first()->Type.'">
         <input type="hidden" id="PurchTax'.$Filter['id'].'" value="'.$rr->Product()->first()->Tax.'">
        </td>


      <td>
        <input type="number" id="Precent'.$Filter['id'].'" step="any"   class="form-control" onkeyup="PurchTotal('.$Filter['id'].')" onclick="PurchTotal('.$Filter['id'].')" >
        </td>

        <td>
        <input type="number" id="Qty'.$Filter['id'].'" step="any"   class="form-control" onkeyup="PurchTotal('.$Filter['id'].')" onclick="PurchTotal('.$Filter['id'].')" >

        </td>

              <td style="display: '.$SowPrice.'">

 <input type="number" id="Price'.$Filter['id'].'" step="any"  class="form-control" onkeyup="PurchTotal('.$Filter['id'].')" onclick="PurchTotal('.$Filter['id'].')" value="'.$pr.'" disabled>
 <input type="hidden" id="Discount'.$Filter['id'].'" step="any"  class="form-control" onkeyup="PurchTotal('.$Filter['id'].')" onclick="PurchTotal('.$Filter['id'].')" value="0" >
<input type="hidden" id="TotalBFTax'.$Filter['id'].'"   class="form-control" >
<input type="hidden" id="Tax'.$Filter['id'].'"   class="form-control" >
  <input type="hidden" id="Total'.$Filter['id'].'"   class="form-control" >
        </td>



            <td>
   <select class="select2 form-control w-100"   id="StorePurch'.$Filter['id'].'" onchange="StoreNamePurch('.$Filter['id'].')">
              ';

        foreach($Stores as $stor){
            if(app()->getLocale() == 'ar' ){
                $StorNamme=$stor->Name;
            }else{

                $StorNamme=$stor->NameEn;
            }

            $output .= '
      <option value="'.$stor->id.'"       ';  if($stor->id == $store){    $output .= '  selected  ';  }   $output .= ' > '.$StorNamme.'</option>
                 ';
        }

        $output .= '

                        </select>
        </td>


             <td>
      <input type="number" step="any" id="DeprecPrecent'.$Filter['id'].'" onkeyup="PurchTotal('.$Filter['id'].')" onclick="PurchTotal('.$Filter['id'].')"   class="form-control" value="0">

          </td>

          <td>
      <input type="number" step="any" id="Deprec'.$Filter['id'].'"   class="form-control" value="0" disabled >
      <input type="hidden" id="StorePurchName'.$Filter['id'].'"   class="form-control"  value="'.$StoreNemo.'" >
          </td>

        <td>
 <button type="button" class="btn btn-default waves-effect waves-themed" style="display:none" id="AddBtnPur'.$Filter['id'].'" onclick="Fun('.$Filter['id'].')">
          <i class="fal fa-plus"></i>
          </button>
          </td>
        </tr>


            ';

        return $output;

    }

    //Filter Putcome
    public function ItemsFilterOutcome($Filter)
    {

        $output = '';

        $units=ProductUnits::where('Product',$Filter['product'])->get();

        $PrrroName=$Filter['PrrroName'];
        $UniiName=$Filter['UniiName'];

        $search=$Filter['search'];
        if(is_numeric($search)){


            $rr = ProductUnits::where('Product', $Filter['product'])->where('Barcode', $search)->first();
        }else {
            $rr = ProductUnits::where('Product', $Filter['product'])->where('Def', 1)->first();
        }




        $output .= '

       <tr id="RowO'.$Filter['id'].'">
        <td>
        '.$PrrroName.'
 <input type="hidden"  id="P_Ar_NameO'.$Filter['id'].'" value="'.$Filter['name_ar'].'">
        <input type="hidden"  id="P_En_NameO'.$Filter['id'].'" value="'.$Filter['name_en'].'">
        <input type="hidden"  id="ProductO'.$Filter['id'].'" value="'.$Filter['product'].'">
        </td>


               <td>
         <input type="text" id="CodePurchO'.$Filter['id'].'" class="form-control" value="'.$Filter['P_Code'].'"  disabled>
         <input type="hidden" id="UnitPurchNameO'.$Filter['id'].'" value="'.$UniiName.'">
        </td>


        <td>
             <select class="select2 form-control w-100"   id="UnitPurchO'.$Filter['id'].'" onchange="UnitCodePurchhO('.$Filter['id'].')">
                <option value=""> '.trans('admin.Choice_Unit').'</option>
              ';

        foreach($units as $uni){
            $nam=Measuerments::find($uni->Unit);


            if(app()->getLocale() == 'ar' ){
                $UnitNamme=$nam->Name;
            }else{

                $UnitNamme=$nam->NameEn;
            }
            $output .= '
            <option value="'.$uni->Unit.'"       ';  if($rr->Unit == $uni->Unit) {    $output .= '  selected  ';  }   $output .= ' > '.$UnitNamme.'</option>
                 ';


        }

        $output .= '

                        </select>
        </td>



        <td>
        <input type="number" id="QtyO'.$Filter['id'].'" step="any" class="form-control" value="1" disabled >
        </td>

        <td>
 <button type="button" class="btn btn-default waves-effect waves-themed"  id="AddBtnPurO'.$Filter['id'].'" onclick="FunO('.$Filter['id'].')">
          <i class="fal fa-plus"></i>
          </button>
          </td>

        </tr>


            ';


        return $output;

    }




}
