<?php


namespace app\api\controller\user;


use app\admin\model\system\StoreBill;
use app\admin\model\system\StoreExtract;
use app\admin\model\system\SystemStore;
use app\models\user\User;
use app\Request;
use crmeb\services\FormBuilder as Form;
use crmeb\services\JsonService as Json;
use crmeb\services\UtilService;
use crmeb\services\UtilService as Util;
use think\facade\Route as Url;

class UserStoreController
{


    /**
     * 显示资金记录
     */
    public function index(Request $request)
    {
        $user = $request->user();
        if (!$user['user_store_id']) return app('json')->fail('非商家身份');
        $store_info = SystemStore::where('id', $user['user_store_id'])->find();
        if (!$store_info) return app('json')->fail('门店不存在');
        $where['store_id'] = $user['user_store_id'];
        $list = StoreBill::where($where)
            ->field(['title', 'type'])
            ->group('type')
            ->distinct(true)
            ->select()
            ->toArray();
        $balance = SystemStore::where('id', $user['user_store_id'])->value('money');
        return app('json')->successful('ok', compact('list', 'balance', 'store_info'));
    }

    /**
     * 显示资金记录ajax列表
     */
    public function billlist(Request $request)
    {
        $user = $request->user();
        if (!$user['user_store_id']) return app('json')->fail('非商家身份');
        $where = Util::getMore([
            ['start_time', ''],
            ['end_time', ''],
            ['nickname', ''],
            ['limit', 20],
            ['page', 1],
            ['type', ''],
        ], $request);
        $where['store_id'] = $user['user_store_id'];
        return app('json')->successful('ok', StoreBill::getBillList($where));
    }

    /**
     * 添加/修改
     * @param int $id
     */
    public function saveExtract(Request $request)
    {
        $user = $request->user();
        if (!$user['user_store_id']) return app('json')->fail('非商家身份');
        $store_info = SystemStore::where('id', $user['user_store_id'])->find();
        if (!$store_info) return app('json')->fail('门店不存在');
        $uid = User::where('user_store_id', $store_info['id'])->column('uid');
        if (!count($uid)) return app('json')->fail('推荐用户不足100人');
        $count = User::where('spread_uid', 'in', $uid)->count();
        if ($count < 100) return app('json')->fail('推荐用户不足100人');
        $data = UtilService::postMore([
            ['alipay_code', ''],
            ['extract_type', ''],
            ['money', 0],
            ['name', ''],
            ['bankname', ''],
            ['cardnum', ''],
            ['weixin', ''],
        ], $request);
        if ($data['money'] > $store_info['money']) Json::fail('可提余额不足');
        if (!$data['cardnum'] == '')
            if (!preg_match('/^([1-9]{1})(\d{14}|\d{18})$/', $data['cardnum']))
                return app('json')->fail('银行卡号输入有误');
        if (StoreExtract::userExtract($store_info, $data))
            return app('json')->success('申请提现成功!');
        else
            return app('json')->fail(StoreExtract::getErrorInfo('提现失败'));
    }
}