<?php
include("../public/impOpreation.php");
include("../library/uploadImages.php");
$do = $_GET['do'];
$today = date("Y-m-d H:i:s");
$date = date("Y-m-d");

if (empty($do)) {
    $smarty->display("header.html");

    $allChasis = R::getAll("select DISTINCT chasisno from importcontract where chasisno != '' and del <2");
    $smarty->assign('allChasis', $allChasis);

    $smarty->display("shippingFilesView/add.html");
    $smarty->display("footer.html");
} elseif ($do == "show") {
    header("location:shippingFilesController.php");
} elseif ($do == 'getFiles') {
    $chasis = filter_input(INPUT_POST, 'chasis');
    $files = R::getRow("SELECT * FROM shippingfile WHERE chasis = '" . $chasis . "'");
    if (!$files) {
        $files = R::dispense('shippingfile');
        $files->chasis = $chasis;
        $files->sysdate = date("Y-m-d H:i:s");
        $files->userid = $_SESSION['userid'];
        R::store($files);
    }
    $files['buybillusername'] = R::getCell("SELECT employeename FROM user WHERE userid = ?", [$files['buybilluserid']]);
    $files['cocusername'] = R::getCell("SELECT employeename FROM user WHERE userid = ?", [$files['cocuserid']]);
    $files['finalbillusername'] = R::getCell("SELECT employeename FROM user WHERE userid = ?", [$files['finalbilluserid']]);
    $files['warehouseusername'] = R::getCell("SELECT employeename FROM user WHERE userid = ?", [$files['warehouseuserid']]);
    $files['shippolicyusername'] = R::getCell("SELECT employeename FROM user WHERE userid = ?", [$files['shippolicyuserid']]);
    $files['eurousername'] = R::getCell("SELECT employeename FROM user WHERE userid = ?", [$files['eurouserid']]);
    $files['customevalusername'] = R::getCell("SELECT employeename FROM user WHERE userid = ?", [$files['customevaluserid']]);
    $files['model4username'] = R::getCell("SELECT employeename FROM user WHERE userid = ?", [$files['model4userid']]);
    $files['endrosementusername'] = R::getCell("SELECT employeename FROM user WHERE userid = ?", [$files['endrosementuserid']]);
    $files['batchreleaseusername'] = R::getCell("SELECT employeename FROM user WHERE userid = ?", [$files['batchreleaseuserid']]);
    $files['trafficnotifyusername'] = R::getCell("SELECT employeename FROM user WHERE userid = ?", [$files['trafficnotifyuserid']]);
    $files['otherusername'] = R::getCell("SELECT employeename FROM user WHERE userid = ?", [$files['otheruserid']]);
    $smarty->assign('file', $files);
    $smarty->display("shippingFilesView/files.html");
} elseif ($do == 'editprint') {
    $smarty->display("header.html");
    $id = filter_input(INPUT_GET, 'id');
    $files = R::getRow("SELECT * FROM shippingfile WHERE id = '" . $id . "'");
    if (!$files) {
        $files = R::dispense('shippingfile');
        $files->chasis = $chasis;
        $files->sysdate = date("Y-m-d H:i:s");
        $files->userid = $_SESSION['userid'];
        R::store($files);
    }
    $files['buybillusername'] = R::getCell("SELECT employeename FROM user WHERE userid = ?", [$files['buybilluserid']]);
    $files['cocusername'] = R::getCell("SELECT employeename FROM user WHERE userid = ?", [$files['cocuserid']]);
    $files['finalbillusername'] = R::getCell("SELECT employeename FROM user WHERE userid = ?", [$files['finalbilluserid']]);
    $files['warehouseusername'] = R::getCell("SELECT employeename FROM user WHERE userid = ?", [$files['warehouseuserid']]);
    $files['shippolicyusername'] = R::getCell("SELECT employeename FROM user WHERE userid = ?", [$files['shippolicyuserid']]);
    $files['eurousername'] = R::getCell("SELECT employeename FROM user WHERE userid = ?", [$files['eurouserid']]);
    $files['customevalusername'] = R::getCell("SELECT employeename FROM user WHERE userid = ?", [$files['customevaluserid']]);
    $files['model4username'] = R::getCell("SELECT employeename FROM user WHERE userid = ?", [$files['model4userid']]);
    $files['endrosementusername'] = R::getCell("SELECT employeename FROM user WHERE userid = ?", [$files['endrosementuserid']]);
    $files['batchreleaseusername'] = R::getCell("SELECT employeename FROM user WHERE userid = ?", [$files['batchreleaseuserid']]);
    $files['trafficnotifyusername'] = R::getCell("SELECT employeename FROM user WHERE userid = ?", [$files['trafficnotifyuserid']]);
    $files['otherusername'] = R::getCell("SELECT employeename FROM user WHERE userid = ?", [$files['otheruserid']]);
    $smarty->assign('file', $files);
    $smarty->display("shippingFilesView/editprint.html");
    $smarty->display("footer.html");
} elseif ($do == 'updateFilesData') {
    $fileid = filter_input(INPUT_POST, 'fileid');
    $col = filter_input(INPUT_POST, 'col');
    $type = filter_input(INPUT_POST, 'type');

    if ($fileid && $fileid > 0) {
        $value = filter_input(INPUT_POST, 'value');
        if ($type == 'note') {
            R::debug(true);
            R::exec("update shippingfile set $col = '$value' where id = $fileid");
        } else {
            $fileName = basename($_FILES['uploadedFile']['name']);
            $handle = new upload($_FILES['uploadedFile']);
            $fileName = uploadfile($handle, '../upload/shippingFiles');
            switch ($col) {
                case 'buybillfile':
                    $userCol = 'buybilluserid';
                    $dateCol = 'buybilldate';
                    break;
            case 'performafile':
                    $userCol = 'performauserid';
                    $dateCol = 'performadate';
                    break;
                case 'cocfile':
                    $userCol = 'cocuserid';
                    $dateCol = 'cocdate';
                    break;
                case 'finalbillfile':
                    $userCol = 'finalbilluserid';
                    $dateCol = 'finalbilldate';
                    break;
                case 'warehousefile':
                    $userCol = 'warehouseuserid';
                    $dateCol = 'warehousedate';
                    break;
                case 'shippolicyfile':
                    $userCol = 'shippolicyuserid';
                    $dateCol = 'shippolicydate';
                    break;
                case 'eurofile':
                    $userCol = 'eurouserid';
                    $dateCol = 'eurodate';
                    break;
                case 'customevalfile':
                    $userCol = 'customevaluserid';
                    $dateCol = 'customevaldate';
                    break;
                case 'model4file':
                    $userCol = 'model4userid';
                    $dateCol = 'model4date';
                    break;
                case 'endrosementfile':
                    $userCol = 'endrosementuserid';
                    $dateCol = 'endrosementdate';
                    break;
                case 'batchreleasefile':
                    $userCol = 'batchreleaseuserid';
                    $dateCol = 'batchreleasedate';
                    break;
                case 'trafficnotifyfile':
                    $userCol = 'trafficnotifyuserid';
                    $dateCol = 'trafficnotifydate';
                    break;
                case 'saleagreementfile':
                    $userCol = 'saleagreementuserid';
                    $dateCol = 'saleagreementdate';
                    break;
                case 'taxinvoicefile':
                    $userCol = 'taxinvoiceuserid';
                    $dateCol = 'taxinvoicedate';
                    break;
                case 'other':
                    $userCol = 'otheruserid';
                    $dateCol = 'otherdate';
                    break;
                default:
                    $col = '';
            }
            $date = date('Y-m-d H:i:s');
            try {
                R::exec("update shippingfile set $col = '$fileName', $userCol = " . $_SESSION['userid'] . ", $dateCol = '$date' where id = $fileid");
                
                $username = R::getCell('SELECT employeename FROM user WHERE userid = ?', [$_SESSION['userid']]);
                
                echo $fileName ? json_encode(array(Host_URL . '/upload/shippingFiles/' . $fileName, $date, $username)) : -1;
            } catch (Exception $e) {
                echo -1; // Handle error
            }
        }
    } else
        echo -1;
} else {
    header("location:shippingFilesController.php");
}
