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

use app\admin\controller\AuthController;
use app\admin\controller\Union;
use app\models\auction\AuctionTime;
use app\models\user\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 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',
            'give',
            'fictitious_price'
        ]);
        $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',
            'give',
            'fictitious_price'
        ]);
        $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());
        }


    }

    /**
     * 修改商品所属人页面
     * @param $id
     * @return string
     * @throws \Exception
     */
    public function belonging($id)
    {
        $f = [];
        $f[] = Form::input('account', '输入用户账号或者ID' );
        $f[] = Form::hidden('id', $id );
        $form = Form::make_post_form('修改', $f, Url::buildUrl('belonging_save'));
        $this->assign(compact('form'));
        return $this->fetch('public/form-builder');

    }

    /**
     * 修改商品所属人
     * @param $account
     * @param $id
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function belonging_save($account, $id)
    {
        if (!$account) return  Json::fail('不能为空');
        $user = User::where('account|uid', '=', $account)->find();
        if (!$user) return  Json::fail('用户不存在');
        $product = \app\admin\model\auction\AuctionProduct::find($id);
        if ($product['is_admin'] != 1 and $product['is_show'] == 1){
            AuctionTime::where('product_id', $product['id'])->delete();// 删除上架信息
        }
        $product['is_show'] = 0;
        $product['uid'] = $user['uid'];
        $res = $product->save();
        if ($res) return  Json::success('修改成功');
        return  Json::fail('修改失败');
    }


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

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

    public function fzsp($id)
    {
        $data = Util::postMore([
            'name',
            'image',
            'price',
            'deduct',
            'rise',
            'info',
            'sort',
            'auction_id',
            'hanging_price',
            'slider_image',
            'description',
            'give',
            'uid',
            'fictitious_price'
        ]);
        $data['slider_image'] = json_encode($data['slider_image']);
        $data['description'] = htmlspecialchars($data['description']);
        $res = model::create($data);
        if ($res){
            return Json::success('修改成功!');
        }else{
            return Json::fail(\app\admin\model\auction\Auction::getErrorInfo());
        }


    }


}