<div class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-body">
            <div class="row">
                <?php if ($permissions->hasPermission('add_crm_contra')) { ?>
                    <div class="col-md-2 pull-right">
                        <div class="floatingContainer ">

                            <a id="add_button" data-toggle="modal" data-target="#contra-modal">
                                <div style="background-color: #4c0870;" class="actionButton">

                                <p class="floatingText"><span class="floatingTextBG"><?php echo CommonLanguage::getMessage('add') ?></span></p>
                                </div>

                            </a>
                        </div>
                    </div>


                <?php } ?>
                <div class="col-md-12 m-t-10">
                    <table class="table table-bordered demo-table-search crm-table" id="tableWithSearch">
                        <thead>
                            <tr>
                                <th class="text-center">#</th>
                                <th class="text-center"><?php print CommonLanguage::getMessage('customer'); ?></th>
                                <th class="text-center"><?php print CommonLanguage::getMessage('Date'); ?></th>
                                <th class="text-center"><?php print CommonLanguage::getMessage('amount'); ?></th>
                                <th class="text-center"><?php print CommonLanguage::getMessage('Action'); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="contra-modal" role="dialog" aria-labelledby="ModalLabel" aria-hidden="true">
    <form id="formadd" method="POST" onsubmit="return false">

        <div class="modal-dialog modal-sm">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                    <h4 class="modal-title-add"><?php print CommonLanguage::getMessage('add_contra'); ?></h4>
                    <h4 class="modal-title-edit hidden"><?php print CommonLanguage::getMessage('edit_contra'); ?></h4>
                </div>
                <div class="modal-body">
                    <div class="row">
                        <?php echo Forms::AlertDiv('contra_alert'); ?>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label><?php print CommonLanguage::getMessage('choose_customer'); ?><span class="red-star">*</span></label>
                                <input class="customer_id full-width" id="customer_id" name="customer_id" type="hidden" required />
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label><?php print CommonLanguage::getMessage('Date'); ?><span class="red-star">*</span></label>
                                <input type="text" id="contra_date" name="contra_date" class="form-control" required />
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label><?php print CommonLanguage::getMessage('amount'); ?><span class="red-star">*</span></label>
                                <input type="text" id="contra_amount" name="contra_amount" class="form-control" required />
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label><?php print CommonLanguage::getMessage('Currency'); ?><span class="red-star">*</span></label>
                                <select class="full-width currency_id" name="currency_id" data-init-plugin="select2">
                                    <?php foreach ($currency_list as $currency) : ?>
                                        <option mysymbol="<?php echo $currency['Currency_Symbol']; ?>" <?php echo $DefaultCurrencyID == $currency['Currency_ID'] ? 'selected' : '' ?> value="<?php echo $currency['Currency_ID']; ?>"><?php echo $currency['Currency_Name'] . " - " . $currency['Currency_Code']; ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="row form-group">
                                <label><?php print CommonLanguage::getMessage('archive'); ?> </label>
                                <input type="file" onchange="ValidateForNewAttachment(this, ['.jpg', '.png', '.jpeg', '.docx', '.pdf'])" name="archive_name">
                            </div>
                        </div>

                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal"><?php print CommonLanguage::getMessage('cancel'); ?></button>
                    <button style="color:white !important;" id="submit_data" class="btn bg-complete-dark"><?php print CommonLanguage::getMessage('Save'); ?></button>
                    <input type="hidden" name="id" id="rowid">
                </div>
            </div>
            <?php echo Forms::ModalLoader('contraDisabler'); ?>
        </div>
    </form>
</div>

<script>
    var contra_list = <?php echo json_encode(CommonLanguage::getMessage('contra_list')); ?>,
        contra_id = 0;
    $(function() {
        build_select2($("#customer_id"), "xhr/customerSearch")

        $("#contra_date").datepicker({
            autoclose: true
        })
        RebuildDataTable(contra_list, [0, 1, 2, 3], 'crmAjax/contraList', 1)

        $("#add_button").on("click", function() {
            contra_id = 0;
        })
        $("#submit_data").on("click", function() {
            if (!$("#formadd").valid()) {
                AddAlert("#contra_alert", '<?php echo CommonLanguage::getMessage('Required_Fields') ?>', 'alert alert-danger');
                return;
            }
            $("#contraDisabler").removeClass('hidden');
            var Data = new FormData(document.getElementById("formadd"));
            Data.append('contra_id', contra_id)
            $.ajax({
                url: "crmAjax/add_edit_contra",
                method: "POST",
                data: Data,
                processData: false,
                contentType: false,
                dataType: "JSON",
                complete: function(jqXHR, status) {
                    if (status == 'success') {
                        if (jqXHR.responseJSON[0] == "success") {
                            RebuildDataTable(contra_list, [0, 1, 2, 3], 'crmAjax/contraList', 1)
                            hideModalAlert('contra-modal', 'contra_alert');
                        }
                        AddAlert("#contra_alert", jqXHR.responseJSON[1], jqXHR.responseJSON[0]);
                    }
                    $("#contraDisabler").addClass('hidden');
                }
            });
        });
        $("#tableWithSearch").on("click", ".edit_contra", function() {
            contra_id = $(this).attr("data-id");
            $("#contra-modal .modal-title-add").addClass('hidden')
            $("#contra-modal .modal-title-edit").removeClass('hidden')
            var row_id = $(this).attr("data-id")
            $("#rowid").val(row_id)
            CommonViewById("xhr/getElement", {
                rowId: row_id,
                rowEntry: 'contra_id',
                table: 'crm_contra_list'
            }, '#contraDisabler', function(response) {
                var Keys = Object.keys(response[0])
                $.each(Keys, function(key, value) {
                    $('#' + value).val(response[0][value])
                });
                $("#currency_id").select2()
                $("#customer_id").val(response[0].customer_id).attr('data-select-name', response[0].Customer_Name)
                build_select2($("#customer_id"), "xhr/customerSearch")
                $("#contra-modal").modal("show")
            })
        });
    })
</script>
<?php
