<link rel="stylesheet" href="<?php echo $this->basePath('js/plugins/jquery-multi-select/css/multi-select.css'); ?>">
<script src="<?php echo $this->basePath('js/plugins/jquery-multi-select/jquery.multi-select.js'); ?>"></script>
<script src="<?php echo $this->basePath('js/plugins/jquery-multi-select/jquery.quicksearch.js'); ?>"></script>
<div class="content-wrapper">
    <?php
    $form = $this->form;
    $form->prepare();

    $form->get('deliveryPrice')->setAttributes([
        'placeholder' => $this->translate('配送费用或费用公式')
    ]);
    ?>
    <?php echo $this->form()->openTag($form); ?>
    <section class="content-header">
        <?php echo $this->partial('layout/breadcrumb'); ?>

        <h1 class="pull-right" id="dbshop-menu-top">
            <button type="submit" class="btn btn-primary btn-sm"><i class="fa fa-check"></i> <?php echo $this->translate('保存商家配送设置'); ?></button>
            <?php echo $this->HelpUrl('shopDelivery'); ?>
        </h1>
    </section>

    <section class="content">

        <div class="row">
            <div class="col-md-12">
                <div class="nav-tabs-custom">
                    <ul class="nav nav-tabs">
                        <li class="active"><a href="#tab_1" data-toggle="tab"><?php echo $this->translate('基本设置'); ?></a></li>
                        <li><a href="#tab_2" data-toggle="tab"><?php echo $this->translate('费用设置'); ?></a></li>
                        <li><a href="#tab_3" data-toggle="tab"><?php echo $this->translate('轨迹设置'); ?></a></li>
                    </ul>
                    <div class="tab-content">
                        <?php echo $this->partial('layout/messages'); ?>
                        <div class="tab-pane active" id="tab_1">
                            <div class="box-body">
                                <div class="form-group">
                                    <label class="col-sm-2 control-label"><?php echo $this->translate('名称'); ?></label>
                                    <div class="col-sm-3">
                                        <p class="form-control-static"><?php echo $this->translate('商家配送'); ?></p>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label"><?php echo $this->translate('货到付款'); ?></label>
                                    <div class="col-sm-2">
                                        <?php echo $this->formElement($form->get('cashOnDelivery')); ?>
                                        <?php echo $this->formElementErrors($form->get('cashOnDelivery'), ['class'=>'error-message']); ?>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label"><?php echo $this->translate('配送状态'); ?><span class="must_add_value">*</span></label>
                                    <div class="col-sm-6">
                                        <div class="checkbox">
                                            <label>
                                                <?php echo $this->formElement($form->get('deliveryState')); ?> <?php echo $this->translate('启用'); ?>
                                                <?php echo $this->formElementErrors($form->get('deliveryState'), ['class'=>'error-message']); ?>
                                            </label>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="tab-pane" id="tab_2">
                            <div class="form-group" id="div-delivery-set">
                                <label class="col-sm-2 control-label"><?php echo $this->translate('费用设置方式'); ?><span class="must_add_value">*</span></label>
                                <div class="col-sm-6">
                                    <?php echo $this->formElement($form->get('deliverySet')); ?>
                                    <?php echo $this->formElementErrors($form->get('deliverySet'), ['class'=>'error-message']); ?>
                                </div>
                            </div>
                            <?php if(isset($this->deliveryIndividuationList) && !empty($this->deliveryIndividuationList)) foreach ($this->deliveryIndividuationList as $indivValue) { ?>
                                <div class="form-group delivery-region-set" id="indiv-<?php echo $indivValue->getIndivId(); ?>" style="<?php if($this->shopDelivery['deliverySet'] == 'T') echo 'display: none;' ?>">
                                    <label class="col-sm-2 control-label"><?php echo $this->translate('个性化设置'); ?></label>
                                    <div class="col-sm-6">
                                        <strong><?php echo $this->translate('配送费用（公式）'); ?></strong>：<?php echo $this->escapeHtml($indivValue->getDeliveryPrice()); ?><br>
                                        <strong><?php echo $this->translate('配送地区'); ?></strong>：
                                        <?php $areaNameStr = ''; $area = unserialize($indivValue->getDeliveryArea()); if(is_array($area) && !empty($area)) foreach ($this->areaArray[$indivValue->getIndivId()] as $areaList) {
                                            $areaNameStr .= $areaList->getRegionName() . ',';
                                        } echo rtrim($areaNameStr, ','); ?>
                                        <br>
                                        <button type="button" onclick="editDeliveryArea(<?php echo $indivValue->getIndivId(); ?>);" class="btn btn-small btn-primary"><i class="icon-arrow-up icon-white"></i> <?php echo $this->translate('编辑'); ?></button>&nbsp;&nbsp;
                                        <button type="button" onclick="delDeliveryArea(<?php echo $indivValue->getIndivId(); ?>);" class="btn btn-small btn-danger"><i class="icon-arrow-up icon-white"></i> <?php echo $this->translate('删除'); ?></button>
                                    </div>
                                </div>
                            <?php } ?>
                            <div class="form-group">
                                <label class="col-sm-2 control-label"><?php echo $this->translate('配送费用(公式)'); ?><span class="must_add_value">*</span></label>
                                <div class="col-sm-6">
                                    <?php echo $this->formElement($form->get('deliveryPrice')); ?>
                                    <?php echo $this->formElementErrors($form->get('deliveryPrice'), ['class'=>'error-message']); ?>
                                </div>
                                <a href="https://docs.loongdomsoft.com/dbshop/system/delivery.html#配送费用公式说明" target="_blank" class="btn btn-info btn-sm"><?php echo $this->translate('配送费用公式说明'); ?></a>
                            </div>
                            <div class="form-group delivery-region-set" style="<?php if($this->shopDelivery['deliverySet'] == 'T') echo 'display: none;' ?>">
                                <label class="col-sm-2 control-label"><?php echo $this->translate('选择适用地区'); ?></label>
                                <div class="col-sm-6">
                                    <input type="hidden" name="indivId" id="indivId" value="" />
                                    <select multiple="multiple" class="my-select-class" id="my-select" name="deliveryArea[]">
                                        <?php if($this->regionArray) foreach ($this->regionArray as $regionValue) { ?>
                                            <option value='<?php echo $regionValue->getRegionId(); ?>'><?php echo str_repeat('&nbsp;', substr_count($regionValue->getRegionPath(), ',') * 3).$this->escapeHtml($regionValue->getRegionName()); ?></option>
                                        <?php } ?>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group delivery-region-set" style="<?php if($this->shopDelivery['deliverySet'] == 'T') echo 'display: none;' ?>">
                                <label class="col-sm-2 control-label"></label>
                                <div class="col-sm-6">
                                    <button type="button" class="btn btn-success" id="save-single-area"><?php echo $this->translate('保存个性设置'); ?></button>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label"><?php echo $this->translate('配送公式验证'); ?></label>
                                <div class="col-sm-6">
                                    <p class="form-control-static">
                                        <?php echo $this->translate('当配送费用(公式)中为单纯的数字费用而非公式时，无需进行公式验证处理。'); ?>
                                    </p>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label"><?php echo $this->translate('商品重量'); ?></label>
                                <div class="col-sm-2">
                                    <div class="input-group">
                                        <input type="number" class="form-control" name="testWeight" id="testWeight" value="0"><span class="input-group-addon"><?php echo $this->translate('克(g)'); ?></span>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label"><?php echo $this->translate('订单总价'); ?></label>
                                <div class="col-sm-2">
                                    <input type="text" class="form-control" name="testTotal" id="testTotal" value="">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label"><?php echo $this->translate('最终配送费用'); ?></label>
                                <div class="col-sm-2">
                                    <input type="text" class="form-control" name="testPrice" id="testPrice" readonly value="">
                                </div>
                                <button type="button" id="click-button-check-price" class="btn btn-success"><?php echo $this->translate('点击验证公式'); ?></button>
                            </div>
                        </div>

                        <div class="tab-pane" id="tab_3">
                            <div class="form-group">
                                <label class="col-sm-2 control-label"><?php echo $this->translate('配送轨迹接口'); ?></label>
                                <div class="col-sm-2">
                                    <?php echo $this->formElement($form->get('deliveryApiType')); ?>
                                    <?php echo $this->formElementErrors($form->get('deliveryApiType'), ['class'=>'error-message']); ?>
                                </div>
                            </div>
                            <div class="form-group company-api" <?php if ($form->get('deliveryApiType')->getValue() == 'kuaidi100Free' || $form->get('deliveryApiType')->getValue() == 'longExpressPush') echo 'style="display: none"'; ?>>
                                <label class="col-sm-2 control-label"><?php echo $this->translate('企业编号'); ?></label>
                                <div class="col-sm-4">
                                    <?php echo $this->formElement($form->get('deliveryApiSecret')); ?>
                                    <?php echo $this->formElementErrors($form->get('deliveryApiSecret'), ['class'=>'error-message']); ?>
                                </div>
                            </div>
                            <div class="form-group company-api long-push-api" <?php if ($form->get('deliveryApiType')->getValue() == 'kuaidi100Free') echo 'style="display: none"'; ?>>
                                <label class="col-sm-2 control-label">KEY</label>
                                <div class="col-sm-4">
                                    <?php echo $this->formElement($form->get('deliveryApiKey')); ?>
                                    <?php echo $this->formElementErrors($form->get('deliveryApiKey'), ['class'=>'error-message']); ?>
                                </div>
                            </div>
                            <div class="form-group long-push-api-order-finish" <?php if ($form->get('deliveryApiType')->getValue() != 'longExpressPush') echo 'style="display: none"'; ?>>
                                <label class="col-sm-2 control-label"><?php echo $this->translate('推送订单签收'); ?></label>
                                <div class="col-sm-3">
                                    <div class="checkbox">
                                        <label>
                                            <?php echo $this->formElement($form->get('pushOrderFinish')); ?> <?php echo $this->translate('订单状态变为确认收货'); ?>
                                            <?php echo $this->formElementErrors($form->get('pushOrderFinish'), ['class'=>'error-message']); ?>
                                        </label>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label"><?php echo $this->translate('状态'); ?></label>
                                <div class="col-sm-3">
                                    <div class="checkbox">
                                        <label>
                                            <?php echo $this->formElement($form->get('deliveryApiState')); ?> <?php echo $this->translate('启用'); ?>
                                            <?php echo $this->formElementErrors($form->get('deliveryApiState'), ['class'=>'error-message']); ?>
                                        </label>
                                    </div>
                                </div>
                            </div>
                        </div>

                    </div>
                </div>
            </div>
        </div>

    </section>
    <?php echo $this->form()->closeTag(); ?>
</div>
<script type="text/javascript">
    $('#deliveryApiType').change(function() {
        if($(this).val() == 'kuaidi100Free' || $(this).val() == 'longExpressPush') {
            $('.company-api').hide();
            if ($(this).val() == 'longExpressPush') {
                $('.long-push-api').show();
                $('.long-push-api-order-finish').show();
            }
        } else {
            $('.company-api').show();
            $('.long-push-api-order-finish').hide();
        }
    });
    //配送地区选择
    $('.my-select-class').multiSelect({
        selectableHeader: "<div class='custom-header'><?php echo $this->translate('待选择地区'); ?></div><input type='text' class='search-input' autocomplete='off' placeholder='<?php echo $this->translate('快速检索'); ?>'>",
        selectionHeader: "<div class='custom-header'><?php echo $this->translate('已选择地区'); ?></div><input type='text' class='search-input' autocomplete='off' placeholder='<?php echo $this->translate('快速检索'); ?>'>",
        afterInit: function(ms){
            let that = this,
                $selectableSearch = that.$selectableUl.prev(),
                $selectionSearch = that.$selectionUl.prev(),
                selectableSearchString = '#'+that.$container.attr('id')+' .ms-elem-selectable:not(.ms-selected)',
                selectionSearchString = '#'+that.$container.attr('id')+' .ms-elem-selection.ms-selected';

            that.qs1 = $selectableSearch.quicksearch(selectableSearchString)
                .on('keydown', function(e){
                    if (e.which === 40){
                        that.$selectableUl.focus();
                        return false;
                    }
                });

            that.qs2 = $selectionSearch.quicksearch(selectionSearchString)
                .on('keydown', function(e){
                    if (e.which == 40){
                        that.$selectionUl.focus();
                        return false;
                    }
                });
        },
        afterSelect: function(){
            this.qs1.cache();
            this.qs2.cache();
        },
        afterDeselect: function(){
            this.qs1.cache();
            this.qs2.cache();
        }
    });

    $(':radio').click(function() {
        if($(this).val() == 'G') {
            $('.delivery-region-set').show();
        } else {
            $('.delivery-region-set').hide();
        }
    });

    $('#click-button-check-price').click(function () {
        $.post("<?php echo $this->url('delivery-set', ['action' => 'checkDeliveryPrice']);?>", $("#<?php echo $form->getName(); ?>").serialize(), function (data) {
            $('#testPrice').val(data.testPrice);
        });
    });

    $('#save-single-area').click(function () {
        $.post("<?php echo $this->url('delivery-set', ['action' => 'addSingleArea']);?>", $("#<?php echo $form->getName(); ?>").serialize(),
            function (data) {
                if(data.state == 'areaNull') {
                    shopMessage('<?php echo $this->translate('请选择适用地区!'); ?>');
                    return false;
                } else {
                    let htmlStr = '';
                    htmlStr = htmlStr + '<label class="col-sm-2 control-label"><?php echo $this->translate('个性化设置'); ?>：</label>';
                    htmlStr = htmlStr + '<div class="col-sm-6">';
                    htmlStr = htmlStr + '<strong><?php echo $this->translate('配送费用（公式）'); ?></strong>：' + data.deliveryPrice +'<br>';
                    htmlStr = htmlStr + '<strong><?php echo $this->translate('配送地区'); ?></strong>：' + data.deliveryArea +'<br>';
                    htmlStr = htmlStr + '<button class="btn btn-small btn-primary" onclick="editDeliveryArea('+ data.indivId +');" type="button"><i class="icon-arrow-up icon-white"></i> <?php echo $this->translate('编辑'); ?></button>';
                    htmlStr = htmlStr + '&nbsp;&nbsp;<button class="btn btn-small btn-danger" onclick="delDeliveryArea('+ data.indivId +');" type="button"><i class="icon-arrow-up icon-white"></i> <?php echo $this->translate('删除'); ?></button>';

                    let addHtmlStr = '<div class="form-group delivery-region-set" id="indiv-'+data.indivId+'">' + htmlStr + '</div></div>';

                    if(data.editType == 'true') {
                        $('#indiv-'+data.indivId).html(htmlStr);
                    } else {
                        $('#div-delivery-set').after(addHtmlStr);
                    }

                    //重置地区选择
                    $('#my-select').multiSelect('deselect_all');
                    //个性化地区置空
                    $('#indivId').val('');
                    //配送费用重置
                    $('#deliveryPrice').val(0);
                }
            }, 'json');
    });

    //个性化地区编辑
    function editDeliveryArea(indivId) {
        $.post("<?php echo $this->url('delivery-set',['action'=>'infoSingleArea']); ?>",{indivId:indivId},
            function (data) {
                if(data.state == 'true') {
                    $('#indivId').val(data.indivId);
                    $('#deliveryPrice').val(data.deliveryPrice);
                    $('#my-select').multiSelect('deselect_all');
                    $('#my-select').multiSelect('select', data.areaId);
                }
            },'json');
    }

    //个性化地区删除
    function delDeliveryArea(indivId) {
        $.post("<?php echo $this->url('delivery-set', ['action'=>'delSingleArea']); ?>", {indivId : indivId},
            function(data){
                if(data.state == 'true') {
                    $('#indiv-'+indivId).remove();
                }
            },'json');
    }
</script>