<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Referral_scheme extends MY_BackendController
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('Referral_scheme_model');

        $this->load->model('Rbac_permission_model', 'Permission_model');
    }

    public function index()
    {
        $this->view('backend/referral_scheme/index', [
            'items' => $this->Referral_scheme_model->listing(),
        ]);
    }

    public function add()
    {
        $this->view('backend/referral_scheme/add', [
            'item' => [
                'join_quantity' => 1,
                'referrer_reward_credits' => 0,
                'joined_reward_credits' => 0,
                'valid_from' => date('Y-m-d H:i'),
                'valid_to' => date('Y-m-d H:i', strtotime('+1 year', time())),
            ]
        ]);
    }

    public function edit()
    {
        $id = $this->input->get('id');
        $this->view('backend/referral_scheme/edit', [
            'id' => $id,
            'item' => $this->Referral_scheme_model->detail($id)
        ]);
    }

    public function save()
    {
        $params = $this->input->post();
        $id = $params['id'] ?? 0;
        $mode = $params['mode'];
        unset($params['id']);
        unset($params['mode']);
        $this->Referral_scheme_model->save($params, $mode, $id);

        redirect('/referral_scheme');
    }

    public function delete()
    {
        $this->Referral_scheme_model->delete($this->input->get('id'));
        echo json_encode('0');
    }
}
