<?php
/**
 * Created by PhpStorm.
 * User: CHALONG LATEX
 * Date: 14-Sep-16
 * Time: 4:22 PM
 */
if(!defined('BASEPATH')) exit('No direct script access allowed');

class Warranty extends MX_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('warranty_model') ;
        $this->load->model('lang_model') ;
        $this->load->helper('string');

       checklogin();
    }

    public function index()
    {
        $data['lists'] = $this->warranty_model->get_all();
        $this->template->write_view('content', 'warranty',$data);
        $this->template->write_view('level_script', 'warranty/listscript');
        $this->template->add_css('assets/css/layout-datatables.css');
        $this->template->render();
    }
    public function lists()
    {
        $data['lists'] = $this->warranty_model->get_all();
        $this->template->write_view('content', 'warranty',$data);
        $this->template->write_view('level_script', 'warranty/listscript');
        $this->template->add_css('assets/css/layout-datatables.css');
        $this->template->render();
    }
    public function form()
    {
        $data['all_codes'] = $this->warranty_model->count_rows();
        $data['no_active_codes'] = $this->warranty_model->where(array("is_activated"=>'N'))->count_rows();

//        pre($data);exit;
        $this->template->write_view('content', 'warrantyform',$data);
        $this->template->add_js('assets/js/progress.js');
        $this->template->render();
    }
    public function addcodes()
    {

        $wcount = $this->input->post('warranty_count');

//        $wcount = 5;
        $is_active = $this->input->post('is_active');

        for ($i=0;($i<$wcount)&&($wcount<=1000);$i++)
        {
            //check duplicate
            do{
                $new_code =  strtoupper("TH-".random_string('alnum', 10));
                $check_exist = $this->warranty_model->where(array("warranty_code" => $new_code))->get();
            }while($check_exist);

            $data["warranty_code"] = $new_code;
            $data["is_activated"] = 'N';
            if($is_active=="on")
                $data["is_active"] = 'Y';
            else
                $data["is_active"] = 'N';

            $this->warranty_model->insert($data);
        }
    }

}