<?php
include("../classes/manager.class.php");
ini_set('display_errors', 1);
error_reporting(E_ALL);
$m = new Manager();

if (!isset($_SESSION['sess_userid'])) { header("Location: " . PROJECT_ADMIN_URL); exit(); }

$output = '';

/** soft delete -> status = deleted */
if (isset($_GET['delete'])) {
    $supplierId = (int)$_GET['delete'];
    $data = array('status' => 'deleted');
    SupplierSetup::UpdateSupplierSetup($supplierId, $data);
    header("Location: " . PROJECT_ADMIN_URL . "supplier_setup.php?view");
    exit();
}

$name = $location = $contact = $email = $short_code = $gst = $ntn = $status = "";
$balance = "0.00"; // keep if you later add this column; harmless if not present

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include('includes/inc_head.php'); ?>
</head>
<body>
<div id="loader-wrapper"><div id="loader"></div><div class="loader-section section-left"></div><div class="loader-section section-right"></div></div>

<div id="container">
<?php include('includes/inc_top.php'); ?>
<div class="row">
<div class="col s12 m12">
    <div class=""><h3>Supplier Setup</h3></div>
    <hr>

    <?php if ($_SESSION['sess_role'] == 'VEC' && isset($_GET['view'])) { ?>
        <a class="btn waves-effect waves-light" style="float:right;margin-left:5px;" href="?add">Add
            <i class="mdi-content-add-box" style="font-size:12px;"></i></a>
        <div class="clearfix"></div>
    <?php } ?>

    <br/>

<?php
/*********************
 * ADD
 *********************/
if (isset($_GET['add'])) {

    if (!empty($_POST)) {
        extract($_REQUEST);

        $data = [];
        // If you later add a `code` column, you can set it here:
        // $data['code'] = 'SUP' . time();
        $data['name']        = $name;
        $data['location']    = $location;
        $data['contact']     = $contact;
        $data['email']       = $email;
        $data['short_code']  = $short_code;
        $data['gst']         = $gst;
        $data['ntn']         = $ntn;
        $data['status']      = $status ?: 'active';
        $data['datetime']    = date('Y-m-d H:i:s');

        $supplierCheck = SupplierSetup::CheckSupplier($name, 0);

        $errors = array();
        if ((strlen($name) == 0) || (strlen($name) > 255)) $errors[] = "Please enter supplier name";
        if ((strlen($location) == 0)) $errors[] = "Please enter location";
        if ((strlen($contact) == 0))  $errors[] = "Please enter contact";
        if ($supplierCheck)           $errors[] = "Supplier name already exists";

        if (count($errors) == 0) {
            $q = SupplierSetup::AddSupplierSetup($data);
            if ($q) {
                echo "<script>window.location.href='?view&sucess'</script>";
            } else {
                echo "<div id='card-alert' class='card orange lighten-5'><div class='card-content orange-text'><p>WARNING : <strong>Attention! </strong>Some error occurred!</p></div></div>";
            }
        } else {
            foreach ($errors as $val) {
                $output .= "<div id='card-alert' class='card orange lighten-5'><div class='card-content orange-text'><p>WARNING : <strong>Attention! </strong>$val</p></div></div>";
            }
        }
    }
    ?>
    <div class="row">
        <form action="" method="post" id="supplier_setup_form" novalidate="novalidate">
            <div class="col s12">
                <?php echo $output; ?>
                <div class="col s8">
                    <div class="row"><span>(*) Mark Fields are mandatory</span><br/><br/></div>

                    <div class="row margin">
                        <div class="input-field col s12">
                            <input type="text" id="name" name="name" maxlength="250" value="<?php echo htmlspecialchars($name); ?>"/>
                            <label for="name">Supplier Name *</label>
                        </div>
                    </div>

                    <div class="row margin">
                        <div class="input-field col s12">
                            <textarea class="materialize-textarea" id="location" name="location"><?php echo htmlspecialchars($location); ?></textarea>
                            <label for="location">Location *</label>
                        </div>
                    </div>

                    <div class="row margin">
                        <div class="input-field col s12">
                            <input type="text" id="contact" name="contact" value="<?php echo htmlspecialchars($contact); ?>"/>
                            <label for="contact">Contact *</label>
                        </div>
                    </div>

                    <div class="row margin">
                        <div class="input-field col s12">
                            <input type="text" id="email" name="email" maxlength="100" value="<?php echo htmlspecialchars($email); ?>"/>
                            <label for="email">Email</label>
                        </div>
                    </div>

                    <div class="row margin">
                        <div class="input-field col s12">
                            <input type="text" id="short_code" name="short_code" maxlength="50" value="<?php echo htmlspecialchars($short_code); ?>"/>
                            <label for="short_code">Short Code</label>
                        </div>
                    </div>

                    <div class="row margin">
                        <div class="input-field col s6">
                            <input type="text" id="gst" name="gst" maxlength="50" value="<?php echo htmlspecialchars($gst); ?>"/>
                            <label for="gst">GST</label>
                        </div>
                        <div class="input-field col s6">
                            <input type="text" id="ntn" name="ntn" maxlength="50" value="<?php echo htmlspecialchars($ntn); ?>"/>
                            <label for="ntn">NTN</label>
                        </div>
                    </div>

                    <div class="row margin">
                        <div class="input-field col s6">
                            <select name="status" id="status">
                                <option value="active" selected>Active</option>
                                <option value="inactive">Inactive</option>
                            </select>
                            <label for="status">Status</label>
                        </div>
                    </div>

                    <div class="row margin">
                        <div class="input-field col s12">
                            <button id="btnsubmit" type="submit" class="btn waves-effect waves-light">Submit</button>
                            <a href="supplier_setup.php?view"><button type="button" class="btn waves-effect waves-light">Cancel</button></a>
                        </div>
                    </div>

                </div>
            </div>
        </form>
    </div>
<?php
} // end add

/*********************
 * VIEW LIST
 *********************/
if (isset($_GET['view'])) {
    if (isset($_GET['sucess'])) { ?>
        <div id='card-alert' class='card green lighten-5'><div class='card-content green-text'><p>Successfully updated!</p></div></div>
    <?php } ?>
    <div class="table-datatables">
        <table id="data-table-simple" class="responsive-table display">
            <thead>
            <tr>
                <th>Action</th>
                <th>Supplier Name</th>
                <th>Location</th>
                <th>Contact</th>
                <th>Email</th>
                <th>GST</th>
                <th>NTN</th>
                <th>Status</th>
                <th>Date</th>
            </tr>
            </thead>
            <tfoot>
            <tr>
                <th>Action</th>
                <th>Supplier Name</th>
                <th>Location</th>
                <th>Contact</th>
                <th>Email</th>
                <th>GST</th>
                <th>NTN</th>
                <th>Status</th>
                <th>Date</th>
            </tr>
            </tfoot>
            <tbody>
            <?php
            $qry = SupplierSetup::GetSupplierSetupList();
            if ($qry) {
                foreach ($qry as $q) {
                    if ($q->status == 'deleted') continue; ?>
                    <tr>
                        <td>
                            <?php if ($_SESSION['sess_role'] == 'VEC' || $_SESSION['sess_role'] == 'VE') { ?>
                                <a href='?edit&id=<?php echo $q->id; ?>&v' title="View">
                                    <span class="mini-stat-icon pink" style="width:20px;height:20px;font-size:12px;line-height:20px;"><i class="mdi-action-description"></i></span>
                                </a>
                                <a href='?edit&id=<?php echo $q->id; ?>' title="Edit">
                                    <span class="mini-stat-icon orange" style="width:20px;height:20px;font-size:12px;line-height:20px;"><i class="mdi-image-edit"></i></span>
                                </a>
                                <a style="cursor:pointer;font-size:10px;padding:1px;" title="Delete">
                                    <span class="btn-warning-confirm<?= $q->id ?> mini-stat-icon deep-orange darken-3" style="width:60px;height:20px;border-radius:5%;font-size:12px;line-height:20px;"><i class="fa fa-arrows-alt">Delete</i></span>
                                </a>
                                <script>
                                    $('.btn-warning-confirm<?= $q->id ?>').click(function () {
                                        swal({
                                                title: "Are you sure?",
                                                text: "Are you sure change status to Delete?",
                                                type: "warning",
                                                showCancelButton: true,
                                                confirmButtonColor: '#DD6B55',
                                                confirmButtonText: 'Yes',
                                                closeOnConfirm: false
                                            },
                                            function () {
                                                swal("Successful!", "Deleted!", "success");
                                                window.location.href = 'supplier_setup.php?delete=<?php echo $q->id; ?>';
                                            });
                                    });
                                </script>
                            <?php } else { ?>
                                <a href='?edit&id=<?php echo $q->id; ?>&v' title="View">
                                    <span class="mini-stat-icon pink" style="width:20px;height:20px;font-size:12px;line-height:20px;"><i class="mdi-action-description"></i></span>
                                </a>
                            <?php } ?>
                        </td>
                        <td><?php echo ucwords($q->name); ?></td>
                        <td><?php echo ucwords($q->location); ?></td>
                        <td><?php echo $q->contact; ?></td>
                        <td><?php echo $q->email; ?></td>
                        <td><?php echo $q->gst; ?></td>
                        <td><?php echo $q->ntn; ?></td>
                        <td><?php echo ($q->status == 'active') ? 'Active' : 'Inactive'; ?></td>
                        <td><?php echo $q->datetime; ?></td>
                    </tr>
                    <?php
                }
            } ?>
            </tbody>
        </table>
    </div>
<?php } // end view

/*********************
 * EDIT / VIEW
 *********************/
if (isset($_GET['edit'])) {
    $id = (int)$_GET['id'];

    if (!empty($_POST)) {
        extract($_REQUEST);
        $data = [];
        $data['name']       = $name;
        $data['location']   = $location;
        $data['contact']    = $contact;
        $data['email']      = $email;
        $data['short_code'] = $short_code;
        $data['gst']        = $gst;
        $data['ntn']        = $ntn;
        $data['status']     = $status;
        $data['datetime']   = 'NOW()';

        $supplierCheck = SupplierSetup::CheckSupplier($name, $id);

        $errors = array();
        if ((strlen($name) == 0) || (strlen($name) > 255)) $errors[] = "Please enter supplier name";
        if ((strlen($location) == 0)) $errors[] = "Please enter location";
        if ((strlen($contact) == 0))  $errors[] = "Please enter contact";
        if ($supplierCheck)           $errors[] = "Supplier name already exists";

        if (count($errors) == 0) {
            $q = SupplierSetup::UpdateSupplierSetup($id, $data);
            if ($q) {
                echo "<script>window.location.href='?view&sucess'</script>";
            } else {
                echo "<div id='card-alert' class='card orange lighten-5'><div class='card-content orange-text'><p>WARNING : <strong>Attention! </strong>Some error occurred!</p></div></div>";
            }
        } else {
            foreach ($errors as $val) {
                $output .= "<div id='card-alert' class='card orange lighten-5'><div class='card-content orange-text'><p>WARNING : <strong>Attention! </strong>$val</p></div></div>";
            }
        }
    }

    if (empty($_POST)) {
        $qry = SupplierSetup::GetSupplierSetupById($id);
        $name       = $qry->name;
        $location   = $qry->location;
        $contact    = $qry->contact;
        $email      = $qry->email;
        $short_code = $qry->short_code;
        $gst        = $qry->gst;
        $ntn        = $qry->ntn;
        $status     = $qry->status;
        // $code    = $qry->code; // only if you add code column later
    }

    if (isset($_REQUEST['v'])) { ?>
        <div class="row">
            <div class="col s12 form-group">
                <div class="col s8">
                    <div class="row"><div class="col s6">Supplier Name:</div><div class="col s6"><?php echo $name; ?></div></div>
                    <div class="row"><div class="col s6">Location:</div><div class="col s6"><?php echo $location; ?></div></div>
                    <div class="row"><div class="col s6">Contact:</div><div class="col s6"><?php echo $contact; ?></div></div>
                    <div class="row"><div class="col s6">Email:</div><div class="col s6"><?php echo $email; ?></div></div>
                    <div class="row"><div class="col s6">Short Code:</div><div class="col s6"><?php echo $short_code; ?></div></div>
                    <div class="row"><div class="col s6">GST:</div><div class="col s6"><?php echo $gst; ?></div></div>
                    <div class="row"><div class="col s6">NTN:</div><div class="col s6"><?php echo $ntn; ?></div></div>
                    <div class="row"><div class="col s6">Status:</div><div class="col s6"><?php echo ($status=='active'?'Active':'Inactive'); ?></div></div>
                </div>
            </div>
        </div>
    <?php } else { ?>
        <div class="row">
            <form action="?edit&id=<?php echo $id ?>" method="post" id="supplier_setup_form" novalidate="novalidate">
                <div class="col s12">
                    <div class="col s8">
                        <div class="row"><?php echo $output; ?></div>
                        <div class="row margin"><span>(*) Mark Fields are mandatory</span><br/><br/></div>

                        <div class="row margin">
                            <div class="input-field col s12">
                                <input type="text" id="name" name="name" maxlength="250" value="<?php echo htmlspecialchars($name); ?>"/>
                                <label for="name" class="control-label">Supplier Name *</label>
                            </div>
                        </div>

                        <div class="row margin">
                            <div class="input-field col s12">
                                <textarea class="materialize-textarea" id="location" name="location"><?php echo htmlspecialchars($location); ?></textarea>
                                <label for="location" class="control-label">Location *</label>
                            </div>
                        </div>

                        <div class="row margin">
                            <div class="input-field col s12">
                                <input type="text" id="contact" name="contact" value="<?php echo htmlspecialchars($contact); ?>"/>
                                <label for="contact" class="control-label">Contact *</label>
                            </div>
                        </div>

                        <div class="row margin">
                            <div class="input-field col s12">
                                <input type="text" id="email" name="email" maxlength="100" value="<?php echo htmlspecialchars($email); ?>"/>
                                <label for="email" class="control-label">Email</label>
                            </div>
                        </div>

                        <div class="row margin">
                            <div class="input-field col s12">
                                <input type="text" id="short_code" name="short_code" maxlength="50" value="<?php echo htmlspecialchars($short_code); ?>"/>
                                <label for="short_code" class="control-label">Short Code</label>
                            </div>
                        </div>

                        <div class="row margin">
                            <div class="input-field col s6">
                                <input type="text" id="gst" name="gst" maxlength="50" value="<?php echo htmlspecialchars($gst); ?>"/>
                                <label for="gst" class="control-label">GST</label>
                            </div>
                            <div class="input-field col s6">
                                <input type="text" id="ntn" name="ntn" maxlength="50" value="<?php echo htmlspecialchars($ntn); ?>"/>
                                <label for="ntn" class="control-label">NTN</label>
                            </div>
                        </div>

                        <div class="row margin">
                            <div class="input-field col s6">
                                <select name="status" id="status">
                                    <option value="active" <?php echo ($status=='active'?'selected':''); ?>>Active</option>
                                    <option value="inactive" <?php echo ($status=='inactive'?'selected':''); ?>>Inactive</option>
                                </select>
                                <label for="status" class="control-label">Status</label>
                            </div>
                        </div>

                        <div class="row margin">
                            <div class="input-field col s12">
                                <?php if ($_SESSION['sess_role'] == 'VE' || $_SESSION['sess_role'] == 'VEC') { ?>
                                    <button id="btnsubmit" type="submit" class="btn waves-effect waves-light">Submit</button>
                                <?php } ?>
                                <a href="supplier_setup.php?view"><button type="button" class="btn waves-effect waves-light">Cancel</button></a>
                            </div>
                        </div>

                    </div>
                </div>
            </form>
        </div>
    <?php }
} // end edit
?>
</div>
</div>
</div>

<?php include('includes/inc_footer.php'); ?>
<?php include('includes/inc_js.php'); ?>
<script type="application/javascript">
$(document).ready(function () {
    var fewSeconds = 2;
    $('#btnsubmit').click(function () {
        var btn = $(this);
        btn.hide();
        setTimeout(function () { btn.show(); }, fewSeconds * 1000);
    });
});
</script>
</body>
</html>
