<?php

namespace App\Controllers;


use App\Models\UserModel;
use PhpParser\Builder\Function_;

class Staff extends BaseController
{
    public function index()
    {
        $userMod = new UserModel();

        // $userMod->tables('staff_details', 'id', ['name', 'designation', 'salary']);
        $userMod->tables('staff_details', 'id', ['name', 'designation', 'salary', 'photo']);
        $data['users'] = $userMod->findAll();
        // //return view('staff_details');
        // print_r($data);
        // // return "hai how are you";
        return view('staff_details', $data);
    }
    public function staff_add_form()
    {
        return view('staff_add');
    }
    public function staff_save()
    {
        $userMod = new UserModel();
        // $userMod->tables('staff_details', 'id', ['name', 'designation', 'salary']);
        $userMod->tables('staff_details', 'id', ['name', 'designation', 'salary', 'photo']);
        $photoName = '';
        $file = $this->request->getFile('photo');
        if ($file->isValid() && ! $file->hasMoved()) {
            $photoName = $file->getRandomName();
            $file->move('uploads/', $photoName);
        }
        $data = [
            'name' => $this->request->getPost('staff_name'),
            'designation' => $this->request->getPost('designation'),
            'salary' => $this->request->getPost('salary'),
            'photo'  => $photoName,
        ];
        $userMod->insert($data);
        return redirect()->to('/');
    }

    public function staff_edit($ida)
    {
        $userMod = new UserModel();
        // $userMod->tables('staff_details', 'id', ['name', 'designation', 'salary']);
        $userMod->tables('staff_details', 'id', ['name', 'designation', 'salary', 'photo']);
        $data['user'] = $userMod->find($ida);
        return view('staff_update', $data);
    }

    public function staff_edit1()
    {
        $userMod = new UserModel();
        // $userMod->tables('staff_details', 'id', ['name', 'designation', 'salary']);
        $userMod->tables('staff_details', 'id', ['name', 'designation', 'salary', 'photo']);
        $id = $this->request->getPost('id');
        $data = $userMod->find($id);
        $photoName = $data['photo'];
        //    $photoName = '';
        $file = $this->request->getFile('photo');
        if ($file->isValid() && ! $file->hasMoved()) {
            $photoName = $file->getRandomName();
            $file->move('uploads/', $photoName);
        }
        $data = [
            'name' => $this->request->getPost('staff_name'),
            'designation' => $this->request->getPost('designation'),
            'salary' => $this->request->getPost('salary'),
            'photo' => $photoName
        ];
        $userMod->update($id, $data);
        return redirect()->to('/');
        // return "updated";
    }

    public function delete($ida)
    {
        $userMod = new UserModel();
        // $userMod->tables('staff_details', 'id', ['name', 'designation', 'salary']);
        $userMod->tables('staff_details', 'id', ['name', 'designation', 'salary', 'photo']);
        $userMod->delete($ida);
        return redirect()->to('/');
    }
}
