<?php
// +----------------------------------------------------------------------
// | Niucloud-admin 企业快速开发的saas管理平台
// +----------------------------------------------------------------------
// | 官方网址:https://www.niucloud.com
// +----------------------------------------------------------------------
// | niucloud团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: Niucloud Team
// +----------------------------------------------------------------------

namespace app\adminapi\controller\shop\admin;

use app\dict\site\SiteDict;
use app\job\schedule\SiteExpireClose;
use app\service\admin\shop\admin\ShopService;
use core\base\BaseAdminController;
use think\Response;

class Shop extends BaseAdminController
{
    /**
     * 店铺列表
     * @return Response
     */
    public function pages()
    {
        $data = $this->request->params([
            [ 'keyword', '' ],
            [ 'status', '' ],
            [ 'group_id', '' ],
            [ 'create_time', [] ],
            [ 'expire_time', [] ],
            [ 'is_self', '' ],
            [ 'category_id', '' ],
        ]);
        return success(( new ShopService() )->getPage($data));
    }

    /**
     * 店铺列表
     * @return Response
     */
    public function lists()
    {
        $data = $this->request->params([
            [ 'keywords', '' ],
            [ 'status', '' ],
            [ 'group_id', '' ],
            [ 'create_time', [] ],
            [ 'expire_time', [] ],
            [ 'is_self', '' ],
            [ 'category_id', '' ],
        ]);
        return success(( new ShopService() )->getList($data));
    }

    /**
     * 店铺详情
     * @param int $id
     * @return Response
     */
    public function info(int $id)
    {
        return success(( new ShopService() )->getInfo($id));
    }

    /**
     * 添加店铺
     * ['site_name' => '', 'username' => '', 'head_img' => '', 'real_name' => '', 'password' => '', 'expire_time' => 0]
     * @return Response
     */
    public function add()
    {
        $data = $this->request->params([
            [ 'site_name', '' ],
            [ 'uid', 0 ],
            [ 'username', '' ],
            [ 'real_name', '' ],
            [ 'password', '' ],
            [ 'group_id', 0 ],
            [ 'expire_time', 0 ],
            [ 'is_self', '' ],
            [ 'category_id', '' ],
            [ 'phone', '' ],
            [ 'apply_id', '' ],
            [ 'business_license', '' ],
        ]);
        $this->validate($data, 'app\validate\site\Site.add');
        if (empty($data[ 'uid' ])) $this->validate($data, 'app\validate\sys\User.add');
        $site_id = ( new ShopService() )->add($data);
        return success('ADD_SUCCESS', [ 'site_id' => $site_id ]);
    }

    /**
     * 编辑店铺
     */
    public function edit($id)
    {
        $data = $this->request->params([
            ['site_name', ''],
            ['group_id', 0],
            ['expire_time', 0],
            ['is_self', ''],
            ['category_id', ''],
            ['phone', ''],
            ['business_license', ''],
            ['latitude', ''],
            ['longitude', ''],
            ['province_id', ''],
            ['city_id', ''],
            ['district_id', ''],
            ['address', ''],
            ['full_address', ''],
        ]);
        $this->validate(array_merge($data, ['site_id' => $id]), 'app\validate\site\Site.edit');
        ( new ShopService() )->edit($id, $data);
        return success('MODIFY_SUCCESS');
    }

    /**
     * 开启店铺
     */
    public function openShop($id)
    {
        ( new ShopService() )->openShop($id);
        return success();
    }

    /**
     * 关闭店铺
     */
    public function closeShop($id)
    {
        ( new ShopService() )->closeShop($id);
        return success();
    }

    /**
     * 删除店铺
     * @param $id
     * @return Response
     */
    public function del($id)
    {
        ( new ShopService() )->del($id);
        return success('DELETE_SUCCESS');
    }

    /**
     * 店铺账户信息修改
     * @param int $id
     * @return Response
     */
    public function editShopAccount(int $id)
    {
        $data = $this->request->params([
            [ 'bank_type', '' ],
            [ 'bank_account_name', '' ],
            [ 'bank_account_no', 0 ],
            [ 'bank_name', '' ],
            [ 'bank_address', '' ]
        ]);
        $this->validate($data, 'app\validate\shop\ShopAccount.edit');
        ( new ShopService() )->editShopAccount($id, $data);
        return success('EDIT_SUCCESS');
    }

    /**
     * 店铺选择列表
     * @return Response
     */
    public function select()
    {
        $data = $this->request->params([
            [ 'keyword', '' ],
            [ 'status', '' ],
            [ 'group_id', '' ],
            [ 'create_time', [] ],
            [ 'expire_time', [] ],
            [ 'is_self', '' ],
            [ 'category_id', '' ],
            [ "site_ids", [] ],
            [ "verify_site_ids", [] ]
        ]);
        return success(( new ShopService() )->getSelectPage($data));
    }

    /**
     * 获取店铺拥有的应用
     * @return Response
     */
    public function addons()
    {
        $data = $this->request->params([
            [ 'title', '' ],
        ]);
        $data = ( new ShopService() )->getSiteAddons($data);
        return success(data:$data);
    }

    /**
     * 获取店铺收支信息
     * @param int $id
     * @return Response
     */
    public function getAccountLogList(int $id)
    {
        return success(( new ShopService() )->getShopAccountLog($id));
    }

    /**
     * 获取店铺提现记录
     * @param int $id
     * @return Response
     */
    public function getShopCashOutList(int $id)
    {
        return success(( new ShopService() )->getShopCashOutlog($id));
    }

    /**
     * 获取店铺账户信息
     * @param int $id
     * @return Response
     */
    public function getShopAccountInfo(int $id)
    {
        return success(( new ShopService() )->getShopAccountInfo($id));
    }

    /**
     * 店铺状态
     * @return Response
     */
    public function getStatusList()
    {
        return success(SiteDict::getStatus());
    }

}