<?
class type extends base
{
  /**
   * [cids 主要获取下拉框内的分类]
   * @Author   huangxiaojian
   * @DateTime 2021-04-30T09:49:16+0800
   * @param    integer                  $pid      [上级ID]
   * @param    integer                  $model_id [模型ID]
   * @return   [type]                             [description]
   */
  public function cids($pid = 0, $model_id = 0)
  {
    if ($model_id > 0)
      $where = "model_id=$model_id";
    else
      $where = "1=1";
    $data = db()->select_all("type", "id,title,pid", $where);
    $res = reSort($data, $pid, 0, TRUE);
    $arr = [];
    foreach ($res as $key => $value) {
      $arr[$value['id']] = $value['title'];
    }
    return $arr;
  }
  /**
   * [childs 根据分类ID获取底下全部分类]
   * @Author   huangxiaojian
   * @DateTime 2021-04-30T17:37:31+0800
   * @param    [type]                   $cid [description]
   * @return   [type]                        [description]
   */
  function childs($cid)
  {
    $data = db()->select_all('type', "id,title,pid", "status=1");
    $res = sons($data, $cid);
    //加上自己
    $res[] = $cid;
    return $res;
  }
  /**
   * [nav 导航]
   * @Author   huangxiaojian
   * @DateTime 2021-04-30T16:30:52+0800
   * @return   [type]                   [description]
   */
  function nav($field = "id,title,url")
  {
    return db()->select_all("type", $field, "status=1 order by sort desc,id desc");
  }
  function list_url($cid)
  {
    $row = db()->select('type', '*', 'id=' . $cid);
    if (!empty($row['url'])) {
      return $row['url'];
    } else {
      $row1 = db()->select("model", "*", "id=" . $row['model_id']);

      return u($row1['mod'], $row1['act'], array('cid' => $row['id']));
    }
  }
  /**
   * [topId 获取最上层cid]
   * @Author   huangxiaojian
   * @DateTime 2021-04-30T23:33:37+0800
   * @param    [type]                   $cid [description]
   * @return   [type]                        [description]
   */
  function topId($cid)
  {
    $row = db()->select('type', 'pid,id,title', 'id=' . $cid);
    //$arr[]=array("id"=>$row['id'],"title"=>$row['title']);
    while ($row['pid'] != 0) {
      $row = db()->select('type', 'pid,id,title', 'isshow=1 and id=' . $row['pid']);
      $cid = $row['id'];
    }
    return $cid;
  }
  /**
   * [firstId 获得最下面一层的cid]
   * @Author   huangxiaojian
   * @DateTime 2021-04-30T23:37:19+0800
   * @param    [type]                   $cid [description]
   * @return   [type]                        [description]
   */
  function firstId($cid)
  {
    $row = db()->select('type', 'pid,id,title', 'pid=' . $cid . ' order by sort desc,id desc');

    while ($row['id'] != 0) {
      $cid = $row['id'];
      $row = $duoduo->select('type', 'pid,id,title', 'pid=' . $row['id'] . ' order by sort desc,id desc');
    }
    return $cid;
  }
  function type()
  {
    return db()->get_table("type", $_GET['cid']);
  }



  /**
   * [position 根据cid获取当前位置]
   * @Author   huangxiaojian
   * @DateTime 2021-04-30T23:28:37+0800
   * @param    [type]                   $cid [description]
   * @return   [type]                        [description]
   */
  function position($cid)
  {

    $row = db()->select('type', 'pid,id,model_id,title,jian_mod,jian_act', 'id=' . $cid);

    $row1 = db()->select("model", "*", "id=" . $row['model_id']);

    $arr[] = array("id" => $row['id'], "title" => $row['title'], "jian_mod" => $row1['mod'], "jian_act" => $row1['act']);
    while ($row['pid'] != 0) {
      $row = db()->select('type', 'pid,id,title,model_id,jian_mod,jian_act', 'id=' . $row['pid']);
      $row1 = db()->select("model", "*", "id=" . $row['model_id']);
      $arr[] = array("id" => $row['id'], "title" => $row['title'], "jian_mod" => $row1['mod'], "jian_act" => $row1['act']);
    }

    $str = '<a href="/">' . 首页 . '</a>';
    foreach (array_reverse($arr) as $row) {
      $str = $str . '><a href="' . u($row['jian_mod'], $row['jian_act'], array('cid' => $row['id'])) . '">' . $row['title'] . '</a>';
    }
    return $str;
  }

  function get_content($cid)
  {
    $content = db()->select('type', 'content', 'id=' . $cid);
    return $content;
  }


  function get_types($pid = 0, $field = "*", $tj = 0)
  {
    if ($tj > 0) {
      $where = " and tj=$tj";
    }
    return db()->select_all("type", $field, "`status`=1 and pid=$pid  $where order by sort desc,id desc");
  }
  function post()
  {
    if (empty($_POST['id'])) {
      $_POST['title'] = str_replace("，", ",", $_POST['title']);
      $rows = explode(",", $_POST['title']);
      foreach ($rows as $key => $value) {
        $_POST['title'] = $value;
        db()->insert(MOD, $_POST);
      }
      count($rows) > 1 ? json(0, "批量添加成功") : json(0, "添加成功");
    }
  }

  function del($ids)
  {
    foreach ($ids as $key => $value) {
      $num = db()->count("article", "cid=" . $value);
      if ($num > 0) {
        json(1, "该分类下有相关文章,不能删除");
      }
    }
  }
}
