<?php
class api extends base
{

  /**
   * 轮播列表  不分页
   */
  public function slidesList()
  {
    $row = db()->select_all('slides', 'title,link,url', 'status = 1 order by sort asc, id desc');
    foreach ($row as $key => &$value) {
      $value['url'] = xcx_img($value['url']);
    }
    json(0, '', $row);
  }

  /*
    推荐产品 / 分类下产品接口  2合1
     */
  public function productList()
  {
    $page     = !($_POST['page']) ? '1' : intval($_POST['page']);
    $pagesize = !($_POST['pagesize']) ? '10' : intval($_POST['pagesize']);
    $frmnum   = ($page - 1) * $pagesize;
    //不指定推荐则为所有
    $tj = $_POST['tj'] ? $_POST['tj'] : '';
    //大分类不指定为所有
    $b_type = $_POST['b_type'] ? $_POST['b_type'] : '';
    //小分类 秋冬装  不指定为所有
    $s_type = $_POST['s_type'] ? $_POST['s_type'] : '';
    //title 有则查询
    $title = $_POST['title'] ? $_POST['title'] : '';
    $where = 'status = 1 ';
    if ($tj) {
      $where .= 'and tj = ' . $tj;
    }
    if ($b_type) {
      $where .= 'and b_type = ' . $b_type;
    }
    if ($s_type) {
      $where .= 'and s_type = ' . $s_type;
    }
    if ($title) {
      $where .= ' and ' . '`title` like "%' . $title . '%" ';
    }
    $total = db()->count('product', $where);
    $row = db()->select_all('product', '*', $where . ' order by tj desc,sort desc,id desc limit ' . $frmnum . ',' . $pagesize);
    foreach ($row as $key => &$value) {
      $value['img']     = xcx_img($value['img']);
      $value['content'] = xcx_content($value['content']);
      $value['s_type']  = $this->product_type[$value['s_type']];
    }
    json(0, "", $row, $total);
  }

  /**
   * 产品详情
   */
  public function productDetail()
  {

    $id = intval($_POST['id']);

    $where = 'id = ' . $id . ' ';
    $row   = db()->select('product', '*', $where);

    // $row['img']     = xcx_img($row['img']);
    // $row['content'] = xcx_content($row['content']);
    json(0, "", $row);
  }

  /**
   * 分类列表 分页
   */
  public function productTypeList()
  {
    $page     = !($_POST['page']) ? '1' : intval($_POST['page']);
    $pagesize = !($_POST['pagesize']) ? '200' : intval($_POST['pagesize']);
    $frmnum   = ($page - 1) * $pagesize;
    $where = 'status = 1 ';
    $total = db()->count('product_type', $where);
    $row = db()->select_all('product_type', '*', $where . '  order by sort asc ,id desc limit ' . $frmnum . ',' . $pagesize);
    foreach ($row as $key => &$value) {
      $value['img'] = xcx_img($value['img']);
    }
    json(0, "", $row, $total);
  }

  /**
   * 文章 分页
   */
  public function getArticleList()
  {
    $page     = !($_POST['page']) ? '1' : intval($_POST['page']);
    $pagesize = !($_POST['pagesize']) ? '10' : intval($_POST['pagesize']);
    $frmnum   = ($page - 1) * $pagesize;

    $tj    = !($_POST['tj']) ? '' : intval($_POST['tj']);
    $id    = !($_POST['id']) ? '' : intval($_POST['id']);
    $where = 'status = 1 ';
    if ($tj) {
      $where .= ' and tj = ' . $tj;
    }
    if ($id) {
      $where .= ' and id = ' . $id;
    }

    $total = db()->count('article', $where);

    $row = db()->select_all('article', '*', $where . ' order by tj desc,sort desc,id desc limit ' . $frmnum . ',' . $pagesize);

    foreach ($row as $key => &$value) {
      $value['img']     = xcx_img($value['img']);
      $value['content'] = xcx_content($value['content']);
      $value['addtime'] = date('Y-m-d H:i:s', $value['addtime']);
    }
    if ($id) {
      if ($row) {
        //next是上一条
        $next_last = $this->next_last($row[0]['id']);
        $row       = ['cur' => $row[0], 'next' => $next_last['next'], 'last' => $next_last['last']];
      } else {
        $row = ['cur' => [], 'next' => [], 'last' => []];
      }
    }
    json(0, "", $row, $total);
  }

  /**
   * [next_last 上下篇链接以及标题]
   * @Author   huangxiaojian
   * @DateTime 2021-05-01T17:42:18+0800
   * @param    [type]                   $id [description]
   * @return   [type]                       [description]
   */
  public function next_last($id)
  {
    $temp = db()->select("article", "title,id", 'id<"' . $id . '"  order by tj desc,sort desc,id desc');

    $data['last'] = $this->get_last_next_data($temp);
    $temp         = db()->select("article", "title,id", 'id>"' . $id . '"  order by tj asc,sort asc,id asc');
    $data['next'] = $this->get_last_next_data($temp);
    return $data;
  }

  public function get_last_next_data($temp)
  {
    if ($temp) {
      $data['title'] = $temp['title'];
      $data['id']    = $temp['id'];
    } else {
      $data['title'] = "没有了";
      $data['id']    = '';
    }
    return $data;
  }

  public function getWebName()
  {
    return json(0, '', WEBNAME);
  }

  public function getRemarksDesc()
  {
    return json(0, '', $this->webset['remarks_desc']);
  }

  public function insertMessage()
  {
    if ($_POST) {
      $is_exists = db()->select('message', 'id', 'tel=' . $_POST['tel']);
      $_POST['addtime'] = time();
      if ($is_exists == '') {
        db()->insert('message', $_POST);
        json(0, '提交成功');
      } else {
        db()->update('message', $_POST, 'tel=' . $_POST['tel']);
        json(0, '资料已更新成功');
      }
    }
  }

  public function insertRemarks()
  {
    if ($_POST) {
      $is_exists = db()->select('remarks', 'id', 'tel=' . $_POST['tel']);
      $_POST['addtime'] = time();
      if ($is_exists == '') {
        db()->insert('remarks', $_POST);
        json(0, '提交成功');
      } else {
        db()->update('remarks', $_POST, 'tel=' . $_POST['tel']);
        json(0, '反馈已更新成功');
      }
    }
  }
  public function getAbout()
  {
    return json(0, '', $this->webset['about']);
  }
}
