<?php

namespace app\adminapi\controller\v1\marketing;

use app\adminapi\controller\AuthController;
use crmeb\traits\CurdControllerTrait;
use crmeb\services\UtilService as Util;
use app\models\store\{StoreRechargeCard as StoreRechargeCardModel, StoreRechargeCardBrokerage};
use app\models\user\{UserRechargeCard as UserRechargeCardModel, UserBrokerage as UserBrokerageModel};

/**
 * 充值卡管理
 * Class StoreRechargeCard
 * @package app\admin\controller\store
 */
class StoreRechargeCard extends AuthController
{

	use CurdControllerTrait;

	/**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $where = Util::getMore([
            ['page', 1],
            ['limit', 20],
            ['is_brokerage', ''],
            ['status', ''],
            ['card_name', '']
        ]);
        $where['mer_id'] = $this->merId ?: '';
        $list = StoreRechargeCardModel::systemPage($where);
        return $this->success($list);
    }

    /**
     * 详情
     * @param $id
     * @return mixed
     */
    public function read($id)
    {
        $info = StoreRechargeCardModel::getOne($id);
        return $this->success(compact('info'));
    }

    /**
     * 保存新建的资源
     * @param int $id
     */
    public function save($id = 0)
    {
        $data = Util::postMore([
            [['title', 's'], ''],
            ['poster', []],
            [['explain', 's'], ''],
            [['is_brokerage', 'd'], 0],
            ['brokerage', []],
            [['price', 'f'], 0],
            [['give_money', 'f'], 0],
            [['status', 'd'], 0],
            [['sort', 'd'], 0],
            ['section_time', []],
            [['poundage', 'f'], 3]
        ]);
        $this->validate($data, \app\adminapi\validates\marketing\StoreRechargeCardValidate::class, 'save');
        if (count($data['section_time']) != 2) return $this->fail('请选择正确的时间区间');
        $data['start_time'] = strtotime($data['section_time'][0]) ?: 0;
        $data['end_time'] = strtotime($data['section_time'][1]) ?: 0;
        if (!$data['start_time'] && $data['end_time']) return $this->fail('请选择正确的开始时间');
        if ($data['start_time'] && !$data['end_time']) return $this->fail('请选择正确的结束时间');
        $data['poster'] = json_encode($data['poster']);
        $data['mer_id'] = $this->merId ?: '';
        $brokerage = $data['brokerage'];
        unset($data['brokerage'], $data['section_time']);
        if ($id) {
            $RechargeCard = StoreRechargeCardModel::get($id);
            if (!$RechargeCard) return $this->fail('数据不存在!');
            StoreRechargeCardModel::edit($data, $id);
            foreach($brokerage as $key => $value){
                $brokerage[$key]['cid'] = !empty($value['cid']) ? $value['cid'] : $id;
            }
            if($data['is_brokerage'] == 1 && !empty($brokerage)){
                StoreRechargeCardBrokerage::saveRechargeCardBrokerage($brokerage, $id);
            }
            return $this->success('编辑充值卡成功!');
        } else {
            $rechargeCard = StoreRechargeCardModel::create($data);
            foreach($brokerage as $key => $value){
                $brokerage[$key]['cid'] = (int)($rechargeCard['id']);
            }
            if($data['is_brokerage'] == 1 && !empty($brokerage)){
                StoreRechargeCardBrokerage::saveRechargeCardBrokerage($brokerage, $rechargeCard['id']);
            }
            return $this->success('添加充值卡成功!');
        }
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        if (!$id) return $this->fail('数据不存在');
        $rechargeCard = StoreRechargeCardModel::get($id);
        if (!$rechargeCard) return $this->fail('数据不存在!');
        if ($rechargeCard['is_del']) return $this->fail('已删除!');
        $data['is_del'] = 1;
        if (!StoreRechargeCardModel::edit($data, $id))
            return $this->fail(StoreRechargeCardModel::getErrorInfo('删除失败,请稍候再试!'));
        else
            return $this->success('删除成功!');
    }

    /**
     * 修改状态
     * @param $id
     * @param $status
     * @return mixed
     */
    public function set_status($id, $status)
    {
        if ($status == '' || $id == 0) return $this->fail('参数错误');
        StoreRechargeCardModel::where(['id' => $id])->update(['status' => $status]);
        return $this->success($status == 0 ? '关闭成功' : '开启成功');
    }

    /**
     * 获取用户充值记录
     */
    public function user_recharge_card()
    {
        $where = Util::getMore([
            ['data', ''],
            ['paid', ''],
            ['page', 1],
            ['limit', 20],
            ['nickname', ''],
        ]);
        $where['mer_id'] = $this->merId ?: '';
        return $this->success(UserRechargeCardModel::getUsereRechargeCardList($where));
    }

    /**
     * 获取用户返现记录
     */
    public function user_brokerage()
    {
        $where = Util::getMore([
            ['data', ''],
            ['role', ''],
            ['page', 1],
            ['limit', 20],
            ['nickname', ''],
            ['uid', ''],
            ['order_id', ''],
        ]);
        $where['mer_id'] = $this->merId ?: '';
        return $this->success(UserBrokerageModel::getUserBrokerageList($where));
    }
}