<notice>
  <!-- 必達ニュースの詳細を表示するモーダル-->
  <div class="modal fade" tabindex="-1" role="dialog" id="notice_modal">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title" id="title"></h4>
        </div>
        <div class="modal-body">
          <div id="body" class="notice-modal-body"></div>
        </div>
        <div class="modal-footer text-center">
          <button type="button" class="button-border-5 mw100" onclick={post_read} id="agreement">確認しました</button>
          <input type="hidden" id="news_id"/>
          <input type="hidden" id="user_id"/>
        </div>
      </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
  </div><!-- /.modal -->

  <!-- ニュースの詳細を表示するモーダル-->
  <div class="modal fade" tabindex="-1" role="dialog" id="notice_detail_modal">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title" id="title"></h4>
        </div>
        <div class="modal-body">
          <div id="body" class="notice-modal-body"></div>
          <div id="err" class="alert alert-warning"></div>
        </div>
        <div class="modal-footer text-center">
          <button type="button" class="button-border-5 mw100" data-dismiss="modal">{ get_button_label() }</button>
        </div>
      </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
  </div><!-- /.modal -->

  <script>
    var user_category = opts.user_category;
    var _this = this;
    var _id = null;
    _this.view_flg = false;

    // マウント時
    this.on('mount', function(){
      $(document).on('click','#login_button', function(event) {
        $('#login_button').prop("disabled", true);
        notice_pre_login();
      });
      $(document).on('click','.news-link', function(event) {
        show_detail(event);
      });
    });

    // 指定されたニュースの詳細を表示する
    show_detail = function(event) {
      var news_id = event.target.getAttribute('data-target');
      var title   = event.target.getAttribute('data-title');
      var text    = event.target.getAttribute('data-text');
      var file    = event.target.getAttribute('data-file');

      // 個別お知らせ表示
      if (text !== '') {
        result = {
          title   : title,
          content : text
        };
        show_detail_modal(result);
        post_read_exec(news_id, 'N');
        return; 

      // ファイル直接リンク
      } else if (file !== '') {
        window.open(file, '_blank');
        post_read_exec(news_id, 'Y');
        return;
      }

      var params = {
        url      : '/ajax/service/news_detail/'+news_id,
        method   : 'GET',
        data_type: 'json',
        datas    : {'newsId': news_id},
        error    : 'お知らせ取得に失敗しました',
        async    : false
      };
      send_api(params, function(result) {
        show_detail_modal(result);
      });
    };

    // 指定されたユーザーに未読の必達があるかの確認
    // @return true - 必達あり / false - なし
    notice_pre_login = function() {
      var id = $('.login_id').val();
      var pw = $('.login_password').val();

      // パラメータの設定
      var params = {
        url      : '/ajax/service/notice_pre_login',
        method   : 'POST',
        data_type: 'json',
        datas    : {'id':id, 'password':pw, 'referer':user_category},
        error    : 'ログイン事前認証に失敗しました',
        async    : false
      };
      send_api(params, function(result) {
        var params = {
          url      : '/ajax/service/required_notice',
          method   : 'GET',
          data_type: 'json',
          datas    : {'id':result},
          error    : 'お知らせ取得に失敗しました',
          async    : false
       };
       _id = result;
       send_api(params, function(result) {
         if (result) {
           $('#agreement').prop("disabled", false);
           show_modal(result, _id);
         }
         else {
           //必達はなし。
           //$('#form_login').submit();
           required_notice_top();
         }
       });
      });
    }

    //APIの実行
    send_api = function(params, done) {
      // ローディング画面の表示
      $('#loading').show();

      // 非同期処理で先にローディング画面だけ表示する
      action_ajax_tab(params)
        .done(function(result) {
          done(result);
        })
        .fail(function(e) {
          $('#login_button').prop("disabled", false);
          if (e.status == 405) post_error(JSON.parse(e.responseText));
          else if (e.status !== 403) post_error(params['error']);
          _this.view_flg = true;
          _this.update();
          // パスワードが違う場合メッセージを表示する
          if (e && e.status === 403) {
            alert_tag = riot.mount('bootstrap-alert', {
              'type': 'warning',
              'message': JSON.parse(e.responseText)
            });
          }
        })
        .always(function(e) {
          // 表示する処理より先に隠す処理が動き、ローディングが消えない可能性があるので一瞬だけ間を開ける
          // setTimeout(function() { $('#loading').hide() }, 50);
          $('#loading').hide();
        });
    }

    //ニュース詳細を表示する
    show_detail_modal = function(news) {
      $('#notice_detail_modal #title').html(news.title);

      if (news.newsId != -9) {
        $('#err').hide();
        $('#body').show();
        $('#notice_detail_modal #body').html(news.content);
      } else {
        $('#body').hide();
        $('#err').show();
        $('#notice_detail_modal #err').html(news.content);
      }
      $('#notice_detail_modal #date').html(news.postDate);
      $('#notice_detail_modal').modal();
    }

    //必須お知らせのモーダル表示
    show_modal = function(news, user_id) {

      $('#notice_modal').show();
      $('#notice_modal').on('hidden.bs.modal', function (e) {
        $('#login_button').prop("disabled", false);
      })
      $('#notice_modal #title').html(news.title);
      $('#notice_modal #body').html(news.content);
      $('#notice_modal #date').html(news.postDate);
      $('#notice_modal #user_id').val(user_id);
      $('#notice_modal #news_id').val(news.newsId);
      $('#notice_modal').modal();
    }

    //指定されたお知らせを既読登録する
    post_read = function(ev) {
      $('#agreement').prop("disabled", true);//二度押し防止
      var user_id = $('#notice_modal #user_id').val();
      var news_id = $('#notice_modal #news_id').val();
      var text    = ev.target.getAttribute('data-text');

      if (text && text !== '') {
          post_read_exec(news_id, 'N');

      } else {
          var params = {
            url      : '/ajax/service/read',
            method   : 'POST',
            data_type: 'json',
            datas    : {'user_id':user_id,'news_id':news_id},
            error    : '既読登録に失敗しました。',
            async    : false
          };
          send_api(params, function(result) {
            $('#notice_modal').hide();
            $('#login_button').prop("disabled", false);
            notice_pre_login();
          });
      }
    }
    get_button_label = function() {
      return app.lang === 'en' ? 'Close' : '閉じる';
    }


    window.required_notice_top = function() {
      if (location.href.match(/mypage\/information/i)) {
          return;
      }
      var id = '';
      var params = {
        url      : '/ajax/service/required_notice_top',
        method   : 'GET',
        data_type: 'json',
        datas    : {'id':_id},
        error    : 'お知らせ取得に失敗しました',
        async    : false
      };

      send_api(params, function(news) {
        if (news && news.fileName !== null) {
          window.open(news.fileName, '_blank');
          post_read_exec(news.newsId, 'Y');

        } else {
          $('#form_login').submit();
        }
      });
    }

    window.post_read_exec = function(news_id, re_notice) {
      if (location.href.match(/backoffice\/information_preview/i)) {
          return;
      }
      var params = {
        url      : '/ajax/service/read_personal_notice',
        method   : 'POST',
        data_type: 'json',
        datas    : {'id':_id,'news_id':news_id,'re_notice':re_notice},
        error    : '既読登録に失敗しました。',
        async    : false
      };
      send_api(params, function(result) {
        required_notice_top();
      });
    }
  </script>
</notice>
