<?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\auction\AuctionProduct as model;
use app\admin\model\User;
use crmeb\services\{ExpressService,
    JsonService,
    JsonService as Json,
    MiniProgramService,
    upload\Upload,
    WechatService,
    FormBuilder as Form,
    CacheService,
    UtilService as Util};
use app\admin\model\system\{
    SystemAttachment as SystemAttachmentModel, SystemAttachmentCategory as Category
};
use think\facade\Route as Url;
use think\facade\Validate;

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


    public function index()
    {
        $list = AuctionGu::select();

        $this->assign('list', $list);
        return $this->fetch();
    }


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

        return Json::successlayui($data);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create($id = 0)
    {
        $f = [];
        $f[] = Form::input('nickname', '场次名称')->col(12);
        $f[] = Form::input('anticipate', '预约价格')->col(12);
        $f[] = Form::frameImageOne('image', '场次主图片(305*305px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')))->icon('image')->width('100%')->height('500px');
        $f[] = Form::radio('status', '状态', 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]])->col(12);
        $f[] = Form::select('auction_gu_id', '绑定场馆')->setOptions(function () {
            $model = new AuctionGu();
            $list = $model->select();
            $menus = [];
            foreach ($list as $menu) {
                $menus[] = ['value' => $menu['id'], 'label' => $menu['name']];
            }
            return $menus;
        })->filterable(1)->col(12);
        $f[] = Form::dateTimes('add_time', '预约开始时间');
        $f[] = Form::dateTimes('end_time', '预约结束时间');
        $f[] = Form::dateTimes('radd_time', '入场时间');
        $f[] = Form::dateTimes('rend_time', '结束时间');
        $f[] = Form::input('sort', '排序', '0');
        $f[] = Form::input('sort', '排序', '0');
        $f[] =  Form::checkbox('site', '开场日期', [1])->options([
            ['value' => 1, 'label' => '周一'],
            ['value' => 2, 'label' => '周二'],
            ['value' => 3, 'label' => '周三'],
            ['value' => 4, 'label' => '周四'],
            ['value' => 5, 'label' => '周五'],
            ['value' => 6, 'label' => '周六'],
            ['value' => 7, 'label' => '周七'],
        ]);

        $form = Form::make_post_form('添加', $f, Url::buildUrl('save'));
        $this->assign(compact('form'));
        return $this->fetch('public/form-builder');

//        $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()
    {
        $mode  = new \app\admin\model\auction\Auction();
        $data = Util::postMore([
            'nickname',
            'image',
            'status',
            'sort',
            'add_time',
            'end_time',
            'radd_time',
            'rend_time',
            'info',
            'anticipate',
            'auction_gu_id',
            'site'
        ]);
        $validate = Validate::rule('nickname', 'require')->rule([
            'nickname' => 'require',
            'image' => 'require',
            'status' => 'require',
            'anticipate' => 'require',
            'add_time' => 'require',
            'end_time' => 'require',
            'radd_time' => 'require',
            'rend_time' => 'require',
            'auction_gu_id' => 'require',
            'site' => 'require'
        ]);
        $validate->message([
            'nickname.require' => '名称不能为空',
            'image.require' => '图片不能为空',
            'status.require' => '状态不能为空',
            'anticipate.require' => '请填写预约价格',
            'add_time.require' => '请选择预约开始时间价格',
            'end_time.require' => '请选择预约结束时间价格',
            'radd_time.require' => '请选择进场时间',
            'rend_time.require' => '请选择结束时间',
            'auction_gu_id.require' => '请选择绑定场馆',
            'site.require' => '请选择开场日期',
        ]);
        $data['site'] = implode($data['site'], ',');
        if (!$validate->check($data)) {
            return Json::fail($validate->getError());
        }
        $res = $mode->save($data);
        if ($res){
            return Json::success('添加成功!');
        }else{
            return Json::fail('添加失败');
        }

    }

    /**
     * 删除
     * @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)->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))return 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 = \app\admin\model\auction\Auction::find($id);
        $f = [];
        $f[] = Form::input('nickname', '场次名称', $data->getData('nickname'))->col(12);
        $f[] = Form::input('anticipate', '预约价格', $data->getData('anticipate'))->col(12);
        $f[] = Form::frameImageOne('image', '场次主图片(305*305px)',Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')), $data->getData('image'))->icon('image')->width('100%')->height('500px');
        $f[] = Form::radio('status', '状态',  $data->getData('status'))->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]])->col(12);
        $f[] = Form::select('auction_gu_id', '绑定场馆', (string)$data->getData('auction_gu_id'))->setOptions(function () {
            $model = new AuctionGu();
            $list = $model->select();
            $menus = [];
            foreach ($list as $menu) {
                $menus[] = ['value' => $menu['id'], 'label' => $menu['name']];
            }
            return $menus;
        })->filterable(1)->col(12);
        $f[] = Form::dateTimes('add_time', '预约开始时间', $data->getData('add_time'));
        $f[] = Form::dateTimes('end_time', '预约结束时间', $data->getData('end_time'));
        $f[] = Form::dateTimes('radd_time', '入场时间', $data->getData('radd_time'));
        $f[] = Form::dateTimes('rend_time', '结束时间', $data->getData('rend_time'));
        $f[] = Form::input('sort', '排序', '0', $data->getData('sort'));
        $f[] = Form::textarea('info', '介绍', $data->getData('info'))->col(12);
        $f[] =  Form::checkbox('site', '开场日期', explode(',', $data->getData('site')))->options([
            ['value' => 1, 'label' => '周一'],
            ['value' => 2, 'label' => '周二'],
            ['value' => 3, 'label' => '周三'],
            ['value' => 4, 'label' => '周四'],
            ['value' => 5, 'label' => '周五'],
            ['value' => 6, 'label' => '周六'],
            ['value' => 7, 'label' => '周七'],
        ]);
        $form = Form::make_post_form('修改', $f, Url::buildUrl('update', compact('id')));
        $this->assign(compact('form'));
        return $this->fetch('public/form-builder');
    }


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

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

    public function update()
    {
        $data = Util::postMore([
            'id',
            'nickname',
            'image',
            'status',
            'sort',
            'add_time',
            'end_time',
            'radd_time',
            'rend_time',
            'info',
            'anticipate',
            'auction_gu_id',
            'site'
        ]);
        $validate = Validate::rule('nickname', 'require')->rule([
            'nickname' => 'require',
            'image' => 'require',
            'status' => 'require',
            'anticipate' => 'require',
            'add_time' => 'require',
            'end_time' => 'require',
            'radd_time' => 'require',
            'rend_time' => 'require',
            'auction_gu_id' => 'require',
            'site' => 'require'
        ]);
        $validate->message([
            'nickname.require' => '名称不能为空',
            'image.require' => '图片不能为空',
            'status.require' => '状态不能为空',
            'anticipate.require' => '请填写预约价格',
            'add_time.require' => '请选择预约开始时间价格',
            'end_time.require' => '请选择预约结束时间价格',
            'radd_time.require' => '请选择进场时间',
            'rend_time.require' => '请选择结束时间',
            'auction_gu_id.require' => '请选择绑定场馆',
            'site.require' => '请选择开场日期',
        ]);
        $data['site'] = implode($data['site'], ',');
        if (!$validate->check($data)) {
            return Json::fail($validate->getError());
        }

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


    }
    /**
     * 商品列表
     * @param $id
     * @return void
     */
    public function product($id)
    {
        $list = \app\admin\model\auction\Auction::select();
        $this->assign([
            'auction'=> $list,
            'id' => $id

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


    }

    /**
     * 商品
     * @param $id
     * @return void
     */
    public function product_list($id)
    {
        $where = Util::getMore([
            ['is_show', ''],
            ['page', 1],
            ['limit', 20],
            ['store_name', ''],
            ['auction_id']
        ]);
        $data = model::list($where, $id);

        foreach ($data['data'] as $key => $val){
            if ($data['data'][$key]['uid'] == 0) $data['data'][$key]['nickname'] = '管理';
        }


        return Json::successlayui($data);
    }

    /**
     * 商品添加
     * @param $id
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function product_edit($id, $auction_id)
    {
        if (!$id) Json::fail('数据不存在');

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

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function product_create($id = 0)
    {

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



    public function product_save($id)
    {
        $data = Util::postMore([
            'name',
            'is_show',
            'image',
            'price',
            'deduct',
            'rise',
            'info',
            'sort',
            'auction_id',
            'hanging_price',
            'slider_image',
            'description'
        ]);
        $data['slider_image'] = json_encode($data['slider_image']);
        $data['description'] = htmlspecialchars($data['description']);
        $auction = \app\admin\model\auction\Auction::where('id' ,$data['auction_id'])->find();
        $auction_gu = \app\admin\model\auction\AuctionGu::where('id' ,$auction['auction_gu_id'])->find();
        $data['uid'] = $auction_gu['uid'];
        $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
     */
    public function product_update($id)
    {
        $data = Util::postMore([
            'id',
            'name',
            'image',
            'price',
            'deduct',
            'rise',
            'info',
            'sort',
            'auction_id',
            'hanging_price',
            'slider_image',
            'description'
        ]);
        $data['slider_image'] = json_encode($data['slider_image']);
        $data['description'] = htmlspecialchars($data['description']);
        $res = model::update($data);
        if ($res){
            return Json::success('修改成功!');
        }else{
            return Json::fail(\app\admin\model\auction\Auction::getErrorInfo());
        }


    }

}