<?php

namespace app\adminapi\controller\v1\marketing;

use app\adminapi\controller\AuthController;
use crmeb\traits\CurdControllerTrait;
use crmeb\services\UtilService as Util;
use app\models\store\{PopScreen as PopScreenModel};

/**
 * 弹屏广告管理
 * Class PopScreen
 * @package app\admin\controller\store
 */
class PopScreen extends AuthController
{

	use CurdControllerTrait;

	protected $bindModel = PopScreenModel::class;

	/**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $where = Util::getMore([
            ['page', 1],
            ['limit', 20],
            ['store_name', '']
        ]);
        $where['mer_id'] = $this->merId ?: '';
        $list = PopScreenModel::systemPage($where);
        return $this->success($list);
    }

    /**
     * 详情
     * @param $id
     * @return mixed
     */
    public function read($id)
    {
        $info = PopScreenModel::getOne($id);
        return $this->success(compact('info'));
    }

    /**
     * 保存新建的资源
     * @param int $id
     */
    public function save($id = 0)
    {
        $data = Util::postMore([
            [['mer_id', 'd'], ''],
            [['title', 's'], ''],
            [['site_url', 's'], ''],
            ['pop_image', ''],
            [['command', 's'], ''],
            [['template_id', 'd'], ''],
            ['bg_image', ''],
            [['share_title', 's'], ''],
            [['share_describe', 's'], ''],
            [['domain_name', 's'], ''],
            ['add_time', ''],
            ['click', 0]
        ]);
        $data['mer_id'] = $this->merId ?: '';
        if ($id) {
            $record = PopScreenModel::get($id);
            if (!$record) return $this->fail('数据不存在!');
            PopScreenModel::edit($data, $id);
            return $this->success('编辑成功!');
        } else {
            $data['add_time'] = time();
            $id = PopScreenModel::insertGetId($data);
            $data['domain_name']='http://pop'.$id.'.lipinmama.cn/';
            PopScreenModel::edit($data, $id);
            return $this->success('添加弹屏广告成功!');
        }
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        if (!$id) return $this->fail('数据不存在');
        $record = PopScreenModel::get($id);
        if (!$record) return $this->fail('数据不存在!');
        if ($record['is_del']) return $this->fail('已删除!');
        $data['is_del'] = 1;
        if (!PopScreenModel::edit($data, $id))
            return $this->fail(PopScreenModel::getErrorInfo('删除失败,请稍候再试!'));
        else
            return $this->success('删除成功!');
    }

}