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

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

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

    public function index()
    {

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

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


    public function list()
    {
        $where = Util::getMore([
            ['is_show', ''],
            ['page', 1],
            ['limit', 20],
            ['auction_id', ''],
            ['store_name', '']
        ]);
        $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();
    }


    public function 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
     * @throws \Exception
     */
    public function delete($id)
    {
        if (!$id)return Json::fail('删除失败');

        $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 = model::update(['is_show' => $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::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);
        $info['slider_image'] = is_string($info['slider_image']) ? json_decode($info['slider_image'], true) : [];
        $info['description'] = htmlspecialchars_decode($info['description']);
        $data['productInfo'] = $info;
        return JsonService::successful($data);
    }

    public function 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());
        }


    }


//    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');
//    }

}