<?php

namespace app\admin\controller\store;

use app\admin\controller\AuthController;
use app\admin\model\store\StoreProductProvider as SPP;
use crmeb\services\FormBuilder as Form;
use crmeb\services\JsonService;
use crmeb\services\UtilService;
use think\facade\Route as Url;

class StoreProductProvider extends AuthController
{
    public function index()
    {
        return $this->fetch();
    }

    public function get_providers()
    {
        $where = UtilService::getMore([
            ['page', 0],
            ['limit', 10],
            ['keyword', ''],
        ]);
        return JsonService::successlayui(SPP::getProviderList($where));
    }

    public function create($id=0)
    {
        if ($id) {
            $pvdr = SPP::get($id);
        }
        $field[] = Form::text('name', '名称', isset($pvdr) ? $pvdr->name : '')->col(12);
        $field[] = Form::text('site', '网站', isset($pvdr) ? $pvdr->site : '');
        $field[] = Form::frameImageOne('logo', 'logo(305*305px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'logo')), isset($pvdr) ? $pvdr->logo : '')->icon('image')->width('100%')->height('500px');
        $field[] = Form::text('source', '来源', isset($pvdr) ? $pvdr->source : '')->col(12);
        $field[] = Form::text('contact', '联系人', isset($pvdr) ? $pvdr->contact : '')->col(12);
        $field[] = Form::text('phone', '电话', isset($pvdr) ? $pvdr->phone : '')->col(12);
        $field[] = Form::text('qq', 'qq', isset($pvdr) ? $pvdr->qq : '')->col(12);
        $field[] = Form::text('wechat', '微信', isset($pvdr) ? $pvdr->wechat : '')->col(12);
        $field[] = Form::textarea('desc', '描述', isset($pvdr) ? $pvdr->desc : '')->rows(3);
        $field[] = Form::select('status', '状态', isset($pvdr) ? strval($pvdr->status) : '0')->setOptions([
            ['label'=>'默认', 'value'=>'0'],
            ['label'=>'下架', 'value'=>'1'],
            ['label'=>'删除', 'value'=>'2'],
        ]) ->col(12);

        $form = Form::make_post_form('供应商', $field, Url::buildUrl('save', ['id' => $id]), 2);
        $this->assign(compact('form'));
        return $this->fetch('public/form-builder');
    }

    public function save($id=0)
    {
        $data = UtilService::postMore([
            ['name', ''],
            ['site', ''],
            ['logo', 0],
            ['source', ''],
            ['contact', ''],
            ['phone', ''],
            ['qq', 0],
            ['wechat', 0],
            ['desc', 0],
            ['status', 0],
        ]);
        if (!$data['name']) {
            return JsonService::fail('请输入名称');
        }
        if ($data['status'] < 0 || $data['status'] > 2) {
            return JsonService::fail('参数错误');
        }

        try {
            if ($id) {
                $res = SPP::edit($data, $id);
            } else {
                $data['add_time'] = time();
                $res = SPP::create($data);
            }
            if ($res) {
                return JsonService::successful('保存成功');
            }else{
                return JsonService::fail('保存失败');
            }
        } catch (\Exception $e) {
            return JsonService::fail($e->getMessage());
        }
    }

    public function mark($id=0,$st=0)
    {
        if (SPP::edit(['status' => intval($st)], $id))
            return JsonService::successful('操作成功');
        else
            return JsonService::fail('操作失败');
    }
}