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

class Home1 extends CI_Controller {

public function __construct(){

parent::__construct();
$this->load->helper('text');
$this->load->helper(array('form','url'));
date_default_timezone_set('Asia/Kolkata');
//$this->load->model('AdminModel');
// $this->load->model('FrontModel');
// $this->load->model('BackModel');
$this->load->helper('date');
$this->load->database();    
$this->load->library('form_validation');
$this->load->library('session');
// $this->load->library('Pdf');
}

public function index(){
$data['success']="";$data['error']="";

if(isset($_POST['submit'])){

$school_full_name=htmlspecialchars($this->input->post('school_full_name'));
$School_afflication=htmlspecialchars($this->input->post('School_afflication'));
$school_address=htmlspecialchars($this->input->post('school_address'));
$school_email=htmlspecialchars($this->input->post('school_email'));
$school_contact_no=htmlspecialchars($this->input->post('school_contact_no'));
$school_website=htmlspecialchars($this->input->post('school_website'));
$admin_name=htmlspecialchars($this->input->post('admin_name'));
$admin_email=htmlspecialchars($this->input->post('admin_email'));
$admin_mob_no=htmlspecialchars($this->input->post('admin_mob_no'));
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%&*_";
$password = substr( str_shuffle( $chars ), 0, 8 );

$data_array=array("schFullName"=>$school_full_name,"schAfflication"=>$School_afflication,"schAddress"=>$school_address,"schEmail"=>$school_email,"schContNo"=>$school_contact_no,"schWebsite"=>$school_website,"adminName"=>$admin_name,"adminEmail"=>$admin_email,"adminMobNo"=>$admin_mob_no,"password"=>$password,"userType"=>'Staff');
$table='schoolregister';
$add_school=$this->FrontModel->insert_data($data_array,$table);
if($add_school){
$data['success']="School added successfully";
$school_id=$add_school;
$staff_table='school_staff';
$staff_data_array=array("school_id"=>$insert_data,"name"=>$admin_name,"mobile_number"=>$admin_mob_no,"email"=>$admin_email,"usertype"=>'staff',"admin"=>1);
$add_to_staff=$this->FrontModel->insert_data($staff_data_array,$staff_table);

if($add_to_staff){
$data['success']="Staff added successfully";
$data_array=array("userName"=>$admin_name,"userEmail"=>$admin_email,"userContact"=>$admin_mob_no,"password"=>$password,"status"=>1,"userType"=>'staff',"school_id"=>$school_id,"user_id"=>$add_to_staff);
$table='users';
$add_login_credentials=$this->FrontModel->insert_data($data_array,$table);
if($add_login_credentials){
$data['success']="Login credentials generated successfully";

$this->session->set_userdata('user_id',$add_to_staff);
$this->session->set_userdata('school_id',$school_id);
$this->session->set_userdata('userName',$admin_name);
$this->session->set_userdata('status',1);
redirect(base_url().'School/');

}
}

}else{
$data['error']='Some error occured';
}

}
$this->load->view('front/header');
$this->load->view('front/school-form',$data);
$this->load->view('front/footer');

}
}

