<?php

use yii\helpers\Html;
use yii\widgets\DetailView;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use backend\models\{SysCountry, Dynamiccss, Attributesvalues};
use kartik\select2\Select2;

/**
 * @var yii\web\View $this
 * @var PortalPeople\shippingcosts\models\SysShippingSetting $model
 * @var yii\widgets\ActiveForm $form
 */
$productTypes = Attributesvalues::formarraytt('product_types');
$algos = \common\components\ClassifyProductsSettings::setTotallingAlgo();
$shipping_group = \backend\models\Shipgroup::find()->where(['status' => 'Active'])->asArray()->all();
$stores = Dynamiccss::getStores();
$values = [
    [   'id' => '1', 'name' => 'Yes' ],
    [   'id' => '0', 'name' => 'No'],
];

$applicable_countries = [
    [   'id' => '1', 'name' => 'All Countries' ],
    [   'id' => '2', 'name' => 'Selected Countries'],
];

$country = SysCountry::find()->where(['status' => "Active"])->asArray()->all();

$handling_fee = [
    [   'id' => '1', 'name' => 'Fixed Price' ],
    [   'id' => '0', 'name' => '%'],
];

$model->countries = $model->countries ? explode(",", $model->countries) : '' ;

if ($model->errors) {
    Yii::warning($model->errors);
}
?>
<style type="text/css">
    .help-block{
        color:#ff8d00;
    }
</style>

<div class="sys-shipping-setting-form">


    <?php $form = ActiveForm::begin(); ?>

    <div class="row">
        <div class="sidebar col-200 col-12 pr-lg-0">
            <ul class="nav nav-pills nav-stacked">
                <li class="nav-item">
                    <?= Html::submitButton('Save', ['class' => 'btn btn-submit nav-link save']) ?>
                </li>
                
                <li class="nav-item">
                    <?php
                    if (Yii::$app->controller->action->id == "update") :
                        echo Html::a('View', ['view', 'id' => $model->id, 'language' => $model->language], ['class' => 'btn btn-success view nav-link']);
                    endif; ?>
                </li>
            </ul>
        </div>

        <div class="col main">
            <div class="card">
                <div class="card-header"><?= Html::encode($this->title) ?></div>
                <div class="card-body">

                    <div class="row">
                        <div class="col-6">
                            <?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>

                            <?= $form->field($model, 'centerid')->dropDownList(ArrayHelper::map($stores, 'id', 'storeid'), ['prompt' => 'Please Select'])->label("Store");?>

                            <?= $form->field($model, 'max_shipping_cost')->textInput(['maxlength' => true]) ?>

                            <?= $form->field($model, 'shipping_group')->dropDownList(ArrayHelper::map($values, 'id', 'name'), ['prompt' => 'Please Select', 'readonly' => true]);?>

                            <?= $form->field($model, 'error_message')->textInput(['maxlength' => true]) ?>

                        </div>

                        <div class="col-6">
                            <?= $form->field($model, 'enabled')->dropDownList(ArrayHelper::map($values, 'id', 'name'), ['prompt' => 'Please Select']);?>

                            <?= $form->field($model, 'package_id')->dropDownList(ArrayHelper::map($values, 'id', 'name'), ['prompt' => 'Please Select', 'readonly' => true])->label("Apply package ID '*' to all items in shipping Group");?>

                            <?= $form->field($model, 'totaling_algo')->dropDownList(ArrayHelper::map($algos, 'name', 'display_name'), ['prompt' => 'Please Select', 'readonly' => true])->label("Totaling Algorithm");?>

                            <?= $form->field($model, 'exclude_fornt_price')->checkbox(['label' => false, 'unchecked' => 0])->label('Exclude Virtual from Cart Price');?>
                        </div>
                    </div>

                    <hr>

                    <div class="row">
                        <div class="col-6">
                            <?= $form->field($model, 'show_ship_status')->dropDownList(ArrayHelper::map($values, 'id', 'name'), ['prompt' => 'Please Select']);?>
                        </div>

                        <div class="col-6" id="default_shipping_status">
                            <?= $form->field($model, 'default_ship_status')->dropDownList(\backend\models\Attributesvalues::formArray('shipping_status'), ['prompt' => 'Please Select']);?>
                        </div>
                    </div>

                    <hr>

                    <div class="row">
                        <div class="col-6">

                            <?= $form->field($model, 'post_code_range')->dropDownList(ArrayHelper::map($values, 'id', 'name'), ['prompt' => 'Please Select', 'readonly' => true])->label("Use Post Code To / From Range");?>

                         </div>
                        <div class="col-6">

                            <?= $form->field($model, 'uk_post_code')->dropDownList(ArrayHelper::map($values, 'id', 'name'), ['prompt' => 'Please Select', 'readonly' => true])->label("UK Postcode Flitering");?>

                         </div>
                    </div>

                    <hr>

                    <div class="row">
                        <div class="col-6">
                            <?= $form->field($model, 'applicable_countries')->dropDownList(ArrayHelper::map($applicable_countries, 'id', 'name'), ['prompt' => 'Please Select', 'readonly' => true]);?>
                         </div>
                        <div class="col-6">
                            <div class="form-group field-sysshippingsetting-countries">
                            <div id="countries_dropdown" style="display:none;">
                                <label class="control-label">Countries</label>
                                    <?php
                                    echo Select2::widget([
                                    'model' => $model,
                                    'attribute' => 'countries',
                                    'data' => ArrayHelper::map($country, 'alpha_3', 'name'),
                                    'options' => [
                                        'id' => 'sysshippingsetting-countries',
                                        'placeholder' => Yii::t('app', 'Countries'),
                                        'class' => 'styled',
                                        'multiple' => true,
                                        'disabled' => true
                                    ],

                                    'pluginOptions' => [
                                        'allowClear' => true
                                    ],
                                    ]);
                                    ?>
                            </div>
                            </div>

                            
                         </div>
                    </div>

                    <hr>

                    <div class="row">
                        <div class="col-6">                        
                            <?= $form->field($model, 'apply_handling_fee')->dropDownList(ArrayHelper::map($values, 'id', 'name'), ['prompt' => 'Please Select', 'readonly' => true])->label("Apply Handling Fee on zero shipping");?>

                            <div class="handling_fee_box" style="display: none;">

                                <?= $form->field($model, 'handling_fee')->textInput(['maxlength' => true, 'readonly' => true]) ?>
                            </div>

                         </div>
                        <div class="col-6">

                            <div class="handling_fee_box" style="display: none;">

                                <?= $form->field($model, 'cal_handling_fee')->dropDownList(ArrayHelper::map($handling_fee, 'id', 'name'), ['prompt' => 'Please Select', 'readonly' => true])->label("Calculate Handling Fee");?>
                            </div>
                            
                         </div>
                    </div>

                    <hr>

                    <div class="row">
                        <div class="col-6">

                             <?= $form->field($model, 'zero_cost_shipping')->textInput(['maxlength' => true, 'readonly' => true]) ?>

                             <?= $form->field($model, 'discount_price')->dropDownList(ArrayHelper::map($values, 'id', 'name'), ['prompt' => 'Please Select', 'readonly' => true])->label("Use Discounted Price");?>
                        </div>
                        <div class="col-6">

                            <?= $form->field($model, 'shipping_promotion')->textInput(['maxlength' => true, 'readonly' => true]) ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <?php ActiveForm::end(); ?>
</div>
<script type="text/javascript">
    checkCounties();
    function checkCounties() {
        let applicable_countries = $("#sysshippingsetting-applicable_countries").val();
        if (applicable_countries == 2) {
            $("#countries_dropdown").show();
        }else{
            $("#countries_dropdown").hide();
        }
    }

    $("#sysshippingsetting-applicable_countries").change(function() {
        checkCounties();
    });

    checkShipStatus();
    function checkShipStatus() {
        let show_ship_status = $("#sysshippingsetting-show_ship_status").val();        
        if (show_ship_status === '1') {
            $("#default_shipping_status").show();
        }else{
            $("#default_shipping_status").hide();
        }
    }

    $("#sysshippingsetting-show_ship_status").change(function() {
        checkShipStatus();
    });

    checkHandlingFee();

    function checkHandlingFee() {
        let apply_handling_fee = $("#sysshippingsetting-apply_handling_fee").val();

        if (apply_handling_fee == '1') {
            console.log(apply_handling_fee);
            $(".handling_fee_box").show();
        }else{
            $("#sysshippingsetting-cal_handling_fee").val('');
            $("#sysshippingsetting-handling_fee").val('');
            $(".handling_fee_box").hide();
        }
    }

    $("#sysshippingsetting-apply_handling_fee").change(function() {
        checkHandlingFee();
    });
    
</script>