';
                    if (is_array($v['update'])) {
                        $result[$k] = '
';
            } elseif (!$col && isset($v['class'])) {
                # 分割线
                $v['name'] = $this->value($v['name'], array('test' => 1));
                if (isset($v['right'])) {
                    $this->card_right = true;
                }
                $this->card = true;
                $result[$k] = Html::hr($v);
            }
        }
        if (Dever::input('ajax') == 1) {
            return implode('', $result);
        }
        return $result;
    }
    public function card()
    {
        if (isset($this->card_right)) {
            return 'layui-col-md6';
        }
        return 'layui-col-md12';
    }
    public function layui_card()
    {
        if (isset($this->card)) {
            return false;
        }
        return true;
    }
    /**
     * update_class
     *
     * @return string
     */
    public function update_class($name, $param, $prefix = '', $data = array())
    {
        $class = '';
        $key = 'show';
        if (isset($param[$key]) && $param[$key]) {
            $parent = array();
            $child = array();
            if (is_string($param[$key])) {
                parse_str($param[$key], $param[$key]);
            }
            if (is_array($param[$key])) {
                foreach ($param[$key] as $k => $v) {
                    if ($k == 'no') {
                        $child[$k . '_' . $v] = $key . '_no';
                    } elseif (strpos($v, '/')) {
                        $parent[$k] = $key . '_' . $k;
                        $k = $k . '_ajax';
                        $value = '';
                        if (isset($data[$name])) {
                            $value = $data[$name];
                        }
                        $v = str_replace('?', '?key='.$name.'&value='.$value.'&source='.$this->name.'&', $v);
                        $child[$k] = $key . '_' . $k .'" dever-ajax="'.Dever::url($v).'"';
                    } else {
                        if (strpos($v, ',')) {
                            $temp = explode(',', $v);
                            foreach ($temp as $v1) {
                                $show = $k . '_' . $v1;
                                if ($prefix) {
                                    $k = str_replace($name, $k, $prefix);
                                    $show = str_replace($name, $show, $prefix);
                                }
                                $parent[$k] = $key . '_' . $k;
                                $child[$show] = $key . '_' . $show;
                            }
                        } else {
                            $show = $k . '_' . $v;
                            if ($prefix) {
                                $k = str_replace($name, $k, $prefix);
                                $show = str_replace($name, $show, $prefix);
                            }
                            $parent[$k] = $key . '_' . $k;
                            $child[$show] = $key . '_' . $show;
                        }
                    }
                }
            }
            $class = implode(' ', $parent) . ' ' . implode(' ', $child);
        } 
        if (isset($param['control'])) {
            $class .= ' ' . $key . '_input';
        }
        return $class;
    }
    /**
     * update_html
     *
     * @return string
     */
    public function create_update_html($key, $value, $data = array(), $index = false, $prefix = '')
    {
        $result = '';
        $value['ext'] = $value['col'] = $key;
        $value['update'] = $value['update'] ? $value['update'] : 'text';
        if (isset($value['match']) && $value['match'] != 'option') {
            if (is_string($value['match']) && strpos($value['match'], '/') !== false) {
                if (strstr($value['match'], 'option||')) {
                    $value['valid'] = 'validate[custom[' . $key . ']]';
                } else {
                    $value['valid'] = 'validate[required,custom[' . $key . ']]';
                }
            } else {
                $value['valid'] = 'validate[required]';
            }
        }
        if (is_string($value['update']) && strpos($value['update'], 'copy.') !== false) {
            $value['value'] = $value['update'];
            $value['update'] = 'hidden';
        }
        if (!$index) {
            $index = $key;
        }
        $this->update_html($value, $key, $index, $data, $result, $prefix);
        return $result;
    }
    /**
     * update_html
     *
     * @return string
     */
    public function update_html($param, $key, $index, $data, &$result, $prefix = '')
    {
        if (isset($param['name']) && isset($param['update']) && $param['update'] != 'hidden' && $param['update'] != 'load') {
            $result .= Html::pageLeft($param);
        }
        if ($param['update']) {
            $order = true;
            # 名称带前缀
            $param['name'] = 'update_' . $param['ext'];
            $param['index'] = $index;
            //$param['value'] = '';
            # 得到当前的值
            //if (isset($data[$index]) && !isset($param['value'])) {
            if (isset($data[$index]) && $data[$index]) {
                $param['value'] = $data[$index];
            }
            # 得到下级的值
            if (isset($param['child_value']) && isset($data['id'])) {
                $param['child_value'] = $this->value($param['child_value'], $data);
            }
            # 验证option是否是匿名函数
            if (isset($param['option'])) {
                $param['option'] = $this->option($param['option']);
            }
            if (isset($data[$index]) && !isset($param['value']) && isset($param['key']) && $param['update'] != 'editor') {
                $param['value'] = Dever::upload($data[$index]);
            }
            # 处理下级字段
            if (isset($param['child'])) {
                $param['child'] = $this->option($param['child']);
                if (isset($data[$key . '_parent'])) {
                    $param['parent_value'] = $data[$key . '_parent'];
                }
            }
            # 设置默认值
            if (isset($param['default'])) {
                $param['default'] = $this->option($param['default']);
            }
            # 处理同步
            if (isset($param['sync']) && strpos($key, '-') && $param['update'] != 'load') {
                $temp = explode('-', $key);
                if (isset($data[$param['sync'][0]])) {
                    if (isset($temp[2])) {
                        $m = 'one';
                    } else {
                        $m = 'select';
                    }
                    if ($data[$param['sync'][0]] > 0) {
                        $load = Dever::load($temp[0] . '/' . $temp[1] . '-' . $m, array('option_' . $param['sync'][1] => $data[$param['sync'][0]], 'order' => array('id' => 'asc')));
                    } else {
                        $load = Dever::load($temp[0] . '/' . $temp[1] . '-' . $m, array('option_' . $param['sync'][3] => $data[$param['sync'][2]], 'option_' . $param['sync'][4] => $data[$param['sync'][0]], 'order' => array('id' => 'asc')));
                    }
                    if ($m == 'one') {
                        $param['value'] = isset($load[$temp[2]]) ? $load[$temp[2]] : $param['default'];
                    } else {
                        $param['value'] = $load;
                    }
                }
                if (!isset($temp[2]) && is_array($param['update'])) {
                    $order = false;
                    if (isset($param['update'][0]) && is_string($param['update'][0]) && strstr($param['update'][0], '.')) {
                        if (isset($param['update'][1]) && $param['update'][1]) {
                            $param['update_load'] = $param['update'][0];
                            $param['update'] = 'load';
                        } else {
                            $param['update'] = Dever::load($param['update'][0]);
                        }
                    } else {
                        $config = Dever::db($temp[0] . '/' . $temp[1])->config;
                        $param['update'] = $config['struct']; 
                    }
                    
                    if (isset($config['manage']['tab'])) {
                        $param['prefix'] = $prefix;
                        $param['tab'] = $config['manage']['tab'];
                        if (isset($config['manage']['tab'])) {
                            $param['lang'] = $config['manage']['lang'];
                        }
                    }
                    //$param['value'] = Dever::db($temp[0] . '/' . $temp[1]);
                }
            }
            $method = $param['update'];
            if (is_array($method)) {
                $c = '';
                if (layadmin()) {
                    $c = 'layui-tab-title';
                }
                $delete = true;
                if (isset($param['option'])) {
                    $delete = false;
                    $result .= '
';
                    $order = array
                    (
                        'col'       => $key,
                        'name'      => $param['desc'],
                        'default'   => '1',
                        'desc'      => $param['desc'],
                        'match'     => 'is_string',
                        'update'    => 'hidden',
                        'option'    => $param['option']
                    );
                    array_unshift($method, $order);
                    if (isset($param['value']) && $param['value']) {
                        $value = Dever::array_decode($param['value']);
                        $param['value'] = array();
                        if (!$value) {
                            foreach ($param['option'] as $k => $v) {
                                if (isset($v['name'])) {
                                    $v = $v['name'];
                                }
                                $param['value'][$v][$key] = $k;
                            }
                        } else {
                            foreach ($param['option'] as $k => $v) {
                                if (isset($v['name'])) {
                                    $v = $v['name'];
                                }
                                foreach ($value[$k-1] as $k1 => $v1) {
                                    $param['value'][$v][$k1] = $v1;
                                }
                            }
                        }
                        
                        
                    } else {
                        $param['value'] = array();
                        foreach ($param['option'] as $k => $v) {
                            if (isset($v['name'])) {
                                $v = $v['name'];
                            }
                            $param['value'][$v][$key] = $k;
                        }
                    }
                } else {
                    $result .= '
';
                    if ($order) {
                        $order = array
                        (
                            'col'       => 'order',
                            'name'      => '排序-按照数字正序排列',
                            'default'   => '1',
                            'desc'      => '排序',
                            'match'     => 'is_string',
                            'update'    => 'text',
                        );
                        array_unshift($method, $order);
                    }
                }
                $tab = '';
                $this->update_child(array(), $method, $key, $result, -1, $param, $delete);
                if (isset($param['value']) && $param['value']) {
                    if (!is_array($param['value'])) {
                        $param['value'] = Dever::array_decode($param['value']);
                    }
                    if (is_array($param['value'])) {
                        $i = 0;
                        foreach ($param['value'] as $k => $v) {
                            $n = $k;
                            if (is_string($k)) {
                                $k = $i;
                            }
                            $this->update_child($v, $method, $key, $result, $k, $param, $delete);
                            $active = '';
                            if ($k == 0) {
                                $active = 'active';
                            }
                            if (layadmin() && $active) {
                                $active .= ' layui-this';
                            }
                            $k = $k + 1;
                            if (!is_string($n)) {
                                $n = '第' . $k . '条';
                            }
                            if (isset($param['option']) && count($param['option']) <= 1) {
                            } else {
                                $tab .= '
'.$n.'';
                                $i++;
                            }
                            
                        }
                    }
                    
                } else {
                    $this->update_child(array(), $method, $key, $result, 0, $param, $delete);
                    $tab = '
第1条';
                }
                if ($tab) {
                    $result = str_replace('{tab}', $tab, $result);
                } else {
                    $result = str_replace('
', '', $result);
                }
                
            } else {
                if (isset($param['update_input']) && isset($data[$index . '_input'])) {
                    $param['update_input_data'] = explode(',', $data[$index . '_input']);
                }
                $param['project'] = $this->project;
                $param['table'] = $this->table;
                if (isset($param['upload_search'])) {
                    $param['upload_search'] = $this->value($param['upload_search'], $data + $param);
                }
                if (isset($param['upload_search_value'])) {
                    $param['upload_search_value'] = $this->value($param['upload_search_value'], $data + $param);
                }
                $param['where_id'] = $this->id;
                $result .= Html::$method($param);
            }
            if (isset($param['desc']) && ($param['update'] == 'text' || $param['update'] == 'password')) {
                $result .= Html::desc($param['desc']);
            }
        }
    }
    /**
     * update_action
     *
     * @return array
     */
    private function update_child($data, $param, $key, &$result, $index, $config, $delete = true)
    {
        if (isset($config['tab']) && $config['tab']) {
            if ($index != 0) {
                $style = 'display:none;';
            } else {
                $style = '';
            }
            $result .= '
';
        } else {
            $style = 'margin:10px;';
            if ($index != 0) {
                $style = 'display:none;margin:10px;';
            }
            $result .= '
';
        }
    }
    private function update_child_data($data, $param, $key, $index, $tab = -1, $config = array())
    {
        $result = '';
        foreach ($param as $k => $v) {
            if (is_string($k)) {
                $v['col'] = $k;
            }
            if ($this->id && $v['col'] == 'id') {
                $v['update'] = 'hidden';
            }
            if (!isset($v['update'])) {
                continue;
            }
            
            if (isset($config['prefix']) && $config['prefix']) {
                if (isset($config['lang']) && $config['lang'] && !in_array($v['col'], $config['lang'])) {
                    continue;
                }
                $v['col'] = $config['prefix'] . '_' . $v['col'];
            }
            $state = false;
            if ($tab == 0) {
                if (!isset($v['tab']) || (isset($v['tab']) && $v['tab'] == $tab)) {
                    $state = true;
                }
            } elseif ($tab > 0) {
                if (isset($v['tab']) && $v['tab'] == $tab) {
                    $state = true;
                }
            } else {
                $state = true;
            }
            if (isset($v['update']) && $v['update'] && $state) {
                
                $v['type'] = $v['update'];
                $prefix = '';
                if ($index < 0) {
                    $v['ext'] = $key . '_c_' . $v['col'];
                    $prefix = $v['ext'];
                } else {
                    $v['ext'] = $key . '_c_' . $index . '_i_' . $v['col'];
                    $prefix = $v['ext'];
                }
                if ($v['col'] == 'order') {
                    $v['default'] = $index+1;
                }
                $class = $this->update_class($v['col'], $v, $prefix);
                if ($v['update'] == 'hidden') {
                    $result .= '
';
                    $this->update_html($v, $key, $v['col'], $data, $result);
                    $result .= '
';
                } else {
                    $result .= '
';
                    $this->update_html($v, $key, $v['col'], $data, $result);
                    $result .= '
';
                }
            }
        }
        return $result;
    }
    /**
     * update_action
     *
     * @return array
     */
    public function update_action_api()
    {
        $method = false;
        $config = $this->config();
        $input = Dever::preInput('update_');
        if ($this->id) {
            # 新增权限
            if (isset($config['manage']['list']) && $config['manage']['list'] == 'update') {
                $auth = 1;
            } else {
                $auth = 3;
            }
            if (Dever::load('manage/auth.oper', $auth) == false) {
                Dever::alert('你没有权限');
            }
            Dever::setInput('where_id', $this->id);
            //Dever::setInput('update_where_id', $this->id);
            $info = $this->load('one', $this->id);
            if ($info) {
                $method = 'update';
                $col = Dever::input('col');
                $value = Dever::input('value');
                if ($col && $value) {
                    $method = 'update_' . $col;
                    //Dever::setInput('set_' . $col, $value);
                    //Dever::setInput('update_' . $col, $value);
                    $param['where_id'] = $this->id;
                    $param['set_' . $col] = $value;
                    $this->load($method, $param);
                    $struct = $this->struct_data($config['struct'], array($col => $value));
                    Log::add($this->project, $this->table, $this->menu_id, '', $method, $struct, $this->id);
                    Dever::out('yes');
                    return;
                }
                $prefix = 'set';
            } else {
                $method = 'insert';
                $prefix = 'add';
            }
        }
        if (!$method) {
            # 新增权限
            if (isset($config['manage']['list']) && $config['manage']['list'] == 'update') {
                $auth = 1;
            } else {
                $auth = 4;
            }
            if (Dever::load('manage/auth.oper', $auth) == false) {
                Dever::alert('你没有权限');
            }
            $method = 'insert';
            $prefix = 'add';
        }
        if ($method && $input) {
            $update = $order = $update_other = array();
            $i = 0;
            $c = array();
            foreach ($input as $k => $v) {
                if ($v === '') {
                    $v = 'null';
                }
                if ($v && is_string($v) && strpos($v, 'copy.') !== false) {
                    $temp = str_replace('copy.', '', $v);
                    if (isset($input['update_' . $temp])) {
                        $v = $input['update_' . $temp];
                    }
                }
                if (strpos($k, '_c_') !== false) {
                    if ($v == 'null') {
                        $v = '';
                    }
                    $temp = explode('_c_', $k);
                    $h = $temp[0];
                    $k = str_replace('update_', $prefix . '_', $h);
                    if (!isset($c[$k])) {
                        $c[$k] = array();
                    }
                    if (strpos($temp[1], '_i_') !== false) {
                        $temp = explode('_i_', $temp[1]);
                        if (!isset($c[$k][$temp[1]])) {
                            $c[$k][$temp[1]] = 0;
                        } else {
                            $c[$k][$temp[1]]++;
                        }
                        $num = $c[$k][$temp[1]];
                        if (strpos($h, '-')) {
                            $h = str_replace('update_', '', $h);
                            if (isset($config['struct'][$h]) && isset($config['struct'][$h]['sync'])) {
                                $s_col = $config['struct'][$h]['sync'][0];
                                $m_col = $config['struct'][$h]['sync'][1];
                                if ($s_col == 'id') {
                                    $s_value = $this->id;
                                } else {
                                    $s_one = $this->load('one', $this->id);
                                    $s_value = $s_one[$s_col];
                                }
                                $t = explode('-', $h);
                                $index = $t[0] . '/' . $t[1];
                                $update_col[$i][$index] = $m_col;
                                $update_set[$i][$index][$num]['set_' . $temp[1]] = $v;
                                $update_add[$i][$index][$num]['add_' . $temp[1]] = $v;
                                $update_add[$i][$index][$num]['add_' . $m_col] = $s_value;
                                $update_id[$i][$index][$num][$m_col] = $s_value;
                                if ($this->id && $temp[1] == 'id') {
                                    $update_id[$i][$index][$num]['id'] = $v;
                                }
                            }
                        } else {
                            $update[$i][$k][$num][$temp[1]] = $v;
                            if ($temp[1] == 'order') {
                                $order[$i][$k][$num] = $v;
                            }
                        }
                    } else {
                        //$update[$i][$k][$c][$temp[1]] = $v;
                    }
                } elseif (strpos($k, '__') !== false) {
                    $temp1 = explode('__', $k);
                    $temp2 = explode('_', $temp1[0]);
                    $i = $temp2[1] - 1;
                    if (strpos($k, 'where_id') !== false) {
                        //$update[$i]['where_id'] = $v;
                    } else {
                        $update[$i]['add_' . $temp1[1]] = $v;
                    }
                } else {
                    $ki = str_replace('update_', $prefix . '_', $k);
                    if (strpos($ki, 'where_id') !== false) {
                        if ($method != 'insert') {
                            $update[$i]['where_id'] = $v;
                        }
                    } else {
                        if (strpos($k, 'l-l')) {
                            $temp = explode('l-l', $k);
                            $k = str_replace('_', '', $temp[2]);
                            $update_other[$i][$temp[1]][$k] = $v;
                        } elseif (strpos($k, '-')) {
                            $k = str_replace('update_', '', $k);
                            if (isset($config['struct'][$k]) && isset($config['struct'][$k]['sync'])) {
                                $s_col = $config['struct'][$k]['sync'][0];
                                $m_col = $config['struct'][$k]['sync'][1];
                                if ($s_col == 'id') {
                                    $s_value = $this->id;
                                } else {
                                    $s_one = $this->load('one', $this->id);
                                    $s_value = $s_one[$s_col];
                                }
                                $temp = explode('-', $k);
                                $index = $temp[0] . '/' . $temp[1];
                                if (isset($temp[2])) {
                                    $update_col[$i][$index] = $m_col;
                                    $update_set[$i][$index][0]['set_' . $temp[2]] = $v;
                                    //$update_set[$i][$index][0]['set_' . $m_col] = $s_value;
                                    $update_add[$i][$index][0]['add_' . $temp[2]] = $v;
                                    $update_add[$i][$index][0]['add_' . $m_col] = $s_value;
                                    $update_id[$i][$index][0][$m_col] = $s_value;
                                }
                            } else {
                                $temp = explode('-', $k);
                                if (isset($temp[2])) {
                                    $k = $temp[0] . '-' . $temp[1];
                                    if (isset($config['struct'][$k]) && isset($config['struct'][$k]['sync'])) {
                                        $s_col = $config['struct'][$k]['sync'][0];
                                        $m_col = $config['struct'][$k]['sync'][1];
                                        if ($s_col == 'id') {
                                            $s_value = $this->id;
                                        } else {
                                            $s_one = $this->load('one', $this->id);
                                            $s_value = $s_one[$s_col];
                                        }
                                        $index = $temp[0] . '/' . $temp[1];
                                        $temp[2] = ltrim($temp[2], '_');
                                        $temp = explode('_', $temp[2]);
                                        $update_col[$i][$index] = $m_col;
                                        $up = array();
                                        $up['add_' . $temp[0] . '_id'] = $temp[1];
                                        $up['add_' . $temp[0] . '_value'] = $v;
                                        $up['add_' . $m_col] = $s_value;
                                        $update_add[$i][$index][] = $up;
                                        $update_delete[$i][$index][][$m_col] = $s_value;
                                    }
                                }
                            }
                        } else {
                            if (is_array($v)) {
                                $k = str_replace('update_', '', $k);
                                $kd = $k . '_id';
                                if (isset($config['struct'][$kd])) {
                                    foreach ($v as $vk => $vt) {
                                        if ($vt == -1) {
                                            if ($vk != 0) {
                                                unset($v[$vk]);
                                            }
                                        }
                                    }
                                    if ($v) {
                                        $vd = end($v);
                                        $kdi = $prefix . '_' . $kd;
                                        $update[$i][$kdi] = $vd;
                                    }
                                }
                                if (isset($config['struct'][$k]) && strpos($config['struct'][$k]['type'], 'int') !== false) {
                                    $v = end($v);
                                }
                            }
                            $update[$i][$ki] = $v;
                        }
                    }
                }
            }
            //print_r($update_set);
            //print_r($update_id);
            //print_r($update_add);die;
            //print_r($input);
            //print_r($update_other);
            //print_r($config['struct']);die;
            # 增加复制的数量
            $copy_num = Dever::input('copy_num');
            if ($copy_num > 0) {
                $update[0]['where_id'] = 0;
                for ($i = 1; $i < $copy_num; $i++) {
                    $update[$i] = $update[0];
                    # 缓存处理
                    $update[$i]['rank'] = $i;
                }
            }
            if ($this->id && !$info) {
                $update[0]['add_id'] = $this->id;
            }
            //print_r($update);die;
            foreach ($update as $k => $v) {
                $method = 'insert';
                $old = array();
                if (isset($v['where_id']) && $v['where_id']) {
                    $method = 'update';
                    $old = $this->load('one', $v['where_id']);
                }
                if (isset($order[$k])) {
                    foreach ($order[$k] as $i => $j) {
                        array_multisort($j, SORT_ASC, $v[$i]);
                    }
                }
                if (isset($v['set_password']) && (!$v['set_password'] || $v['set_password'] == 'null')) {
                    unset($v['set_password']);
                }
                $id = $this->load($method, $v);
                if ($id) {
                    $struct = $this->struct_data($config['struct'], $v, $old);
                    Log::add($this->project, $this->table, $this->menu_id, '', $method, $struct, $id ? $id : $this->id);
                    if ($update_other && isset($update_other[$k])) {
                        foreach ($update_other[$k] as $k1 => $v1) {
                            $v1['id'] = $id;
                            $this->load($method, $v1, $this->table . '__' . $k1);
                        }
                    }
                    if (isset($update_add[$k]) && $update_add[$k]) {
                        foreach ($update_add[$k] as $k1 => $v1) {
                            $col = $update_col[$k][$k1];
                            foreach ($v1 as $k2 => $v2) {
                                if (isset($update_set[$k][$k1][$k2]['set_' . $col])) {
                                    $update_set[$k][$k1][$k2]['set_' . $col] = $id;
                                }
                                if (isset($v2['add_' . $col])) {
                                    $v2['add_' . $col] = $id;
                                }
                                $check = false;
                                if (isset($update_id[$k][$k1][$k2])) {
                                    if (isset($update_id[$k][$k1][$k2][$col])) {
                                        $update_id[$k][$k1][$k2][$col] = $id;
                                    }
                                    $check = Dever::load($k1 . '-one', $update_id[$k][$k1][$k2]);
                                } elseif (isset($update_delete[$k][$k1][$k2])) {
                                    # 清理
                                    Dever::load($k1 . '-delete', $update_delete[$k][$k1][$k2]);
                                }
                                
                                if ($check) {
                                    unset($update_set[$k][$k1][$k2]['set_id']);
                                    $update_set[$k][$k1][$k2]['where_id'] = $check['id'];
                                    Dever::load($k1 . '-update', $update_set[$k][$k1][$k2]);
                                } else {
                                    Dever::load($k1 . '-insert', $v2);
                                }
                            }
                        }
                    }
                    $this->spec($id, $config);
                }
            }
        }
        Dever::out('yes');
    }
    private function spec($id, $config)
    {
        if (isset($config['manage']['sku'])) {
            $spec_table = $config['manage']['sku']['spec'];
            $sku_table = $config['manage']['sku']['sku'];
        }
        if (!isset($sku_table)) {
            $sku_table = 'info_sku';
        }
        $sku_table = $this->project . '/' . $sku_table;
        $spec_type = Dever::input('update_spec_type');
        if ($spec_type == 2) {
            $spec = Dever::input('spec');
            $sku = Dever::input('skus');
            if ($spec && $sku) {
                if (!isset($spec_table)) {
                    $spec_table = 'info_spec';
                }
                $spec_table = $this->project . '/' . $spec_table;
                $spec_value_table = $spec_table . '_value';
                $spec = Dever::json_decode($spec);
                if ($spec) {
                    $sku_id = array();
                    Dever::db($spec_table)->updates(array('option_info_id' => $id, 'set_state' => 2));
                    Dever::db($spec_value_table)->updates(array('option_info_id' => $id, 'set_state' => 2));
                    foreach ($spec as $k1 => $v1) {
                        if (is_numeric($v1['id'])) {
                            $spec_id = Dever::db($spec_table)->update(array('where_id' => $v1['id'], 'set_state' => 1));
                        } else {
                            $spec_id = Dever::db($spec_table)->insert(array('name' => $v1['title'], 'info_id' => $id));
                        }
                        foreach ($v1['child'] as $k2 => $v2) {
                            $v2['checked'] = $v2['checked'] ? 1 : 2;
                            if (is_numeric($v2['id'])) {
                                Dever::db($spec_value_table)->update(array('where_id' => $v2['id'], 'set_state' => 1, 'set_is_checked' => $v2['checked']));
                            } else {
                                $sku_id[$v2['id']] = Dever::db($spec_value_table)->insert(array('value' => $v2['title'], 'is_checked' => $v2['checked'], 'info_id' => $id, 'spec_id' => $spec_id));
                            }
                        }
                    }
                    Dever::db($spec_table)->delete(array('info_id' => $id, 'state' => 2));
                    Dever::db($spec_value_table)->delete(array('info_id' => $id, 'state' => 2));
                    $this->sku($id, $sku_table, $sku, $sku_id);
                }
            }
        } else {
            $sku = Dever::input('skus');
            if ($sku) {
                $this->sku($id, $sku_table, $sku);
            }
        }
    }
    private function sku($id, $sku_table, $sku, $sku_id = false)
    {
        Dever::db($sku_table)->updates(array('option_info_id' => $id, 'set_state' => 2));
        foreach ($sku as $k1 => $v1) {
            if ($sku_id) {
                foreach ($sku_id as $k2 => $v2) {
                    $k1 = str_replace($k2, $v2, $k1);
                }
            }
            $sku_data = array();
            $sku_data['info_id'] = $id;
            $sku_data['key'] = $k1;
            $sku_info = Dever::db($sku_table)->one($sku_data);
            foreach ($v1 as $k3 => $v3) {
                $sku_data[$k3] = $v3;
            }
            if ($sku_info) {
                $sku_data['where_id'] = $sku_info['id'];
                $sku_data['state'] = 1;
                Dever::db($sku_table)->update($sku_data);
            } else {
                Dever::db($sku_table)->insert($sku_data);
            }
        }
        Dever::db($sku_table)->delete(array('info_id' => $id, 'state' => 2));
    }
    public function struct_data($struct, $data, $old = array())
    {
        $log = $tlog = array();
        foreach ($struct as $i => $j) {
            /*
            if ($i == 'reorder' || $i == 'id') {
                continue;
            }
            */
            if (isset($j['list_name'])) {
                $j['name'] = $j['list_name'];
            } elseif (strpos($j['name'], '-')) {
                $temp = explode('-', $j['name']);
                $j['name'] = $temp[0];
            }
            if (isset($j['list_table'])) {
                $j['list'] = $j['list_table'];
                if ($j['list'] == false) {
                    $data[$i] = false;
                }
            }
            $list = (isset($j['list']) && is_string($j['list']) && $j['list'] != 'table');
            $update = true;
            if ($old && isset($j['update'])) {
                $update = (is_string($j['update']) && $j['update'] != 'linkage');
            }
            if (isset($data['where_' . $i]) && $data['where_' . $i]) {
                $log[$j['name']] = $data['where_' . $i];
            } elseif (isset($data['set_' . $i]) && $data['set_' . $i]) {
                $log[$j['name']] = $data['set_' . $i];
            } elseif (isset($data['add_' . $i]) && $data['add_' . $i]) {
                $log[$j['name']] = $data['add_' . $i];
            } elseif (isset($data[$i]) && $data[$i]) {
                if ($i == 'id') {
                    $log[$j['name']] = $data[$i];
                } else {
                    $log[$j['name']] = $list ? $this->value($j['list'], $data) : $data[$i];
                }
            }
            if (isset($log[$j['name']])) {
                $tlog[$j['name']] = $log[$j['name']];
                if (is_array($tlog[$j['name']])) {
                    if (isset($tlog[$j['name']][0]) && is_array($tlog[$j['name']][0])) {
                        $tlog[$j['name']] = json_encode($tlog[$j['name']]);
                        if (isset($old[$i]) && is_array($old[$i])) {
                            $old[$i] = json_encode($old[$i]);
                        }
                    } elseif (isset($tlog[$j['name']][0])) {
                        $tlog[$j['name']] = implode(',', $tlog[$j['name']]);
                        if (isset($old[$i]) && is_array($old[$i])) {
                            $old[$i] = implode(',', $old[$i]);
                        }
                    }
                } elseif (strpos($log[$j['name']], 'http://wx.qlogo.cn/') !== false || strpos($log[$j['name']], '.jpg') !== false || strpos($log[$j['name']], '.gif') !== false || strpos($log[$j['name']], '.png') !== false) {
                    $log[$j['name']] = '

';
                    if (isset($old[$i]) && $old[$i]) {
                        $old[$i] = Dever::pic($old[$i]);
                        $old[$i] = '

';
                    }
                } elseif (is_numeric($log[$j['name']]) && (strpos($i, 'time') !== false || strpos($i, 'date') !== false)) {
                    $log[$j['name']] = date("Y-m-d H:i:s", $log[$j['name']]);
                    if (isset($old[$i]) && $old[$i]) {
                        $old[$i] = date("Y-m-d H:i:s", $old[$i]);
                    }
                } elseif (isset($j['decode'])) {
                    $log[$j['name']] = Dever::array_decode($log[$j['name']]);
                    if (isset($old[$i]) && $old[$i]) {
                        $old[$i] = Dever::array_decode($old[$i]);
                    }
                }
                
                if (isset($j['option']) && !$list && $update) {
                    $name = array();
                    $old_name = array();
                    $option = $this->option($j['option']);
                    if (is_array($option)) {
                       foreach ($option as $a => $b) {
                            if (!is_array($log[$j['name']])) {
                                if ($a == $log[$j['name']]) {
                                    $name[] = (is_array($b) && isset($b['name'])) ? $b['name'] : $b;
                                }
                            } elseif (in_array($a, $log[$j['name']])) {
                                $name[] = (is_array($b) && isset($b['name'])) ? $b['name'] : $b;
                            }
                            if ($old && isset($old[$i])) {
                                if (!is_array($old[$i])) {
                                    if ($a == $old[$i]) {
                                        $old_name[] = (is_array($b) && isset($b['name'])) ? $b['name'] : $b;
                                    }
                                } elseif (in_array($a, $old[$i])) {
                                    $old_name[] = (is_array($b) && isset($b['name'])) ? $b['name'] : $b;
                                }
                            }
                        }
                    }
                    
                    $log[$j['name']] = implode(',', $name);
                    if ($old_name) {
                        $old[$i] = implode(',', $old_name);
                    }
                }
                if ($log[$j['name']] == 'null') {
                    unset($log[$j['name']]);
                }
                if ($old && strstr($i, 'area')) {
                    $old[$i] = Dever::load('area/api')->string($old[$i]);
                    if (isset($log[$j['name']]) && $log[$j['name']]) {
                        $log[$j['name']] = Dever::load('area/api')->string($log[$j['name']]);
                    }
                }
                if (isset($log[$j['name']]) && isset($old[$i]) && $old[$i] != $tlog[$j['name']] && $old[$i] != $log[$j['name']]) {
                    $log[$j['name']] = array('更新前' => $old[$i], '更新后' => $log[$j['name']]);
                } elseif ($old) {
                    //unset($log[$j['name']]);
                }
            }
        }
        return $log;
    }
    /**
     * delete_action
     *
     * @return array
     */
    public function delete_action_api()
    {
        if (Dever::load('manage/auth.oper', 5) == false) {
            Dever::alert('您没有权限');
        }
        $config = $this->config();
        if (isset($config['struct']['state']) && (!isset($config['manage']['delete']) || (isset($config['manage']['delete']) && $config['manage']['delete']))) {
            $name = '逻辑删除';
            $this->load('update', array('where_id' => $this->id, 'state' => 2));
        } else {
            $name = '物理删除';
            $this->load('delete', $this->id);
        }
        Log::add($this->project, $this->table, $this->menu_id, '', 3, array('ID' => $this->id, '方式' => $name), $this->id);
        return 'reload';
        return 'reloaderror';
    }
    /**
     * recovery_action
     *
     * @return array
     */
    public function recovery_action_api()
    {
        if (Dever::load('manage/auth.oper', 5) == false) {
            Dever::alert('您没有权限');
        }
        $this->load('update', array('where_id' => $this->id, 'state' => 1));
        Log::add($this->project, $this->table, $this->menu_id, '', 4, array('ID' => $this->id, '方式' => '逻辑恢复'), $this->id);
        return 'reload';
    }
    /**
     * list_data
     *
     * @return array
     */
    public function list_data()
    {
        $config = $this->config();
        $data = array();
        if (isset($config['manage']['data']) && $config['manage']['data']) {
            foreach ($config['manage']['data'] as $k => $v) {
                if (is_object($v[1])) {
                    $function = $v[1];
                    $v[1] = $function();
                }
                $link = '';
                if (isset($v[2]) && $v[2]) {
                    $link = 'project/database/list?project=' . $this->project . '&table=' . $this->table . '&' . $v[2];
                    $link = 'href="' . Dever::url($link, 'manage') . '"';
                }
                $data[] = '
                '.$v[0].'
                  '.$v[1].'
';
            }
        }
        
        return $data;
    }
    /**
     * list_data
     *
     * @return array
     */
    public function list_data_show()
    {
        $config = $this->config();
        if (isset($config['manage']['data']) && $config['manage']['data']) {
            return 'display:block';
        }
        
        return 'display:none';
    }
    /**
     * chart
     *
     * @return array
     */
    public function chart($config, $key, $data, $struct)
    {
        if (!$this->chart) {
            $this->chart = array();
            $this->chart['x'] = array();
            $this->chart['data'] = array();
        }
        if ($config['x'] == $key) {
            $this->chart['x'][] = $data;
        } elseif (in_array($key, $config['data'])) {
            if (!isset($this->chart['data'][$key])) {
                $this->chart['data'][$key] = array
                (
                    'name' => $struct['name'],
                    'type' => 'line',
                    'stack' => 'Total',
                    'data' => array(),
                );
            }
            $this->chart['data'][$key]['data'][] = $data;
        }
    }
    /**
     * chart_data
     *
     * @return array
     */
    public function list_chart()
    {
        $config = $this->config();
        $chart = '';
        if (isset($config['manage']['chart']) && $this->chart) {
            $this->chart['x'] = array_reverse($this->chart['x']);
            $x = Dever::json_encode($this->chart['x']);
            $this->chart['data'] = array_values($this->chart['data']);
            foreach ($this->chart['data'] as $k => $v) {
                $this->chart['data'][$k]['data'] = array_reverse($this->chart['data'][$k]['data']);
            }
            $data = Dever::json_encode($this->chart['data']);
            $echarts = Dever::assets('lib/echarts/echarts.common.min.js', 'script');
            $chart = '";
        }
        
        return $chart;
    }
    /**
     * chart_data
     *
     * @return array
     */
    public function list_chart_show()
    {
        $config = $this->config();
        if (isset($config['manage']['chart'])) {
            return 'display:block';
        }
        
        return 'display:none';
    }
    /**
     * stat 为主动统计提供相关数据
     *
     * @return array
     */
    public function stat()
    {
        //bundle 包依赖工具
        $config = $this->config();
        if (empty($config['manage']['stat'])) {
            Dever::alert('该项目主动统计功能未开放');
        }
        $data = $config['manage'];
        $data['table'] = '';
        $method = $this->project . '/' . $this->table . '-all';
        $gp = Dever::input('gp', 'month,day');
        $col = is_string($gp) ? explode(',', $gp) : $gp;
        $data['search'] = $this->stat_search($config['struct'], $gp, $config['manage']['stat'], $config['manage']['search']);
        foreach ($col as $k => $v) {
            $this->stat_data($data, $config['struct'], $v, $method);
        }
        return $data;
    }
    /**
     * stat 为主动统计提供导出数据 后续优化
     *
     * @return array
     */
    public function stat_excel()
    {
        Dever::$global['base']['excel'] = 1;
        //print_r($data['thead']);die;
        $data = $this->stat();
        if ($data && isset($data['list'])) {
            $xls = new \PHPExcel();
            $xls->getProperties()->setCreator("Maze Stat Excel")
                ->setLastModifiedBy("Maze Stat Excel")
                ->setTitle("Office 2007 XLSX Maze Document")
                ->setSubject("Office 2007 XLSX Maze Document")
                ->setDescription("Dever Stat Excel")
                ->setKeywords("Dever Stat Excel")
                ->setCategory("Dever Stat Excel");
            $info = $this->info();
            $name = $info['list_header'] . '的' . $info['stat_desc'] . '-' . $info['project_name'];
            $start = 65;
            $s = 0;
            foreach ($data['thead'] as $k => $v) {
                if ($s > 0) {
                    $xls->createSheet();
                }
                $act = $xls->setActiveSheetIndex($s);
                $act->setTitle($v[0]);
                $s++;
                $i = 0;
                foreach ($v as $kj => $vj) {
                    $num = 1;
                    $cell = $this->_getChr($start, $i);
                    $act->setCellValue($cell . '' . $num, $vj);
                    $act->getColumnDimension($cell)->setWidth(20);
                    $i++;
                    if (isset($data['list'][$k])) {
                        foreach ($data['list'][$k] as $ki => $vi) {
                            if (isset($vi[$kj])) {
                                $num += 1;
                                $act->setCellValue($cell . $num, $vi[$kj]);
                            }
                        }
                    }
                }
            }
            //$act->getStyle('A1:'.$k.'20')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);   // 垂直居中
            //$act->getStyle('A1:'.$k.'20')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);// 水平居中
            $xls->setActiveSheetIndex(0);
            $filename = $name . '.xlsx';
            header('Pragma: public');
            header('Expires: 0');
            header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
            header('Content-Type:application/force-download');
            header('Content-Type:application/vnd.ms-execl');
            header('Content-Type:application/octet-stream');
            header('Content-Type:application/download');
            header("Content-Disposition:attachment;filename='" . $filename . "'");
            header('Content-Transfer-Encoding:binary');
            $write = \PHPExcel_IOFactory::createWriter($xls, 'Excel2007');
            $write->save('php://output');
        }
    }
    /**
     * stat_search
     *
     * @return mixed
     */
    private function stat_search($struct, $col, $stat, $search)
    {
        unset($struct['id']);
        unset($struct['cdate']);
        if ($stat) {
            $stat = explode(',', $stat);
            foreach ($stat as $k => $v) {
                if (isset($struct[$v])) {
                    unset($struct[$v]);
                }
            }
        }
        $struct['day']['name'] = '天';
        $struct['month']['name'] = '月';
        $struct['year']['name'] = '年';
        $param['update'] = 'checkbox';
        $param['name'] = 'gp';
        $param['value'] = $col;
        $param['option'] = $struct;
        $html = '
';
        return $html;
    }
    /**
     * stat_data
     *
     * @return mixed
     */
    private function stat_data(&$data, $struct, $col, $method)
    {
        Dever::setInput('group', $col);
        switch ($col) {
            case 'year':
                $name = '按年统计';
                break;
            case 'month':
                $name = '按月统计';
                break;
            case 'day':
                $name = '按天统计';
                break;
            default:
                $name = $struct[$col]['name'];
                break;
        }
        $list = Dever::load($method, array('group' => $col, 'col' => 'count(1) as total,id'));
        $data['table'] .= '
';
        $data['table'] .= '| ' . $name . ' | 统计数字 | 
|---|
';
        $data['thead'][$col] = array($name, '统计数字');
        if ($list) {
            $data['table'] .= '';
            $total = 0;
            foreach ($list as $k => $v) {
                if (isset($v[$col])) {
                    if (isset($struct[$col]['option'])) {
                        $option = $this->option($struct[$col]['option']);
                        if (isset($option[$v[$col]])) {
                            $v[$col] = $option[$v[$col]];
                            if (is_array($v[$col])) {
                                $v[$col] = $v[$col]['name'];
                            }
                        }
                    }
                    $data['table'] .= '| ' . $v[$col] . ' | ' . $v['total'] . ' | 
';
                    $total += $v['total'];
                    $data['list'][$col][] = array($v[$col], $v['total']);
                }
            }
            $data['table'] .= '';
            if ($total > 0 && $k > 0) {
                $data['table'] .= '| 汇总 | ' . $total . ' | 
';
                $data['list'][$col][] = array('汇总', $total);
            }
        }
        $data['table'] .= '
';
    }
    /**
     * load
     *
     * @return mixed
     */
    private function load($method = 'one', $param = array(), $table = false)
    {
        if (!$table) {
            $table = $this->table;
        }
        return Dever::load($this->project . '/' . $table . '-' . $method, $param);
    }
    /**
     * __call
     *
     * @return object
     */
    public function __call($method, $param)
    {
        if (strpos($method, 'html_') !== false) {
            return Html::text($param);
        }
        return $this;
    }
    /**
     * option
     *
     * @return string
     */
    private function option($option, $value = false)
    {
        if (is_object($option)) {
            $function = $option;
            $option = $function();
        }
        if ($value && is_array($value)) {
            $state = false;
            foreach ($value as $k => $v) {
                if ($v === true) {
                    $state = true;
                    break;
                }
            }
            if ($state) {
                $option = array_intersect_key($option, $value);
            } else {
                $option = $value;
            }
        }
        return $option;
    }
    /**
     * option
     *
     * @return string
     */
    public function manage()
    {
        $config = $this->config();
        return $config;
    }
    /**
     * valid
     *
     * @return string
     */
    public function valid()
    {
        //http://placehold.it/350x150
        $config = $this->config();
        if (isset($config['struct'])) {
            $rule = array();
            $rule['required']['regex'] = 'none';
            $rule['required']['alertText'] = '此项不能为空';
            $rule['required']['alertTextCheckboxMultiple'] = '请选择一个选项';
            $rule['required']['alertTextCheckboxe'] = '请选择一个选项';
            $rule['required']['alertTextDateRange'] = '日期范围不可空白';
            foreach ($config['struct'] as $k => $v) {
                if (isset($v['match']) && $v['match'] != 'option' && is_string($v['match']) && strpos($v['match'], '/') !== false) {
                    if (strstr($v['match'], 'option||')) {
                        $v['match'] = str_replace('option||', '', $v['match']);
                    }
                    $rule[$k]['regex'] = $v['match'];
                    $rule[$k]['alertText'] = $v['desc'];
                }
            }
        }
        $script = '';
        //print_r($script);die;
        return $script;
    }
    /**
     * save
     *
     * @return string
     */
    public function save()
    {
        $config = $this->config();
        if (isset($config['manage']['save'])) {
            return 'yes';
        }
        return 'no';
    }
}