<?php

namespace App\DataTables\BillingDesks;

use App\Models\OrderManagement\BillingDesk;
use App\Models\UserManagement\Admin;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Validation\Rule;
use Yajra\DataTables\DataTablesEditor;

class BillingDeskDataTableEditor extends DataTablesEditor{
    protected $model = BillingDesk::class;

    /**Get create action validation rules.
     * @return array
     */
    public function createRules(){
        $table_name = $this->resolveModel()->getTable();
        $managers_table_name = (new Admin)->getTable();

        return  [
            "name" => ["required",'min:4'],
            "code" => ["nullable",'min:2',
                Rule::unique($table_name,"code")
           ],
            "managers" => ['nullable','array','min:1'],
            "managers.*" => [
                'nullable',
                "int",
                Rule::exists($managers_table_name,"id")
            ],
        ];
    }

    /**Get edit action validation rules.
     * @param Model $model
     * @return array
     */
    public function editRules(Model $model){
        $table_name = $this->resolveModel()->getTable();
        $managers_table_name = (new Admin)->getTable();
        return  [
            "name" => ["required",'min:4'],
            "code" => ["nullable",'min:2',
                Rule::unique($table_name,"code")
                    ->ignore($model->getKey())
            ],
            "managers" => ['nullable','array','min:1'],
            "managers.*" => [
                'nullable',
                "int",
                Rule::exists($managers_table_name,"id")
            ],
        ];
    }

    /**
     * Get remove action validation rules.
     *
     * @param Model $model
     * @return array
     */
    public function removeRules(Model $model){
       // $role_table_name = $this->resolveModel()->getTable();
        return [
//            "ids" => ['required',"array","min:1"],
//            "ids.*" => ['required',"int","exists:$role_table_name,id,deleted_at,NULL"],
        ];
    }

    public function attributes(){
        return [
            "name" => __("Name"),
            "code" => __("Code"),
            "city" => __("City"),
            "managers" => __("Managers"),
            "managers.*" => __("Manager"),
        ];
    }

    public function updating(BillingDesk $billingDesk, array $data){
        return $this->editing($billingDesk,$data);
    }

    public function creating(BillingDesk $billingDesk, array $data){
        return $this->editing($billingDesk,$data);
    }

    protected function editing(BillingDesk $billingDesk, $data, $event = "ing"){

        if ($event == "ed" and isset($this->currentData['managers'])){
            $billingDesk->managers()->sync($this->currentData['managers']);
        }

        unset($data['managers']);

        return $data;
        //return ($event === "ed") ? $data : $billingDesk;
    }


    public function created(BillingDesk $billingDesk, $data){
         $this->editing($billingDesk,$data,"ed");

        return $billingDesk;
    }

    public function updated(BillingDesk $billingDesk, $data){
         $this->editing($billingDesk,$data,"ed");
         return $billingDesk;
    }
}
