<?php
namespace app\admin\controller\auction;

use app\admin\controller\AuthController;
use app\admin\controller\Union;
use app\admin\model\user\User;
use app\admin\model\user\UserBill;
use crmeb\services\{ExpressService,
    JsonService,
    MiniProgramService,
    upload\Upload,
    WechatService,
    FormBuilder as Form,
    CacheService,
    UtilService as Util,
    JsonService as Json};
use app\admin\model\system\{
    SystemAttachment as SystemAttachmentModel, SystemAttachmentCategory as Category
};
use app\admin\model\auction\AuctionBooking as model;
use think\facade\Route as Url;

/**
 * 竞拍管理
 * Class StoreOrder
 * @package app\admin\controller\store
 */
class AuctionBooking extends AuthController
{

    public function index()
    {

        $list = \app\admin\model\auction\Auction::select();

        $this->assign([
            'year' => get_month(),
            'auction'=> $list

        ]);
        return $this->fetch();
    }


    /**
     * 获取列表
     * @return void
     */
    public function list()
    {
        $where = Util::getMore([
            ['is_show', ''],
            ['page', 1],
            ['limit', 20],
            ['auction_id', ''],
            ['store_name', ''],
            ['data', '']
        ]);
        $data = model::list($where);
        foreach ($data['data'] as $key => $val){
            if ($data['data'][$key]['uid'] == 0) $data['data'][$key]['nickname'] = '管理';
        }


        return Json::successlayui($data);
    }

    public function auction_list()
    {
        $list = model::select()->toArray();

        Json::successful($list);
    }


    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create($id = 0)
    {
        $data = \app\admin\model\auction\Auction::select()->toArray();

        $this->assign(['id' => $id, 'list' => $data]);
        return $this->fetch();
    }


    /**
     * 添加
     * @param $id
     * @return void
     */
    public function save($id)
    {
        $data = Util::postMore([
            'name',
            'is_show',
            'image',
            'price',
            'deduct',
            'rise',
            'info',
            'sort',
            'auction_id'
        ]);
        $model = new model();
        $res = $model->save($data);
        if ($res){
            return Json::success('添加成功!');
        }else{
            return Json::fail(\app\admin\model\auction\Auction::getErrorInfo());
        }

    }

    /**
     * 删除
     * @param $id
     * @return void
     * @throws \Exception
     */
    public function delete($id)
    {
        if (!$id) Json::fail('删除失败');

        $res = model::where('id', $id)->delete();
        if ($res){
            return Json::success('删除成功!');
        }else{
            return Json::fail(\app\admin\model\auction\Auction::getErrorInfo());
        }

    }


    /**
     * 修改状态
     * @param $id
     * @param $status
     * @return void
     */
    public function set_status($id, $status)
    {

        if (empty($id)) Json::fail('修改失败');

        $res = model::update(['is_show' => $status, 'id' => $id]);
        if ($res){
            return Json::success('修改成功!');
        }else{
            return Json::fail(\app\admin\model\auction\Auction::getErrorInfo());
        }
    }

    /**
     * 编辑页面
     * @param $id
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit($id)
    {
        if (!$id) Json::fail('数据不存在');
        $data = \app\admin\model\auction\Auction::select()->toArray();

        $this->assign(['id' => $id, 'list' => $data]);
        return $this->fetch();
    }


    public function get_list($id)
    {
        if (!$id) Json::fail('数据不存在');
        $model = new model();
        $info = $model->find($id);

        return JsonService::successful($info);
    }

    /**
     * 修改
     * @param $id
     * @return void
     */
    public function update($id)
    {
        $data = Util::postMore([
            'id',
            'name',
            'is_show',
            'image',
            'price',
            'deduct',
            'rise',
            'info',
            'sort',
            'auction_id'
        ]);
        $res = model::update($data);
        if ($res){
            return Json::success('修改成功!');
        }else{
            return Json::fail(\app\admin\model\auction\Auction::getErrorInfo());
        }


    }


    /**
     * 退回预约卷
     * @param $id
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function th($id)
    {
        if (!$id) Json::fail('没有数据');
        $data = model::find($id);
        if ($data['status'] < 1) Json::fail('已退回请勿重复提交');

        $userModel = new User();
        $user = $userModel->where('uid', $data['uid'])->find();
        if (!$user) Json::fail('没有数据');

        $user['anticipate'] = $user['anticipate']+ $data['anticipate'];
        $bill = UserBill::create([
            'uid' => $user['uid'],
            'pm' => 1,
            'title' => '预约卷退回',
            'category' => 'anticipate',
            'type' => 'add_anticipate',
            'number' => $data['anticipate'],
            'balance' => $user['anticipate'],
            'mark' => '后台操作退回',
            'add_time' => time(),
        ]);

        if ($bill){
            $res = $user->save();
            model::where('id', $id)->update(['status' => 0]);
            if ($res){
                Json::successful('退回成功');
            }else{
                Json::fail('退回失败');
            }

        }else{
            Json::fail('错误');
        }
    }

}