<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\BaseProduct;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Form\NestedForm;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Admin\Repositories\BaseProductCategory;
use Illuminate\Http\Request;
class BaseProductController extends AdminController
{


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(BaseProduct::with('baseProductCategory'), function (Grid $grid) {
            $grid->column('id','ID')->sortable();
            $grid->column('title');
            $grid->column('sku');
            $grid->column('baseProductCategory.name','Category Name');
            $grid->column('issuance_date');
            $grid->column('order')->orderable();
            $grid->column('enabled')->switch();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            // 筛选
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('sku');
                $filter->like('title');
                $filter->equal('category_id','Category')->select(BaseProductCategory::selectOptions());
                $filter->equal('enabled', 'enabled')->select([
                    1 => 'Yes',
                    0 => 'No',
                ]);
            });
            //排序
           $grid->model()->orderBy("order",'asc')->orderBy('id', 'desc');
            // 禁用查看按钮
            //$grid->showQuickEditButton();
            //$grid->enableDialogCreate();
            //$grid->disableEditButton();
        });
    }



    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new BaseProduct(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('keywords');
            $show->field('description');
            $show->field('sku');
            $show->field('baseProductCategory.name','Category Name');
            $show->field('issuance_date');
            $show->field('order');
            $show->field('enabled');
            $show->field('content');
            $show->field('parameters');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new BaseProduct(), function (Form $form) {
            $form->select('category_id', 'Category Name')
                ->options(BaseProductCategory::selectOptions())
                ->required();
//            $form->select('city');
//            $form->text('title')->required();
//            $form->text('keywords');
//            $form->text('description');
//            $form->text('sku')->required();
//            $form->date('issuance_date');
//            $form->table('parameters','Parameters', function (Form\NestedForm $table) {
//                $table->text('key')->required();
//                $table->text('value')->required();
//            });
//            $form->editor('content');
//            $form->number('order');
//            $form->switch('enabled')->default(1);

            $form->table('parameters','Parameters', function (Form\NestedForm $table) {
                $table->text('key')->required();
                $table->text('value')->required();
            })->default([
                ['key' => 'color', 'value' => 'white'],
            ]);
            //插入JS
            $this->addParametersJs();
        });

    }

    /*
     * 以json型式返回产品参数
     */
    public static function parameter(Request $request)
    {
        $id = $request->query('q');
        $content = BaseProductCategory::getParameter($id);
        return $content;
    }

    private function addParametersJs()
    {
        //插入JS
        Admin::script(
            <<<JS
var fill_param = function (key,val) {
    lastForm = $(".has-many-table-parameters-form:last");
    lastForm.find('input').eq(0).val(key);
    lastForm.find('input').eq(1).val(val);
}
$('select[name="category_id"]').on('change', function() {
    var category_id = $(this).val();
    // 清空现有的表格行
    $('.has-many-table-parameters-form').remove();
    if (category_id > 0) {
        $.ajax({
            url: '/prime-control/base-product/parameter',      // 请求的 URL
            data: { q: category_id},
            type: 'GET',              // GET 请求
            success: function(data) { // 成功时执行的代码
                // 动态添加新数据到表格
                $.each(data, function(index, item) {
                    $(".has-many-table-parameters").find(".add").click();
                    fill_param(item.key,item.value);
                    console.log(item);
                });
            },
            error: function(error) {  // 错误时执行的代码
                console.log('error:', error);
            }
        });
    }

});
JS
        );
    }
}
