<?php
defined('BASEPATH') or exit('No direct script access allowed');

/**
 * 配置相关
 */
class NewShop extends MY_Controller
{
    /**
     * @var array Post 数据
     */
    private $postData;

    private $gTypeMap;

    private $imgName;

    private $gType;

    public function __construct()
    {
        parent::__construct();

        $this->postData = $this->input->post();

        $this->gTypeMap = oo::commonOprModel('NewShop')->getGTypeMap();

        $this->imgName = $this->postData['img_name'] ?? 'firstPay';

        $this->gType = $this->gTypeMap[$this->imgName];
    }

    //region 首充礼包

    /**
     * 配置列表
     */
    public function firstPay()
    {
        $this->imgName = __FUNCTION__;
        $this->gType = $this->gTypeMap[$this->imgName];

        $this->shopList();
    }

    /**
     * 配置新增
     */
    public function firstPayGoodsAdd()
    {
        oo::commonOprModel('NewShop')->insertFirstPayGoods($this->postData, $this->gType);

        return $this->response(0, '操作成功');
    }

    /**
     * 配置更新
     */
    public function firstPayGoodsUpdate()
    {
        oo::commonOprModel('NewShop')->updateFirstPayGoods($this->postData);

        return $this->response(0, '操作成功');
    }

    //endregion

    //region 冲刺礼包

    /**
     * 配置列表
     */
    public function sprintPay()
    {
        $this->imgName = __FUNCTION__;
        $this->gType = $this->gTypeMap[$this->imgName];

        $this->shopList();
    }

    /**
     * 商品配置更新
     */
    public function sprintPayGoodsUpdate()
    {
        oo::commonOprModel('NewShop')->updateSprintPayGoods($this->postData);

        return $this->response(0, '操作成功');
    }

    /**
     * 通用配置更新
     */
    public function sprintPayCommonConfUpdate()
    {
        oo::commonOprModel('NewShop')->updateGoodsCommonConf($this->postData);

        return $this->response(0, '操作成功');
    }

    /**
     * 冲刺礼包配置文件上传
     */
    public function importSprintPayGoodsConf()
    {
        $this->imgName = 'sprintPay';

        $this->gType = $this->gTypeMap[$this->imgName];

        $this->importConfigExcelFile();
    }


    //endregion

    //region 体力不足礼包

    /**
     * 配置列表
     */
    public function unfitnessPay()
    {
        $this->imgName = __FUNCTION__;
        $this->gType = $this->gTypeMap[$this->imgName];

        $this->shopList();
    }

    /**
     * 配置更新
     */
    public function unfitnessPayGoodsUpdate()
    {
        oo::commonOprModel('NewShop')->updateUnfitnessPayGoods($this->postData, $this->imgName);

        return $this->response(0, '操作成功');
    }

    /**
     * 弹窗配置
     */
    public function unfitnessPopConf()
    {
        $config = oo::commonOprModel('config')->getCon("unfitness", "popWindowsConfig") ?? '[]';
        $data['list'] = json_decode($config, true);

        // 活动列表
        $data['activityList'] = json_encode(UNFITNESS_POP_ACT_LIST);

        // 数数分组
        $data['ssGroupList'] = array_column(oo::commonOprModel('config')->getUserGroupConfigByCid(5), 'name', 'id');

        $this->load->view("newShop/unfitnessPopConf", $data);
    }

    // 弹窗配置更新
    public function unfitnessPopConfigUpdate()
    {
        $data = $this->input->post();

        oo::commonOprModel('config')->updateCon("unfitness", "popWindowsConfig", json_encode($data['conf'], JSON_NUMERIC_CHECK), '体力不足弹窗配置');

        $filename = 'UnfitnessPopConfigV2' . time() . '.json';

        oo::commonOprModel("json")->SaveJson($data, $filename);
        oo::commonOprModel("json")->setStaticConfig("UnfitnessPopConfigV2", $filename);

        return $this->response(0, '操作成功');
    }

    /**
     * 体力不足礼包配置文件上传
     */
    public function importUnfitnessPayGoodsConf()
    {
        $this->imgName = oo::commonOprModel('NewShop')::IMG_NAME_UNFITNESS_PAY;

        $this->gType = $this->gTypeMap[$this->imgName];

        $this->importConfigExcelFile();
    }

    //endregion

    //region 升级礼包

    /**
     * 配置列表
     */
    public function upgradePay()
    {
        $this->imgName = __FUNCTION__;
        $this->gType = $this->gTypeMap[$this->imgName];

        $this->shopList();
    }

    /**
     * 商品配置更新
     */
    public function upgradePayGoodsUpdate()
    {
        oo::commonOprModel('NewShop')->updateUpgradePayGoods($this->postData);

        return $this->response(0, '操作成功');
    }

    /**
     * 通用配置更新
     */
    public function upgradePayCommonConfUpdate()
    {
        oo::commonOprModel('NewShop')->updateGoodsCommonConf($this->postData);

        return $this->response(0, '操作成功');
    }

    /**
     * 冲刺礼包配置文件上传
     */
    public function importUpgradePayGoodsConf()
    {
        $this->imgName = oo::commonOprModel('NewShop')::IMG_NAME_UPGRADE_PAY;

        $this->gType = $this->gTypeMap[$this->imgName];

        $this->importConfigExcelFile();
    }

    //endregion

    /**
     * 礼包列表通用展示
     */
    private function shopList()
    {
        $data = [];

        //礼包资源
        $data['imgSkinList'] = oo::commonOprModel('NewShop')->getImgSkinList($this->imgName);;

        //价格sku
        $data['skuList'] = oo::cfg('sku');

        //上下架
        $data['statusList'] = [0 => '下架', 1 => '上架'];

        //数数分组
        $data['ssGroupList'] = array_column(oo::commonOprModel('config')->getUserGroupConfigByCid(5), 'name', 'id');

        //读新商品配置表
        $data["shopList"] = oo::commonOprModel('NewShop')->showGoods($this->gType);

        //读取礼包通用配置
        $data["commonConf"] = oo::commonOprModel('NewShop')->showCommonConf($this->imgName);

        $this->load->view("newShop/$this->imgName", $data);
    }

    /**
     * 配置文件上传
     */
    private function importConfigExcelFile()
    {
        $this->load->library('uploader', [
            'savePath' => 'Excel/uploads',
            'maxSize' => 1024 * 50,
            'allowFiles' => array('.xls', '.xlsx', '.json', '.png', '.jpg')]);

        $fileContent = $this->uploader->getFileInfo();

        $response = oo::commonOprModel('NewShop')->importGoodsConfig($fileContent, $this->imgName);
        if ($response[0] === -1) {
            return $this->response($response[0], $response[1]);
        }

        return $this->response(0, '操作成功');
    }
}
