<?php 
defined('BASEPATH') OR exit('No direct script access allowed');

class ResetPassword extends CI_Controller {

    public function index()
    {
        $ref = $this->input->get('ref');
        $this->data['ref'] = $ref;
        $this->load->view('Header');
        $this->load->view('Resetpass', $this->data, false);
        $this->load->view('Footer');
    }

    public function submit()
    {
        $pass = $this->input->post('password');
        $cfpass = $this->input->post('cfpass');
        $ref = $this->input->post('ref');
        $this->db->where('ref', $ref);
        $this->db->where('status', 0);
        $query = $this->db->get('resetpassword', 1);
        if($pass != null && $cfpass != null && $ref != null)
        {
            if($pass == $cfpass)
            {
                if($query->num_rows() == 1)
                {
                    $data = $query->row_array();
                    $object = array(
                        'status'    =>  1
                    );
                    $this->db->where('id_reset', $data['id_reset']);
                    $this->db->update('resetpassword', $object);
                    $object = array(
                        'password'  => sha1($pass.'fbgray')
                    );
                    $this->db->where('id_users', $data['id_users']);
                    $this->db->update('users', $object);
                    echo json_encode(['status' => 1, 'msg' => 'เปลี่ยนรหัสผ่านเสร็จสิ้น']);
                }else{
                    echo json_encode(['status' => 0, 'msg' => 'ไม่สามารถเปลี่ยนรหัสผ่านได้กรุณาคลิกลิ้งค์เปลี่ยนรหัสในอีเมลล์ใหม่อีกครั้ง']);
                }
            }else{
                echo json_encode(['status' => 0, 'msg' => 'รหัสผ่านและยืนยันรหัสผ่านไม่ตรงกัน']);
            }
        }else{
            echo json_encode(['status' => 0, 'msg' => 'กรุณากรอกข้อมูลให้ครบถ้วน']);
        }

    }

}

/* End of file Resetpassword.php */
