<?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\Goods;
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 RuleAddedController extends Curd
{
    public function __construct()
    {
        $this->model = new RuleAdded();
        $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_addtimes';
        $query = $this->doSelect($where, $field, $order);
        return $this->doFormat($query, $format, $limit);
    }

    public function selectList(Request $request)
    {
        $data = $this->model->where('rule_added_status', 'ACTIVED')
            ->select('rule_added_id', 'rule_added_name')
            ->get()
            ->toArray();

        return json_success('', $data);
    }

    public function info(Request $request): Response
    {
        $id = $request->get('rule_added_id');
        $added = RuleAdded::find($id);
        if (!$added) {
            return json_fail('数据不存在');
        }

        $components = RuleAddedComponent::where('join_component_rule_added_id', $id)
            ->get()
            ->toArray();

        foreach ($components as &$component) {
            $goods = [];
            if (!empty($component['rule_added_component_goods_json'])) {
                $goodsJson = json_decode($component['rule_added_component_goods_json'], true);

                foreach ($goodsJson as $item) {
                    $goodsInfo = [];
                    if (isset($item['goods_id'])) {
                        $goodsInfo['goods_name'] = $item['goods_name'];
//                        $goods = Goods::where('goods_id',$item['goods_id'])->select('goods_name');
                        $sku = GoodsSku::where('goods_sku_id', $item['goods_sku_id'])->where('goods_sku_status','ON')->select('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 $item) {
                                if (is_array($item)) {
                                    $item = implode(',', $item);
                                }
                                $skuTitle .= $item . '-';
                            }
                            $goodsInfo['sku_title'] = rtrim($skuTitle, '-');
                        }
                    }
                    if (isset($item['coupon_id'])){
                        $coupon = Coupon::where('coupon_id',$item['coupon_id'])->select('coupon_name')->first();
                        $goods[] = ['goods_name'=>$coupon->coupon_name];
                    }
                    if (!empty($goodsInfo)) {
                        $goods[] = $goodsInfo;
                    }
                }
            }
            $component['goods'] = $goods;
            $component['rule_added_component_goods_nbr'] = intval($component['rule_added_component_goods_nbr']);
        }
        return json_success('', $components);
    }

    protected function insertInput(Request $request): array
    {
        $data = $this->inputFilter($request->post());
        $data['rule_added_id'] = "RA" . date("ymdHi") . random_string(4, 'up');

        return $data;
    }

    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];
    }

}