class BatchesController < ApplicationController    
	 skip_before_filter  :verify_authenticity_token 
     # before_action :authenticate_member!
     before_action :set_batch,:authenticate_member!, only: [:show, :edit, :update, :destroy]
    def index
    	@product_category_id=""
    	@clients=Client.all.order('id desc')
    	@product_categories=ProductCategory.all.order('id desc')
    	@batches=Batch.joins('inner join users on users.id=batches.user_id')
    				  .select('batches.*,users.name as user_name').order('batches.id desc').page(params[:page])
      @count = Batch.joins('inner join users on users.id=batches.user_id')
                      .select('batches.*').count

    end

    def show
        @batchLineItems=BatchLineItem.where("batch_id=?",params[:id]).page(params[:page]).per(10)
        @result_count= @batchLineItems.total_count
    end


    def new
      @batch = Batch.new
      @clients=Client.all.order('id desc')    
      @product=ProductCategory.all.order('id desc')
    end

    def edit
    end


    def create
      date = DateTime.now.beginning_of_day.utc.to_time.strftime("%Y-%m-%d") 
      order_ref = params[:batch][:order_ref]   
      adult_or_child=params[:adult_or_child]
      client_id=params[:client_id]
      product_category_id=params[:product_category_id]
      @batch = Batch.create_batch(date,order_ref,2,"adult_or_child",client_id,product_category_id)

      respond_to do |format|
        if @batch.save
          format.html { redirect_to @batch, notice: 'Batch was successfully created.' }
          format.json { render :show, status: :created, location: @batch }
        else
          format.html { render :new }
          format.json { render json: @batch.errors, status: :unprocessable_entity }
        end
      end
    end


    def update

      respond_to do |format|
        if @batch.update(batch_params)
          format.html { redirect_to @batch, notice: 'Batch was successfully updated.' }
          format.json { render :show, status: :ok, location: @batch }
        else
          format.html { render :edit }
          format.json { render json: @batch.errors, status: :unprocessable_entity }
        end
      end
    end

    def destroy
      @batch.destroy
      respond_to do |format|
        format.html { redirect_to batches_url, notice: 'Batch was successfully destroyed.' }
        format.json { head :no_content }
      end
    end

    def search
    	client_id=params[:client_id]
        product_category_id=params[:product_category_id]
        search_keyword = params[:search_keyword]

        if client_id == "" && product_category_id == "" && search_keyword == "" then
            @batches=Batch.joins('inner join users on users.id=batches.user_id')
                      .select('batches.*,users.name as user_name').order('batches.id desc').page(params[:page])
            @count = Batch.joins('inner join users on users.id=batches.user_id')
                      .select('batches.*').count

        elsif client_id == "" && product_category_id == "" then
            @batches=Batch.joins('inner join users on users.id=batches.user_id')
                     .select('batches.*,users.name as user_name').where('LOWER(order_ref) like ?', "%#{params[:search_keyword]}%").order('batches.id desc').page(params[:page])  
            @count=Batch.joins('inner join users on users.id=batches.user_id')
                      .select('batches.*').where('LOWER(order_ref) like ?', "%#{params[:search_keyword]}%").count 

        elsif  client_id == "" then 
            @batches=Batch.joins('inner join users on users.id=batches.user_id')
                      .select('batches.*,users.name as user_name').where('product_category_id=? and LOWER(order_ref) like ?',params[:product_category_id], "%#{params[:search_keyword]}%").order('batches.id desc').page(params[:page])

            @count=Batch.joins('inner join users on users.id=batches.user_id')
                      .select('batches.*').where('product_category_id=? and LOWER(order_ref) like ?',params[:product_category_id], "%#{params[:search_keyword]}%").count
        
        
        elsif product_category_id == "" then
            @batches=Batch.joins('inner join users on users.id=batches.user_id')
                     .select('batches.*,users.name as user_name').where('client_id=? and LOWER(order_ref) like ?', params[:client_id], "%#{params[:search_keyword]}%").order('batches.id desc').page(params[:page])
            
            @count=Batch.joins('inner join users on users.id=batches.user_id')
                      .select('batches.*').where('client_id=? and LOWER(order_ref) like ?', params[:client_id], "%#{params[:search_keyword]}%").count              
        else 
            @batches=Batch.joins('inner join users on users.id=batches.user_id')
                     .select('batches.*,users.name as user_name').where('product_category_id=? and client_id=? and LOWER(order_ref) like ?',params[:product_category_id], params[:client_id], "%#{params[:search_keyword]}%").order('batches.id desc').page(params[:page])                                                                       
            @count=Batch.joins('inner join users on users.id=batches.user_id')
                      .select('batches.*').where('product_category_id=? and client_id=? and LOWER(order_ref) like ?',params[:product_category_id], params[:client_id], "%#{params[:search_keyword]}%").count
                          
        end           
    end
    private
    # Use callbacks to share common setup or constraints between actions.
    def set_batch
      @batch = Batch.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def batch_params
        params.require(:batch).permit(:date,:user_id, :client_id, :order_ref,:batch_start,:batch_end,:exported_by,:qty_processing,:qty_success,:qty_fail,:batch_start_time,:batch_end_time,:export_count,:remark,:adult_or_child,:product_category_id) 
    end
end