<?php

class Employee extends MY_BackendController
{
    protected $requiredRights = [
        'isAdmin' => true,
        'isSuperAdmin' => false,
        'isSiteAdmin' => false,
    ];

    protected $layout = "layout/home";
    protected $pageName = "職員";
    public $_module = 'employee';
    public $_controller_ = '職員';

    public function __construct()
    {
        parent::__construct();
        $this->load->model('Employee_model');
        $this->load->model('Tenant_model');
        $this->load->model('Rbac_role_model');
        $this->load->model('Rbac_user_role_model');
        $this->load->model('Employee_income_type_model');
    }

    public function index()
    {
        $template = $this->isSiteAdmin()  ? 'backend/employee/index_siteadmin' : 'backend/employee/index_admin';
        $items = $this->isSiteAdmin()  ? $this->Employee_model->listing_siteadmin() : $this->Employee_model->listing();

        return $this->view($template, [
            'items' => $items
        ]);
    }

	public function add()
	{
		if ($this->input->method() == 'post') {
			$params = $this->input->post();
			$params['isSiteAdmin'] = $this->isSiteAdmin();

			switch (true) {
				case $params['password'] !== $params['confirm_password']:
					$this->_data['__alert_failure_message'] = '登入密碼與確認登入密碼不相符，請重新輸入。';
					break;
				default:
					$this->Employee_model->create_in_employee_tenant($params);
					return redirect('/employee');
			}
		}

		if (! $this->isSiteAdmin()) {
			$params['tenant_no'] = $_SESSION['tenant_no'];
		}
		return $this->view('backend/employee/add', [
			'username' => $params['username'] ?? null,
			'password' => $params['password'] ?? null,
			'confirm_password' => $params['confirm_password'] ?? null,
			'tenant_no' => $params['tenant_no'] ?? null,
			'tenants' => $this->Tenant_model->listing(),
			'chi_name' => $params['chi_name'] ?? null,
			'eng_name' => $params['eng_name'] ?? null,
			'display_name' => $params['display_name'] ?? null,
			'gender' => isset($params) && isset($params['gender']) ? intval($params['gender']) : null,
			'genders' => [
				['id' => 0, 'name' => '女'],
				['id' => 1, 'name' => '男'],
			],
			'income' => $params['income'] ?? null,
			'commission' => $params['commission'] ?? null,
			'display_name' => $params['display_name'] ?? null,
			'remark' => $params['remark'] ?? null,
			'role_id' => $params['role_id'] ?? null,
			'roles' => $this->Rbac_role_model->listing(),
			'income_type_id' => $params['income_type_id'] ?? null,
			'income_types' => $this->Employee_income_type_model->listing(),
			'isSiteAdmin' => $this->isSiteAdmin(),
		]);
	}

	public function edit($id)
    {
        $tenantNo = $this->input->get('tenant');
        $params = $this->Employee_model->detail_tenant($id, $tenantNo);
        $params['tenant_no'] = $tenantNo;
	
        $role = $this->Rbac_role_model->detail_userId_tenant($id, $tenantNo);
        if ($role) {
            $params['role_id'] = $role['role_id'];
        }

        $incomeLookup = $this->Employee_income_lookup_model->detail_userId_tenant($id, $tenantNo);
        if ($incomeLookup) {
            $params['income'] = $incomeLookup['income'];
            $params['commission'] = $incomeLookup['commission'];
            $params['income_type_id'] = $incomeLookup['employee_income_type_id'];
            $params['income'] = $incomeLookup['income'];
        }


        if ($this->input->method() == 'post') {
            $params = $this->input->post();
			$tenantNo = $this->input->get('tenant');
            $params['tenant_no'] = $tenantNo;
            switch (true) {
                case $params['password'] !== $params['confirm_password']:
                    $this->_data['__alert_failure_message'] = '登入密碼與確認登入密碼不相符，請重新輸入。';
                    break;
                default:

                    $this->Employee_model->update_in_employee_tenant($params, $id);
                    return redirect('/employee');
            }
        }


        return $this->view('backend/employee/edit', [
            'id' => $id,
            'username' => $params['username'] ?? null,
            'password' => null,
            'confirm_password' => null,
            'tenant' => $params['tenant_no'],
            'tenant_no' => $this->Tenant_model->detail_no($params['tenant_no'])['name'],
            'chi_name' => $params['chi_name'] ?? null,
            'eng_name' => $params['eng_name'] ?? null,
            'display_name' => $params['display_name'] ?? null,
            'gender' => intval($params['gender']),
            'genders' => [
                ['id' => 0, 'name' => '女'],
                ['id' => 1, 'name' => '男'],
            ],
            'income' => $params['income'] ?? null,
            'income_type_id' => $params['income_type_id'] ?? null,
            'commission' => $params['commission'] ?? 0,
            'remark' => $params['remark'] ?? null,
            'role_id' => $params['role_id'] ?? null,
            'roles' => $this->Rbac_role_model->listing(),
            'income_types' => $this->Employee_income_type_model->listing(),
			'data'=>$params
        ]);
    }
}
