<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$this->load->helper('url');
$this->load->helper('form');
?>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Quotation</title>
        <style type="text/css">
            .quot_items_detail_add{
                border: solid 1px silver;
                border-collapse: collapse;
                text-align: center;
                font-family: fantasy;
                width: 100%;
            }
            .rem_bor{
                border: none;
            }
            .quot_items_table{
                border: solid 1px silver;
                border-collapse: collapse;
                margin-top: 20px;
                text-align: center;
                font-family: inherit;
                width: 100%;
            }
            .quot_items_table th{ 
                border: #000 solid 2px;
            }
            .qt_det_td1{
                width: 100px;
            }
            .qt_det_td2{
                width:190px;
            }
        </style>
        <script type="text/javascript">
            var incr = 0;
            var totalAmount = parseFloat(0);
            var discount = parseFloat(0);
            var grandTotal = parseFloat(0);
            function addRowToTable() {
                if (validateFields()) {
                    incr++;
                    var cust = document.getElementById('quot_customer');
                    if (incr === 1) {
                        cust.disabled = true;
                        document.getElementById('txt_quot_customer').value = cust.value;
                    }
                    var printcode = document.getElementById('quot_print_code').value;
                    if (checkPrintItemAlreadyAdded(printcode)){
                        var qty = document.getElementById('quot_qty').value;
                        var price = document.getElementById('quot_price').value;
                        var tb = document.getElementById('table_quot_items');
                        var row = tb.rows.length;
                        var tr = tb.insertRow(row);
                        var td1 = tr.insertCell(0);
                        var remove_button = document.createElement("input");
                        remove_button.setAttribute("value", "Remove row");
                        remove_button.setAttribute("type", "button");
                        remove_button.setAttribute("onclick", "removeRowFromTable(this)");
                        td1.appendChild(remove_button);
                        var td2 = tr.insertCell(1);
                        var txt_pr_code = document.createElement("input");
                        txt_pr_code.setAttribute("type", "text");
                        txt_pr_code.setAttribute("value", printcode);
                        txt_pr_code.setAttribute("name", "quot_pr_code" + incr);
                        txt_pr_code.className = "rem_bor";
                        td2.appendChild(txt_pr_code);
                        var td3 = tr.insertCell(2);
                        var txt_pr_qty = document.createElement("input");
                        txt_pr_qty.setAttribute("type", "text");
                        txt_pr_qty.setAttribute("value", qty);
                        txt_pr_qty.setAttribute("name", "quot_pr_qty" + incr);
                        txt_pr_qty.className = "rem_bor";
                        td3.appendChild(txt_pr_qty);
                        var td4 = tr.insertCell(3);
                        var txt_pr_price = document.createElement("input");
                        txt_pr_price.setAttribute("type", "text");
                        txt_pr_price.setAttribute("name", "quot_pr_price" + incr);
                        txt_pr_price.setAttribute("value", price);
                        txt_pr_price.className = "rem_bor";
                        td4.appendChild(txt_pr_price);
                        totalAmount = parseFloat(totalAmount) + parseFloat(price);
                        document.getElementById("txt_total").value = totalAmount;
                        grandTotal = totalAmount - discount;
                        document.getElementById("txt_grand_total").value = grandTotal;
                        document.getElementById("txt_table_row_count").value = incr;
                        clearFields(document.getElementById('quot_qty'));
                        clearFields(document.getElementById('quot_price'));
                        clearFields(document.getElementById('quot_print_code'));
                        document.getElementById('quot_print_code').focus();
                    }
                }
            }

            function checkPrintItemAlreadyAdded(procode) {
                var booln = true;
                var tb = document.getElementById("table_quot_items");
                var rws = tb.rows.length;
                for (var i = 0; i < rws; i++) {
                    var tr_code = tb.rows[i].cells[1].childNodes[0].value;
                    if (procode === tr_code) {
                        booln = false;
                        alert("Print Item Alredy In The Quotatin.");
                    }
                }
                return booln;
            }

            function removeRowFromTable(remove) {
                var txt_price = remove.parentNode.parentNode.cells[3].childNodes[0];
                var price = txt_price.value;
                totalAmount = parseFloat(totalAmount) - parseFloat(price);
                document.getElementById("txt_total").value = totalAmount;
                grandTotal = totalAmount - discount;
                document.getElementById("txt_grand_total").value = grandTotal;
                remove.parentNode.parentNode.remove();
            }

            function clearFields(input_element) {
                input_element.value = "";
            }

            function validateFields() {
                var bool = true;
                var printcode = document.getElementById('quot_print_code');
                var qty = document.getElementById('quot_qty');
                var price = document.getElementById('quot_price');
                var cust = document.getElementById('quot_customer');
                if (cust.value === "") {
                    cust.parentNode.style.border = "1px solid red";
                    bool = false;
                } else {
                    cust.parentNode.style.border = "";
                }
                if (printcode.value === "") {
                    printcode.parentNode.style.border = "1px solid red";
                    bool = false;
                } else {
                    printcode.parentNode.style.border = "";
                }
                if (qty.value === "") {
                    qty.parentNode.style.border = "1px solid red";
                    bool = false;
                } else {
                    qty.parentNode.style.border = "";
                }
                if (price.value === "") {
                    price.parentNode.style.border = "1px solid red";
                    bool = false;
                } else {
                    price.parentNode.style.border = "";
                }
                return bool;
            }
            function validateSubmit(form) {
                var bool = true;
                var total = document.getElementById("txt_total");
                if (total.value === "0") {
                    bool = false;
                    alert("Add Quotation Details to the Quotation And Then Submit.");
                }
                if (bool) {
                    form.submit();
                }
            }
        </script>
    </head>
    <body>
    <center>
        <div style="border: 2px solid black; width: 800px;">
            <div style="font-family: sans-serif; font-size: 30px; font-weight: bolder; margin-top: 50px; margin-bottom: 30px;">Quotation Add</div>

            <div class="item_mange" style="width: 100%;">
                <table cellpadding="5" class="quot_items_detail_add" border="1">
                    <tbody>
                        <tr>
                            <td>Customer</td>
                            <td style="background-color: #99ccff;"></td>
                            <td>Print Code</td>
                            <td style="background-color: #99ccff;"></td>
                            <td>Quantity</td>
                            <td style="background-color: #99ccff;"></td>
                            <td>Price</td>
                        </tr>
                        <tr>
                            <td>
                                <input id="quot_customer" class="rem_bor" type="text" name="quot_customer" value="" list="customer_list" placeholder="Select a Customer" />
                                <datalist id="customer_list">
                                    <?php
                                    foreach ($customers->result() as $cus) {
                                        ?>
                                        <option value="<?php echo $cus->customer_name; ?>">
                                            <?php
                                        }
                                        ?>
                                </datalist>
                            </td>
                            <td style="background-color: #99ccff;"></td>
                            <td><input id="quot_print_code" class="rem_bor" type="text" name="quot_print_code" value="" placeholder="Enter a Product Code"  /></td>
                            <td style="background-color: #99ccff;"></td>
                            <td><input id="quot_qty" class="rem_bor" type="number" name="quot_qty" value="" placeholder="Enter the Qty"  /></td>
                            <td style="background-color: #99ccff;"></td>
                            <td><input id="quot_price" class="rem_bor" type="number" name="quot_price" value="" placeholder="Enter the Price"  /></td>
                        </tr>
                        <tr>
                            <td>
                            </td>
                            <td style="background-color: #99ccff;"></td>
                            <td></td>
                            <td style="background-color: #99ccff;"></td>
                            <td></td>
                            <td style="background-color: #99ccff;"></td>
                            <td align="right"><input type="button" value="Add Row Details" onclick="addRowToTable()" /></td>
                        </tr>
                    </tbody>
                </table>
            </div>
            <?php
            $from_attributes = array('onsubmit' => "validateSubmit(this); return false;");
            echo form_open("Quotation_Controller/saveQuotationAndEmail", $from_attributes);
            ?>
            <div class="quot_items">
                <table border="1" class="quot_items_table">
                    <thead style="border: #000 solid 3px; background-color: #f1f1f1;">
                        <tr>
                            <th class="qt_det_td1">Remove</th>
                            <th class="qt_det_td2">Item Code</th>
                            <th class="qt_det_td1">Quantity</th>
                            <th class="qt_det_td2">Price</th>
                        </tr>
                    </thead>
                    <tbody id="table_quot_items">
                    </tbody>
                </table>
            </div>
            <div class="quot_details" align="right" style="width: 800px;">
                <table border="0">
                    <tbody>
                        <tr>
                            <td class="qt_det_td1">Total</td>
                            <td>:</td>
                            <td class="qt_det_td2"><input id="txt_total" type="text" name="quot_total" value="0" readonly=""/></td>
                        </tr>
                        <tr>
                            <td class="qt_det_td1">Discount</td>
                            <td>:</td>
                            <td class="qt_det_td2"><input id="txt_discount" type="text" name="quot_discount" value="0" readonly=""/></td>
                        </tr>
                        <tr>
                            <td class="qt_det_td1">Grand Total</td>
                            <td>:</td>
                            <td class="qt_det_td2"><input id="txt_grand_total" type="text" name="quot_grand_total" value="0" readonly=""/></td>
                        </tr>
                    </tbody>
                </table>
                <div style=""><input hidden="" id="txt_quot_customer" type="text" name="txt_quot_customer" value="" /><input hidden="" id="txt_table_row_count" type="text" name="txt_table_row_count" value="" /></div>
            </div>
            <div align="right">
                <input type="submit" value="Submit & Send Mail" />
            </div>
            <?php
            echo form_close();
            ?>
        </div>
    </center>
</body>
</html>


