<?php


namespace app\controller\manager;

use Exception;
use think\facade\Config as CConfig;

/**
 * 额外配置
 * Class Config
 * @package app\controller\manager
 */
class Config extends Base
{
    private string $extraPath = '';

    protected function initialize()
    {
        parent::initialize();

        $this->extraPath = config_path().'extra/';
        if (!is_dir($this->extraPath)) {
            if (is_writable(config_path())) {
                mkdir($this->extraPath, 0777, true);
            } else {
                halt('请联系系统管理人员配置文件夹读写权限！请添加'.$this->extraPath.'文件夹的读写权限');
            }
        } elseif (!is_writable($this->extraPath)) {
            halt('请联系系统管理人员配置文件夹读写权限！请添加'.$this->extraPath.'文件夹的读写权限');
        }
    }

    public function other()
    {
        if ($this->request->isPost()) {
            $data = input("post.");
            unset($data['_token']);
            $php = var_export($data, true);
            file_put_contents($this->extraPath.'other.php', '<?php'.PHP_EOL.'return '.$php.';');
            return $this->json();
        } else {
            CConfig::load('extra/other', 'other');
            $this->data['item'] = config('other');
            return $this->view();
        }
    }

    public function wechat()
    {
        if ($this->request->isPost()) {
            $data = input("post.");
            unset($data['_token']);
            $php = var_export($data, true);
            file_put_contents($this->extraPath.'wechat.php', '<?php'.PHP_EOL.'return '.$php.';');
            return $this->json();
        } else {
            CConfig::load('extra/wechat', 'wechat');
            $this->data['item'] = config('wechat');
            return $this->view();
        }
    }

    public function alipay()
    {
        if ($this->request->isPost()) {
            $data = input("post.");
            unset($data['_token']);
            $php = var_export($data, true);
            file_put_contents($this->extraPath.'alipay.php', '<?php'.PHP_EOL.'return '.$php.';');
            return $this->json();
        } else {
            CConfig::load('extra/alipay', 'alipay');
            $this->data['item'] = config('alipay');
            return $this->view();
        }
    }

    /**
     * 小程序配置
     */
    public function miniProgram()
    {
        if ($this->request->isPost()) {
            $data = input("post.");
            unset($data['_token']);
            unset($data['image']);

            // 字符串与数组转换
            $data['poster'] = empty($data['poster'] ?? '') ? [] : explode(',', $data['poster']);
            $footBarList    = array_values($data['footBar'] ?? []);
            foreach ($footBarList as &$item) {
                $icons        = empty($item['icon']) ? [] : explode(',', $item['icon']);
                $item['icon'] = array_filter($icons);
            }
            unset($item);
            $data['footBar'] = $footBarList;
            // 按sort排序
            $recommendList = $data['recommend'] ?? [];
            if (count($recommendList)) {
                $recommendKeys = array_column($recommendList, 'sort');
                array_multisort($recommendKeys, SORT_ASC, $recommendList);
            }
            $data['recommend'] = array_values($recommendList);

            $php = var_export($data, true);
            file_put_contents($this->extraPath.'mini_program.php', '<?php'.PHP_EOL.'return '.$php.';');
            return $this->json();

        } else {
            CConfig::load('extra/mini_program', 'mini_program');
            $data = config('mini_program');
            if ($data) {
                $data['poster'] = implode(',', $data['poster'] ?? []);
                $footBarList    = $data['footBar'] ?? [];
                foreach ($footBarList as &$item) {
                    $item['icon'] = implode(',', $item['icon'] ?? []);
                }
                unset($item);
                $data['footBar'] = $footBarList;
            }
            // 底部默认导航
            $data['footBarIcons'] = [
                ['key' => 'home', 'name' => '首页', 'aux' => '图标大小为 40 * 40； 第1图为默认图，第2图为高亮图', 'multi' => 1],
                ['key' => 'category', 'name' => '分类', 'aux' => '图标大小为 40 * 40； 第1图为默认图，第2图为高亮图', 'multi' => 1],
                ['key' => 'my', 'name' => '我的', 'aux' => '图标大小为 40 * 40； 第1图为默认图，第2图为高亮图', 'multi' => 1],
                ['key' => 'cart', 'name' => '购物车', 'aux' => '图标大小为 120 * 120', 'multi' => 0],
            ];
            $this->data           = array_merge($this->data, $data);
            return $this->view();
        }
    }


    public function __call($name, $args)
    {
        if ($this->request->isPost()) {
            try {
                $data = input("post.");
                $php  = var_export($data, true);
                file_put_contents(config_path().'extra/'.$name.'.php', '<?php'.PHP_EOL.'return '.$php.';');
                return $this->json();
            } catch (Exception $e) {
                return $this->json(4001, $e->getMessage());
            }
        } else {
            CConfig::load('extra/'.$name, $name);
            $this->data['item']   = config($name);
            $this->data['action'] = $name;
            return $this->view('manager/config/'.unCamelize($name, '_'));
        }
    }

    public function notice()
    {
        CConfig::load('extra/notice', 'notice');
        $oldData = config('notice');;
        if ($this->request->isPost()) {
            try {
                $data = input("post.");
                if (!isset($oldData['version']) || empty($oldData['version']) || $data['content'] != $oldData['content']) {
                    $data['version'] = time();
                }
                $php = var_export($data, true);
                file_put_contents(config_path().'extra/notice.php', '<?php'.PHP_EOL.'return '.$php.';');
                return $this->json();
            } catch (Exception $e) {
                return $this->json(4001, $e->getMessage());
            }
        }
        $this->data['item'] = $oldData;
        return $this->view();
    }
}