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

use app\admin\controller\AuthController;
use app\admin\controller\Union;
use app\admin\model\auction\AuctionGu;
use app\admin\model\User;
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 think\facade\Route as Url;

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


    public function index()
    {


        return $this->fetch();
    }


    public function list()
    {
        $where = Util::getMore([
            ['status', ''],
            ['page', 1],
            ['limit', 20],
            ['auction']
        ]);
        $data = \app\admin\model\auction\Auction::list($where);

        return Json::successlayui($data);
    }

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

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


    public function save($id)
    {
        $mode  = new \app\admin\model\auction\Auction();
        $data = Util::postMore([
            'nickname',
            'image',
            'status',
            'sort',
            'time',
            'rtime',
            'info',
            'anticipate',
            'auction_gu_id'
        ]);


        $time = explode('-', $data['time']);

        $data['add_time'] = trim($time[0]);
        $data['end_time'] = trim($time[1]);
        $rtime = explode('-', $data['rtime']);
        $data['radd_time'] = trim($rtime[0]);
        $data['rend_time'] = trim($rtime[1]);

        if (strtotime($time[0]) > strtotime( $time[1])) Json::fail('预约时间选择错误');
        if (strtotime($time[0]) > strtotime( $time[1])) Json::fail('进场时间选择错误');

        $res = $mode->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('删除失败');
        $model = new \app\admin\model\auction\Auction();

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

    }


    public function set_status($id, $status)
    {

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

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

    public function edit($id)
    {
        if (!$id) Json::fail('数据不存在');
        $data = [];
        $model = new AuctionGu();
        $auction = $model->select();
        if ($id > 0) $data = \app\admin\model\auction\Auction::find($id)->toArray();

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


    public function get_auction($id)
    {
        if (!$id) Json::fail('数据不存在');
        $info = \app\admin\model\auction\Auction::find($id)->toArray();

        $data['productInfo'] = $info;
        return JsonService::successful($data);
    }

    public function update()
    {
        $data = Util::postMore([
            'id',
            'nickname',
            'image',
            'time',
            'rtime',
            'sort',
            'info',
            'anticipate',
            'auction_gu_id'
        ]);
        $time = explode('-', $data['time']);
        $data['add_time'] = trim($time[0]);
        $data['end_time'] = trim($time[1]);
        $rtime = explode('-', $data['rtime']);
        $data['radd_time'] = trim($rtime[0]);
        $data['rend_time'] = trim($rtime[1]);

        if (strtotime($time[0]) > strtotime( $time[1])) Json::fail('预约时间选择错误');
        if (strtotime($time[0]) > strtotime( $time[1])) Json::fail('进场时间选择错误');

        $res = \app\admin\model\auction\Auction::update($data);
        if ($res){
            return Json::success('修改成功!');
        }else{
            return Json::fail(\app\admin\model\auction\Auction::getErrorInfo());
        }


    }


//    public function edit($id)
//    {
//        if (!$id) Json::fail('数据不存在');
//
//
//        $list = \app\admin\model\auction\Auction::get($id);
//        if (!$list) Json::fail('数据不存在');
//
//        $f = array();
//        $f[] = Form::input('id', 'ID', $list->getData('id'))->disabled(1);
//        $f[] = Form::input('nickname', '名称', $list->getData('nickname'));
//        $f[] = Form::uploadImageOne('image', '图片','/index.php/admin/widget.images/upload',$list->getData('image'));
//        $f[] = Form::radio('is_promoter', '推广员',$list->getData('status'))->options([['value' => 1, 'label' => '启用'], ['value' => 0, 'label' => '禁用']]);
//        $f[] = Form::dateTime('time', '推广员',$list->getData('add_time'));
//
//        $form = Form::make_post_form('添加用户通知', $f, Url::buildUrl('update', array('uid' => $id)), 5);
//        $this->assign(compact('form'));
//        return $this->fetch('public/form-builder');
//    }

}