<div class="modal inmodal fade" id="cEInvoiceModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-50" role="document">
        <div class="modal-content">
            <div class="modal-body">
                <div class="row">
                    <div class="col-xs-10 p-l-0">
                        <h4>@Loc.CEInvoice</h4>
                    </div>
                    <div class="col-xs-2">
                        <i class="fa fa-times clickable-icon pull-right" data-dismiss="modal" title="@Loc.Close"></i>
                    </div>
                </div>
                <div class="form-horizontal">
                    <div class="row m-t-sm">
                        <div class="form-group">
                            <label class="control-label col-xs-5 control-label-left">@Loc.ExportType</label>
                            <div class="col-xs-7 m-l-n">
                                @Html.DropDownList("cEInvoiceExportType", new List<SelectListItem> {
                                    new SelectListItem{ Text = "0 - " + Loc.GeneralDocument, Value = "0"},
                                    new SelectListItem{ Text = "4 - " + Loc.WarningType, Value = "4"},
                                    new SelectListItem{ Text = "7 - " + Loc.AnswerConsentOrRefusal, Value = "7"},
                                    new SelectListItem{ Text = "105 - " + Loc.OrderFormExportType, Value = "105"},
                                    new SelectListItem{ Text = "226 - " + Loc.OrderRevocation, Value = "226"},
                                    new SelectListItem{ Text = "230 - " + Loc.OrderChange, Value = "230"},
                                    new SelectListItem{ Text = "231 - " + Loc.OrderResponse, Value = "231"},
                                    new SelectListItem{ Text = "325 - " + Loc.OrderBuyer, Value = "325"},
                                    new SelectListItem{ Text = "326 - " + Loc.PartialAccount, Value = "326"},
                                    new SelectListItem{ Text = "351 - " + Loc.OrderForm, Value = "351"},
                                    new SelectListItem{ Text = "380 - " + Loc.CommercialFinalAccount, Value = "380"},
                                    new SelectListItem{ Text = "381 - " + Loc.Approval, Value = "381"},
                                    new SelectListItem{ Text = "383 - " + Loc.Indebtedness, Value = "383"},
                                    new SelectListItem{ Text = "384 - " + Loc.CorrectiveAccount, Value = "384"},
                                    new SelectListItem{ Text = "386 - " + Loc.AdvancePaymentInvoice, Value = "386"},
                                    new SelectListItem{ Text = "394 - " + Loc.LeasingAccount, Value = "394"},
                                    new SelectListItem{ Text = "632 - " + Loc.ReceiptType, Value = "632"},
                                }, "", new { @class = "form-control custom-select" })
                            </div>
                        </div>
                        <div id="cEInvoiceDateContainer">
                            <div class="form-group">
                                <label class="control-label col-xs-5 control-label-left">@Loc.CNDateFrom</label>
                                <div class="col-xs-7 m-l-n">
                                    <div class="input-group">
                                        <span class="input-group-addon">
                                            <i class="far fa-calendar alt"></i>
                                        </span>
                                        <input class="form-control datepicker datepicker-cEInvoice" id="cEInvoiceDateFrom" />
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label col-xs-5 control-label-left">@Loc.CNDateTo</label>
                                <div class="col-xs-7 m-l-n">
                                    <div class="input-group">
                                        <span class="input-group-addon">
                                            <i class="far fa-calendar alt"></i>
                                        </span>
                                        <input class="form-control datepicker datepicker-cEInvoice" id="cEInvoiceDateTo" />
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-xs-5 control-label-left">@Loc.OriginatorDocumentReference</label>
                            <div class="col-xs-7 m-l-n">
                                <input class="form-control" id="cEInvoiceOriginatorDocument" />
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-xs-5 control-label-left">@Loc.OrderReference</label>
                            <div class="col-xs-7 m-l-n">
                                <input class="form-control" id="cEInvoiceOrderRef" />
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-xs-5 control-label-left">@Loc.ProjectReference</label>
                            <div class="col-xs-7 m-l-n">
                                <input class="form-control" id="cEInvoiceProjectRef" />
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-xs-5 control-label-left">@Loc.ContractDocumentReference</label>
                            <div class="col-xs-7 m-l-n">
                                <input class="form-control" id="cEInvoiceContractRef" />
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-xs-5 control-label-left">@Loc.RotationNumberReference</label>
                            <div class="col-xs-7 m-l-n">
                                <input class="form-control" id="cEInvoiceRotationNumber" />
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-xs-5 control-label-left">@Loc.ObjectCode</label>
                            <div class="col-xs-7 m-l-n">
                                <input class="form-control" id="cEInvoiceObjectNo" />
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-xs-5 control-label-left">@Loc.DispatchNotesNumber</label>
                            <div class="col-xs-7 m-l-n">
                                <input class="form-control" id="cEInvoiceDespatchDocRef" />
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-xs-5 control-label-left">@Loc.TrrNumber</label>
                            <div class="col-xs-7 m-l-n">
                                <input class="form-control typeahead_3 typeahead_wide" id="cEInvoiceBnkAcctNo" />
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" data-dismiss="modal" class="btn btn-link"><i class="fa fa-times m-r-xs text-primary"></i>@Loc.Cancel</button>
                <button type="button" class="btn btn-primary" id="btnSetCEInvoiceData" onclick="setCEInvoiceData()"><i class="fa fa-check m-r-xs"></i>@Loc.Save</button>
            </div>
        </div>
    </div>
</div>

<script>
    $(document).ready(function () {
        $('#cEInvoiceModal .modal-dialog').draggable();

        $("#cEInvoiceExportType").on("change", function () {
            if ($("#cEInvoiceExportType option:selected").val() === "381" || $("#cEInvoiceExportType option:selected").val() === "383") {
                $("#cEInvoiceDateContainer").removeClass("hidden");
            } else {
                $("#cEInvoiceDateContainer").addClass("hidden");
            }
        });

        initializeBankAccountsTypeahead("@Url.Action("SearchSubjectBanksAccounts", "Subjects")", $("#cEInvoiceBnkAcctNo"), $("#cEInvoiceBnkAcctNo"));
    });

    async function openCEInvoiceModal() {
        await getCeInvoiceData();
        $("#cEInvoiceModal").modal("show");
    }

    async function getCeInvoiceData() {
        await fetch('@Url.Action("GetCEInvoiceData", "Move")' + "?qid=" + $("#Qid").val(), {
            method: "GET",
            headers: { 'Content-Type': 'application/json' }
        }).then((response) => response.json()
        ).then(async (data) => {
            if (data) {
                $("#cEInvoiceExportType").val(data.EInvoiceExportType);
                $("#cEInvoiceDateFrom").datepicker("setDate", data.CNDateFrom);
                $("#cEInvoiceDateTo").datepicker("setDate", data.CNDateTo);
                $("#cEInvoiceOriginatorDocument").val(data.OriginatorDocRef);
                $("#cEInvoiceOrderRef").val(data.OrderRef);
                $("#cEInvoiceProjectRef").val(data.ProjectRef);
                $("#cEInvoiceContractRef").val(data.ContractRef);
                $("#cEInvoiceRotationNumber").val(data.RotationNumber);
                $("#cEInvoiceObjectNo").val(data.ObjectNo);
                $("#cEInvoiceDespatchDocRef").val(data.DespatchDocRef);
                $("#cEInvoiceBnkAcctNo").val(data.BnkAcctNo);
            } else
                $("#cEInvoiceExportType").val($("#DocTypeSelect option:selected").attr("data-eInvoiceExportType2"));

            $("#cEInvoiceExportType").trigger("change");
        });
    }

    async function setCEInvoiceData() {
        $("#btnSetCEInvoiceData").find("i").addClass("fa-spinner fa-spin");
        $("#btnSetCEInvoiceData").prop("disabled", true);

        var data = {};
        data.MoveQId = $("#Qid").val();
        data.EInvoiceExportType = $("#cEInvoiceExportType option:selected").val();
        data.CNDateFrom = $("#cEInvoiceDateFrom").datepicker("getDate") ? $("#cEInvoiceDateFrom").datepicker("getDate").toISOString() : null;
        data.CNDateTo = $("#cEInvoiceDateTo").datepicker("getDate") ? $("#cEInvoiceDateTo").datepicker("getDate").toISOString() : null;
        data.AcKey = $("#AcKey").val();
        data.OriginatorDocRef = $("#cEInvoiceOriginatorDocument").val();
        data.OrderRef = $("#cEInvoiceOrderRef").val();
        data.ProjectRef = $("#cEInvoiceProjectRef").val();
        data.ContractRef = $("#cEInvoiceContractRef").val();
        data.RotationNumber = $("#cEInvoiceRotationNumber").val();
        data.ObjectNo = $("#cEInvoiceObjectNo").val();
        data.DespatchDocRef = $("#cEInvoiceDespatchDocRef").val();
        data.BnkAcctNo = $("#cEInvoiceBnkAcctNo").val();

        await fetch('@Url.Action("SetCEInvoiceData", "Move")', {
            method: "POST",
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify(data)
        }).then((response) => response.json()
        ).then(async (response) => {
            $("#btnSetCEInvoiceData").find("i").removeClass("fa-spinner fa-spin");
            $("#btnSetCEInvoiceData").prop("disabled", false);

            if (!response) {
                $().customModal({
                    destroyOnClose: true,
                    showCloseButton: true,
                    type: "alert",
                    text: "@Loc.ErrorSaving",
                    buttons: [
                        {
                            icon: 'fa fa-check',
                            text: $("#ConfirmLoc").val(),
                            closeOnAction: true,
                            type: 'primary'
                        }
                    ]
                });
            } else
                $("#cEInvoiceModal").modal("hide");
        });
    }
</script>