<?php
require('../config.php');
require '../../../vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\IOFactory;

// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);


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

    // valid extensions
    $extensions = array('xlsx', 'xls');

    $inputFileName = $_FILES['importEmployeesSalaryFile']['name'];
    $extension = pathinfo($inputFileName, PATHINFO_EXTENSION);
    if (in_array($extension, $extensions)) {

        $inputFileTempName = $_FILES['importEmployeesSalaryFile']['tmp_name'];

        /** Load $inputFileName to a Spreadsheet Object  **/
        $spreadsheet = IOFactory::load($inputFileTempName);
        $data = $spreadsheet->getActiveSheet()->toArray();


        $columnNames = array('employee_id' => 0, 'basic_salary' => 1, 'hra' => 2, 'books_per' => 3, 'special_allowance' => 4, 'company_epf' => 5, 'ctc' => 6, 'epf_status' => 7, 'esi_status' => 8, 'tds' => 9, 'revision_start_date' => 10, 'revision_end_date' => 11);

        if (sizeof($columnNames) == sizeof($data[0])) {

            foreach ($data[0] as $key => $column) {
                if (array_key_exists(trim($column), $columnNames)) {
                    $columnNames[$column] = $key;
                } else {
                    echo "<script>alert('`$column` is not a valid column name !');</script>";
                    echo "<script>location.href='../importEmployeesSalary';</script>";
                    exit();
                }
            }
        } else {
            echo "<script>alert('Please Maintain all Column Names like:- `employee_id`, `basic_salary`, `hra`, `books_per`, `special_allowance`, `company_epf`, `ctc`, `epf_status`, `esi_status`, `tds`, `revision_start_date`, `revision_end_date` ');</script>";
            echo "<script>location.href='../importEmployeesSalary';</script>";
            exit();
        }

        array_splice($data, 0, 1);
        $dataLength = sizeof($data);
        $insertCount = 0;
        $empExist = '';
        foreach ($data as $inner) {

            $employee_id = trim($inner[$columnNames['employee_id']]);
            $basic_salary = trim($inner[$columnNames['basic_salary']]);
            $hra = trim($inner[$columnNames['hra']]);
            $books_per = trim($inner[$columnNames['books_per']]);
            $special_allowance = trim($inner[$columnNames['special_allowance']]);
            $company_epf = trim($inner[$columnNames['company_epf']]);
            $ctc = trim($inner[$columnNames['ctc']]);
            $epf_status = trim($inner[$columnNames['epf_status']]);
            $esi_status = trim($inner[$columnNames['esi_status']]);
            $tds = trim($inner[$columnNames['tds']]);
            $revision_start_date = trim($inner[$columnNames['revision_start_date']]);
            $revision_end_date = trim($inner[$columnNames['revision_end_date']]);

            // echo $employee_id . " " . $basic_salary . " " . $hra . " " . $books_per . " " . $special_allowance . " " . $company_epf . " " . $employee_epf . " " . $ctc . " " . $epf_status . " " . $esi_status . "<br>";

            // $sCount = mysqli_num_rows(mysqli_query($db, "SELECT * FROM `employee_salary` WHERE `employee_id`='$employee_id' "));
            // if ($sCount == 0) {

            $q = mysqli_query($db, "INSERT INTO `employee_salary` (`employee_id`, `basic_salary`, `hra`, `books_per`, `special_allowance`, `company_epf`, `ctc`, `epf_status`, `esi_status`, `tds`, `revision_start_date`, `revision_end_date`) VALUES ('$employee_id', '$basic_salary', '$hra', '$books_per', '$special_allowance', '$company_epf', '$ctc', '$epf_status', '$esi_status', '$tds', '$revision_start_date', '$revision_end_date' ) ");
            // } else {
            //     $empExist .= $employee_id . ", ";
            //     $q = false;
            // }

            if ($q) {
                $insertCount++;
            }
        }


        if ($dataLength == $insertCount) {
            echo "<script>alert('All Data imported Successfully !');</script>";
        }
        // else if (!empty($empExist)) {
        //     echo "<script>alert('" . $empExist . " These Employee id data already exist in database and other data imported Successfully !');</script>";
        // } 
        else {
            echo "<script>alert('" . ($dataLength - $insertCount) . " rows not imported !');</script>";
        }
        echo "<script>location.href='../importEmployeesSalary';</script>";
        exit();
    } else {
        echo "<script>alert('Invalid File Formate, Please upload only .xlsx, .xls');</script>";
        echo "<script>location.href='../importEmployeesSalary';</script>";
        exit();
    }
}
