<?php

namespace App\Admin\Controllers;

use App\Models\Goods;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Http\Request;
use App\Models\CartItem;
use App\Models\Order;
use App\Models\Activity;
use App\Models\GridMy;
use App\Admin\Extensions\Tools\UserGender;
use App\Models\Cut;
use Encore\Admin\Widgets\Table;

class CutController extends Controller {
	use HasResourceActions;

	/**
	 * Index interface.
	 *
	 * @param Content $content
	 * @return Content
	 */
	public function index(Content $content, Request $request) {
		return $content->header ( '砍价' )->description ( '列表' )->body ( $this->grid () );
	}

	/**
	 * Edit interface.
	 *
	 * @param mixed $id
	 * @param Content $content
	 * @return Content
	 */
	public function edit($id, Content $content) {
		return $content->header ( '砍价' )->description ( '编辑' )->body ( $this->form ()->edit ( $id ) );
	}

	/**
	 * Create interface.
	 *
	 * @param Content $content
	 * @return Content
	 */
	public function create(Content $content) {
		return $content->header ( '砍价' )->description ( '添加' )->body ( $this->form () );
	}

	/**
	 * Make a grid builder.
	 *
	 * @return Grid
	 */
	protected function grid() {
		$grid = new GridMy ( new Cut () );
		$grid->disableCreateButton();
		if (request ()->goods_id) {
			$grid->model ()->where ( 'goods_id', '=', request ()->goods_id );
		}
		$grid->model ()->orderBy ( 'updated_at', 'desc' );
		$grid->user_id ( '发起人uid' );

		$grid->status ( '状态' )->display ( function () {
			return Cut::$statusArr [$this->status];
		} );
		$grid->column ( '帮砍人及价格' )->display ( function () {
			$str = '';
			foreach ( $this->cutLogs as $item ) {
				$str .= $item->cut_user_id . "  " . $item->cut_price;
				$str .= "<br/>";
			}
			return $str;
		} );
		$grid->current_price ( '当前价格' );
		$grid->final_price ( '砍成价' )->sortable ();
		$grid->created_at ( '发起时间' );
		$grid->actions ( function ($actions) {
			$actions->disableView ();
		} );

		$grid->disableExport ();
		$grid->filter ( function ($filter) {
			$filter->disableIdFilter ();
			$filter->like ( 'name', '砍价名称' );
		} );

		$grid->column ( 'title', '历史砍价' )->modal ( '最新评论', function ($model) {

// 			$comments = $model->comments ()->take ( 10 )->get ()->map ( function ($comment) {
// 				return $comment->only ( [ 
// 						'id',
// 						'content',
// 						'created_at'
// 				] );
// 			} );

			return new Table ( [ 
					'ID',
					'内容',
					'发布时间'
			], [['a'=>'b']] );
		} );
		return $grid;
	}

	/**
	 * Make a form builder.
	 *
	 * @return Form
	 */
	protected function form() {
		$form = new Form ( new Goods () );
		$form->text ( 'sku', '砍价sku' )->required ();
		$form->text ( 'name', '砍价名称' )->required ();
		$form->currency ( 'price', '市场价' )->symbol ( '￥' );
		$form->currency ( 'base_price', '保底价' )->symbol ( '￥' );
		$form->number ( 'stock', '砍价数量' )->required ();
		$form->number ( 'cut_num', '砍价数量' )->required ();
		$form->radio ( 'is_free_shipping', '是否包邮' )->options ( Goods::$isFreeShippingArr )->default ( '1' );
		$form->number ( 'sort', '排序' );
		$form->hidden ( 'activity_id' )->default ( request ()->activity_id );
		$form->saving ( function (Form $form) {
			$form->model ()->product_id = Goods::getProductId ( $form->sku );
			$form->model ()->img_url = Goods::getSkuInfo ( $form->sku ) ['image_colors'] [0] ['main_image'];
		} );
		return $form;
	}
}
