<?php
declare (strict_types = 1);

namespace app\admin\controller\shop;

use app\common\controller\Backend;
use think\annotation\route\Group;
use think\annotation\route\Route;
use app\admin\traits\Actions;
use app\common\model\Customer;
use app\common\model\User;
use app\common\model\StockConfig;
use app\common\model\CustomerSpec;
use app\api\service\SpecService;
use app\common\model\ShopList as ShopListModel;

#[Group("shop/shop_list")]
class ShopList extends Backend
{
    use Actions{
        add as protected _add;
        edit as protected _edit;
    }

    protected function _initialize()
    {
        parent::_initialize();
        $this->model = new ShopListModel();

        $this->assign('customerList', Customer::where('status', 1)->column('name','id'));
        $this->assign('platformList', site_config('addonsd.platform_list'));
        $this->assign('userList', User::where('status',Customer::STATUS_NORMAL)->column('nickname','id'));
        $this->assign('fieldList', StockConfig::where('type_id', 'variety_name')->order('sort desc')->column('title','id'));

        $this->assign('typeList',  StockConfig::where('type_id', 'variety_name')->order('sort desc')->column('title','id'));
        $this->assign('packingList', StockConfig::where('type_id', 'packing_box')->column('title','id'));
        $this->relationField=['customer','staff'];
    }   

 
    #[Route('GET,POST','specs')]
    public function specs()
    {
        if($this->request->isAjax()){
            $ids = $this->request->post('ids/d', 0);
            $params = $this->request->post('');
            if(empty($ids)) return resp_json(0, '请选择店铺规格');
            $this->model->where('id', $ids)->save([
                'type_spec'  => isset($params['all_data'])?json_encode($params['all_data'], JSON_UNESCAPED_UNICODE):null,
            ]);
            return resp_json(200,'操作成功');
        }else{
          
            $ids =$this->request->get('ids/d', 0);
            $rows = $this->model::where('id', $ids)->where('status', 1)->find();
            $this->assign('ids', $ids);
            $this->assign('row', $rows->type_spec??'');
            return $this->fetch();
        }
    }

  
    #[Route('GET','get_box')]
    public function get_box()
    {
        if($this->request->isAjax()){
            $ids = $this->request->get('ids/d', 0);
            $type_id = $this->request->get('type_id/d', 0);
            if(empty($ids) || empty($type_id)) return resp_json(0, '参数错误');
            $customer_id = $this->model::where('id', $ids)->value('customer_id');
            $rows = CustomerSpec::alias('a')
                ->leftjoin('product_config b', 'a.product_id=b.id') //规格
                ->leftjoin('stock_config c', 'a.type_id=c.id') //品种
                ->leftjoin('stock_config d', 'a.box_id=d.id') //包装箱
                ->field('a.*,b.title,c.title as type_name,d.title as box_name')
                ->field('a.*,b.title')
                ->where('a.type_id', $type_id)
                ->where('a.customer_id', $customer_id)
                ->where('a.status', CustomerSpec::NORMAL)
                ->order('a.sort desc')->select();
            return json($rows, 200);
        }
    }
 
}
