{% extends "base.html" %}
{% block content %}

    <div>
        <form id="form_search" class="form-inline" style="text-align:left">
            <div class="container-fluid">
                <div class="row show-grid">
        <span class="col-md-4">
          <select id="order" name="order" class="form-control form-control-sm">
            <option value="desc">최근순</option>
            <option value="asc">오래된 순</option>
          </select>
          <select id="option" name="option" class="form-control form-control-sm">
            <option value="all">전체</option>
            <option value="completed">완료</option>
          </select>
        </span>
                    <span class="col-md-8">
          <input id="search_word" name="search_word" class="form-control form-control-sm w-75" type="text"
                 placeholder="" aria-label="Search">
          <button id="search" class="btn btn-sm btn-outline-success">검색</button>
          <button id="reset_btn" class="btn btn-sm btn-outline-success">리셋</button>
        </span>
                </div>
            </div>
        </form>
        <div id='page1'></div>
        {{ macros.m_hr_head_top() }}
        {{ macros.m_row_start('0') }}
        {{ macros.m_col(2,  macros.m_strong('Poster')) }}
        {{ macros.m_col(10,  macros.m_strong('Info')) }}
        {{ macros.m_row_end() }}
        {{ macros.m_hr_head_bottom() }}
        <div id="list_div"></div>
        <div id='page2'></div>
    </div>

    {#    <script src="{{ url_for('.static', filename='js/sjva_global1.js') }}"></script>#}
    {#    <script src="{{ url_for('.static', filename='js/sjva_ui14.js') }}"></script>#}
    <script type="text/javascript">
        var package_name = "{{arg['package_name']}}";
        var sub = "{{arg['sub']}}";
        var current_data = null;

        $(document).ready(function () {
            {#global_sub_request_search('1');#}
            console.log('ready')
            globalRequestSearchTest('1');
        });

        function globalRequestSearchTest(page, move_top = true) {
            console.log('........................')
            var formData = getFormdata('#form_search')
            formData += '&page=' + page;
            $.ajax({
                {#url: '/' + PACKAGE_NAME + '/ajax/' + MODULE_NAME + '/web_list2',#}
                url: '/' + package_name + '/ajax/' + sub + '/web_list3',
                type: "POST",
                cache: false,
                data: formData,
                dataType: "json",
                success: function (data) {
                    console.log(data)
                    current_data = data;
                    {#if (move_top)#}
                    {#  window.scrollTo(0,0);#}
                    {#make_list(data.list)#}
                    {#make_page_html(data.paging)#}
                }
            });
        }

        $("#search").click(function (e) {
            e.preventDefault();
            {#global_sub_request_search('1');#}
            globalRequestSearch('1');
        });

        $("body").on('click', '#page', function (e) {
            e.preventDefault();
            global_sub_request_search($(this).data('page'));
        });

        $("#reset_btn").click(function (e) {
            e.preventDefault();
            document.getElementById("order").value = 'desc';
            document.getElementById("option").value = 'all';
            document.getElementById("search_word").value = '';
            global_sub_request_search('1')
        });


        $("body").on('click', '#json_btn', function (e) {
            e.preventDefault();
            var id = $(this).data('id');
            for (i in current_data.list) {
                if (current_data.list[i].id == id) {
                    m_modal(current_data.list[i])
                }
            }
        });

        $("body").on('click', '#self_search_btn', function (e) {
            e.preventDefault();
            var search_word = $(this).data('title');
            document.getElementById("search_word").value = search_word;
            global_sub_request_search('1')
        });

        $("body").on('click', '#remove_btn', function (e) {
            e.preventDefault();
            id = $(this).data('id');
            $.ajax({
                url: '/' + package_name + '/ajax/' + sub + '/db_remove',
                type: "POST",
                cache: false,
                data: {id: id},
                dataType: "json",
                success: function (data) {
                    if (data) {
                        $.notify('<strong>삭제되었습니다.</strong>', {
                            type: 'success'
                        });
                        global_sub_request_search(current_data.paging.current_page, false)
                    } else {
                        $.notify('<strong>삭제 실패</strong>', {
                            type: 'warning'
                        });
                    }
                }
            });
        });

        $("body").on('click', '#request_btn', function (e) {
            e.preventDefault();
            var content_code = $(this).data('content_code');
            $(location).attr('href', '/' + package_name + '/' + sub + '/request?content_code=' + content_code)
        });


        function make_list(data) {
            //console.log(data)
            str = '';
            for (i in data) {
                //console.log(data[i])
                str += m_row_start();
                str += m_col(1, data[i].id);
                tmp = (data[i].status == 'completed') ? '완료' : '미완료';
                str += m_col(1, tmp);
                tmp = data[i].created_time + '(추가)';
                if (data[i].completed_time != null)
                    tmp += data[i].completed_time + '(완료)';
                str += m_col(3, tmp)
                tmp = data[i].savepath + '<br>' + data[i].filename + '<br><br>';
                tmp2 = m_button('json_btn', 'JSON', [{'key': 'id', 'value': data[i].id}]);
                tmp2 += m_button('request_btn', '작품 검색', [{'key': 'content_code', 'value': data[i].content_code}]);
                tmp2 += m_button('self_search_btn', '목록 검색', [{'key': 'title', 'value': data[i].title}]);
                tmp2 += m_button('remove_btn', '삭제', [{'key': 'id', 'value': data[i].id}]);
                tmp += m_button_group(tmp2)
                str += m_col(7, tmp)
                str += m_row_end();
                if (i != data.length - 1) str += m_hr();
            }
            document.getElementById("list_div").innerHTML = str;
        }


    </script>
{% endblock %}