<?php

namespace App;
use Auth;
use DB;
use EF;

use Illuminate\Database\Eloquent\Model;

class Department extends Model
{

   private $generic_condition;

   public function __construct()
    {
           
       $this->setCondition(['is_recycle' => '0']);
    }

    public function setCondition($condition = [])
    {
        $this->generic_condition = $condition;
    }

    public function getCondition()
    {
        return $this->generic_condition;
    }

    public function storeDepartment($object)
    {
        return DB::transaction(function () use ($object)  {

            
            $department_label_list = EF::departmentWhiteLabel();

            $department                = new Department;
            $department->code          = $object[$department_label_list['department_code']];
            $department->name          = $object[$department_label_list['department_name']];
            $department->description   = $object[$department_label_list['department_description']];

            $department->save();

            return with($department);

        });
    }

    
     public function updateDepartment($object,$department_id)
    {

       return DB::transaction(function () use ($object, $department_id) {

            $department = Department::find($department_id);

            if(isset($department->id))
            {
            $department_label_list = EF::departmentWhiteLabel();
            
            $department->code          = $object[$department_label_list['department_code']];
            $department->name          = $object[$department_label_list['department_name']];
            $department->description   = $object[$department_label_list['department_description']];

            $department->update();

                return with($department);
                
                return 1;
            }
            
            return 0;
        });
    }
    public function getDepartmentList()
    {
        return Department::where($this->getCondition())->orderBy('id','desc')->get();
    }

    public function removeDepartment($id)
    {
        return DB::transaction(function () use ($id) {

            $department = Department::find($id);

            if(isset($department->id))
            {

                $department->is_recycle   = '1';
                
                $department->update();
                

                return 1;
            }
            
            return 0;
        });
    }
}
