<?php
/**
 * @link      http://www.u-bo.com
 * @copyright 南京友博网络科技有限公司
 * @license   http://www.u-bo.com/license/
 */

namespace home\modules\customer\models;

use Yii;
use yii\db\Query;
use yii\db\ActiveRecord;
use common\components\Tool;
use common\components\Tree;
use common\models\TreeList;

/**
 * 客户类别表 ({{%customer_category}})
 *
 * 客户类别模型
 *
 *@property integer  $cat_id       (客户类别ID)
 *@property string   $name         (客户类别名称)
 *@property integer  $parent_id    (父级ID)
 *@property string   $parent_ids   (父级ID集)
 *@property string   $child_ids    (子ID集)
 *@property integer  $sort         (排序)
 *@property smallint $status       (状态：0 禁用 1开启 99逻辑删除 默认开启)
 *@property integer  $user_id      (创建人ID，关联表qc_user)
 *@property integer  $create_time  (创建时间)
 *@property integer  $update_time  (更新时间)
 *@property string   $ip           (创建人IP)
 *
 * @author zealxd http://blog.u-bo.com
 * @since 0.1
 */
class Category extends TreeList
{
    const STATUS_ENABLE  = 1;//启用
    const STATUS_DISABLE = 0;//禁用

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%customer_category}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'sort', 'status'], 'required'],
            [['parent_id', 'sort', 'status', 'user_id', 'create_time', 'update_time'], 'integer'],
            [['name'], 'string', 'max' => 50],
            [['parent_ids', 'child_ids'], 'string', 'max' => 255],
            [['ip'], 'string', 'max' => 15]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'cat_id'      => 'ID',
            'name'        => '类别名称',
            'parent_id'   => '父级id',
            'parent_ids'  => '父级所有id',
            'child_ids'   => '子级所有id',
            'sort'        => '排序',
            'status'      => '状态',
            'user_id'     => '创建人id',
            'create_time' => '创建时间',
            'update_time' => '更新时间',
            'ip'          => 'ip地址',
        ];
    }


    /**
     * 列表页项目类别树形结构列表显示
     *
     * @param  integer $curCat 默认选择节点
     * @param  [type]  $where  关键字搜索
     * @return array   $list   列表内容
     */
    public static function getIndexTree($curCat = 0, $where = [], $showFirst = 0)
    {
        $str = "<tr>
            <td>\$checkbox</td>
            <td class='key_num'></td>
            <td class='theleft'>\$spacer\$name  </td>
            <td>\$create_time</td>
            <td align='center' style='vertical-align:middle'>\$str_status</td>
            <td align='center'>\$str_operation</td>
            </tr>";
        return parent::getIndexTreeList(0, [], $str,'cat_id', 466, 194, $showFirst);
    }

    /**
     * 生成项目类别下拉框
     *
     * @param  integer $curCat      默认选择节点
     * @param  integer $disabledId  忽略节点
     * @return array   $list        上级类别下拉框内容
     */
    public static function getDropDownTree($curCat = 0, $disabledId = 0,$isNewRecord = 'true', $pk = 'child_ids')
    {
        return parent::getDropDownTreeList($curCat, $disabledId, 'cat_id', $isNewRecord, $pk);
    }

    /**
     * 返回状态列表
     */
    public static function getStatusList()
    {
        return [
            self::STATUS_ENABLE => '启用',
            self::STATUS_DISABLE => '禁用',
        ];
    }

    /**
     * 插入更新前置操作
     */
    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            //判断是否是插入操作
            if ($this->isNewRecord) {
                $this->user_id =  Yii::$app->user->identity->user_id;
                $this->ip = Yii::$app->getRequest()->getUserIP();
                $this->create_time = time();
            } else {
                $this->update_time = time();
            }
            return true;
        } else {
            return false;
        }
    }

    /**
     * 更新后更新项目类别表
     */
    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes);
        //更新父级ID集，子集ID集
        parent::repairTree(self::tableName());
        //若用户修改了项目类别的状态则更新其子项目类别状态
        if ($this->status!='' && $this->status != $changedAttributes['status']) {
            parent::updateStatus(self::tableName(), $this->cat_id);
        }
        //检查更新项目类别禁用状态
        parent::updateStatusFalse(self::tableName());
    }

    /**
     * 删除后更新项目类别表
     */
    public function afterDelete()
    {
        parent::afterDelete();
        parent::repairTree(self::tableName());
    }

}
