<mc-order-cart>
    <link rel="stylesheet" href="./css/order-cart-mc.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css">
    <div class="main">
        <div class="content">
            <div class="page-title">
                <h2>注文用カート</h2>
            </div>
            <h3 class="data-empty">カートに商品はありません</h3>
            <div class="order-list">
                <p class="required-text" style="text-align:right"><b>＊</b>が付いている項目は必須項目です。</p>
                <div class="order-item-box">
                    <table class="order-item">
                        <tbody each = { element, index in list_cart } id="{ 'item_' + index }" index={index} class="result-cart">
                            <tr>
                                <input type="hidden" value="1" name="mc_order_flg" id="mc_order_flg_{ index }">
                                <td class="left" style="width:52%">
                                    <dl>
                                        <dt>販売元メーカー</dt>
                                        <dd>{ element.sales_origin_maker_name }</dd>
                                    </dl>
                                    <dl>
                                        <dt>製造元メーカー</dt>
                                        <dd>{ element.maker_name }</dd>
                                    </dl>
                                    <dl>
                                        <dt>品番</dt>
                                        <dd>{ element.supp_sku }</dd>
                                    </dl>
                                    <dl>
                                        <dt>品名</dt>
                                        <dd>{ element.item_name }</dd>
                                    </dl>
                                    <dl>
                                        <dt>容量・規格</dt>
                                        <dd>{ element.capacity } {(carts.capacity != '' && carts.kikaku != '') ? '・' : '' } { element.kikaku }</dd>
                                    </dl>
                                    <dl if={element.show_cancel_msg}>
                                        <dd style="color: red;">受注発注品（キャンセル・返品不可）</dd>
                                    </dl>

                                </td>
                                <td style="width: 23%">
                                    <dl>
                                        <dt>定価</dt>
                                        <dd>{  !isNaN(parseFloat(element.list_price)) ? (element.list_price == 0  ? 'お見積り' : parseFloat(element.list_price).toLocaleString('en') + '円' ) : '' }</dd>
                                        <input type="hidden" id="{ 'teika_tanka_' + index}" value="{ element.list_price }">
                                    </dl>
                                    <dl>
                                        <dt>BPM特別価格</dt>
                                        <!-- <dd>{ !isNaN(parseFloat(element.tanka)) ? (element.tanka == 0 || (element.list_price == element.tanka) ? 'お見積り' : parseFloat(element.tanka).toLocaleString('en') + '円' ) : '' }<span></span></dd> -->
                                        <dd>{ !isNaN(parseFloat(element.tanka)) ? parseFloat(element.tanka).toLocaleString('en') + '円' : '' }<span></span></dd>
                                        <input type="hidden" id="{ 'tanka_' + index}" value="{ element.tanka }">
                                    </dl>
                                </td>
                                <td style="position: relative;">
                                    <dl>
                                        <dt><span>数量</span><span class="required-text">＊</span></dt>
                                        <dd>
                                            <input type="text" id="{ 'suryo_' + index }" class="order-input spinner cart-suryo" name="{ index }" value="{ element.suryo }" min="1" tabindex="{index}" maxLength="10" oninput="javascript: if (this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);" style="width: 30px" disabled="disabled">
                                            <span class="msg-item-sku-exist" style="position: absolute;width: 200px;top:100px;">{ !element.item_sku_exist ? 'この商品はカタログから削除されています' : ''}</span>
                                            <div class='alert-item-deleted' style="box-shadow: 1px 1px 1px 1px;font-weight: bold;font-size: 16px;padding: 10px;border: 1px solid;color: red;border-radius: 6px;position:absolute;bottom:-25px;right:-12px;display:{(element.is_deleted == 1) ? 'block' : 'none'}"><h4>販売中止</h4></div>
                                            <input type="hidden" id="{ 'tanka_' + index }" class="cart-tanka" value="{element.tanka}">
                                            <input type="hidden" id="check_deleted_{index}" value="{element.is_deleted}">
                                            <input type="hidden" id="cart_seq_{index}" value="{element.cart_seq}">
                                        </dd>
                                    </dl>
                                </td>
                                <td>
                                    <dl>
                                        <dt>販売金額</dt>
                                        <!-- <dd if={element.list_price == element.tanka}>お見積り</dd> -->
                                        <dd id="{ 'total_price_' + index }" >{ parseInt(element.tanka * element.suryo).toLocaleString('en') }<span>円</span></dd>
                                    </dl>
                                </td>
                                <td class="center"  ><button class="reset-btn" id="{ index }" style="z-index:10;display: inline; }" onclick ={ deleteCart } value="{ index }"><i class="fa fa-times" aria-hidden="true" style="z-index: 0;"></i>削除</button></td>
                            </tr>
                            <tr>
                                <td style="text-align: left;">
                                    <dl>
                                        <dt>都道府県<span class="required-text">＊</span></dt>
                                        <dd>
                                            <select onchange="{ changePrefecture }">
                                                <option value="">---</option>
                                                <option each={item in list_prefecture} value="{item}" selected='{ item == user_prefecture ? 'selected' : ''}' >{item}</option>
                                            </select>
                                        </dd>
                                    </dl>
                                </td>
                            </tr>
                            <tr>
                                <td style="text-align: left;">
                                    <dl style="display:flex !important;">
                                        <dt style="width:67px;">販売店<span class="required-text">＊</span></dt>
                                        <!-- <dd>{ element.dealer_co_name }</dd> -->
                                        <dd style="padding-left: 14px">
                                            <select id="maker_list_{index}" onchange="{changeMakerDealer}">
                                                <option value="">---</option>
                                                <option each={maker_dealer_item in element.list_name} value="{maker_dealer_item.dealer_co_cd}" rel={maker_dealer_item.dealer_dep_cd} selected='{ (element.dealer_co_cd == maker_dealer_item.dealer_co_cd && element.dealer_dep_cd == maker_dealer_item.dealer_dep_cd && element.favorite_dealer_flg_previous == 1) || element.maker_list == maker_dealer_item.dealer_co_cd ? 'selected' : ''}'>{maker_dealer_item.dealer_co_name} {maker_dealer_item.dealer_dep_name}</option>
                                                <option value="other_dealer">リストにない販売店</option>
                                            </select>
                                            <input type="hidden" id="dealer_co_cd_{index}" value="{maker_dealer_item.dealer_co_cd}">
                                            <input type="hidden" id="dealer_dep_cd_{index}" value="{maker_dealer_item.dealer_dep_cd}">
                                        </dd>

                                    </dl>
                                    <div style="display:flex; align-items:center;">
                                        <p class="note-dealer">販売店様とお取引がない場合はこちら</p>
                                        <a class="mc31-modal-open"> <i class="fa fa-question-circle" style="color:#D3D3D3; font-size:24px; padding:10px;"></i></a>
                                    </div><br>
                                </td>
                                <td colspan="3"  style="text-align: left;">
                                    <!-- <input type="checkbox" name="favorite_dealer_flg" value="1" id="favorite_dealer_flg_{ index }" class="favorite_dealer_flg" onchange="{ checkDealer }"  checked="{ checkDealerCoCd(element.dealer_co_cd, element.dealer_dep_cd, index, element.list_name)}"><label for="favorite_dealer_flg_{ index }" class="favorite_dealer_flg">この商品は今後この販売店に発注する</label> -->
                                    <input type="checkbox" name="favorite_dealer_flg" value="1" id="favorite_dealer_flg_{ index }" class="favorite_dealer_flg" onchange="{ checkDealer }"  checked="{ element.favorite_dealer_flg_previous == 1 ? 'checked' : ''}"><label for="favorite_dealer_flg_{ index }" class="favorite_dealer_flg">この商品は今後この販売店に発注する</label>
                                </td>
                            </tr>
                            <tr>
                                <td style="text-align: right;display: none;" class="unlisted_dealers_{ index }">
                                    <dl>
                                        <dt style="color: #0000cd;">リストにない販売店<span class="required-text">＊</span></dt>
                                        <dd>
                                            <input id="recommended_dealer_name_{ index }" value="" name="recommended_dealer_name" style="width: 67%">
                                        </dd>
                                    </dl>
                                </td>

                            </tr>
                            <!-- <tr style="display:{(element.is_deleted == 1 ) ? 'block' : 'none'}">
                               <td style="text-align: left;"><b style="color:red;">販売中止</b></td>
                            </tr> -->
                            <tr>
                                <td colspan="4" style="text-align: left !important;padding-bottom: 0;">
                                    <dt>日本ジェネティクス向け</dt>
                                </td>
                            </tr>
                            <tr>
                                <td class="comment" style="padding-top: 0">
                                    <dl>
                                        <!-- <dt>メーカーと販売店への連絡コメント</dt> -->
                                        <dd><textarea id="{'comment_usr_' + index}">{ element.comment_usr }</textarea></dd>
                                    </dl>
                                </td>
                                <td class="codeTd" colspan="4" style="padding-top: 0">
                                    <!-- <dl style="text-align: left;">
                                        <dt>希望納期<span class="required-text">＊</span></dt>
                                        <dd><input type="text" class="comment_deliverydate" name="{'delivery_date_' + index}" id="{'delivery_date_' + index}" value="{ element.delivery_date }" placeholder="yyyy/mm/dd"></dd>
                                    </dl> -->
                                    <!-- <dl  style="display: { arColumn(list_field, 'navigation_id', 'F006').length ? 'block' : 'none'}">
                                      <dt><span>{arColumn(list_field, 'navigation_id', 'F006')[0]['display_contents']}</span><span class="required-text">＊</span></dt>
                                      <dd><input type="text" class="comment_deliverydate" name="{'comment_deliverydate_' + index}" id="{'comment_deliverydate_' + index}" value="{ element.deliverydate }" placeholder="yyyy/mm/dd"></dd>
                                    </dl> -->
                                    <dl style="display: { arColumn(list_field, 'navigation_id', 'F001').length ? 'block' : 'none'}">
                                        <dt><span>{ arColumn(list_field, 'navigation_id', 'F001')[0]['display_contents'] }</span><span class="required-text">＊</span></dt>
                                        <dd>
                                            <select id="customer_use_code_1_{index}">
                                                <option value="">----</option>
                                                <option each = { e, i in cus_use_code_1 } selected="{(element.customer_use_code_1 == e.code_value) ? 'selected' : ''}" value="{ e.code_value }">{ e.code_value } : { e.code_definition }</option>
                                            </select>
                                        </dd>
                                    </dl>
                                    <dl style="display: { arColumn(list_field, 'navigation_id', 'F002').length ? 'block' : 'none'}">
                                        <dt><span>{ arColumn(list_field, 'navigation_id', 'F002')[0]['display_contents'] }</span><span class="required-text">＊</span></dt>
                                        <dd>
                                            <select id="customer_use_code_2_{index}">
                                                <option value="">----</option>
                                                <option each = { e, i in cus_use_code_2 } selected="{element.customer_use_code_2 == e.code_value ? 'selected' : ''}" value="{ e.code_value }" >{ e.code_value } : { e.code_definition }</option>
                                            </select>
                                        </dd>
                                    </dl>
                                </td>
                            </tr>
                            <tr>
                                <td colspan="4" style="text-align: left !important;padding-bottom: 0;">
                                    <dt>販売店向けコメント</dt>
                                </td>
                            </tr>
                            <tr>
                                <td class="comment" style="padding-top: 0">
                                    <textarea id="{'comment_spp_' + index}">{ element.comment_spp }</textarea>
                                </td>
                            </tr>
                            <tr>
                                <td colspan="4" class="left" style="padding-bottom: 0;">
                                    <dt>発送先</dt>
                                </td>
                            </tr>
                            <tr>
                                <td colspan="4" class="left" style="display: flex">
                                    <div>
                                        <div class="item">
                                            <input index="{index}" type="radio" name="{'customer_use_code_3_' + index}" class="customer_use_code_3" value="{ DEALER_ADDRESS }" id="{ 'use_code_3_' + DEALER_ADDRESS + '_' + index}">
                                            <label for="{ 'use_code_3_' + DEALER_ADDRESS + '_' + index}">販売店</label>
                                        </div>
                                        <div class="item">
                                            <input index="{index}" type="radio" name="{'customer_use_code_3_' + index}" class="customer_use_code_3" value="{ USER_ADDRESS }" id="{ 'use_code_3_' + USER_ADDRESS + '_' + index}">
                                            <label for="{ 'use_code_3_' + USER_ADDRESS + '_' + index}">ご登録住所</label>
                                        </div>
                                        <div class="item">
                                            <input index="{index}" type="radio" name="{'customer_use_code_3_' + index}" class="customer_use_code_3" value="{ OTHER_ADDRESS }" id="{ 'use_code_3_' + OTHER_ADDRESS + '_' + index}">
                                            <label for="{ 'use_code_3_' + OTHER_ADDRESS + '_' + index}">その他住所</label>
                                        </div>
                                    </div>
                                    <div index="{index}" class="address">
                                        <div class="left address-info" style="margin-top: 30px">
                                            <div>施設名</div>
                                            <span class="nonyu_saki_text" id="nonyu_saki_text_{index}" hidden></span>
                                            <textarea class="nonyu_saki" placeholder="企業名部署名、大学名学部名研究室名などをご記入ください"></textarea>
                                        </div>
                                        <div class="left address-info">
                                            <div>郵便番号</div>
                                            <span class="nonyu_zip_1_text" id="nonyu_zip_1_text_{index}" hidden></span>
                                            <input class="nonyu_zip nonyu_zip_1" type="text" maxlength="3" style="">
                                            <span class="dash">-</span>
                                            <span id="nonyu_zip_2_text_{index}" class="nonyu_zip_2_text" hidden></span>
                                            <input class="nonyu_zip nonyu_zip_2" type="text" maxlength="4" style="width: 60px">
                                        </div>
                                        <div class="left address-info">
                                            <div>住所</div>
                                            <span class="nonyu_address1_text" id="nonyu_address1_text_{index}" hidden></span>
                                            <textarea class="nonyu_address1"></textarea>
                                        </div>
                                        <div class="left address-info">
                                            <div>お電話番号</div>
                                            <span id="nonyu_tel_1_text_{index}" class="nonyu_tel_1_text" hidden></span>
                                            <input class="nonyu_tel nonyu_tel_1" type="text" maxlength="4" >
                                            <span class="dash">-</span>
                                            <span id="nonyu_tel_2_text_{index}" class="nonyu_tel_2_text" hidden></span>
                                            <input class="nonyu_tel nonyu_tel_2" type="text" maxlength="4" style="width: 50px">
                                            <span class="dash">-</span>
                                            <span class="nonyu_tel_3_text_{idex}" class="nonyu_tel_3_text" hidden></span>
                                            <span class="nonyu_tel_3_text" hidden></span>
                                            <input class="nonyu_tel nonyu_tel_3" type="text" maxlength="4" style="width: 60px">
                                        </div>
                                    </div>
                                </td>
                            </tr>
                        </tbody>
                        <tfoot>
                            <tr>
                                <td colspan="4" class="left required-text">
                                    <p>【ご注意ください】</p>
                                    <p>※WEBに「在庫あり」と表示されていてもご注文のタイミングにより在庫切れとなる場合がございます。</p>
                                    <p>※納期をお急ぎの場合はコメント欄に納入期限をご入力ください。別途担当者よりご連絡申し上げます。</p>
                                    <p style="margin-left: 15px">状況によりましてはご希望に沿えない場合がございます。予めご了承いただけますようお願いいたします。</p>
                                </td>
                            </tr>
                            <tr>
                                <td class="left">
                                    <button class="color-btn orange-btn" type="button" style="display: { 'inline' }"　onclick={ redirectOrderCart2 }>内容確認へ進む</button>
                                </td>
                                <td colspan="3">
                                    <dl>
                                        <dt>商品代金</dt>
                                        <dd id="total"></dd>
                                    </dl>
                                    <dl>
                                        <dt><a class="mc31-postage-modal-open"><i class="fa fa-question-circle" style="color: #D3D3D3; font-size:18px; padding:0px 5px;"></i></a>送料</dt>
                                        <dd id="postage"></dd>
                                    </dl>
                                    <dl>
                                        <dt>小計</dt>
                                        <dd id="total_postage"></dd>
                                    </dl>
                                    <dl>
                                        <dt>消費税(10%)</dt>
                                        <dd id="tax"></dd>
                                    </dl>
                                    <dl class="total">
                                        <dt>合計</dt>
                                        <dd id="total_all"></dd>
                                    </dl>
                                    <dl>
                                        <dd style="text-align: left;font-size: 11px;"><b style="color:red;">{msg}</b></dd>
                                    </dl>
                                </td>
                                <td></td>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
        </div>
    </div>
    <div class="o_modal" id="mc31-modal">
      <div class="o_modal_box" >
        <div class="o_modal_head">
          <a id="mc31-modal-close"><i class="fa fa-times"></i></a>
        </div>
        <div class="o_modal_body">

            <p style="padding-bottom:10px;">お取引実績がない、もしくは販売店様がお分かりにならない場合</p>
            <p>お手数ですが弊社までお問合せください。</p><br>
            <p>ngc_ecgoing@genetics-n.co.jp</p>
        </div>
      </div>
    </div>

    <div class="o_modal" id="mc31-postage">
      <div class="o_modal_box" >
        <div class="o_modal_head">
          <a id="mc31-postage-modal-close"><i class="fa fa-times"></i></a>
        </div>
        <div class="o_modal_body postage-msg">
            <div>
                <span>ご注文金額合計</span> 
                <span style="margin-left:20%;">送料</span>
            </div>

            <div class="postage-msg-main"></div>

            <br><p>税抜表示</p>
            <p>送料ご請求先：GeneF@N会員様</p><br>
        </div>
      </div>
    </div>

    <script>
        var user_info = JSON.parse(sessionStorage.getItem('EcUserInfo'));
        this.user_prefecture = user_info.prefecture;
        this.list_button = user_info.list_navigation.B;
        this.list_field = user_info.list_navigation.F;
        this.list_cart = opts.result;
        this.cus_use_code_1 = opts.cus_use_code_1;
        this.cus_use_code_2 = opts.cus_use_code_2;
        this.maker_dealer_list = opts.maker_dealer_list;
        this.list_name = opts.list_name;
        this.genPostageList = opts.genPostageList;
        this.msg = '';
        this.list_maker_dealer = opts.list_maker_dealer;
        this.DEALER_ADDRESS = 1;
        this.USER_ADDRESS = 2;
        this.OTHER_ADDRESS = 3;
        var thistag = this;
        var list_cart_update = this.list_cart;
        this.list_prefecture = arMergeKeepBase(list_prefectures, this.list_maker_dealer);

        this.on('mount', function () {
            if (!list_cart_update.length) {
                $('.order-list').hide();
                thistag.list_cart=list_cart_update;
            } else {
                $('.data-empty').hide();
                list_cart_update.forEach((item,id)=>{
                    $(`#maker_list_${id}`).val(item.dealer_co_cd);
                });
            }
            //thistag.checkPriceToVisibleMsg();
            this.updateTotal();
            this.updateAddress();

            var postAge = this.genPostageList;
            var postAgeMsg = ''; 
            for (var i = 0;i < postAge.length; i++) {
                if (!postAge[i]['cart_total_max']) {
                    postAgeMsg += '<p>'+(postAge[i]['cart_total_min']).toLocaleString('en')+'円以上 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '+(postAge[i]['postage']).toLocaleString('en')+'円</p>';   
                }

                if (!postAge[i]['cart_total_min']) {
                    postAgeMsg += '<p>&nbsp; '+(postAge[i]['cart_total_max']+1).toLocaleString('en')+'円未満 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '+(postAge[i]['postage']).toLocaleString('en')+'円</p>';   
                }

                if (postAge[i]['cart_total_max'] && postAge[i]['cart_total_min']) {
                    postAgeMsg += '<p>'+(postAge[i]['cart_total_max']+1).toLocaleString('en')+'円未満'+'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'+(postAge[i]['postage']).toLocaleString('en')+'円</p>';
                }

            }
            $('.postage-msg-main').append(postAgeMsg);
        });


        this.checkDealerCoCd = function(dealer_co_cd, dealer_dep_cd, index, list_dealer_name){
            var index_dealer = 0;
            if (list_dealer_name) {
                list_dealer_name.forEach(function(element) {
                    if (element.dealer_co_cd == dealer_co_cd && element.dealer_dep_cd == dealer_dep_cd) {
                        index_dealer++;
                    }
                });
            }
            if (dealer_co_cd && index_dealer) {
                return 'checked';
            }
            return '';

        }

        $(function(){
            $('input[type=radio][class=customer_use_code_3]').change(function() {
            var index = $(this).attr('index');
            var value = $(this).val();
            if (value != thistag.DEALER_ADDRESS) {
                if ( value == thistag.USER_ADDRESS ) {
                    var zip = user_info.zip != null ? user_info.zip.replace('-','') : '';
                    var zip_1 = zip.slice(0,3);
                    var zip_2 = zip.slice(3,7);
                    var tel_1 = '';
                    var tel_2 = '';
                    var tel_3 = '';
                    if (user_info.sosiki_telephone != null) {
                        if (user_info.sosiki_telephone.indexOf('-') != -1) {
                            var telephone_arr = user_info.sosiki_telephone.split('-');
                            tel_1 = telephone_arr[0] != undefined ? telephone_arr[0] : '';
                            tel_2 = telephone_arr[1] != undefined ? telephone_arr[1] : '';
                            tel_3 = telephone_arr[2] != undefined ? telephone_arr[2] : '';
                        } else {
                            tel_1 = user_info.sosiki_telephone.slice(0,3);
                            tel_2 = user_info.sosiki_telephone.slice(3,6);
                            tel_3 = user_info.sosiki_telephone.slice(6,10);
                        }
                    }
                    var nonyu_add_1 = '';
                    if (user_info.building) {
                        nonyu_add_1 = user_info.prefecture + user_info.address + '　' + user_info.building;
                    } else {
                        nonyu_add_1 = user_info.prefecture + user_info.address;
                    }
                        
                    $(".address[index=" + index + "]").find('.nonyu_saki').val(user_info.kikan_name + ' ' + user_info.shozoku_name);
                    $(".address[index=" + index + "]").find('.nonyu_zip_1').val(zip_1);
                    $(".address[index=" + index + "]").find('.nonyu_zip_2').val(zip_2);
                    $(".address[index=" + index + "]").find('.nonyu_address1').val(nonyu_add_1);
                    $(".address[index=" + index + "]").find('.nonyu_tel_1').val(tel_1);
                    $(".address[index=" + index + "]").find('.nonyu_tel_2').val(tel_2);
                    $(".address[index=" + index + "]").find('.nonyu_tel_3').val(tel_3);
                    $(".address[index=" + index + "]").find('.nonyu_saki_text').text(user_info.kikan_name + ' ' + user_info.shozoku_name);
                    $(".address[index=" + index + "]").find('.nonyu_zip_1_text').text(zip_1);
                    $(".address[index=" + index + "]").find('.nonyu_zip_2_text').text(zip_2);
                    $(".address[index=" + index + "]").find('.nonyu_address1_text').text(nonyu_add_1);
                    $(".address[index=" + index + "]").find('.nonyu_tel_1_text').text(tel_1);
                    $(".address[index=" + index + "]").find('.nonyu_tel_2_text').text(tel_2);
                    $(".address[index=" + index + "]").find('.nonyu_tel_3_text').text(tel_3);

                    $(".address[index=" + index + "]").find('span').show();
                    $(".address[index=" + index + "]").find('input, textarea').hide();
                } else {
                    $(".address[index=" + index + "]").find('.nonyu_saki').val('');
                    $(".address[index=" + index + "]").find('.nonyu_zip').val('');
                    $(".address[index=" + index + "]").find('.nonyu_address1').val('');
                    $(".address[index=" + index + "]").find('.nonyu_tel').val('');
                    $(".address[index=" + index + "]").find('span:not(.dash)').hide();
                    $(".address[index=" + index + "]").find('input, textarea').show();
                }
                $(".address[index=" + index + "]").show();
            } else {
                $(".address[index=" + index + "]").hide();
            }
        });
            $('.nonyu_zip').keyup(function () {
                if ($(this).val().trim().length == $(this).attr('maxlength') && thistag.checkInteger($(this).val().trim())) {
                    $(this).next().next('input').focus();
                }
            })
            var spinner = $( ".spinner" ).spinner();
            $(".comment_deliverydate").on('blur', function(e){

                var date = new Date();
                var currentDate = $(e.target).val();

                var date_regex_only_num =   /^[0-9]{4}(0[1-9]|1[0-2])(0[1-9]|[1-2][0-9]|3[0-1])$/ ;

                if(date_regex_only_num.test(currentDate)){
                    var y = currentDate.substring(0,4);
                    var m = currentDate.substring(4,6);
                    var d = currentDate.substring(6,8);
                    currentDate = ''+y+'/'+m+'/'+d+'';
                    $(e.target).val(currentDate);
                }

                var date_regex =   /^[0-9]{4}\/(0[1-9]|1[0-2])\/(0[1-9]|[1-2][0-9]|3[0-1])$/ ;
                if(!(date_regex.test(currentDate)) && currentDate != '')
                {
                    alert("本日以降の日付を選択してください。");
                    $(this).val('');
                    return false;
                }

                var yesterday = new Date(date.getTime());
                yesterday.setDate(date.getDate() - 1);

                if( new Date( currentDate ) < yesterday){
                    alert("本日以降の日付を選択してください。");
                    $(this).val('');
                    return false;
                }

            });

            $( ".cart-suryo" ).spinner({
                stop: function( event, ui ) {
                    var id = event.target.id;
                    var suryo = $('#'+id).val();

                    if(suryo <= 0){
                        alert('0以上の正数を入力してください。');
                        $('#' + id).val('1');
                    }
                    thistag.suryoChange(event);
                },
            });

            $('.mc31-modal-open').on('click',function(e){
                $('#mc31-modal').fadeIn();
            });

            $('#mc31-modal-close').on('click',function(){
                $('#mc31-modal').fadeOut();
            });

            $('.mc31-postage-modal-open').on('click',function(e){
                $('#mc31-postage').fadeIn();
            });

            $('#mc31-postage-modal-close').on('click',function(){
                $('#mc31-postage').fadeOut();
            });
        // =====================

            $('.comment_deliverydate').datetimepicker({
                format: 'Y/m/d',
                onShow:function( ct ){
                    this.setOptions({
                        minDate: new Date()
                    })
                },
                timepicker:false
            });
            // $('.comment_deliverydate').datetimepicker('update', '');

        // ====================

            if(ec31_cart_list.length > 0){
                $.each(thistag.list_cart,function(index,element){
                    jQuery.grep(ec31_cart_list, function( n, i ) {
                        if(n.cart_seq == element.cart_seq){
                            //console.log( n.cart_seq + "===" + element.cart_seq );
                            thistag.list_cart[index]['suryo'] = n.suryo;
                        }
                    });
                });
                thistag.list_cart = data.list_cart;
                thistag.update(thistag.list_cart);
                thistag.updateTotal();
            }
            
           
        });

        this.changeMakerDealer = function(e){
            var value = $(e.target).val();
            var split = value.split("_");
            var dealer_co_cd = split[0];
            var dealer_dep_cd = split[1];

            var dealer = $(e.target).val();
            var dealer_rel = $(e.target).find(':selected').attr('rel');
            var index_check = $(e.target).attr('id').match(/\d+/)[0];
            // console.log("index_check==" + index_check);
            if (list_cart_update.length > 0) {
                if (dealer == 'other_dealer') {
                    $('.unlisted_dealers_' + index_check).show();
                    $('#favorite_dealer_flg_' + index_check).prop('disabled', true);
                    $('#favorite_dealer_flg_' + index_check).prop('checked', false);
                    return;
                } else {
                    $('.unlisted_dealers_' + index_check).hide();
                    $('#favorite_dealer_flg_' + index_check).prop('disabled', false);
                }
                var item_sku = list_cart_update[index_check].item_sku;
                // console.log(list_cart_update);
                for(var i = 0; i < list_cart_update.length; i++) {
                    var item_sku_other = list_cart_update[i].item_sku;
                    // console.log("===============================");
                    // console.log("item_sku_other==" + item_sku_other);
                    // console.log("item_sku_current==" + item_sku);
                    // console.log("dealer==" + dealer);
                    // console.log("dealer_rel==" + dealer_rel);
                    if (item_sku == item_sku_other) {
                        if (dealer == '') {
                            $('#favorite_dealer_flg_' + i).prop('checked', false);
                        }
                        if (dealer != 'other_dealer') {
                            $('.unlisted_dealers_' + i).hide();
                            $('#recommended_dealer_name_' + i).val('');
                            $('#favorite_dealer_flg_' + i).prop('disabled', false);
                        }

                        $('#maker_list_' + i + ' option[value="' + dealer +'"][rel="' + dealer_rel + '"]').prop('selected', true);
                    }
                }
            }

        }

        this.checkDealer = function(e){
            if (list_cart_update.length > 0) {
                var index_check = $(e.target).attr('id').match(/\d+/)[0];
                var dealer = $('#maker_list_' + index_check).val();
                var item_sku = list_cart_update[index_check].item_sku;
                 for(var i = 0; i < list_cart_update.length; i++) {
                    var item_sku_other = list_cart_update[i].item_sku;
                    if (item_sku == item_sku_other) {
                        if ($(e.target).is(":checked")) {
                            if (dealer != '') {
                                $('#favorite_dealer_flg_' + i).prop('checked', true);
                            }
                        } else {
                            $('#favorite_dealer_flg_' + i).prop('checked', false);
                        }
                    }
                }

            }
        }

        this.checkPriceToVisibleMsg = function(e){
            $.each(thistag.list_cart, function(i, e){
                if(parseInt(e.tanka) === parseInt(e.list_price)){
                    thistag.msg = '※BPM特別価格に「お見積り」が含まれる場合、定価をベースに算出しています。';
                }
            });
            thistag.update(thistag.msg);
        }
        this.updateTotal = function (e) {
            var list_cart = thistag.list_cart;
            var total = 0;
            var postage = 0;
            for(var i = 0; i < list_cart.length; i++) {
                if (!isNaN($('#suryo_' + i).val()) && !isNaN($('#tanka_' + i).val())) {
                    total += $('#suryo_' + i).val() * $('#tanka_' + i).val();
                }
            }

            postage = this.getPostageVal(total);
            var total_convert = parseInt(total).toLocaleString('en');
            var postage_convert = parseInt(postage).toLocaleString('en');
            var total_postage = parseInt(total) + parseInt(postage);
            var total_postage_convert = parseInt(total_postage).toLocaleString('en');
            var tax = (total + postage) * 0.1;
            var tax_convert = parseInt(tax).toLocaleString('en');
            var total_all_convert = parseInt(total + postage + tax).toLocaleString('en');
            $('#total').text(total_convert + '円');
            $('#tax').text(tax_convert + '円');
            $('#postage').text(postage_convert + '円');
            $('#total_postage').text(total_postage_convert + '円');
            $('#total_all').text(total_all_convert + '円');
        }
        this.deleteCart = function (e) {
            var index = e.target.value;
            var list_cart = thistag.list_cart;
            var accessTokenCognito = sessionStorage.getItem('accessTokenCognito');
            var url = URL_API + '/api/cart/create?token=' + accessTokenCognito;
            var data = [];
            data.push({
                cart_seq: list_cart[index].cart_seq,
                status: 99
            });
            $.ajax({
                url: url,
                type: 'POST',
                timeout: 20000,
                data: {
                    data: data
                },
                success: function (data) {
                    if(data.status == 100){
                        riot.mount('app','maintain',{ messege:data.messege });
                        return false;
                    }
                    riot.route.exec();
                },
                error: function (xhr, stt, error) {
                    alert('error');
                }
            });
        }
        this.noteAll = function () {
            var accessTokenCognito = sessionStorage.getItem('accessTokenCognito');
            var agent = sessionStorage.getItem('agent_id');
            var url = URL_API + '/api/cart/create_mc_screen?token=' + accessTokenCognito;
            var data = [];
            var cart_update = [];
            var currentdate = new Date();
            var date = ((currentdate.getDate() < 10) ? "0":"") + currentdate.getDate();
            var month = (((currentdate.getMonth() + 1) < 10) ? "0":"") + (currentdate.getMonth()+1);
            var year = currentdate.getFullYear();
            var order_date = year + '-' +  month + '-' +  date;

            $.each(list_cart_update, function (index, element) {
                var select_dep = $('#maker_list_'+index + ' option:selected').attr('rel');
                //  var cart = {
                //     cart_seq: element.cart_seq,
                //     dealer_co_cd: $('#maker_list_'+index).val(),
                //     suryo: $('#suryo_'+index).val(),
                //     order_date: order_date,
                //     favorite_dealer_flg: $('#favorite_dealer_flg_'+index).is(":checked") ? 1 : '',
                //     recommended_dealer_name: $('#maker_list_'+index).val() == 'other_dealer' ? $('#recommended_dealer_name_' + index).val() : '',
                //     mc_order_flg: $('#mc_order_flg_' + index).val(),
                //     supp_sku: element.supp_sku,
                //     teika_tanka: $('#teika_tanka_'+index).val(),
                //     tanka: $('#tanka_'+index).val(),
                //     comment_usr: $('#comment_usr_'+index).val(),
                //     deliverydate: $('#comment_deliverydate_'+index).val(),
                //     customer_use_code_1: $("#customer_use_code_1_" + index).val(),
                //     customer_use_code_2: $("#customer_use_code_2_" + index).val(),
                //     agent_users_id: agent,
                //     status: 4
                // };
                // data.push(cart);
                var delivery_date_post = $('#delivery_date_' + index).val();
                 data.push({
                    cart_seq: element.cart_seq,
                    dealer_co_cd: $('#maker_list_'+index).val() != 'other_dealer' ? $('#maker_list_'+index).val() : '',
                    dealer_dep_cd: select_dep != 'other_dealer' ? select_dep : '',
                    suryo: $('#suryo_'+index).val(),
                    order_date: order_date,
                    favorite_dealer_flg: $('#favorite_dealer_flg_'+index).is(":checked") ? 1 : '',
                    recommended_dealer_name: $('#maker_list_'+index).val() == 'other_dealer' ? $('#recommended_dealer_name_' + index).val() : '',
                    mc_order_flg: $('#mc_order_flg_' + index).val(),
                    // deliverydate: delivery_date_post,
                    supp_sku: element.supp_sku,
                    teika_tanka: $('#teika_tanka_'+index).val(),
                    tanka: $('#tanka_'+index).val(),
                    comment_usr: $('#comment_usr_'+index).val(),
                    customer_use_code_1: $("#customer_use_code_1_" + index).val(),
                    customer_use_code_2: $("#customer_use_code_2_" + index).val(),
                    agent_users_id: agent,
                    status: 4
                  });
            });

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

                    alert('注文しました。');
                    thistag.list_cart = '';
                    thistag.update(thistag.list_cart);
                    $('.order-list').hide();
                    $('.data-empty').show();
                    // var status = 0;
                    // var screen = 'mc31';
                    // var url_get = URL_API + '/api/cart/list_mc?token=' + accessTokenCognito + '&status=' + status + '&screen=' + screen;
                    // $.ajax({
                    //     url: url_get,
                    //     type: 'GET',
                    //     timeout: 60000,
                    //     success: function (data) {
                    //         if(data.status == 100){
                    //             riot.mount('app','maintain',{ messege:data.messege });
                    //             return false;
                    //         }
                    //         if(data.status == 'not_authorize'){
                    //             var url = "#";
                    //             $(location).attr("href", url );
                    //             return false;
                    //         }

                    //         // riot.mount('app', 'mc-order-cart', { result: data.cart_list });

                    //     },
                    //     error: function (xhr, stt, err) {
                    //         alert('error');
                    //     }
                    // });
                    // riot.route.exec();
                },
                error: function (e) {
                    alert('エラー');
                }
            });

        }

        $('.favorite_dealer_flg').on('change', function() {
            alert(this.val());
        });
        this.redirectOrderCart2 = function () {
            if (list_cart_update.length > 0) {
                var check_deleted = 0;
                var check_required_deliverydate = 0;
                var check_rule_deliverydate = 0;
                var check_required_deliverydate_cus_code_1 = 0;
                var check_required_deliverydate_cus_code_2 = 0;
                var error = [];
                var suryo_error = 0;
                var maker_list_error = 0;
                var list_dealer_error = 0;
                var delivery_date_error = 0;
                var comment_usr_max_error = 0;
                var comment_spp_max_error = 0;
                var apos_error = 0;
                var nonyu_saki_error = {};
                var nonyu_zip_error = {};
                var nonyu_address1_error = {};
                var nonyu_tel_error = {};
                var total = 0;

                for(var i = 0; i < list_cart_update.length; i++) {
                    var deleted = $("#check_deleted_"+i).val();
                    if(deleted == 1){
                       check_deleted = 1;
                    }

                    if($('#comment_deliverydate_'+i).parent().parent().css('display') !== 'none' && $('#comment_deliverydate_'+i).val() == ""){
                        check_required_deliverydate = 1;
                    }

                    var date = new Date();
                    var yesterday = new Date(date.getTime());
                    yesterday.setDate(date.getDate() - 1);

                    if(  $('#comment_deliverydate_'+i).parent().parent().css('display') !== 'none' && new Date( $('#comment_deliverydate_'+i).val() ) < yesterday){
                        $('#comment_deliverydate_'+i).val('');
                        check_rule_deliverydate = 1;
                    }

                    if( $('#customer_use_code_1_'+i).parent().parent().css('display') !== 'none' &&  $('#customer_use_code_1_'+i).val() == 0){
                        check_required_deliverydate_cus_code_1 = 1;
                    }

                    if($('#customer_use_code_2_'+i).parent().parent().css('display') !== 'none' && $('#customer_use_code_2_'+i).val() == 0){
                        check_required_deliverydate_cus_code_2 = 1;
                    }
                    if($('#suryo_' + i).val() <= 0){
                        suryo_error++;
                    }
                    if($('#maker_list_' + i).val() == ''){
                        maker_list_error++;
                    }

                    if ($('#maker_list_' + i).val() == 'other_dealer') {
                        if($('#recommended_dealer_name_' + i).val().trim() == ''){
                            list_dealer_error++;
                        }
                    }

                    if($('#delivery_date_' + i).val() == ''){
                        delivery_date_error++;
                    }

                    if($('#comment_usr_' + i).val().length > 100){
                        comment_usr_max_error++;
                    }

                    if($('#comment_spp_' + i).val().length > 100){
                        comment_spp_max_error++;
                    }

                    if($('#comment_usr_' + i).val().includes("'") || $('#comment_spp_' + i).val().includes("'")) {
                        apos_error++;
                    }

                    // check address info
                    var address_type = $('.customer_use_code_3[index=' + i + ']:checked').val();
                    if (address_type != thistag.DEALER_ADDRESS) {
                        var nonyu_saki = $(".address[index=" + i + "]").find('.nonyu_saki').val().trim();
                        var nonyu_zip_1 = $(".address[index=" + i + "]").find('.nonyu_zip_1').val().trim();
                        var nonyu_zip_2 = $(".address[index=" + i + "]").find('.nonyu_zip_2').val().trim();
                        var nonyu_address1 = $(".address[index=" + i + "]").find('.nonyu_address1').val().trim();
                        var nonyu_tel_1 = $(".address[index=" + i + "]").find('.nonyu_tel_1').val().trim();
                        var nonyu_tel_2 = $(".address[index=" + i + "]").find('.nonyu_tel_2').val().trim();
                        var nonyu_tel_3 = $(".address[index=" + i + "]").find('.nonyu_tel_3').val().trim();
                        if (address_type == thistag.OTHER_ADDRESS) {
                            if (nonyu_saki.length == 0) {
                                nonyu_saki_error.require = 1;
                            } else if (nonyu_saki.length > 100) {
                                nonyu_saki_error.max_length = 1;
                            }
                            
                            if ((nonyu_zip_1.length + nonyu_zip_2.length) == 0) {
                                nonyu_zip_error.require = 1;
                            } else if (!thistag.checkInteger(nonyu_zip_1) || !thistag.checkInteger(nonyu_zip_2)) {
                                nonyu_zip_error.integer = 1;
                            } else if (nonyu_zip_1.length != 3 || nonyu_zip_2.length != 4) {
                                nonyu_zip_error.format = 1;
                            }
                            
                            if (nonyu_address1.length == 0) {
                                nonyu_address1_error.require = 1;
                            } else if(nonyu_address1.length > 100) {
                                nonyu_address1_error.max_length = 1;
                            }
                            
                            if ((nonyu_tel_1.length + nonyu_tel_2.length + nonyu_tel_3.length) == 0) {
                                nonyu_tel_error.require = 1;
                            } else if (!thistag.checkInteger(nonyu_tel_1) || !thistag.checkInteger(nonyu_tel_2) || !thistag.checkInteger(nonyu_tel_2)) {
                                nonyu_tel_error.integer = 1;
                            } else if (!thistag.checkFormatTel(nonyu_tel_1, nonyu_tel_2, nonyu_tel_3)){
                                nonyu_tel_error.format = 1;
                            }
                            if(nonyu_saki.includes("'")|| nonyu_address1.includes("'")){
                                apos_error++;
                            }
                        }
                        list_cart_update[i].nonyu_saki = nonyu_saki;
                        list_cart_update[i].nonyu_zip = nonyu_zip_1 + '-' + nonyu_zip_2;
                        list_cart_update[i].nonyu_address1 = nonyu_address1;
                        list_cart_update[i].nonyu_tel = nonyu_tel_1 + '-' + nonyu_tel_2 + '-' + nonyu_tel_3;
                    }
                    if(address_type == thistag.DEALER_ADDRESS) {
                        var nonyu_add_1 = '';
                        if (user_info.building) {
                            nonyu_add_1 = user_info.prefecture + user_info.address + '　' + user_info.building;
                        } else {
                            nonyu_add_1 = user_info.prefecture + user_info.address;
                        }
                        list_cart_update[i].nonyu_saki = user_info.kikan_name + ' ' + user_info.shozoku_name;
                        list_cart_update[i].nonyu_zip = user_info.zip;
                        list_cart_update[i].nonyu_address1 = nonyu_add_1;
                        list_cart_update[i].nonyu_tel = user_info.sosiki_telephone;
                    }
                    list_cart_update[i].customer_tanto = user_info.name;
                    list_cart_update[i].customer_use_code_3 = address_type;
                    list_cart_update[i].customer_use_code_3_text = $('.customer_use_code_3[index=' + i + ']:checked').next('label').text();
                    // list_cart_update[i].siire_tanka = list_cart_update[i].tanka;
                    // list_cart_update[i].list_price = list_cart_update[i].list_price;
                    list_cart_update[i].suryo = $('#suryo_' + i).val();
                    list_cart_update[i].comment_usr = $('#comment_usr_' + i).val();
                    list_cart_update[i].comment_spp = $('#comment_spp_' + i).val();
                    list_cart_update[i].deliverydate = $('#comment_deliverydate_'+i).val();
                    list_cart_update[i].customer_use_code_1 = $("#customer_use_code_1_"+i).val();
                    list_cart_update[i].customer_use_code_2 = $("#customer_use_code_2_"+i).val();
                    if(!$('#recommended_dealer_name_'+i).val()){
                        list_cart_update[i].dealer_co_cd = $("#maker_list_"+i+" option:selected").val();
                        list_cart_update[i].dealer_dep_cd = $("#maker_list_"+i + " option:selected").attr("rel");
                    } else {
                        list_cart_update[i].dealer_co_cd = null;
                        list_cart_update[i].dealer_dep_cd = null;
                    }
                    list_cart_update[i].maker_list_text = $("#maker_list_"+i+" option:selected").text();
                    list_cart_update[i].favorite_dealer_flg = $('#favorite_dealer_flg_'+i).is(":checked") ? 1 : null,
                    list_cart_update[i].mc_order_flg = $("#mc_order_flg_"+i).val();
                    list_cart_update[i].recommended_dealer_name = $('#maker_list_'+i+" option:selected").val() == 'other_dealer' ? $('#recommended_dealer_name_' + i).val() : '',
                    list_cart_update[i].recommended_dealer_name_text = $('#recommended_dealer_name_'+i).val(),
                    total += $('#suryo_' + i).val() * list_cart_update[i].tanka;
                }
                if(suryo_error > 0){
                    error.push("数量：0以上の正数を入力してください。\n");
                }
                if(maker_list_error > 0){
                    error.push("販売店は必須項目です。\n");
                }
                if(list_dealer_error > 0){
                    error.push("販売店を入力してください。\n");
                }
                if(check_required_deliverydate == 1){
                    error.push("希望納期は必須項目です。\n");
                }
                if(check_rule_deliverydate == 1){
                    error.push("希望納期に本日以降の日付を選択してください。\n");
                }
                if(check_required_deliverydate_cus_code_1 == 1){
                    error.push("コストセンターコードは必須項目です。\n");
                }
                if(check_required_deliverydate_cus_code_2 == 1){
                    error.push("科目コードは必須項目です。\n");
                }
                if(check_deleted == 1){
                    error.push("カート内に販売中止の商品が存在しています。\n");
                }
                if(delivery_date_error > 0){
                    error.push("希望納期は必須項目です。\n");
                }
                if(comment_usr_max_error > 0){
                    error.push("日本ジェネティクス向けコメントは100文字以内で入力してください\n");
                }
                if(comment_spp_max_error){
                    error.push("販売店向けコメントは100文字以内で入力してください\n");
                }
                if(apos_error){
                    error.push("コメント欄や発送先に「'」は入力できません。\n");
                }

                if (nonyu_saki_error.require != undefined) error.push("施設名は入力必須です。\n");
                if (nonyu_saki_error.max_length != undefined) error.push("施設名は100文字以内で入力してください。\n");

                if (nonyu_zip_error.require != undefined) error.push("郵便番号は入力必須です。\n");
                if (nonyu_zip_error.integer != undefined) error.push("郵便番号は数字で入力してください。\n");
                if (nonyu_zip_error.format != undefined) error.push("郵便番号は3桁＋4桁で入力してください。\n");

                if (nonyu_address1_error.require != undefined) error.push("住所は入力必須です。\n");
                if (nonyu_address1_error.max_length != undefined) error.push("住所は100文字以内で入力してください。\n");
                
                if (nonyu_tel_error.require != undefined) error.push("電話番号は入力必須です。\n");
                if (nonyu_tel_error.integer != undefined) error.push("電話番号は数字で入力してください。\n");
                if (nonyu_tel_error.format != undefined) error.push("電話番号を正しく入力してください。\n");
                if(error.length > 0){
                    var err = error.join("");
                    alert(err);
                    return false;
                }
                var postageRes = this.getPostageVal(total);
                if (postageRes) {
                    if(!confirm('このご注文金額は送料が発生します。\nよろしいですか？\n\n送料ご請求先：GeneF@N会員様') ) {
                        return 0;
                    }
                } 
                var tax = Math.floor((total+postageRes) * 0.1);
                // var postCart = confirm('注文用カートの内容で注文します');
                // if (postCart) {
                    //this.noteAll();
                    riot.mount('app', 'mc-order-cart-2', { result: list_cart_update, total:  total, tax: tax, postage: postageRes, postageList: this.genPostageList, cus_use_code_1: opts.cus_use_code_1, cus_use_code_2: opts.cus_use_code_2, list_maker_dealer: opts.list_maker_dealer});
                // }

            } else {
                alert('選択する商品がありません');
            }
        }
        function filterSuryo(arr,cart_seq,suryo){

            var suryo_change = false;
            if(ec31_cart_list.length > 0){
                jQuery.grep(arr, function( n, i ) {
                    if(n.cart_seq == cart_seq){
                        suryo_change = true;
                        ec31_cart_list[i].suryo = suryo;
                    }
                });
                if(!suryo_change){
                    ec31_cart_list.push({cart_seq:cart_seq,suryo:suryo});
                }
            } else {
                ec31_cart_list.push({cart_seq:cart_seq,suryo:suryo});
            }
            suryo_change = false;
        }
        this.suryoChange = function(e) {
            var index = e.target.attributes.tabindex.value;
            var suryo = $('#suryo_'+index).val();
            var suryo_change = false;
            var arr_index = '';
            var cart_seq = $('#cart_seq_' + index).val();
            filterSuryo(ec31_cart_list,cart_seq,suryo);
            var total_price = ($('#tanka_'+index).val() * suryo);
            $('#total_price_'+index).text(total_price.toLocaleString('en')+'円');
            var list_cart = thistag.list_cart;
            var total = 0;
            for(var i = 0; i < list_cart.length; i++) {
                if (!isNaN($('#suryo_' + i).val()) && !isNaN($('#tanka_' + i).val())) {
                    total += $('#suryo_' + i).val() * $('#tanka_' + i).val();
                }
            }

            postage = this.getPostageVal(total);
            var total_convert = parseFloat(total).toLocaleString('en');
            var postage_convert = parseInt(postage).toLocaleString('en');
            var total_postage = parseInt(total) + parseInt(postage);
            var total_postage_convert = parseInt(total_postage).toLocaleString('en');
            var tax = Math.floor((total+postage) * 0.1);
            var tax_convert = parseFloat(tax).toLocaleString('en');
            var total_all_convert = parseFloat(total + postage + tax).toLocaleString('en');
            var postage = this.getPostageVal(total);
            $('#total').text(total_convert + '円');
            $('#postage').text(postage_convert + '円');
            $('#tax').text(tax_convert + '円');
            $('#total_postage').text(total_postage_convert + '円');
            $('#total_all').text(total_all_convert + '円');
        }
        this.changePrefecture = function (e) {
            var index = e.target.value;
            var accessTokenCognito = sessionStorage.getItem('accessTokenCognito');
            var url = URL_API + '/api/cart/getListFollowPrefecture?token=' + accessTokenCognito;
            $.ajax({
                url: url,
                type: 'GET',
                data: {prefecture: index},
            })
            .done(function(rp) {
                $(e.target).parent().parent().parent().parent().next('tr').find('select option').remove();
                var rp = rp.prefecture;
                var html = '';
                html += '<option value="">---</option>';
                for (var i = 0; i < rp.length; i++) {
                    var item = rp[i];
                    html += '<option value="' + item.dealer_co_cd + '" rel ="' + item.dealer_dep_cd + '">' + item.dealer_co_name + ' ' + item.dealer_dep_name + '</option>'
                }
                html += '<option value="other_dealer">リストにない販売店</option>';
                $(e.target).parent().parent().parent().parent().next('tr').find('select').append(html);
                console.log("success");
            })
            .fail(function() {
                console.log("error");
            })
            .always(function() {
                console.log("complete");
            });
            
        }
        
        this.checkInteger = function (value) {
            var re = /^([0-9]*)$/;
            return re.test(value);
        }
        
        this.checkFormatTel = function (nonyu_tel_1, nonyu_tel_2, nonyu_tel_3) {
            var result = false;
            // format xxx-xxx-xxxx, xx-xxxx-xxxx, xxx-xxxx-xxxx
            if ((nonyu_tel_1.length == 3 && nonyu_tel_2.length == 3 && nonyu_tel_3.length == 4)
             || (nonyu_tel_1.length == 2 && nonyu_tel_2.length == 4 && nonyu_tel_3.length == 4)
             || (nonyu_tel_1.length == 3 && nonyu_tel_2.length == 4 && nonyu_tel_3.length == 4)
            ) {
                result = true;
            }
            return result;
        }

        this.updateAddress = function () {
            var list_cart = thistag.list_cart;
            for ( var i = 0; i < list_cart.length; i++) {
                if ( list_cart[i].customer_use_code_3 != undefined && list_cart[i].customer_use_code_3 != thistag.DEALER_ADDRESS ) {
                    var zip = list_cart[i].nonyu_zip.split('-');
                    var zip_1 = zip[0] != undefined ? zip[0] : '';
                    var zip_2 = zip[1] != undefined ? zip[1] : '';
                    var nonyu_tel = list_cart[i].nonyu_tel.split('-');
                    var nonyu_tel_1 = nonyu_tel[0] != undefined ? nonyu_tel[0] : '';
                    var nonyu_tel_2 = nonyu_tel[1] != undefined ? nonyu_tel[1] : '';
                    var nonyu_tel_3 = nonyu_tel[2] != undefined ? nonyu_tel[2] : '';

                    $(".address[index=" + i + "]").find('.nonyu_saki').val(list_cart[i].nonyu_saki);
                    $(".address[index=" + i + "]").find('.nonyu_address1').val(list_cart[i].nonyu_address1);
                    $(".address[index=" + i + "]").find('.nonyu_zip_1').val(zip_1);
                    $(".address[index=" + i + "]").find('.nonyu_zip_2').val(zip_2);
                    $(".address[index=" + i + "]").find('.nonyu_tel_1').val(nonyu_tel_1);
                    $(".address[index=" + i + "]").find('.nonyu_tel_2').val(nonyu_tel_2);
                    $(".address[index=" + i + "]").find('.nonyu_tel_3').val(nonyu_tel_3);

                    if (list_cart[i].customer_use_code_3 == thistag.USER_ADDRESS) {
                        $(".address[index=" + i + "]").find('.nonyu_saki_text').text(list_cart[i].nonyu_saki);
                        $(".address[index=" + i + "]").find('.nonyu_address1_text').text(list_cart[i].nonyu_address1);
                        $(".address[index=" + i + "]").find('.nonyu_zip_1_text').text(zip_1);
                        $(".address[index=" + i + "]").find('.nonyu_zip_2_text').text(zip_2);
                        $(".address[index=" + i + "]").find('.nonyu_tel_1_text').text(nonyu_tel_1);
                        $(".address[index=" + i + "]").find('.nonyu_tel_2_text').text(nonyu_tel_2);
                        $(".address[index=" + i + "]").find('.nonyu_tel_3_text').text(nonyu_tel_3);
                        $(".address[index=" + i + "]").find('span').show();
                        $(".address[index=" + i + "]").find('input, textarea').hide();
                        $(`#nonyu_saki_text_${i}`).val(list_cart[i].nonyu_saki);
                        $(`#nonyu_zip_1_text_${i}`).val(zip_1);
                        $(`#nonyu_zip_2_text_${i}`).val(zip_2);
                        $(`#nonyu_address1_text_${i}`).val(list_cart[i].nonyu_address1);
                        $(`#nonyu_tel_1_text_${i}`).val(nonyu_tel_1);
                        $(`#nonyu_tel_2_text_${i}`).val(nonyu_tel_2);
                        $(`#nonyu_tel_3_text_${i}`).val(nonyu_tel_3);
                    } else {
                        $(".address[index=" + i + "]").find('span:not(.dash)').hide();
                        $(".address[index=" + i + "]").find('input, textarea').show();
                    }
                    $('input:radio[name=customer_use_code_3_' + i + '][value=' + list_cart[i].customer_use_code_3 + ']').prop('checked', true);
                    $(".address[index=" + i + "]").show();
                } else {
                    $(".address[index=" + i + "]").hide();
                    $('input:radio[name=customer_use_code_3_' + i + '][value=' + thistag.DEALER_ADDRESS + ']').prop('checked', true);
                }
            }
        }

        this.getPostageVal = function (total) {
            var postage = 0;
            var poList = this.genPostageList;
            for (var i = 0; i < poList.length; i++) {
                var minVal = poList[i]['cart_total_min'];
                var maxVal = poList[i]['cart_total_max'];
                if (minVal <= total && total <= maxVal) {
                    postage = poList[i]['postage'];
                    break;
                }
            }
            return postage;
        }
    </script>
</mc-order-cart>
