<?php
include("../../classes/manager.class.php");
//ini_set('display_errors', 1);
//error_reporting(E_ALL);
$m = new Manager();
$output = '';
$amount = '';
$withholding_tax = '';
$sales_tax = '';
$goods_return = '';
$shelf_rent = '';
$builty_charges = '';
$other_expense = '';
$customer_id = "";
$payment_type = "";
$bank_name = "";
$account_no = "";
$hide = "";
$cheque_no = "";
$cheque_bank_name = "";
$cheque_date = "";
$cheque_payee = "";
$from_bank_name = "";
$party_name = "";
if (!isset($_SESSION['sess_userid'])) {
    header("Location: " . PROJECT_ADMIN_URL);
    exit();
}
if (isset($_GET['id'])) {
    $checkPay = Payment::GetPaymentSetupById($_GET['id']);
    $checkInv = Invoice::GetInvoiceSetupById($checkPay->invoice_reference_code);
//    if (!empty($checkInv) && $checkInv->status == 'Paid') {
//        header("Location: " . PROJECT_ADMIN_URL . "transaction/payment.php?view");
//        exit();
//    }
}
$current_date = date('d-m-Y');
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include('../includes/inc_head.php'); ?>
    <script>
        $(function () {
            $('.datepicker').pickadate({
                format: 'dd-mm-yyyy',
                selectMonths: true,
                selectYears: true,
            });
        });
    </script>
    <script>
        $(function () {
            $('#datepicker').pickadate({
                format: 'dd-mm-yyyy',
                selectMonths: true,
                selectYears: true,
            });
            $('#cheque_date').pickadate({
                format: 'dd-mm-yyyy',
                selectMonths: true,
                selectYears: true,
            });
        });
    </script>
</head>
<body>
<!-- Start Page Loading -->
<div id="loader-wrapper">
    <div id="loader"></div>
    <div class="loader-section section-left"></div>
    <div class="loader-section section-right"></div>
</div>
<!-- End Page Loading -->
<div id="container">
    <?php include('../includes/inc_top.php'); ?>
    <div class="row">
        <div class="col s12 m12">
            <div class="">
                <h3>Payment</h3>
            </div>
            <hr>
            <?php if ($_SESSION['sess_role'] == 'VEC') {
                if (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"></i></a>
                    <div class="clearfix"></div>
                <?php }
            } ?>
            <br/>
            <?php
            if (isset($_GET['add']) || isset($_GET['edit'])) {
                if (isset($_GET['edit'])) {
                    extract($_REQUEST);
                    $getPayment = Payment::GetPaymentSetupById($id);
                    $customer_id = $getPayment->customer_id;
                    $invoice_reference_code = $getPayment->invoice_reference_code;
                    $payment_type = $getPayment->payment_type;
                    $amount = $getPayment->amount;
                    $withholding_tax = $getPayment->withholding_tax;
                    $sales_tax = $getPayment->sales_tax;
                    $goods_return = $getPayment->goods_return;
                    $shelf_rent = $getPayment->shelf_rent;
                    $builty_charges = $getPayment->builty_charges;
                    $other_expense = $getPayment->other_expense;
                    $party_name = $getPayment->party_name;
                    $from_bank_name = $getPayment->from_bank_name;
                    $cheque_no = $getPayment->cheque_number;
                    $cheque_bank_name = $getPayment->cheque_bank_name;
                    $cheque_payee = $getPayment->cheque_payee_name;
                    $cheque_date = date('d-m-Y', strtotime($getPayment->cheque_date));
                    $bank_name = $getPayment->bank_name;
                    $account_no = $getPayment->bank_account_number;
                    $payment_date = date('d-m-Y', strtotime($getPayment->create_date));
                    $paymentNumber = $getPayment->code;
                    $description = $getPayment->description;
                } else if (isset($_GET['add']) || isset($_GET['ref'])) {
                    $paymentfilter = " ORDER BY id DESC limit 1 ";
                    $payments = Payment::GetPaymentList($paymentfilter);
                    if (!empty($payments[0]->code)) {
                        $paymentNumber = intval($payments[0]->code);
                        $paymentNumber = $paymentNumber + 1;
                    } else {
                        $paymentNumber = __SALES__;
                    }
                }
                if (isset($_GET['ref'])) {
                    $selectedInvoice = Invoice::GetInvoiceSetupById($_GET['ref']);
                    $amount = $selectedInvoice->total_amount;
                    $invoice_reference_code = $selectedInvoice->id;
                    $customer_id = $selectedInvoice->customer_id;
                    $payment_date = date('d-m-Y');
                }
            }
            if (isset($_POST) && !empty($_POST)) {
                extract($_REQUEST);
                //print_r($_REQUEST);
                //exit;
                $data['customer_id'] = $customer_name;
                $data['code'] = $payment_number;
                $data['invoice_reference_code'] = $invoice_reference_code;
                $data['payment_type'] = $payment_type;
                $data['amount'] = $amount;
                $data['withholding_tax'] = $withholding_tax;
                $data['sales_tax'] = $sales_tax;
                $data['goods_return'] = $goods_return;
                $data['shelf_rent'] = $shelf_rent;
                $data['builty_charges'] = $builty_charges;
                $data['other_expense'] = $other_expense;
                $data['party_name'] = $party_name;
                $data['from_bank_name'] = $from_bank_name;
                $data['cheque_number'] = $cheque_no;
                $data['cheque_bank_name'] = $cheque_bank_name;
                $data['cheque_payee_name'] = $cheque_payee;
                $data['cheque_date'] = date('Y-m-d H:i:s', strtotime(date('H:i:s'), strtotime($cheque_date)));
                $data['bank_name'] = $bank_name;
                $data['bank_account_number'] = $account_no;
                $data['create_date'] = date('Y-m-d H:i:s', strtotime(date('H:i:s'), strtotime($payment_date)));
                $data['datetime'] = 'NOW()';
                $data['description'] = $description;

                $errors = array();
                // Validate the input
                $filterPayments = " WHERE cheque_number = '$cheque_no'";
                $checkPayments = Payment::GetPaymentList($filterPayments);

                if (isset($_GET['add']) || isset($_GET['ref'])) {
                    if (!empty($checkPayments) && $payment_type == 'bank') {
                        array_push($errors, "Cheque No Already Exists");
                    }
                }


                if ((strlen($customer_name) == 0) || (strlen($customer_name) > 255))
                    array_push($errors, "Please enter customer name");
                if ((strlen($payment_date) == 0))
                    array_push($errors, "Please enter payment date");
                if ((strlen($payment_type) == 0))
                    array_push($errors, "Please enter payment type");
                if ((strlen($amount) == 0 || $amount == 0))
                    array_push($errors, "Please enter payment amount");
                if ($payment_type == 'bank') {
                    if ((strlen($bank_name) == 0))
                        array_push($errors, "Please enter bank name");
                }

                if (count($errors) == 0) {
                    if (isset($_GET['add']) || isset($_GET['ref'])) {
                        $q = Payment::AddPayment($data);

                    } else {
                        $q = Payment::UpdatePaymentSetup($id, $data);
                    }
                    if ($q) {
                            // If the payment query was successful
                            
                                // Prepare the data for updating the purchase table
                                $paymentMode = isset($_POST['paymentmode']) ? $_POST['paymentmode'] : '';
                                if (!empty($paymentMode)) {
                                    // Data to update in the purchase table
                                    $invoiceUpdateData = [
                                        'status' => $paymentMode, // Update status with the payment mode
                                    ];

                                    // Call the PurchaseSetup::UpdatePurchaseSetup method
                                    $updateResult = Invoice::UpdateInvoiceSetup($invoice_reference_code, $invoiceUpdateData);

                                    // Check if the update was successful
                                    if ($updateResult) {
                                        // Optionally, log success or display a success message
                                        echo "<div class='alert alert-success fade in'>
                                                <button type='button' class='close' data-dismiss='alert'>&times;</button>
                                                <strong>Attention!</strong> Invoice status updated successfully. </div>";
                                    } else {
                                        // Optionally, display an error message
                                        echo "<div class='alert alert-warning fade in'>
                                                <button type='button' class='close' data-dismiss='alert'>&times;</button>
                                                <strong>Attention!</strong> Failed to update Invoice status. </div>";
                                    }
                                }
                            

                            // Redirect or display a success message
                            echo "<script>window.location.href = 'invoice.php?view&success'</script>";
                            echo "<div class='alert alert-success fade in'>
                                    <button type='button' class='close' data-dismiss='alert'>&times;</button>
                                    <strong>Attention!</strong> Payment submitted successfully! </div>";
                        } else {
                            echo "<div class='alert alert-error fade in'>
                                    <button type='button' class='close' data-dismiss='alert'>&times;</button>
                                    <strong>Attention!</strong> Some error occurred! </div>";
                        }

                }
            }
            ?>
            <form action="" method="post" id="payment_create_form" novalidate="novalidate">
                <div class="row">
                    <div class="col s12 form-group">
                        <?php echo $output; ?>
                        <div class="row">
                            <div class="col m6 s12">
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="sales_person"
                                               name="sales_person" readonly
                                               value='<?php echo $_SESSION['sess_username']; ?>'/>
                                        <input class="form-control" type="hidden" id="sales_person_id"
                                               name="sales_person_id"
                                               value='<?php echo $_SESSION['sess_userid']; ?>'/>
                                        <label for="sales_person" class="control-label">Sales Person *</label>
                                    </div>
                                </div>
                            </div>
                            <div class="col m6 s12">
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="payment_number"
                                               name="payment_number" readonly
                                               value='<?php echo $paymentNumber; ?>'/>
                                        <label for="payment_number" class="control-label">Payment No. *</label>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="clearfix"></div>
                        <div class="row">
                            <div class="col m6 s12">
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <?php
                                        $customersfilter = " WHERE status = 'active'";
                                        $customers = CustomerSetup::GetCustomerSetupList($customersfilter);
                                        ?>
                                        <select class="form-control" required id="customer_name"
                                                name="customer_name">
                                            <option></option>
                                            <?php
                                            if ($customers) {
                                                foreach ($customers AS $customersRow) {
                                                    echo '<option ';
                                                    if ($customersRow->id == $customer_id) {
                                                        echo 'selected';
                                                    }
                                                    echo ' value="' . $customersRow->id . '" >' . $customersRow->name . '</option>';
                                                }
                                            }
                                            ?>
                                        </select>
                                        <label for="customer_name" class="control-label">Customer *</label>
                                    </div>
                                </div>
                            </div>
                            <div class="col m6 s12">
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" required type="text" id="datepicker"
                                               name="payment_date"
                                               placeholder="<?= date('01-01-Y') ?>"
                                               value='<?= !empty($payment_date) ? $payment_date : $current_date; ?>'/>
                                        <label for="payment_date" class="control-label">Payment Date *</label>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="clearfix"></div>
                        <div class="row">
                            <div class="col m6 s12">
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <select class="form-control" id="payment_type" required
                                                name="payment_type">
                                            <option></option>
                                            <option <?= $payment_type == 'cash' ? 'selected' : '' ?>
                                                value="cash">Cash
                                            </option>
                                            <option <?= $payment_type == 'bank' ? 'selected' : '' ?>
                                                value="bank">Bank
                                            </option>
                                            <option <?= $payment_type == 'party' ? 'selected' : '' ?>
                                                value="party">Party
                                            </option>
                                            <option <?= $payment_type == 'online' ? 'selected' : '' ?>
                                                value="online">Online
                                            </option>
                                        </select>
                                        <label for="payment_type" class="control-label">Payment Type *</label>
                                    </div>
                                </div>
                            </div>
                            <div class="col m6 s12">
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" placeholder="00000.00"
                                               onkeypress="return isDoubleNumberKey(event)" required type="text"
                                               id="amount"
                                               value="<?= $amount ?>"
                                               name="amount"/>
                                        <label for="amount" class="control-label">Amount *</label>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col m6 s12">
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <?php
                                        if (isset($_GET)) {
                                            $filterInvoices = " WHERE customer_id = '$customer_id'";
                                        }
                                        $invoices = Invoice::GetInvoiceList($filterInvoices);
                                        ?>
                                        <select class="form-control" id="invoice_reference_code"
                                                name="invoice_reference_code">
                                            <option></option>
                                            <?php
                                            //echo $invoice_reference_code;
                                            if ($invoices) {
                                                foreach ($invoices AS $invoicesRow) {
                                                    echo '<option ';
                                                    if ($invoicesRow->id == $invoice_reference_code) {
                                                        echo 'selected';
                                                    }
                                                    echo ' value="' . $invoicesRow->id . '" >' . $invoicesRow->code . '</option>';
                                                }
                                            }
                                            ?>
                                        </select>
                                        <label for="invoice_reference_code" class="control-label">Invoice Ref.</label>
                                    </div>
                                </div>
                            </div>
                           <div class="col m6 s12">
                              <label class="control-label">Payment Mode</label>
                              <select class="form-control" id="paymentmode" name="paymentmode">
                                  <option value="paid" <?= $paymentmode === 'paid' ? 'selected' : '' ?>>Full Payment</option>
                                  <option value="Pending" <?= $paymentmode === 'Pending' ? 'selected' : '' ?>>Partial Payment</option>
                              </select>
                      
                          </div>
                      </div>
                            <div id="bankDetails">
                                <div <?= $payment_type != 'party' ? 'style="display:none;"' : '' ?>
                                    class="col m6 s12" id="party_name_detail">
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <input class="form-control" type="text" id="party_name"
                                                <?php if ($payment_type != 'party') {
                                                    echo 'disabled';
                                                } ?> name="party_name" value="<?= $party_name ?>"/>
                                            <label for="party_name" class="control-label">Party Name</label>
                                        </div>
                                    </div>
                                </div>
                                <div <?= $payment_type != 'online' ? 'style="display:none;"' : '' ?>
                                    class="col m6 s12" id="from_bank_name_detail">
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <input class="form-control" type="text" id="from_bank_name"
                                                <?php if ($payment_type != 'online') {
                                                    echo 'disabled';
                                                } ?> name="from_bank_name" value="<?= $from_bank_name ?>"/>
                                            <label for="from_bank_name" class="control-label">From Bank Name</label>
                                        </div>
                                    </div>
                                </div>
                                <div class="clearfix"></div>
                                <br>

                                <div class="col m6 s12">
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <?php
                                            $bankfilter = " WHERE status = 'active' ORDER BY bank_name";
                                            $banks = BankSetup::GetBankSetupList($bankfilter);
                                            ?>
                                            <select class="form-control" id="bank_name" required
                                                    name="bank_name">
                                                <option></option>
                                                <?php
                                                if ($banks) {
                                                    foreach ($banks AS $banksRow) {
                                                        echo '<option ';
                                                        if ($banksRow->id == $bank_name) {
                                                            echo 'selected';
                                                        }
                                                        echo ' value="' . $banksRow->id . '" >' . $banksRow->bank_name . '</option>';
                                                    }
                                                }
                                                ?>
                                            </select>
                                            <label for="bank_name" class="control-label">Bank Name *</label>
                                        </div>
                                    </div>
                                </div>
                                <div class="col m6 s12">
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <input class="form-control" readonly type="text" id="account_no"
                                                   name="account_no" value="<?= $account_no ?>"/>
                                            <label for="account_no" class="control-label">Account No.</label>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <?php if ($payment_type == 'cash' || $payment_type == 'online') {
                            $hide = 'style="display:none;"';
                        } ?>
                        <div <?= $hide ?> class="row" id="chequeDetails">
                            <div class="col m6 s12">
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="cheque_no"
                                            <?php if ($payment_type == 'cash' || $payment_type == 'online') {
                                                echo 'disabled';
                                            } ?> name="cheque_no" value="<?= $cheque_no ?>"/>
                                        <label for="cheque_no" class="control-label">Cheque No.</label>
                                    </div>
                                </div>
                            </div>
                            <div class="col m6 s12">
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="cheque_bank_name"
                                            <?php if ($payment_type == 'cash' || $payment_type == 'online') {
                                                echo 'disabled';
                                            } ?> name="cheque_bank_name" value="<?= $cheque_bank_name ?>"/>
                                        <label for="cheque_bank_name" class="control-label">Cheque Bank</label>
                                    </div>
                                </div>
                            </div>
                            <div class="clearfix"></div>
                            <br>

                            <div class="col m6 s12">
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="cheque_date"
                                            <?php if ($payment_type == 'cash' || $payment_type == 'online') {
                                                echo 'disabled';
                                            } ?> name="cheque_date" value="<?= $cheque_date ?>"/>
                                        <label for="cheque_date" class="control-label">Cheque Date</label>
                                    </div>
                                </div>
                            </div>
                            <div class="col m6 s12">
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="cheque_payee"
                                            <?php if ($payment_type == 'cash' || $payment_type == 'online') {
                                                echo 'disabled';
                                            } ?> name="cheque_payee" value="<?= $cheque_payee ?>"/>
                                        <label for="cheque_payee" class="control-label">Cheque Payee</label>
                                    </div>
                                </div>
                            </div>
                            <br>
                        </div>
                        <div class="clearfix"></div>
                        <div class="col m12 col s12">
                            <div class="row margin">
                                <div class="input-field col s12">
                                    <input class="form-control" type="text" id="description"
                                           name="description" maxlength="100"
                                           value='<?php echo $description; ?>'/><br/>
                                    <label for="description" class="control-label">Description</label>
                                </div>
                            </div>
                        </div>
                        <div class="clearfix"></div>
                        <br>
                        <div class="row">
                            <div class="col m6 s12">
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" placeholder="00000.00"
                                               onkeypress="return isDoubleNumberKey(event)" type="text"
                                               id="withholding_tax"
                                               value="<?= $withholding_tax ?>"
                                               name="withholding_tax"/>
                                        <label for="withholding_tax" class="control-label">Withholding Tax - (4.5%)</label>
                                    </div>
                                </div>
                            </div>
                            <div class="col m6 s12">
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" placeholder="00000.00"
                                               onkeypress="return isDoubleNumberKey(event)" type="text"
                                               id="sales_tax"
                                               value="<?= $sales_tax ?>"
                                               name="sales_tax" style="display: none;"/>
                                        <label for="sales_tax" class="control-label" style="display: none;">Sales Tax - (17%)</label>
                                    </div>
                                </div>
                            </div>
                            <div class="col m6 s12">
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control"
                                               onkeypress="return isDoubleNumberKey(event)" type="text"
                                               id="goods_return"
                                               value="<?= $goods_return ?>"
                                               name="goods_return"/>
                                        <label for="goods_return" class="control-label">Goods Return</label>
                                    </div>
                                </div>
                            </div>
                            <div class="col m6 s12">
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control"
                                               onkeypress="return isDoubleNumberKey(event)" type="text"
                                               id="shelf_rent"
                                               value="<?= $shelf_rent ?>"
                                               name="shelf_rent"/>
                                        <label for="shelf_rent" class="control-label">Shelf Rent</label>
                                    </div>
                                </div>
                            </div>
                            <div class="col m6 s12">
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control"
                                               onkeypress="return isDoubleNumberKey(event)" type="text"
                                               id="builty_charges"
                                               value="<?= $builty_charges ?>"
                                               name="builty_charges"/>
                                        <label for="builty_charges" class="control-label">Builty Charges</label>
                                    </div>
                                </div>
                            </div>
                            <div class="col m6 s12">
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control"
                                               onkeypress="return isDoubleNumberKey(event)" type="text"
                                               id="other_expense"
                                               value="<?= $other_expense ?>"
                                               name="other_expense" style="display: none;"/>
                                        <label for="other_expense" class="control-label" style="display: none;">Other Expense</label>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="clearfix"></div>
                        <br><br>
                    </div>
                </div>

                <div class="clearfix"></div>
                <button id="btnsubmit" type="submit" class="btn waves-effect waves-light">Submit</button>
                <button type="button" class="btn waves-effect waves-light"
                        onclick="location.href='payment.php?view'">Cancel
                </button>
            </form>
        </div>
    </div>
</div>
</div>
<?php include('../includes/inc_footer.php'); ?>

<?php include('../includes/inc_js.php'); ?>
<script type="application/javascript">
    <?php if(isset($_GET['edit']) && $payment_type == 'bank'){?>
    $(document).ready(function () {
        $('#bankDetails').show();
        $('#bank_name').attr('required', true);
        $('#bank_name').attr('disabled', false);
        $('#party_name').attr('required', true);
        $('#party_name').attr('disabled', false);
        $('#cheque_no').attr('disabled', false);
        $('#cheque_bank_name').attr('disabled', false);
        $('#cheque_payee').attr('disabled', false);
        $('#cheque_date').attr('disabled', false);
        $('#account_no').attr('disabled', false);
        $('#bank_name').rules('add', {
            required: true,
            messages: {
                required: "<span class='label label-warning val_mes'>Please select bank name</span>"
            }
        });
    });
    <?php }?>
    $('#payment_type').on('change', function () {
        if ($(this).val() == 'bank') {
            $('#chequeDetails').show();
            $('#party_name_detail').hide();
            $('#from_bank_name_detail').hide();

            $('#bank_name').attr('required', true);
            $('#bank_name').attr('disabled', false);
            $('#bank_name').rules('add', {
                required: true,
            });

            $('#account_no').attr('required', true);
            $('#account_no').attr('disabled', false);
            $('#account_no').rules('add', {
                required: true,
            });

            $('#from_bank_name').attr('required', false);
            $('#from_bank_name').attr('disabled', true);
            $('#from_bank_name').rules('add', {
                required: false,
            });

            $('#party_name').attr('required', false);
            $('#party_name').attr('disabled', true);
            $('#party_name').rules('add', {
                required: false,
            });

            $('#cheque_no').attr('required', true);
            $('#cheque_no').attr('disabled', false);
            $('#cheque_no').rules('add', {
                required: true,
                messages: {
                    required: "<span class='label label-warning val_mes'>Please enter cheque number</span>"
                }
            });

            $('#cheque_bank_name').attr('required', true);
            $('#cheque_bank_name').attr('disabled', false);
            $('#cheque_bank_name').rules('add', {
                required: true,
                messages: {
                    required: "<span class='label label-warning val_mes'>Please enter cheque bank name</span>"
                }
            });

            $('#cheque_payee').attr('required', true);
            $('#cheque_payee').attr('disabled', false);
            $('#cheque_payee').rules('add', {
                required: true,
                messages: {
                    required: "<span class='label label-warning val_mes'>Please enter cheque payee name</span>"
                }
            });

            $('#cheque_date').attr('required', true);
            $('#cheque_date').attr('disabled', false);
            $('#cheque_date').rules('add', {
                required: true,
                messages: {
                    required: "<span class='label label-warning val_mes'>Please enter cheque date</span>"
                }
            });
//                    $('#bankDetails').show();
//                    $('#bank_name').attr('required', true);
//                    $('#bank_name').attr('disabled', false);
//                    $('#cheque_no').attr('disabled', false);
//                    $('#account_no').attr('disabled', false);
        } else if ($(this).val() == 'cash') {
            $('#chequeDetails').hide();
            $('#party_name_detail').hide();
            $('#from_bank_name_detail').hide();

            $('#bank_name').attr('required', true);
            $('#bank_name').attr('disabled', false);
            $('#bank_name').rules('add', {
                required: true,
            });

            $('#account_no').attr('required', true);
            $('#account_no').attr('disabled', false);
            $('#account_no').rules('add', {
                required: true,
                messages: {
                    required: "<span class='label label-warning val_mes'>Please enter account number</span>"
                }
            });

            $('#from_bank_name').attr('required', false);
            $('#from_bank_name').attr('disabled', true);
            $('#from_bank_name').rules('add', {
                required: false,
            });

            $('#party_name').attr('required', false);
            $('#party_name').attr('disabled', true);
            $('#party_name').rules('add', {
                required: false,
            });

            $('#cheque_no').attr('required', false);
            $('#cheque_no').attr('disabled', true);
            $('#cheque_no').rules('add', {
                required: false,
            });

            $('#cheque_bank_name').attr('required', false);
            $('#cheque_bank_name').attr('disabled', true);
            $('#cheque_bank_name').rules('add', {
                required: false,
            });

            $('#cheque_payee').attr('required', false);
            $('#cheque_payee').attr('disabled', true);
            $('#cheque_payee').rules('add', {
                required: false,
            });

            $('#cheque_date').attr('required', false);
            $('#cheque_date').attr('disabled', true);
            $('#cheque_date').rules('add', {
                required: false,
            });
//                    $('#bankDetails').hide();
//                    $('#bank_name').attr('required', false);
//                    $('#bank_name').attr('disabled', true);
//                    $('#cheque_no').attr('disabled', true);
//                    $('#account_no').attr('disabled', true);
        } else if ($(this).val() == 'party') {
            $('#chequeDetails').show();
            $('#party_name_detail').show();
            $('#from_bank_name_detail').hide();

            $('#bank_name').attr('required', false);
            $('#bank_name').attr('disabled', true);
            $('#bank_name').rules('add', {
                required: false,
            });

            $('#account_no').attr('required', false);
            $('#account_no').attr('disabled', true);
            $('#account_no').rules('add', {
                required: false,
            });

            $('#from_bank_name').attr('required', false);
            $('#from_bank_name').attr('disabled', true);
            $('#from_bank_name').rules('add', {
                required: false,
            });

            $('#party_name').attr('required', true);
            $('#party_name').attr('disabled', false);
            $('#party_name').rules('add', {
                required: true,
                messages: {
                    required: "<span class='label label-warning val_mes'>Please enter party name</span>"
                }
            });

            $('#cheque_no').attr('required', true);
            $('#cheque_no').attr('disabled', false);
            $('#cheque_no').rules('add', {
                required: true,
                messages: {
                    required: "<span class='label label-warning val_mes'>Please enter cheque number</span>"
                }
            });

            $('#cheque_bank_name').attr('required', true);
            $('#cheque_bank_name').attr('disabled', false);
            $('#cheque_bank_name').rules('add', {
                required: true,
                messages: {
                    required: "<span class='label label-warning val_mes'>Please enter cheque bank name</span>"
                }
            });

            $('#cheque_payee').attr('required', true);
            $('#cheque_payee').attr('disabled', false);
            $('#cheque_payee').rules('add', {
                required: true,
                messages: {
                    required: "<span class='label label-warning val_mes'>Please enter cheque payee name</span>"
                }
            });

            $('#cheque_date').attr('required', true);
            $('#cheque_date').attr('disabled', false);
            $('#cheque_date').rules('add', {
                required: true,
                messages: {
                    required: "<span class='label label-warning val_mes'>Please enter cheque date</span>"
                }
            });
        } else if ($(this).val() == 'online') {
            $('#chequeDetails').hide();
            $('#party_name_detail').hide();
            $('#from_bank_name_detail').show();

            $('#bank_name').attr('required', true);
            $('#bank_name').attr('disabled', false);
            $('#bank_name').rules('add', {
                required: true,
            });

            $('#account_no').attr('required', true);
            $('#account_no').attr('disabled', false);
            $('#account_no').rules('add', {
                required: true,
                messages: {
                    required: "<span class='label label-warning val_mes'>Please enter account number</span>"
                }
            });

//                    $('#from_bank_name').attr('required', true);
            $('#from_bank_name').attr('disabled', false);
//                    $('#from_bank_name').rules('add', {
//                        required: true,
//                        messages: {
//                            required: "<span class='label label-warning val_mes'>Please enter bank name</span>"
//                        }
//                    });

            $('#party_name').attr('required', false);
            $('#party_name').attr('disabled', true);
            $('#party_name').rules('add', {
                required: false,
                messages: {
                    required: "<span class='label label-warning val_mes'>Please enter party name</span>"
                }
            });

            $('#cheque_no').attr('required', false);
            $('#cheque_no').attr('disabled', true);
            $('#cheque_no').rules('add', {
                required: false,
                messages: {
                    required: "<span class='label label-warning val_mes'>Please enter cheque number</span>"
                }
            });

            $('#cheque_bank_name').attr('required', false);
            $('#cheque_bank_name').attr('disabled', true);
            $('#cheque_bank_name').rules('add', {
                required: false,
                messages: {
                    required: "<span class='label label-warning val_mes'>Please enter cheque bank name</span>"
                }
            });

            $('#cheque_payee').attr('required', false);
            $('#cheque_payee').attr('disabled', true);
            $('#cheque_payee').rules('add', {
                required: false,
                messages: {
                    required: "<span class='label label-warning val_mes'>Please enter cheque payee name</span>"
                }
            });

            $('#cheque_date').attr('required', false);
            $('#cheque_date').attr('disabled', true);
            $('#cheque_date').rules('add', {
                required: false,
                messages: {
                    required: "<span class='label label-warning val_mes'>Please enter cheque date</span>"
                }
            });
        }
    });
    $('#bank_name').on('change', function () {
        //if ($(this).val() != 'bank') {
        $('body.container').block({message: 'Loading...'});
        $.get('getBankDetails.php?id=' + $(this).val(), function (data) {
            $("#account_no").val(data);
            $("label[for='account_no']").addClass("active");
            $('body.container').unblock();
        });
        //}
    });
    // initialize
    $('#invoice_reference_code').material_select();

    // setup listener for custom event to re-initialize on change
    $('#invoice_reference_code').on('contentChanged', function () {
        $(this).material_select();
    });

    $('#customer_name').on('change', function () {
        $(this).material_select();
        $('body.container').block({message: 'Loading...'});
        $.get('getFilterInvoices.php?id=' + $(this).val(), function (data) {
            $("#invoice_reference_code").empty();
            $("#invoice_reference_code").append(data);
            $('body.container').unblock();
            $('#invoice_reference_code').trigger('contentChanged');
        });
    });
    function isDoubleNumberKey(evt) {
        var charCode = (evt.which) ? evt.which : evt.keyCode;
        if (charCode != 46 && charCode > 31
            && (charCode < 48 || charCode > 57))
            return false;

        return true;
    }
    $(document).ready(function () {
        var fewSeconds = 2;
        $('#btnsubmit').click(function () {
            // Ajax request
            var btn = $(this);
            btn.hide();
            setTimeout(function () {
                btn.show();
            }, fewSeconds * 1000);
        });
    });
</script>
</body>
</html>