<?php

namespace app\admin\controller\member;

use app\admin\validate\member\RuleAddedValidate;
use app\admin\validate\member\RulePricingValidate;
use app\controller\Curd;
use app\model\Coupon;
use app\model\GoodsSku;
use app\model\RuleAdded;
use app\model\RuleAddedComponent;
use app\model\RulePricing;
use app\model\SysSerial;
use support\exception\BusinessException;
use support\Request;
use support\Response;

class RuleAddedComponentController extends Curd
{
    public function __construct()
    {
        $this->model = new RuleAddedComponent();
//        $this->validate = true;
//        $this->validateClass = new RuleAddedValidate();
    }

    public function select(Request $request): Response
    {
        [$where, $format, $limit, $field, $order] = $this->selectInput($request);
        $order = $request->get('order', 'desc');
        $field = $field ?? 'rule_added_component_addtimes';
        $query = $this->doSelect($where, $field, $order);
        return $this->doFormat($query, $format, $limit);
    }

    protected function afterQuery($items)
    {
        foreach ($items as &$item) {
            $goods = [];
            $goodsIds = [];
            if (!empty($item->rule_added_component_goods_json)) {
                $goodsJson = json_decode($item->rule_added_component_goods_json, true);
                foreach ($goodsJson as $key => $item2) {
                    $goodsInfo = [];
                    if (isset($item2['goods_id'])) {
                        $goodsInfo['goods_name'] = $item2['goods_name'];
                        $sku = GoodsSku::where('goods_sku_id', $item2['goods_sku_id'])->where('goods_sku_status','ON')->select('goods_sku_id', 'goods_sku_sales_price', 'goods_sku_specs_json')->first();
                        if (!empty($sku->goods_sku_specs_json)) {
                            $specsJson = json_decode($sku->goods_sku_specs_json, true);
                            $skuTitle = '';
                            foreach ($specsJson as $item3) {
                                if (is_array($item3)) {
                                    $item3 = implode(',', $item3);
                                }
                                $skuTitle .= $item3 . '-';
                            }
                            $goodsInfo['sku_title'] = rtrim($skuTitle, '-');
                            $goodsJson[$key]['sku'][] = [
                                'goods_sku_id' => strval($sku->goods_sku_id),
                                'goods_sku_sales_price' => $sku->goods_sku_sales_price,
                                'goods_sku_title' => $goodsInfo['sku_title']
                            ];
                        }
                    }
                    if (isset($item2['coupon_id'])) {
                        $coupon = Coupon::where('coupon_id', $item2['coupon_id'])->select('coupon_name')->first();
                        $goods[] = ['goods_name' => $coupon->coupon_name];
                    }
                    if (!empty($goodsInfo)) {
                        $goods[] = $goodsInfo;
                    }


                    if (isset($item2['goods_id'])) {
                        $goodsIds[] = $item2['goods_id'];
                    } elseif (isset($item2['coupon_id'])) {
                        $goodsIds[] = $item2['coupon_id'];
                    }
                }
                $item->rule_added_component_goods_json = $goodsJson;
            }
            // $item->rule_added_component_json = [];
            if (!empty($item->rule_added_component_json)){
                $item->rule_added_component_json = json_decode($item->rule_added_component_json);
            }else{
                $item->rule_added_component_json = [];
            }
            $item->rule_added_component_goods_nbr = intval($item->rule_added_component_goods_nbr);
            $item->goodsIds = $goodsIds;
            $item->goods = $goods;
        }

        return $items;
    }


//    protected function updateInput(Request $request): array
//    {
//        $primary_key = $this->model->getKeyName();
//        $id = $request->post($primary_key);
//        $data = $this->inputFilter($request->post());
//        if (!empty($data['rule_pricing_goods_json'])) {
//            $data['rule_pricing_goods_json'] = json_encode(explode(',', $data['rule_pricing_goods_json']));
//        } else {
//            $data['rule_pricing_goods_json'] = '[]';
//        }
//        $model = $this->model->find($id);
//        if (!$model) {
//            throw new BusinessException('记录不存在', 2);
//        }
//        unset($data[$primary_key]);
//        return [$id, $data];
//    }

}