<ec91>
  <link rel="stylesheet" href="css/EC91.css">
  <link rel="stylesheet" href="css/jquery.multiselect.css">
  
  
  <div class="main">
    <div class="content">
      <div class="page-title">
        <h2>承認者設定</h2>
      </div>
      <div class="form-wrapper">
        <table>
          <tr>
            <th style="width:135px">承認対象お客様法人</th>
            <td>
              {dep_xref_list_name_1[0].comp_name}
              <!-- <select id="name1_search" disabled="disabled">
                 <option each="{name1 in dep_xref_list_name_1}" value="{name1.comp_name}" >{name1.comp_name}</option>
              </select> -->
            </td>
          </tr>
          <tr>
            <th>承認対象お客様部門</th>
            <td>
              <select id="approver_dep_cd_search">
                  <option value="" kyoten_code="">全部門</option>
                 <option each="{name2 in dep_xref_list_name_2}" value="{name2.kyoten_name}" kyoten_code="{name2.kyoten_code}">{name2.kyoten_name}</option>
              </select>
            </td>
            <td class="btnTd">
              <button style="background:#1C6BC0; color:#FFFFFF; border-radius:5px;" type="button" id="search_list" class="submit">検索</button>
            </td>
          </tr>
          <tr>
          </tr>
        </table>
      </div>
      <div class="table-wrapper">
        <div class="table-head">
          <button style="background:#FFFFFF; color:#1C6BC0; border-radius:5px;" type="button" id="EC91-add-open">承認者新規追加</button>
        </div>
        <table class="blue-table">
          <thead>
            <tr>
              <th>承認者</th>
              <th>法人名</th>
              <th>部署名</th>
              <th>承認申請者</th>
              <th>承認金額<br/>最小</th>
              <th>承認金額<br/>最大</th>
              <th>承認階層</th>
              <th></th>
            </tr>
          </thead>
          <tbody>

            <tr each="{item,index in approval_list}">
              <!--　<td>{item.seq}</td>　-->
              <td>{item.approver_name}</td>
              <td>{item.approver_co_name}</td>
              <td>{item.approver_dep_name}</td>
              <td>{item.approved_name}</td>
              <td>{item.boundary_value_min}</td>
              <td>{item.boundary_value_max}</td>
              <td>{item.approver_level}</td>
              <td class="btnTd">
                <input type="hidden" id="approver_co_cd_{index}" value="{item.approver_co_cd}">
                <input type="hidden" id="approver_dep_cd_{index}" value="{item.approver_dep_cd}">
                <button type="button" index={index} class="EC91-edit-open" onclick="{openEdit}">編集</button>
                <button type="button" index={index} class="EC91-delete color-btn red-btn" onclick="{delete}">削除</button>
              </td>
            </tr>
            
          </tbody>
        </table>
         <div id="paging" class="text-center">
          <ul class="pagination" id="pagination_ec91"></ul>
         </div>
      </div>
    </div>
  </div>
  <div class="modal-wrapper">
    <div class="o_modal" id="EC91-add">
      <div class="o_modal_box">
        <div class="o_modal_head">
          <a class="EC91-add-close"><i class="fa fa-times"></i></a>
        </div>
        <div class="o_modal_body">
          <div class="o_modal_main">
            <h3 class="modal-title">承認者新規登録</h3>
            <div class="modal-form-box">
              <table>
                <span style="float: right;">*入力必須項目</span>
                <tr>
                  <th>承認者*</th>
                  <td>
                    <!-- <input type="text" id="id_add" onchange="{checkUserId}" class="input_check_rule"> -->
                    <div class="div-wraper-autocompleter"><input type="text" id="id_add" class="input_check_rule"></div>
                  </td>
                </tr>
                <tr>
                  <th>法人名*</th>
                  <td>
                    <span id="approver_name1_add" comp_code="{dep_xref_list_name_1[0].comp_code}" name="{dep_xref_list_name_1[0].comp_name}">{dep_xref_list_name_1[0].comp_name}</span>
                  </td>
                </tr>
                <tr>
                  <th>部署名*</th>
                  <td>
                    <select id="approver_name2_add">
                        <option value="" kyoten_code="" name="">----</option>
                        <option each="{name2,i2 in dep_xref_list_name_2}" value="{name2.kyoten_name}" kyoten_code="{name2.kyoten_code}" name="{name2.kyoten_name}">{name2.kyoten_name}</option>
                    </select>
                  </td>
                </tr>
                <tr>
                  <th>承認申請者*</th>
                  <td>
                    <!-- <input type="text" id="ec_users_id_add" onchange="{checkUserApproverId}" class="input_check_rule"> -->
                    <!-- <div>{users_approved_list}</div> -->
                    <select id="ec_users_id_add" name="basic[]" multiple="multiple" class="3col active" style="max-width: 560px">
                      <option each="{users_approved in users_approved_list}"  if="{app_type!=users_approved.name}" value="{users_approved.id}" kyoten_code="{users_approved.sosiki_code}">{users_approved.name}</option>
                    </select>
                  </td>
                </tr>
                <tr>
                  <th>承認階層*</th>
                  <td>
                    <select id="approver_level_add">
                      <option value="1" selected="selected">1</option>
                      <option value="2">2</option>
                      <option value="3">3</option>
                      <option value="4">4</option>
                      <option value="5">5</option>
                      <option value="6">6</option>
                    </select>
                  </td>
                </tr>
                <!-- <tr>
                  <th>承認対象</th>
                  <td><input type="text"></td>
                </tr> -->
                <tr>
                  <th>承認金額-最小*</th>
                  <td><input type="text" style="text-align:right" id="approver_min_add" value="0" class="input_check_rule"><span style="margin-left: 10px">最小金額は「0」</span></td>
                </tr>
                <tr>
                  <th>承認金額-最大*</th>
                  <td><input type="text" style="text-align:right" value="10000000" id="approver_max_add" class="input_check_rule"><span style="margin-left: 10px">最大金額は「10000000」</span></td>
                </tr>
              </table>
              <div class="btn-wrapper">
                <button type="reset" class="reset-btn" id="reset_add"><i class="fa fa-refresh"></i>クリア</button>
                <button style="background:#2EB533; color:#FFFFFF; border-radius:5px;" type="button" class="submit" id="add_approver_info" onclick="{postApprovalInfo}">登録</button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="o_modal" id="EC91-edit">
      <div class="o_modal_box">
        <div class="o_modal_head">
          <a class="EC91-edit-close"><i class="fa fa-times"></i></a>
        </div>
        <div class="o_modal_body">
          <div class="o_modal_main">
            <h3 class="modal-title">承認者設定更新</h3>
            <div class="modal-form-box">
              <table each="{item_edit in arr_edit}">
                <span style="float: right;">*入力必須項目</span>
                <tr>
                  <th>承認者*{index_edit}</th>
                  <td>
                    <!-- <input type="text" id="id_edit" value="{item_edit.id}" onchange="{checkUserId}" class="input_check_rule"> -->
                    <div class="div-wraper-autocompleter"><input type="text" id="id_edit" class="input_check_rule" value="{item_edit.approver_name}" autocomplete="off"></div>
                    <input type="hidden" id="id_edit_old_name" value="{item_edit.approver_name}">
                    <input type="hidden" id="id_edit_old" value="{item_edit.id}">
                  </td>
                </tr>
                <tr>
                  <th>法人名*</th>
                  <td>
                    <select id="approver_name1_edit" disabled="{member_type != 0 ? 'disabled' : ''}">
                      <option each="{name1,i1 in dep_xref_list_name_1}" selected="{(name1.comp_name == item_edit.approver_co_name ) ? 'selected' : ''}" value="{name1.comp_name}" comp_code="{name1.comp_code}">{name1.comp_name}</option>
                    </select>
                    <input type="hidden" id="approver_name1_edit_old" value="{item_edit.approver_co_name}">
                  </td>
                </tr>
                <tr>
                  <th>部署名*</th>
                  <td>
                    <select id="approver_name2_edit">
                      <option value="" kyoten_code="">----</option>
                      <option each="{name2,i2 in dep_xref_list_name_2}" selected="{name2.kyoten_name == item_edit.approver_dep_name ? 'selected' : ''}" value="{name2.kyoten_name}" kyoten_code="{name2.kyoten_code}" if="{app_type!=kyoten_name}">{name2.kyoten_name}</option>
                    </select>
                    <input type="hidden" id="approver_name2_edit_old" value="{item_edit.approver_dep_name}">
                  </td>
                </tr>
                <tr>
                  <th>承認申請者*</th>
                  <td>
                        <!-- <input type="text" id="ec_users_id_edit" value="{item_edit.approver_ec_users_id}" onchange="{checkUserApproverId}" class="input_check_rule" index={index}> -->
                        <div class="div-wraper-autocompleter"><input type="text" id="ec_users_id_edit" class="input_check_rule" value="{item_edit.approved_name}" autocomplete="off">

                        </div>
                        <input type="hidden" id="ec_users_id_edit_old_name" value="{item_edit.approved_name}">
                        <input type="hidden" id="ec_users_id_edit_old" value="{item_edit.approver_ec_users_id}">
                  </td>
                </tr>
                <tr>
                  <th>承認階層*</th>
                  <td>
                    <select id="approver_level_edit">
                      <option value="1" selected="{item_edit.approver_level == '1' ? 'selected' : ''}">1</option>
                      <option value="2" selected="{item_edit.approver_level == '2' ? 'selected' : ''}">2</option>
                      <option value="3" selected="{item_edit.approver_level == '3' ? 'selected' : ''}">3</option>
                      <option value="4" selected="{item_edit.approver_level == '4' ? 'selected' : ''}">4</option>
                      <option value="5" selected="{item_edit.approver_level == '5' ? 'selected' : ''}">5</option>
                      <option value="6" selected="{item_edit.approver_level == '6' ? 'selected' : ''}">6</option>
                    </select>
                    <input type="hidden" id="approver_level_edit_old" value="{item_edit.approver_level}">
                  </td>
                </tr>
                <!-- <tr>
                  <th>承認対象</th>
                  <td><input type="text"></td>
                </tr> -->
                <tr>
                  <th>承認金額-最小*</th>
                  <td>
                    <input type="text" style="text-align:right" id="approver_min_edit" value="{item_edit.boundary_value_min}" class="input_check_rule"><span style="margin-left: 10px">最小金額は「0」</span>
                    <input type="hidden" style="text-align:right" id="approver_min_edit_old" value="{item_edit.boundary_value_min}">
                  </td>
                </tr>
                <tr>
                  <th>承認金額-最大*</th>
                  <td>
                    <input type="text" style="text-align:right" id="approver_max_edit" value="{item_edit.boundary_value_max}" class="input_check_rule"><span style="margin-left: 10px">最大金額は「10000000」</span>
                    <input type="hidden" style="text-align:right" id="approver_max_edit_old" value="{item_edit.boundary_value_max}">
                  </td>
                  <input type="hidden" id="seq_edit" value="{item_edit.seq}">
                </tr>
              </table>
              <div class="btn-wrapper">
                <button type="reset" class="reset-btn EC91-edit-close" id="reset_edit"><i class="fa fa-times"></i>キャンセル</button>
                <button type="button" class="submit" id="edit_approver_info" onclick="{postApprovalInfo}">更新</button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  <script>
    /* global $ */
    this.member_type = opts.member_type;
    this.approval_list = opts.approval_list;
    this.dep_xref_list_name_1 = opts.dep_xref_list_name_1;
    this.dep_xref_list_name_2 = opts.dep_xref_list_name_2;
    this.users_approver_list = opts.users_approver_list;
    this.users_approved_list = [];
    this.total = opts.total;
    this.edit_approval_list = [];
    this.index_edit = '';
    this.current_page = 1;
    this.arr_edit = [];
    this.app_type = '';
    this.user_approver_add_suggest = '';
    this.user_approver_edit_suggest = '';
    this.user_approved_edit_suggest = '';
    var thisTag = this;

    $(function(){
      // thisTag.usersApproverSuggest();
      $("#id_add").change(function(e) {
        thisTag.update({app_type:$("#id_add").val()});
        if($(e.target).val() == '') {
          thisTag.update({user_approver_add_suggest: ''});
        }
      });

      $("#approver_name2_add").change(function(e) {
        if ($(this).val() == '') {
          thisTag.update({
                  users_approved_list: ''
                });
          $('select[multiple]').multiselect();
          $('select[multiple]').multiselect('reload');

        } else {
          var kyoten_code = $('option:selected', '#approver_name2_add').attr('kyoten_code');
          thisTag.getApprovedUsers('add',kyoten_code);
        }
      });

      $("#id_edit").change(function(e) {
        if($(e.target).val() == '') {
          thisTag.update({user_approver_edit_suggest: ''});
        }
      });
      $("#ec_users_id_edit").change(function(e) {
        if($(e.target).val() == '') {
          thisTag.update({user_approved_edit_suggest: ''});
        }
      });
      let userApproverAdd = thisTag.users_approver_list;
      let arrUsersApprover = [];
      let arrUsersApproved = [];

      for (var i = 0; i < userApproverAdd.length; i++) {
        arrUsersApprover.push({value: userApproverAdd[i].name, label: userApproverAdd[i].name, index: i});
      }

      $("#id_add").autocompleter("destroy");
      $('#id_add').autocompleter({ 
        limit:10, 
        source: arrUsersApprover,
        callback: function(value, index, object) {
          if($("#id_add").val() != '' && userApproverAdd[object.index].id) {
            thisTag.update({
              user_approver_add_suggest: userApproverAdd[object.index].id,
            });
          } else {
            thisTag.update({user_approver_add_suggest: ''});
          }
        }
      });
      $('select[multiple].active.3col').multiselect({
          maxWidth: 552,
          columns: 2,
          placeholder: '承認対象ユーザー名',
          search: true,
          selectAll: true
        });
      //add-modal
      $('#EC91-add-open').on('click',function(e){
        $("#approver_name2_add").val("");
        thisTag.update({
          users_approved_list: ''
        });
        $('select[multiple]').multiselect('reload');
        // $("#ec_users_id_add").siblings("#ms-list-1")
                      // .find("li.selected input[type='checkbox']").click();
        $("#approver_min_add").val("0");
        $("#approver_max_add").val("10000000");
        $("#id_add").val("");
        $('#EC91-add').fadeIn();
      });
      $('.EC91-add-close').on('click',function(e){
        $('#EC91-add').fadeOut();
      });
      //edit-modal
      // $('.EC91-edit-open').on('click',function(e){
    
      // });
      $('.EC91-edit-close').on('click',function(e){
        var id_edit_old_name = $("#id_edit_old_name").val();
        var approver_ec_users_id_old = $("#ec_users_id_edit_old").val();
        var approver_ec_users_id_old_name = $("#ec_users_id_edit_old_name").val();
        var boundary_value_min_old = $("#approver_min_edit_old").val();
        var boundary_value_max_old = $("#approver_max_edit_old").val();
        var approver_name1_edit_old = $("#approver_name1_edit_old").val();
        var approver_name2_edit_old = $("#approver_name2_edit_old").val();
        var approver_level_edit_old = $("#approver_level_edit_old").val();
        $("#id_edit").val(id_edit_old_name);
        $("#ec_users_id_edit").val(approver_ec_users_id_old_name);
        $("#approver_min_edit").val(boundary_value_min_old);
        $("#approver_max_edit").val(boundary_value_max_old);
        $("#approver_name1_edit").val(approver_name1_edit_old);
        $("#approver_name2_edit").val(approver_name2_edit_old);
        $("#approver_level_edit").val(approver_level_edit_old);
        $('#EC91-edit').fadeOut();
      });
      // Pagination
        var totalPages = Math.ceil(thisTag.total/LIMIT_ON_PAGE);
        var visiblePages = 1;
        if(totalPages != 0){
            visiblePages =  5;
        } 
        $('#pagination_ec91').twbsPagination({
            totalPages: totalPages,
            visiblePages: visiblePages,
            startPage: thisTag.current_page,
                initiateStartPageClick: false,
            onPageClick: function (event, page) {
                thisTag.current_page = page;
                thisTag.getApprovalInfo(page);
            }
        });

        // Search Button 
        $("#search_list").on('click',function(){
            var page = 1;
            thisTag.getApprovalInfo(page);
        });

        // Clear Button
        $(".reset-btn").on('click',function(e){
            var id = e.target.id;
            switch(id){
                case 'reset_add':
                    // $("#ec_users_id_add").val("");
                    $("#approver_min_add").val("");
                    $("#approver_max_add").val("");
                    $("#id_add").val("");
                    $("#approver_name2_add").val("");
                    thisTag.update({
                      user_approver_add_suggest: '',
                      users_approved_list: ''
                    });
                    $('select[multiple]').multiselect('reload');
                    break;
                case 'reset_edit': 
                    var id_edit_old = $("#id_edit_old").val();
                    var id_ol_name = $("#id_edit_old_name").val();
                    var ec_users_id_edit_old_name = $("#ec_users_id_edit_old_name").val();
                    var approver_ec_users_id_old = $("#ec_users_id_edit_old").val();
                    var boundary_value_min_old = $("#approver_min_edit_old").val();
                    var boundary_value_max_old = $("#approver_max_edit_old").val();
                    var approver_name1_edit_old = $("#approver_name1_edit_old").val();
                    var approver_name2_edit_old = $("#approver_name2_edit_old").val();
                    var approver_level_edit_old = $("#approver_level_edit_old").val();
                    $("#id_edit").val(id_ol_name);
                    $("#ec_users_id_edit").val(ec_users_id_edit_old_name);
                    $("#approver_min_edit").val(boundary_value_min_old);
                    $("#approver_max_edit").val(boundary_value_max_old);
                    $("#approver_name1_edit").val(approver_name1_edit_old);
                    $("#approver_name2_edit").val(approver_name2_edit_old);
                    $("#approver_level_edit").val(approver_level_edit_old);
                    break; 
            } 
        });
    });

    this.delete = function(e){

        var accessTokenCognito = localStorage.getItem('accessTokenCognito');
        var url = URL_API + '/api/delete_approver_user';
        var seq = e.item.item.seq;

        var query = [];

        query.push('token=' +accessTokenCognito);
        query.push('seq=' + seq);

        url += '?' + query.join('&');

        if(!confirm('削除します。\nよろしいですか？')){
            return false;
        }

        $.ajax({
            url:url,
            type:'GET',
            timeout: 60000,
            success: function (data){
                if(data.status == 100){
                    riot.mount('app','maintain',{ messege:data.messege });
                    return false;
                }
                if(data.messege == 'success'){
                    var page = 1;
                    thisTag.getApprovalInfo(page);
                }
            },
            error: function(xhr, stt, err){
                alert('error');
            }
        });

    }
    
    this.checkUserId =  function(e){
        var accessTokenCognito = localStorage.getItem('accessTokenCognito');
        var url = URL_API + '/api/check_user_id';
        var user_id = '';
        if (e.currentTarget.id == 'id_add') {
          user_id = thisTag.user_approver_add_suggest;
        } else {
          user_id = thisTag.user_approver_edit_suggest;
        }
        // var user_id = e.target.value;
        var comp_code = $('#approver_name1_add').attr('comp_code');
        var kyoten_code = $('option:selected', '#approver_name2_add').attr('kyoten_code');
        var query = [];
        if(user_id == ''){
            return false;
        }
        query.push('token=' +accessTokenCognito);
        query.push('id=' + encodeURIComponent(user_id));
        // query.push('kyoten_code=' + kyoten_code);
        if(e.currentTarget.id == 'ec_users_id_edit'){
            var user_id_old = $("#ec_users_id_edit_old").val();
            if(user_id_old == user_id){
                return;
            }
        }
        url += '?' + query.join('&');

        $.ajax({
            url:url,
            type:'GET',
            timeout: 60000,
            success: function (data){
                if(data.status == 100){
                    riot.mount('app','maintain',{ messege:data.messege });
                    return false;
                }
                if(data.status == 'error'){
                    alert(data.messege);
                    if(e.target.id == 'ec_users_id_edit'){
                        $(e.target).val(user_id_old);
                        return false;
                    }
                    $(e.target).val('');
                    return false;
                }
            },
            error: function(xhr, stt, err){
                alert('error');
            }
        });

    }
    this.checkUserApproverId =  function(e){
        var accessTokenCognito = localStorage.getItem('accessTokenCognito');
        var url = URL_API + '/api/check_approver_user_id';
        var user_id = e.target.value;
        var comp_code = $('#approver_name1_add').attr('comp_code');
        // var kyoten_code = $('option:selected', '#approver_name2_add').attr('kyoten_code');
        var query = [];
        if(user_id == ''){
            return false;
        }
        query.push('token=' +accessTokenCognito);
        query.push('user_id=' + encodeURIComponent(user_id));
        query.push('comp_code=' + comp_code);
        // query.push('kyoten_code=' + kyoten_code);
        if(e.target.id == 'ec_users_id_edit'){
            var user_id_old = $("#ec_users_id_edit_old").val();
            if(user_id_old == user_id){
                return;
            }
        }
        url += '?' + query.join('&');

        $.ajax({
            url:url,
            type:'GET',
            timeout: 60000,
            success: function (data){
                if(data.status == 100){
                    riot.mount('app','maintain',{ messege:data.messege });
                    return false;
                }
                if(data.status == 'error'){
                    alert(data.messege);
                    if(e.target.id == 'ec_users_id_edit'){
                        $(e.target).val(user_id_old);
                        return false;
                    }
                    $(e.target).val('');
                    return false;
                }
            },
            error: function(xhr, stt, err){
                alert('error');
            }
        });

    }
    // Open Edit
    this.openEdit = function(e){
        thisTag.arr_edit = [];
        var index = e.currentTarget.attributes.index.nodeValue;
        thisTag.arr_edit[0] = thisTag.approval_list[index];
        var n=thisTag.approval_list[index].boundary_value_max;
        
        thisTag.update(thisTag.arr_edit);
        thisTag.update({
          user_approver_edit_suggest: $("#id_edit_old").val(),
          user_approved_edit_suggest: $("#ec_users_id_edit_old").val()
        });
        thisTag.getListSuggestApproverEdit();
        var kyoten_code = $('option:selected', '#approver_name2_edit').attr('kyoten_code');
        thisTag.getApprovedUsers('edit',kyoten_code);
        thisTag.getListSuggestApprovedEdit();
        $('#EC91-edit').fadeIn();
        $("#approver_name2_edit").change(function(e) {
          $('#ec_users_id_edit').val('');
          if ($(this).val() == '') {
            thisTag.update({user_approver_edit_suggest: ''});
            $("#ec_users_id_edit").autocompleter("destroy");
          } else {
            var kyoten_code = $('option:selected', '#approver_name2_edit').attr('kyoten_code');
            thisTag.getApprovedUsers('edit',kyoten_code);
          }
        });
        //$(".new_value").val(n.toLocaleString());
    }

    this.getListSuggestApproverEdit = function() {
      let userApprovedEdit = thisTag.users_approver_list;
      let res = [];
      for (var i = 0; i < userApprovedEdit.length; i++) {
        res.push({value: userApprovedEdit[i].name, label: userApprovedEdit[i].name, index: i});
      }
      $("#id_edit").autocompleter("destroy");
      $('#id_edit').autocompleter({ 
        limit:10, 
        source: res, 
        callback: function(value, index, object) {
          if($("#id_edit").val() != '' && userApprovedEdit[object.index].id) {
            thisTag.update({user_approver_edit_suggest: userApprovedEdit[object.index].id});
          } else {
            thisTag.update({user_approver_edit_suggest: ''});
          }
        }
      });
    }

    this.getListSuggestApprovedEdit = function() {
      let userApprovedEdit = thisTag.users_approved_list;
      let res = [];
      for (var i = 0; i < userApprovedEdit.length; i++) {
        if(userApprovedEdit[i].name!=$("#id_edit").val()){
          res.push({value: userApprovedEdit[i].name, label: userApprovedEdit[i].name, index: i});
        }
      }
      $("#ec_users_id_edit").autocompleter("destroy");
      $('#ec_users_id_edit').autocompleter({ 
        limit:10, 
        source: res, 
        callback: function(value, index, object) {
          if($("#ec_users_id_edit").val() != '' && userApprovedEdit[object.index].id) {
            thisTag.update({user_approved_edit_suggest: userApprovedEdit[object.index].id});
          } else {
            thisTag.update({user_approved_edit_suggest: ''});
          }
        }
      });
    }
    

    // Get List Approval Info
    this.getApprovalInfo = function(page){
        var accessTokenCognito = localStorage.getItem('accessTokenCognito');
        var url = URL_API + '/api/get_approver_info';
        var approver_dep_cd = $('option:selected',"#approver_dep_cd_search").attr('kyoten_code');
        var query = [];
        query.push('token=' +accessTokenCognito);
        query.push('page=' + page);
        query.push('limit=' + LIMIT_ON_PAGE);
        if(approver_dep_cd !== ''){
            query.push('approver_dep_cd=' + encodeURI(approver_dep_cd));
        }
        
        url += '?' + query.join('&');
        $.ajax({
            url:url,
            type:'GET',
            timeout: 60000,
            success: function (data){
                if(data.status == 100){
                    riot.mount('app','maintain',{ messege:data.messege });
                    return false;
                }
                thisTag.approval_list = data.approval_list;

                thisTag.update(thisTag.approval_list);
                var totalPages = Math.ceil(data.total/LIMIT_ON_PAGE);
                    thisTag.total_page = totalPages;
                var visiblePages = 1;
                if(totalPages != 0){
                    visiblePages =  5;
                } 

                var $pagination = $('#pagination_ec91');
                $pagination.twbsPagination('destroy');
                $pagination.twbsPagination({
                    totalPages: totalPages,
                    visiblePages: visiblePages,
                    startPage: thisTag.current_page,
                    initiateStartPageClick: false,
                    onPageClick: function (event, page) {
                        thisTag.current_page = page;
                        thisTag.getApprovalInfo(page);
                    }
                });
                
            },
            error: function(xhr, stt, err){
                alert('error');
            }
        });
    }

    this.getApprovedUsers = function(action,kyoten_code) {
      var accessTokenCognito = localStorage.getItem('accessTokenCognito');
        var url = URL_API + '/api/get_approved_users';
        var user_id = '';
        var query = [];
        if(kyoten_code == ''){
            return false;
        }
        query.push('token=' +accessTokenCognito);
        query.push('kyoten_code=' + encodeURIComponent(kyoten_code));
        url += '?' + query.join('&');

        $.ajax({
            url:url,
            type:'GET',
            timeout: 60000,
            success: function (data){
                if(data.status == 100){
                    riot.mount('app','maintain',{ messege:data.messege });
                    return false;
                }
                if(data.status == 'error'){
                    alert(data.messege);
                    return false;
                }

                thisTag.update({
                  users_approved_list: data.users_approved_list
                });
                if (action == 'add') {
                  // $('select[multiple]').multiselect();
                  $('select[multiple]').multiselect('reload');
                } else if (action == 'edit') {
                  thisTag.getListSuggestApprovedEdit();
                }
            },
            error: function(xhr, stt, err){
                alert('error');
            }
        });
    }
    // check Max Min Price
    this.checkPrice = function(min,max){

        if(parseFloat(min) > parseFloat(max)){
            var msg = '承認金額-最小は承認金額-最大より大きいです。';
            return msg; 
        } else if(parseFloat(min) == parseFloat(max)){
            var msg = '承認金額-最小は承認金額-最大と同じです。再度入力してください。';
            return msg; 
        } else {
            return true;
        }
    }

    this.validateRequire = function(field,value,name=''){
        if(value == '' || (field == 'approver_ec_users_id' && value.length == 0) || name != ''){
            var msg = '';
            switch(field){
                case 'id':
                    if (name) {
                      var check_name = arColumn(thisTag.users_approver_list, 'name', name);
                      if (!(check_name.length > 0)) {
                        msg = '承認者は入力必須です。'; 
                      } else {
                        return true;
                      }
                    } else {
                      msg = '承認者は入力必須です。'; 
                    }
                    break;
                case 'approver_name2_add': 
                    msg = '部署名は入力必須です。'; 
                    break;
                case 'approver_ec_users_id': 
                    if (name) {
                      var check_name = arColumn(thisTag.users_approved_list, 'name', name);
                      console.log(check_name);
                      if (!(check_name.length > 0)) {
                        msg = '承認申請者は入力必須です。'; 
                      } else {
                        return true;
                      }
                    } else {
                      msg = '承認申請者は入力必須です。';
                    }
                    break;
                case 'boundary_value_max': 
                    msg = '承認金額-最大 は入力必須です。'; 
                    break;
                case 'boundary_value_min': 
                    msg = '承認金額-最小 は入力必須です。'; 
                    break;
            }
            return msg;
        } else {
            return true;
        }
    }

    this.validateMaximum = function(field,value){

        if(value.length > 255){

            var msg = '';
            switch(field){
                case 'id':
                    msg = 'ログインIDを255文字以内に入力してください。'; 
                    break;
                case 'approver_ec_users_id': 
                    msg = '承認対象ユーザー名を255文字以内に入力してください。 '; 
                    break;
                // case 'boundary_value_max': 
                //     msg = '承認金額-最小 は入力必須です。'; 
                //     break;
                // case 'boundary_value_min': 
                //     msg = '承認金額-最大 は入力必須です。'; 
                //     break;
            }
            return msg;
        } else {
            return true;
        }
    }
    this.minAndMaxEmpty = function(min,max){
        if(min == '' && max == ''){
            var msg = '承認金額-最小または承認金額-最大のいずれかが入力必要です。';
            return msg;
        } else {
            return true;
        }
    }
    this.validateTypeInt = function(field,value){
        var pattern = /^[0-9]*$/;
        if(!pattern.test(value) && value != ''){
            var msg = '';
            switch(field){
                case 'boundary_value_max': 
                    msg = '承認金額-最大を正数のみで入力してください。'; 
                    break;
                case 'boundary_value_min': 
                    msg = '承認金額-最小を正数のみで入力してください。'; 
                    break;
            }
            return msg;
        } else {
            return true;
        }
    }
    // Post Approval Info Add
   this.postApprovalInfo = function(e){
        var checkAction = e.target.attributes.id.nodeValue;
        
        var accessTokenCognito = localStorage.getItem('accessTokenCognito');
        var url = URL_API + '/api/post_approver_info';
        var query = [];
        var data = {};
        query.push('token=' + accessTokenCognito);
        url += '?' + query.join('&');

        switch(checkAction){
            case 'add_approver_info':
                var approver_name1 = $("#approver_name1_add").attr('name');
                var approver_name2 = $('option:selected', '#approver_name2_add').attr('name');
                var comp_code = $('#approver_name1_add').attr('comp_code');
                var kyoten_code = $('option:selected', '#approver_name2_add').attr('kyoten_code');
                var approver_ec_users_id = $("#ec_users_id_add").val();
                var approver_level = $("#approver_level_add").val();
                var boundary_value_min = $("#approver_min_add").val();
                var boundary_value_max =$("#approver_max_add").val();//$("#approver_max_add").val().split(',').join('');
                var id_name_add = $("#id_add").val();
                var id = thisTag.user_approver_add_suggest;
                var msg = [];
                delete msg[0];
                // check id
                if(thisTag.validateRequire('id',id,id_name_add) != true){
                    $("#id_add").val('');
                    msg.push(thisTag.validateRequire('id',id,id_name_add));
                }

                if(thisTag.validateMaximum('id',id) != true){
                    $("#id_add").val('');
                    msg.push(thisTag.validateMaximum('id',id));
                }

                if(thisTag.validateRequire('approver_name2_add',approver_name2) != true){
                    $("#approver_name2_add").val('');
                    msg.push(thisTag.validateRequire('approver_name2_add',approver_name2));
                }
                // check ec user id
                if(thisTag.validateRequire('approver_ec_users_id',approver_ec_users_id) != true){
                    $("#ec_users_id_add").val('');
                    msg.push(thisTag.validateRequire('approver_ec_users_id',approver_ec_users_id));
                }

                if(thisTag.validateMaximum('approver_ec_users_id',approver_ec_users_id) != true){
                    $("#ec_users_id_add").val('');
                    msg.push(thisTag.validateMaximum('approver_ec_users_id',approver_ec_users_id));
                }
                if(boundary_value_min==''){
                  alert("承認金額-最小 は入力必須です");
                  return false;
                }
                if(boundary_value_max==''){
                  alert("承認金額-最小 は入力必須です");
                  return false;
                }
                // check if max min empty
                // if(thisTag.minAndMaxEmpty(boundary_value_min,boundary_value_max) != true){
                //     msg.push(thisTag.minAndMaxEmpty(boundary_value_min,boundary_value_max));
                // }
                // check min
                // if(thisTag.validateRequire('boundary_value_min',boundary_value_min) != true){
                //     $("#approver_min_add").val('');
                //     msg.push(thisTag.validateRequire('boundary_value_min',boundary_value_min));
                // }
                if(thisTag.validateTypeInt('boundary_value_min',boundary_value_min) != true){
                    $("#approver_min_add").val('');
                    msg.push(thisTag.validateTypeInt('boundary_value_min',boundary_value_min));
                }

                // check max
                // if(thisTag.validateRequire('boundary_value_max',boundary_value_max) != true){
                //     $("#approver_max_add").val('');
                //     msg.push(thisTag.validateRequire('boundary_value_max',boundary_value_max));
                // }
                if(thisTag.validateTypeInt('boundary_value_max',boundary_value_max) != true){
                    $("#approver_max_add").val('');
                    msg.push(thisTag.validateTypeInt('boundary_value_max',boundary_value_max));
                }

                

                // check min >< max
                if(thisTag.checkPrice(boundary_value_min,boundary_value_max) != true){
                    $("#approver_min_add").val('');
                    $("#approver_max_add").val('');
                    msg.push(thisTag.checkPrice(boundary_value_min,boundary_value_max));
                }
                if(msg.length > 0){
                    alert(msg.join("\n"));
                    return false;
                }
                var listApproverInfo = [];
                $.each(approver_ec_users_id,function(index, element) {
                  var arrApp = {
                    id: id,
                    approver_name1: approver_name1,
                    approver_name2: approver_name2,
                    comp_code: comp_code,
                    kyoten_code: kyoten_code,
                    approver_ec_users_id: element,
                    approver_level: approver_level,
                    boundary_value_min: boundary_value_min,
                    boundary_value_max: boundary_value_max
                  };

                  listApproverInfo.push(arrApp);
                });
                var data = listApproverInfo;
                var action = 'add';
                break;
            case 'edit_approver_info':
                var approver_name1 = $("#approver_name1_edit").val();
                var approver_name2 = $("#approver_name2_edit").val();
                var comp_code = $('option:selected', '#approver_name1_edit').attr('comp_code');
                var kyoten_code = $('option:selected', '#approver_name2_edit').attr('kyoten_code');
                var approver_ec_users_id_old = $("#ec_users_id_edit_old").val();
                var ec_users_id_edit_old_name = $("#ec_users_id_edit_old_name").val();
                var approver_level = $("#approver_level_edit").val();
                var boundary_value_min = $("#approver_min_edit").val();
                var boundary_value_max = $("#approver_max_edit").val();//$("#approver_max_edit").val().split(',').join('');
                var boundary_value_min_old = $("#approver_min_edit_old").val();
                var boundary_value_max_old = $("#approver_max_edit_old").val();
                var id_name_edit = $("#id_edit").val();
                var approver_ec_users_id_name = $("#ec_users_id_edit").val();
                var id = thisTag.user_approver_edit_suggest;
                var approver_ec_users_id = thisTag.user_approved_edit_suggest;
                var id_old = $("#id_edit_old").val();
                var id_ol_name = $("#id_edit_old_name").val();
                var approver_name2_edit_old = $("#approver_name2_edit_old").val();
                var seq = $("#seq_edit").val();
                var msg = [];
                delete msg[0];
                // check id
                if(thisTag.validateRequire('id',id,id_name_edit) != true){
                    $("#id_edit").val(id_ol_name);
                    msg.push(thisTag.validateRequire('id',id,id_name_edit));
                }
                if(thisTag.validateMaximum('id',id) != true){
                    $("#id_edit").val(id_ol_name);
                    msg.push(thisTag.validateMaximum('id',id));
                }

                if(thisTag.validateRequire('approver_name2_add',approver_name2) != true){
                    $('#approver_name2_edit').val(approver_name2_edit_old).change();
                    $('#ec_users_id_edit').val(ec_users_id_edit_old_name);
                    msg.push(thisTag.validateRequire('approver_name2_add',approver_name2));
                }
                // check ec user id
                if(thisTag.validateRequire('approver_ec_users_id',approver_ec_users_id,approver_ec_users_id_name) != true){
                    $("#ec_users_id_edit").val(ec_users_id_edit_old_name);
                    msg.push(thisTag.validateRequire('approver_ec_users_id',approver_ec_users_id,approver_ec_users_id_name));
                }

                if(thisTag.validateMaximum('approver_ec_users_id',approver_ec_users_id) != true){
                    $("#ec_users_id_edit").val(ec_users_id_edit_old_name);
                    msg.push(thisTag.validateMaximum('approver_ec_users_id',approver_ec_users_id));
                }
                 if(boundary_value_min==''){
                  alert("承認金額-最小 は入力必須です");
                  return false;
                }
                if(boundary_value_max==''){
                  alert("承認金額-最小 は入力必須です");
                  return false;
                }
                // check if max min empty
                // if(thisTag.minAndMaxEmpty(boundary_value_min,boundary_value_max) != true){
                //     msg.push(thisTag.minAndMaxEmpty(boundary_value_min,boundary_value_max));
                // }
                // check min
                // if(thisTag.validateRequire('boundary_value_min',boundary_value_min) != true){
                //     $("#approver_min_edit").val(boundary_value_min_old);
                //     msg.push(thisTag.validateRequire('boundary_value_min',boundary_value_min));
                // }
                if(thisTag.validateTypeInt('boundary_value_min',boundary_value_min) != true){
                    $("#approver_min_edit").val(boundary_value_min_old);
                    msg.push(thisTag.validateTypeInt('boundary_value_min',boundary_value_min));
                }

                // check max
                // if(thisTag.validateRequire('boundary_value_max',boundary_value_max) != true){
                //     $("#approver_max_edit").val(boundary_value_max_old);
                //     msg.push(thisTag.validateRequire('boundary_value_max',boundary_value_max));
                // }
                if(thisTag.validateTypeInt('boundary_value_max',boundary_value_max) != true){
                    $("#approver_max_edit").val(boundary_value_max_old);
                    msg.push(thisTag.validateTypeInt('boundary_value_max',boundary_value_max));
                }

                // check min >< max
                if(thisTag.checkPrice(boundary_value_min,boundary_value_max) != true){
                    $("#approver_min_edit").val(boundary_value_min_old);
                    $("#approver_max_edit").val(boundary_value_max_old);
                    msg.push(thisTag.checkPrice(boundary_value_min,boundary_value_max));
                }

                // result
                if(msg.length > 0){
                    alert(msg.join("\n"));
                    return false;
                }
                var data = {
                    seq: seq,
                    id: id,
                    approver_name1: approver_name1,
                    approver_name2: approver_name2,
                    comp_code: comp_code,
                    kyoten_code: kyoten_code,
                    approver_ec_users_id: approver_ec_users_id,
                    approver_ec_users_id_old: approver_ec_users_id_old,
                    approver_level: approver_level,
                    boundary_value_min: boundary_value_min,
                    boundary_value_max: boundary_value_max
                };
                var action = 'edit';
                break;
            default:
        }


        $.ajax({
            url:url,
            type:'POST',
            timeout: 60000,
            data:{
                data:data,
                action:action
            },
            success: function (data){
                if(data.status == 100){
                    riot.mount('app','maintain',{ messege:data.messege });
                    return false;
                }
                if(data.message == 'success'){
                    if(action == 'add'){
                        alert("登録しました。");
                    } 
                    if(action == 'edit'){
                        alert("編集しました。");
                    }
                    
                    $('#EC91-edit').fadeOut();
                    $('#EC91-add').fadeOut();
                } else {
                    var msg = [];

                      if(data.message == 'user_approver_exist'){
                       alert(data.errors);
                        return false;
                     }
                     if (data.message == 'validateError') {
                      var msgValidate = [];
                        $.each(data.errors,function(index, el) {
                          msgValidate.push(el);
                        });
                        alert(msgValidate.join("\n"));
                        return false;
                     }

            
                    for(var i in data.errors){
                        var errors = data.errors[i];
                        for( var k in errors){
                            for(var h in errors[k]){
                                if($.inArray(errors[k][h], msg ) == -1){
                                    msg.push(errors[k][h]);
                                }
                            }
                        }
                    }
                    alert(msg.join("\n"));
                }
                var page = 1;
                thisTag.getApprovalInfo(page);

            },
            error: function(xhr, stt, err){
                alert('error');
            }
        });
    }
  </script>
</ec91>
