<?php
/**
 * Created by PhpStorm.
 * User: xurongyao <763569752@qq.com>
 * Date: 2018/6/14 下午5:25
 */

namespace app\admin\controller\finance;

use app\admin\controller\AuthController;
use FormBuilder\exception\FormBuilderException;
use think\Exception;
use think\facade\Route;
use app\admin\model\user\{User, UserBill};
use app\admin\model\finance\FinanceModel;
use crmeb\services\{FormBuilder, JsonService, UtilService as Util, JsonService as Json, UtilService};

/**
 * 微信充值记录
 * Class UserRecharge
 * @package app\admin\controller\user
 */
class Finance extends AuthController
{
    /**
     * 显示资金记录
     */
    public function bill()
    {
        $list = UserBill::where('category', 'not in', 'integral')
            ->field(['title', 'type'])
            ->group('type')
            ->distinct(true)
            ->select()
            ->toArray();
        $this->assign('selectList', $list);
        return $this->fetch();
    }

    /**
     * 显示资金记录ajax列表
     */
    public function billlist()
    {
        $where = Util::getMore([
            ['start_time', ''],
            ['end_time', ''],
            ['nickname', ''],
            ['limit', 20],
            ['page', 1],
            ['type', ''],
        ]);
        return Json::successlayui(FinanceModel::getBillList($where));
    }

    /**
     *保存资金监控的excel表格
     */
    public function save_bell_export()
    {
        $where = Util::getMore([
            ['start_time', ''],
            ['end_time', ''],
            ['nickname', ''],
            ['type', ''],
        ]);
        FinanceModel::SaveExport($where);
    }

    /**
     * 显示佣金记录
     */
    public function commission_list()
    {
        $this->assign('is_layui', true);
        return $this->fetch();
    }

    /**
     * 佣金记录异步获取
     */
    public function get_commission_list()
    {
        $get = Util::getMore([
            ['page', 1],
            ['limit', 20],
            ['nickname', ''],
            ['price_max', ''],
            ['price_min', ''],
            ['order', ''],
            ['excel', ''],
        ]);
        return Json::successlayui(User::getCommissionList($get));
    }

    /**
     * 显示操作记录
     */
    public function index3()
    {

    }

    /**
     * 佣金详情
     */
    public function content_info($uid = '')
    {
        if ($uid == '') return $this->failed('缺少参数');
        $this->assign('userinfo', User::getUserinfo($uid));
        $this->assign('uid', $uid);
        return $this->fetch();
    }

    /**
     * 佣金提现记录个人列表
     */
    public function get_extract_list($uid = '')
    {
        if ($uid == '') return Json::fail('缺少参数');
        $where = Util::getMore([
            ['page', 1],
            ['limit', 20],
            ['start_time', ''],
            ['end_time', ''],
            ['nickname', '']
        ]);
        return Json::successlayui(UserBill::getExtrctOneList($where, $uid));
    }


    /**
     * 创建form表单
     *
     * @param int $id
     * @return string
     * @throws Exception
     * @throws FormBuilderException|\Exception
     */
    public function create($id = 0)
    {

        if (!$id) $this->failed('参数错误');
        $info = UserBill::get($id);
        if (!$info) $this->failed('记录不存在');
        $field[] = FormBuilder::textarea('admin_mark', '备注', isset($info) ? $info->admin_mark : '')->col(24)->rows(20);
        $form = FormBuilder::make_post_form('添加计划', $field, Route::buildUrl('save', ['id' => $id]), 2);
        $this->assign(compact('form'));
        return $this->fetch('public/form-builder');
    }

    /**
     * 添加或者修改
     * @param $id
     * @return void
     */
    public function save($id = 0)
    {
        if (!$id) $this->failed('参数错误');
        $info = UserBill::get($id);
        if (!$info) $this->failed('记录不存在');
        $data = UtilService::postMore([
            ['admin_mark', ''],
        ]);
        UserBill::beginTrans();
        try {
            //修改
            if (UserBill::edit($data, $id)) {
                UserBill::commitTrans();
                JsonService::successful('修改成功');
            } else {
                UserBill::rollbackTrans();
                JsonService::fail('修改失败');
            }
        } catch (\Exception $e) {
            UserBill::rollbackTrans();
            JsonService::fail($e->getMessage());
        }
    }
}