<div class="mb-1 pt-2 text-right btn-groups">
    <div class="btn-group" id="item-btn-group">
        <button type="button" class="btn btn-sm btn-primary sise-act save-button" data-value="save" {{ $formA['FormVars']['Hidden']['SaveButton'] }}>
            {{ $formA['FormVars']['Title']['SaveButton'] }}
        </button>
        @include('front.dabory.erp.partial.select-btn-options', [
            'selectBtns' => $formA['SelectButtonOptions'],
            'eventClassName' => 'sise-act',
        ])
    </div>
</div>

<div class="card p-2" id="sise-form">
    <button type="button" id="modal-media-btn" hidden
            class="btn btn-success btn-open-modal">
    </button>
    <div class="card-header" id="frm">
        <input type="hidden" id="Id" name="Id" value="0">
        <div class="row">
            <div class="col-12 card-header-item">
                <div class="card card card-info mb-3 mb-md-2 mb-lg-0 border-light">
{{--                    <div class="card-header p-0 mb-2">--}}
{{--                        <p class="card-title p-1 ml-2">{{ $formA['FormVars']['Title']['CardTitle1'] }}</p>--}}
{{--                    </div>--}}
                    <div class="card-body">
                        <div class="form-group {{ $formA['FormVars']['Display']['SiseType'] }} flex-column mb-2">
                            <label class="m-0">{{ $formA['FormVars']['Title']['SiseType'] }}</label>
                            <div class="d-flex align-items-center">
                                <input type="radio" value="golf" name="sisetype" class="text-center" id="sisetype-check-0"> <label class="mb-0 mr-1" for="sisetype-check-0">골프</label>
                                <input type="radio" value="condo" name="sisetype" class="text-center" id="sisetype-check-1"> <label class="mb-0 mr-1" for="sisetype-check-1">콘도</label>
                                <input type="radio" value="fitness" name="sisetype" class="text-center" id="sisetype-check-2"> <label class="mb-0 mr-1" for="sisetype-check-2">휘트니스</label>
                            </div>
                        </div>
                        <div class="form-group {{ $formA['FormVars']['Display']['Area'] }} flex-column mb-2">
                            <label class="m-0">{{ $formA['FormVars']['Title']['Area'] }}</label>
                            <select class="w-100 rounded" id="area-select">
                                <!-- <option value="1">수도권</option>
                                <option value="2">강원</option>
                                <option value="3">충청</option>
                                <option value="4">영남</option>
                                <option value="5">제주</option>
                                <option value="6">호남</option> -->
                                <option value="100">수도권/강원/충청권</option>
                                <option value="200">영남/제주권</option>
                                <option value="300">호남</option>
                            </select>
                        </div>
                        <div class="form-group {{ $formA['FormVars']['Display']['MembershipName'] }} flex-column mb-2">
                            <label class="m-0">{{ $formA['FormVars']['Title']['MembershipName'] }}</label>
                            <input type="text" id="membership-name-txt" class="rounded w-100" autocomplete="off"
                                   maxlength="{{ $formA['FormVars']['MaxLength']['MembershipName'] }}"
                                {{ $formA['FormVars']['Required']['MembershipName'] }}>
                        </div>
                        <div class="form-group {{ $formA['FormVars']['Display']['MembershipCode'] }} flex-column mb-2">
                            <label class="m-0">{{ $formA['FormVars']['Title']['MembershipCode'] }}</label>
                            <input type="text" id="membership-code-txt" class="rounded w-100" autocomplete="off"
                                   maxlength="{{ $formA['FormVars']['MaxLength']['MembershipCode'] }}"
                                {{ $formA['FormVars']['Required']['MembershipCode'] }}>
                        </div>
                        <div class="form-group {{ $formA['FormVars']['Display']['TodayPrice'] }} flex-column mb-2">
                            <label class="m-0">{{ $formA['FormVars']['Title']['TodayPrice'] }}</label>
                            <input type="text" id="today-price-txt" class="rounded w-100" autocomplete="off"
                                   maxlength="{{ $formA['FormVars']['MaxLength']['TodayPrice'] }}"
                                {{ $formA['FormVars']['Required']['TodayPrice'] }}>
                        </div>
                        <div class="form-group {{ $formA['FormVars']['Display']['YesterdayPrice'] }} flex-column mb-2">
                            <label class="m-0">{{ $formA['FormVars']['Title']['YesterdayPrice'] }}</label>
                            <input type="text" id="yesterday-price-txt" class="rounded w-100" autocomplete="off"
                                   maxlength="{{ $formA['FormVars']['MaxLength']['YesterdayPrice'] }}"
                                {{ $formA['FormVars']['Required']['YesterdayPrice'] }}>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="mb-1 pt-2 text-right btn-groups">
    <div class="btn-group" id="item-btn-group">
        <button type="button" class="btn btn-sm btn-primary sise-act" data-value="del2">삭제</button>
    </div>
</div>

<div class="card p-2" id="sise-del-form">
    <button type="button" id="modal-media-btn" hidden
            class="btn btn-success btn-open-modal">
    </button>
    <div class="card-header" id="frm">
        <input type="hidden" id="Id" name="Id" value="0">
        <div class="row">
            <div class="col-12 card-header-item">
                <div class="card card card-info mb-3 mb-md-2 mb-lg-0 border-light">
                    <div class="card-body">
                        <div class="form-group {{ $formA['FormVars']['Display']['SiseDate'] }} flex-column mb-2">
                            <label class="m-0">{{ $formA['FormVars']['Title']['SiseDate'] }}</label>
                            <input type="text" id="sise-date-txt" class="rounded w-100" autocomplete="off"
                                maxlength="{{ $formA['FormVars']['MaxLength']['SiseDate'] }}" placeholder="YYYYmmdd 형식으로 입력해주세요."
                                {{ $formA['FormVars']['Required']['SiseDate'] }}>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

@once
@push('js')
<script src="{{ csset('/js/modals-controller/a-type/common.js') }}"></script>
    <script>
        $(document).ready(async function() {
            PopupPopupForm1FormAPostSiseForm.mediaModal = await include_media_library('media-body', 'post')
            $(document).on('file.paste', '#modal-media', function (event, file_url_list, id_list, unique_key) {
                $(unique_key).val(file_url_list[0])
            });

            $('.sise-act').on('click', function () {
                switch( $(this).data('value') ) {
                    case 'save': PopupPopupForm1FormAPostSiseForm.btn_act_save(); break;
                    case 'new': PopupPopupForm1FormAPostSiseForm.btn_act_new(); break;
                    case 'del': Atype.btn_act_del('#sise-form #frm', function () {
                        $('#modal-select-popup.show').trigger('list.requery')
                        $('#modal-select-popup.show').modal('hide')
                    }, 'PopupPopupForm1FormAPostSiseForm'); break;
                    case 'del2': PopupPopupForm1FormAPostSiseForm.sise_multi_delete(); break;
                }
            });

            // sisetype 라디오 버튼 클릭 시 이벤트 핸들러
            $('#sise-form').on('change', 'input[name="sisetype"]', function() {
                var selectedType = $(this).val();
                PopupPopupForm1FormAPostSiseForm.change_option(selectedType);
            });
        });

        (function( PopupPopupForm1FormAPostSiseForm, $, undefined ) {
            PopupPopupForm1FormAPostSiseForm.formA = {!! json_encode($formA) !!};
            PopupPopupForm1FormAPostSiseForm.mediaModal

            PopupPopupForm1FormAPostSiseForm.delete_media_id = function (media_dom_id) {
                $('#sise-form').find(media_dom_id).val('')
            }

            PopupPopupForm1FormAPostSiseForm.btn_act_save = function () {
                Atype.set_parameter_callback(PopupPopupForm1FormAPostSiseForm.parameter)
                Atype.btn_act_save('#sise-form #frm', function () {
                    $('#modal-select-popup.show').trigger('list.requery')
                    $('#modal-select-popup.show').modal('hide')
                }, 'PopupPopupForm1FormAPostSiseForm');
            }

            PopupPopupForm1FormAPostSiseForm.btn_act_new = async function () {
                $('#modal-select-popup.popup-popup-form1-form-a-post-sise-form .modal-header').removeClass('bg-grey-700 px-0')
                $('#modal-select-popup.popup-popup-form1-form-a-post-sise-form .modal-body button').removeClass('bg-grey-700 border-grey-700 bg-grey-700-hover')
                $('#modal-select-popup.popup-popup-form1-form-a-post-sise-form .modal-dialog').css('maxWidth', '800px');

                Atype.set_parameter_callback(PopupPopupForm1FormAPostSiseForm.parameter)
                Atype.btn_act_new('#sise-form #frm')
            }

            PopupPopupForm1FormAPostSiseForm.btn_act_new_callback = function () {
                PopupPopupForm1FormAPostSiseForm.btn_act_new()
                $('#sisetype-check-0').prop('checked', true)
            }

            PopupPopupForm1FormAPostSiseForm.show_popup_callback = async function (id) {
                PopupPopupForm1FormAPostSiseForm.btn_act_new()
                await PopupPopupForm1FormAPostSiseForm.fetch_sise(Number(id));
            }

            PopupPopupForm1FormAPostSiseForm.parameter = function () {
                const sise_form = $('#sise-form')
                let id = Number( $(sise_form).find('#Id').val() )
                let parameter = {
                    Id: id,
                    PostTypeId: 59,
                    PostTitle: $(sise_form).find('#membership-name-txt').val(),
                    Pc5: $(sise_form).find('input[name=sisetype]:checked').val(),   //
                    Pc2: $(sise_form).find('#area-select').val(),
                    Pc3: $(sise_form).find('#today-price-txt').val(),
                    Pc4: $(sise_form).find('#yesterday-price-txt').val(),
                    Pc1: $(sise_form).find('#membership-code-txt').val(),
                }
                if (id < 0) {
                    parameter = { Id: id }
                }

                return parameter;
            }

            PopupPopupForm1FormAPostSiseForm.del_parameter = function () {
                const sise_del_form = $('#sise-del-form')
                let parameter = {
                    PostTypeId: 59,
                    Pc6: $(sise_del_form).find('#sise-date-txt').val(),
                }
                return parameter;
            }

            PopupPopupForm1FormAPostSiseForm.fetch_sise = async function (id) {
                const response = await get_api_data(PopupPopupForm1FormAPostSiseForm.formA['General']['PickApi'], {
                    Page: [ { Id: id } ]
                })

                PopupPopupForm1FormAPostSiseForm.set_sise_ui(response)
            }

            PopupPopupForm1FormAPostSiseForm.fetch_sise_date = async function (parameter) {
                let data = [];
                let siseDelParameter = PopupPopupForm1FormAPostSiseForm.get_sise_parameter(999,0,parameter.Pc6);
                const response = await get_api_data(PopupPopupForm1FormAPostSiseForm.formA['General']['PageApi'], siseDelParameter);
                let pages = response.data.Page;

                if(pages){
                    pages.forEach(page => {
                        data.push({ Id: -Math.abs(page.Id) });
                    });

                    if (data.length == 0) {
                        iziToast.error({
                            title: 'Error',
                            message: $('#click-the-checkbox-es-of-line-for-action').text(),
                        });
                        return false;
                    }
                }
                return data;
            }

            // 특정 시세일자 data만 삭제
            PopupPopupForm1FormAPostSiseForm.sise_multi_delete = async function () {
                const sise_del_form = $('#sise-del-form')
                const frm = '#sise-del-form #frm';

                let parameter = {
                    PostTypeId: 59,
                    Pc6: $(sise_del_form).find('#sise-date-txt').val(),
                }

                if (window.dom_required_check(`${frm} input`)) {
                    iziToast.warning({
                        title: 'Warning',
                        message: $('#required-item-omitted').text(),
                    });
                    return;
                }

                const data = await PopupPopupForm1FormAPostSiseForm.fetch_sise_date(parameter);
                if (! data) return;

                let response = await get_api_data(type1['General']['ActApi'], { Page: data });
                if (response.data.Page) {
                    iziToast.success({
                        title: 'Success',
                        message: $('#action-completed').text(),
                    });

                    // 부모 페이지 이벤트 트리거
                    $('#modal-select-popup.show').trigger('list.requery');
                    $('#modal-select-popup.show').modal('hide');

                } else {
                    let message = response.data.body ?? $('#api-request-failed-please-check').text();
                    iziToast.error({
                        title: 'Error',
                        message: message,
                    });
                }
            }

            PopupPopupForm1FormAPostSiseForm.change_option = function (type, area){
                const sise_form = $('#sise-form')
                var areaSelect = $('#area-select');
                areaSelect.empty();
                if (type === 'golf') {
                    areaSelect.append('<option value="100">수도권/강원/충청</option>');
                    areaSelect.append('<option value="200">영남/제주권</option>');
                    areaSelect.append('<option value="300">호남권</option>');
                } else if (type === 'condo') {
                    areaSelect.append('<option value="400">체인형콘도</option>');
                    areaSelect.append('<option value="500">스키장콘도</option>');
                } else if (type === 'fitness') {
                    areaSelect.append('<option value="600">강남</option>');
                    areaSelect.append('<option value="700">강북</option>');
                }
                if(area){
                    areaSelect.val(area);
                }
            }
            // 추가
            PopupPopupForm1FormAPostSiseForm.get_sise_parameter = function (limit, offset, siseDate) {

                let parameter = {
                    PageVars: {
                        Query : `post_type_id='59' and pc6='${siseDate}'`,
                        Limit : Number(limit),
                        Offset: Number(offset),
                    }
                }
                return parameter;
            }

            PopupPopupForm1FormAPostSiseForm.set_sise_ui = async function (response) {
                if (isEmpty(response.data) || response.data.apiStatus) {
                    return;
                }

                const sise = response.data.Page[0];
                const sise_form = $('#sise-form')
                $(sise_form).find('#Id').val(sise.Id)
                $(sise_form).find('#membership-name-txt').val(sise.PostTitle)
                $(sise_form).find(`input[name='sisetype'][value='${sise.Pc5}']`).prop('checked', true)
                $(sise_form).find('#area-select').val(sise.Pc2)
                $(sise_form).find('#today-price-txt').val(sise.Pc3)
                $(sise_form).find('#yesterday-price-txt').val(sise.Pc4)
                $(sise_form).find('#membership-code-txt').val(sise.Pc1)

                PopupPopupForm1FormAPostSiseForm.change_option(sise.Pc5, sise.Pc2);
            }

        }( window.PopupPopupForm1FormAPostSiseForm = window.PopupPopupForm1FormAPostSiseForm || {}, jQuery ));
    </script>
@endpush
@endonce
