<?php
namespace PortalPeople\qvc\models\products;
use yii;
use yii\helpers\Json;

class Qvc extends \common\models\DynamicModel
{

    public static function segment_title()
    {
        return 'QVC Attributes';
    }
    /**
     * Undocumented function
     *
     *    'colour' => [
     *        'rules' => [
     *            ['string']
     *        ],
     *        'label' => Yii::t('app', "Colour",),
     *        'input' => 'default',
     *     ],
     * @return void
     */
    public static function localattributes()
    {
        return [
            'id' => [
                'rules' => [
                    ['integer']
                ],
                'label' => Yii::t('app', "ID",),
                'input' => 'default',
            ],
            'name' => [
                'rules' => [
                    ['string']
                ],
                'label' => Yii::t('app', "Name",),
                'input' => 'default',
            ],
            'qty' => [
                'rules' => [
                    ['integer']
                ],
                'label' => Yii::t('app', "QTY",),
                'input' => 'default',
            ],
            'haulierid' => [
                'rules' => [
                    ['safe']
                ],
                'label' => Yii::t('app', "Haulier ID",),
                'input' => [
                    'input' => 'checkboxList',
                    'fieldclass' => null,
                    'fieldlist' => \backend\models\Accounts::intids('haulier'),
                    'options' => ['label' => false, 'prompt' => 'Please Select'],
                    'width' => 'col-md-12',
                ],
            ],
        ];
    }


    public function dataLoad($modelData) {
        $ext_data = !empty($modelData->data) && !is_array($modelData->data) ? json_decode($modelData->data, true) : [];
        $data = $ext_data['qvc'] ?? null;
        
        // Populate from stored ext_data first
        if ($data !== null) {
            $this->load($data, '');
        }
        
        // If we have an id, build the erp_link and fetch live data from NetSuite
        if (!empty($this->id)) {

        }
        $decoded = is_array($modelData->data) ? $modelData->data : (json_decode($modelData->data, true) ?? []);
        $decoded['qvc'] = $this;
        $modelData->data = $decoded;
    }
}

