<?php namespace Manage\Api\Page;
use Dever;
use Manage\Lib\Page;
ini_set("memory_limit", -1);
set_time_limit(0);
# 更新页
class Update extends Page
{
    public function __construct($load = '', $input = true)
    {
        parent::__construct('update', $load, $input);
    }
    public function get(&$value = array(), &$option = array())
    {
        $func = $this->checkFunc();
        $remote = $show = $spec = $source = $default = array();
        $data['update'] = $data['field'] = $data['option'] = array();
        $this->setting('field', $data['update'], true, 'text');
        foreach ($data['update'] as $k => $v) {
            if ($v['type'] == 'tree' || $v['type'] == 'upload' || $v['type'] == 'cascader' || $v['type'] == 'checkbox' || isset($v['multiple'])) {
                if (isset($v['value']) && $v['value']) {
                    $v['value'] = explode(',', $v['value']);
                    foreach ($v['value'] as $k1 => $v1) {
                        if (is_numeric($v1)) {
                            $v['value'][$k1] = (float) $v1;
                        }
                    }
                } else {
                    $v['value'] = array();
                }
            }
            if (isset($v['source'])) {
                $source[$v['key']] = $v['source'];
            }
            if (isset($v['remote'])) {
                $remote[$v['key']] = array($k, $v['remote'], $v['key']);
                if (isset($v['remote_default']) && !$v['remote_default']) {
                    unset($remote[$v['key']][2]);
                }
            }
            if (isset($v['spec_data'])) {
                $spec[$v['key']] = array($k, $v['spec_data'], $v['key'] . '_spec', $v['spec'], $v['spec_field']);
            }
            if (isset($v['show']) && is_string($v['show'])) {
                $show[$v['key']] = array($k, $v['show']);
            }
            if (isset($v['default']) && is_string($v['default']) && strstr($v['default'], '{')) {
                $default[$v['key']] = array($k, $v['default']);
            }
            $data['field'][$v['key']] = $v['value'];
            if ($v['type'] == 'sku') {
                $data['field'][$v['key'] . '_spec'] = [];
            }
            if (isset($v['option'])) {
                $data['option'][$v['key']] = $v['option'];
                unset($data['update'][$k]['option']);
            }
        }
        $active = $this->column($data);
        $data['info_id'] = false;
        if (!$this->info && $active) {
            $this->info = $this->db->find($active);
            if ($this->info) {
                $data['info_id'] = $this->info['id'];
                if (!$func) {
                    $func = $this->getFunc('edit', '编辑', 1);
                    if (!$func && $this->menu && $this->menu['show'] == 1) {
                        Dever::error('无操作权限');
                    }
                }
            }
        } elseif (!$func) {
            $func = $this->getFunc('update', '更新', 1);
            if (!$func && $this->menu && $this->menu['show'] == 1) {
                Dever::error('无操作权限');
            }
        }
        if ($this->info) {
            $info = $this->info;
            $this->setInfo($info, $data, $remote, $show, $source, $default, 1, $this->config['field']);
            if ($spec) {
                foreach ($spec as $k => $v) {
                    $data['update'][$spec[$k][0]]['remote'] = Dever::url($spec[$k][1], array('value' => '', 'table' => $this->db->config['load'], 'id' => false));
                    $result = Dever::call($spec[$k][1], array($spec[$k][3], $spec[$k][4], $this->info['id']));
                    if ($result) {
                        $data['field'][$spec[$k][2]] = $result;
                    }
                }
            }
        } elseif ($value) {
            $field = array();
            if (isset($this->config['field']) && $this->config['field']) {
                foreach ($this->config['field'] as $k => $v) {
                    if (isset($v['field'])) {
                        $field[$v['field']] = $v;
                        if (!isset($field[$v['field']]['index'])) {
                            $field[$v['field']]['index'][] = $k;
                        }
                    } else {
                        $field[$k] = $v;
                    }
                }
            }
            foreach ($value as $k => &$v) {
                $this->setInfo($v, $data, $remote, $show, $source, $default, 2, $field);
                $option[$k] = $data['option'];
            }
            if (isset($data['reset'])) {
                foreach ($data['reset'] as $k1 => $v1) {
                    if (isset($data['option'][$k1])) {
                        unset($data['option'][$k1]);
                        $data['field'][$k1] = $v1;
                    }
                }
            }
        } else {
            if ($remote) {
                $info = array();
                foreach ($remote as $k => $v) {
                    $data['update'][$v[0]]['remote'] = Dever::url($v[1], array('value' => '', 'table' => $this->db->config['load'], 'id' => false));
                    if (isset($v[2]) && isset($data['option'][$v[2]]) && $data['option'][$v[2]] && $m = Dever::issets($data['option'][$v[2]][0], 'id')) {
                        $result = Dever::call($v[1], array($m, $this->db->config['load'], false), 'api');
                        if ($result) {
                            $this->setUpdate($info, $data, $result);
                        }
                    }
                }
            }
            if ($show) {
                foreach ($show as $k => $v) {
                    $data['update'][$v[0]]['show'] = true;
                }
            }
        }
        
        $data['desc'] = $this->config['desc'] ?? '';
        $data['drag'] = $this->config['drag'] ?? false;
        $this->layout($data);
        $this->control($data);
        $this->tab($data, 'step');
        if (!$data['step']) {
            $this->tab($data);
        }
        return $data;
    }

    private function setInfo(&$info, &$data, $remote, $show, $source, $default, $type = 1, $field = array())
    {
        if ($source) {
            foreach ($source as $k => $v) {
                $t = array();
                foreach ($v as $v1) {
                    $t[] = $info[$v1] ?? '';
                }
                $info[$k] = implode(',', $t);
            }
        }
        foreach ($info as $k => $v) {
            if ($v == null) {
                $v = '';
            }
            if (isset($data['field'][$k])) {
                if (is_array($data['field'][$k])) {
                    if ($v) {
                        $v = explode(',', $v);
                        foreach ($v as $k1 => $v1) {
                            if (is_numeric($v1)) {
                                $v[$k1] = (float) $v1;
                            }
                        }
                    } else {
                        $v = array();
                    }
                    $info[$k] = $v;
                }
                if (isset($field[$k]) && isset($field[$k]['update'])) {
                    $v = $field[$k]['update'];
                }
                if ($k == 'cdate') {
                   $field[$k]['type'] = 'date';
                }
                if (isset($field[$k]) && isset($field[$k]['type']) && $field[$k]['type'] == 'date' && $v) {
                    $v = date('Y-m-d H:i:s', $v);
                }
                if ($type == 1) {
                    $data['field'][$k] = $v;
                }
                if (isset($remote[$k])) {
                    $data['update'][$remote[$k][0]]['remote'] = Dever::url($remote[$k][1], array('value' => '', 'table' => $this->db->config['load'], 'id' => false));
                    if ($field[$k]['type'] == 'cascader' && !isset($field[$k]['option'])) {
                        
                    } else {
                        $result = Dever::call($remote[$k][1], array($v, $this->db->config['load'], $info['id'] ?? false), 'api');
                        if ($result) {
                            $this->setUpdate($info, $data, $result);
                        }
                    }
                }
                if (isset($show[$k])) {
                    $data['update'][$show[$k][0]]['show'] = $this->getShow($show[$k][1], $info);
                    $info[$k] = $data['update'][$show[$k][0]]['show'];
                }
            }
            if (isset($field[$k]) && isset($field[$k]['index'])) {
                foreach ($field[$k]['index'] as $v1) {
                    $info[$v1] = $v;
                }
            }
        }
        if ($default) {
            foreach ($default as $k => $v) {
                $data['update'][$v[0]]['value'] = $this->getShow($v[1], $info);
                $info[$k] = $data['field'][$k] = $data['update'][$v[0]]['value'];
            }
        }
    }

    private function setUpdate(&$info, &$data, $result, $remote = array())
    {
        foreach ($data['update'] as $k => $v) {
            if (isset($result[$v['key']])) {
                # 批量更新时,默认数据需要重置
                if (!isset($data['reset'][$v['key']])) {
                    $data['reset'][$v['key']] = $data['field'][$v['key']];
                }
                if (isset($result[$v['key']]['option'])) {
                    $data['option'][$v['key']] = $result[$v['key']]['option'];
                    unset($result[$v['key']]['option']);
                }
                if (empty($data['field'][$v['key']]) && isset($result[$v['key']]['value'])) {
                    if (is_array($data['field'][$v['key']]) && !is_array($result[$v['key']]['value'])) {
                        $data['field'][$v['key']] = explode(',', $result[$v['key']]['value']);
                    } else {
                        $data['field'][$v['key']] = $result[$v['key']]['value'];
                    }
                }
                if (isset($result[$v['key']]['set']) && $info) {
                    $info = array_merge($info, $result[$v['key']]['set']);
                }
                $data['update'][$k] = array_merge($data['update'][$k], $result[$v['key']]);
            }
        }
    }

    private function control(&$data)
    {
        $data['control'] = array();
        if (isset($this->config['control']) && $data['control'] = $this->config['control']) {
            foreach ($data['control'] as $k => $v) {
                foreach ($data['update'] as $k1 => $v1) {
                    if ($v1['key'] == $k) {
                        $show = true;
                        foreach ($v as $k2 => $v2) {
                            if (is_array($v2)) {
                                $temp = false;
                                foreach ($v2 as $k3 => $v3) {
                                    if (is_array($data['field'][$k2]) && in_array($v3, $data['field'][$k2])) {
                                        $temp = true;
                                    } elseif ($data['field'][$k2] == $v3) {
                                        $temp = true;
                                    }
                                }
                                $show = $temp;
                            } else {
                                if (is_array($data['field'][$k2]) && !in_array($v2, $data['field'][$k2])) {
                                    $show = false;
                                } elseif ($data['field'][$k2] != $v2) {
                                    $show = false;
                                }
                            }
                        }
                        $data['update'][$k1]['show'] = $show;
                    }
                } 
            }
        }
    }

    private function tab(&$data, $type = 'tab')
    {
        $field = $this->input('field', '');
        $data[$type] = array();
        //if (empty($data['layout']) && !$field && isset($this->config[$type])) {
        if (empty($data['layout']) && isset($this->config[$type])) {
            foreach ($this->config[$type] as $k => $v) {
                if (is_string($v)) {
                    $field = array();
                    $data[$type][] = array
                    (
                        'name' => $k,
                        'update' => $this->getUpdate($v, $data['update'], $field),
                        'field' => $field,
                    );
                } else {
                    $field = array();
                    $result = array();
                    $result['name'] = $k;
                    foreach ($v as $v1) {
                        $result['layout'][] = $this->getUpdate($v1, $data['update'], $field);
                    }
                    $result['field'] = $field;
                    $data[$type][] = $result;
                }
            }
            $data['update'] = array();
        }
    }

    private function layout(&$data)
    {
        $field = $this->input('field', '');
        $data['layout'] = array();
        if (!$field && isset($this->config['layout'])) {
            foreach ($this->config['layout'] as $k => $v) {
                $field = array();
                $data['layout'][] = $this->getUpdate($v, $data['update'], $field);
            }
            $data['update'] = array();
        }
    }

    private function getUpdate($set, $update, &$field)
    {
        $result = array();
        if (is_string($set)) {
            $set = explode(',', $set);
            foreach ($set as $k => $v) {
                foreach ($update as $value) {
                    if ($value['key'] == $v) {
                        $result[] = $value;
                        $field[] = $v;
                    }
                }
            }
        } else {
            foreach ($set as $k => $v) {
                foreach ($update as $value) {
                    if ($value['key'] == $k) {
                        $result[] = array('span' => $v, 'update' => array($value));
                        $field[] = $k;
                    }
                }
            }
        }
        return $result;
    }

    public function do_commit(){}
    public function do()
    {
        $this->checkFunc();
        $update = array();
        $this->setting('field', $update, true, 'text');
        if ($update) {
            $data = $other = $sku = array();
            $input = Dever::input();
            $id = false;
            if (isset($input['id']) && $input['id'] > 0) {
                $id = $input['id'];
            }
            foreach ($update as $k => $v) {
                if (isset($input[$v['key']])) {
                    if (isset($v['rules'])) {
                        $this->checkRules($v, $input[$v['key']]);
                    }
                    if ($v['type'] == 'sku') {
                        if (isset($input[$v['key'] . '_spec']) && isset($input[$v['key']])) {
                            $sku[$v['key']] = array($v['where'], $v['content']['field'], $v['spec'], $v['spec_field'], $input[$v['key'] . '_spec'], $input[$v['key']]);
                        }
                        
                    } elseif (strpos($v['key'], '/') && $v['type'] != 'hidden') {
                        if (isset($v['field'])) {
                            $value = $input[$v['key']] ?? false;
                            if (strpos($v['key'], '#')) {
                                $v['key'] = str_replace('#', '', $v['key']);
                            }
                            $other_id = $input[$v['key'] . '_id'] ?? 0;
                            $value = array
                            (
                                0 => array('id' => $other_id, $v['field'] => $value)
                            );
                            if (isset($other[$v['key']])) {
                                $other[$v['key']][3][0] += $value[0];
                            } else {
                                $other[$v['key']] = array($v['where'], false, false, $value);
                            }
                        } else {
                            $other[$v['key']] = array($v['where'], $v['content']['field'], $v['content']['drag'], $input[$v['key']]);
                        }
                    } else {
                        $this->doData($data, $v['key'], $input[$v['key']], $this->config['field']);
                    }
                } elseif ($id) {
                    $data[$v['key']] = '';
                }
                if (isset($data[$v['key']]) && !$data[$v['key']] && isset($v['empty']) && !$v['empty']) {
                    unset($data[$v['key']]);
                }
            }
            if (!$data && !$other && !$sku) {
                Dever::error('无效数据');
            }
            if ($data) {
                $this->exists($id, $data, $this->config['field']);
                $result = $this->start($id, $data);
                if ($result == 'end') {
                    return '操作成功';
                }
                if ($id) {
                    $info = $this->db->find($id);
                    if ($info) {
                        $state = $this->db->update($info['id'], $data);
                        if ($state) {
                            $id = $info['id'];
                        }
                    } else {
                        $data['id'] = $id;
                        $id = $this->db->insert($data);
                    }
                } else {
                    $id = $this->db->insert($data);
                }
            }
            if (!$id) {
                Dever::error('操作失败');
            }
            $this->other($id, $data, $other);
            $this->sku($id, $data, $sku);
            $this->end($id, $data);
            return '操作成功';
        }
    }

    private function doData(&$data, $key, $value, $field = array())
    {
        if (is_array($value)) {
            if (isset($value[0])) {
                $value = ltrim(implode(',', $value), ',');
            } else {
                $value = Dever::json_encode($value);
            }
        }
        if (isset($field[$key]) && isset($field[$key]['field'])) {
            $key = $field[$key]['field'];
        }
        if ($value && isset($field[$key]) && $handle = Dever::issets($field[$key], 'handle')) {
            $value = Dever::call($handle, array($value));
            if (is_array($value) && isset($value[$key])) {
                foreach ($value as $k => $v) {
                    $data[$k] = trim($v);
                }
                return;
            }
        } elseif (isset($field[$key]) && isset($field[$key]['type']) && $field[$key]['type'] == 'date' && $value) {
            $value = \Dever\Helper\Date::mktime($value);
        }
        $data[$key] = trim($value);
    }

    private function exists($id, $data, $field)
    {
        if (isset($this->config['check']) && $this->config['check']) {
            $check = explode(',', $this->config['check']);
            $where = array();
            $name = array();
            foreach ($check as $k => $v) {
                if (isset($data[$v]) && $data[$v]) {
                    if (isset($field[$v]) && isset($field[$v]['name'])) {
                        $n = $field[$v]['name'];
                    } elseif (isset($this->db->config['struct'][$v])) {
                        $n = $this->db->config['struct'][$v]['name'];
                    } else {
                        $n = $v;
                    }
                    $where[$v] = $data[$v];
                    $name[] = $n;
                }
            }
            if ($where) {
                if ($id) {
                    $where['id'] = array('!=', $id);
                }
                $info = $this->db->find($where);
                if ($info) {
                    $name = implode('、', $name);
                    Dever::error($name . '已存在');
                }
            }
        }
    }

    private function start($id, &$data)
    {
        if (isset($this->config['start']) && $this->config['start']) {
            $data['id'] = $id;
            if (is_array($this->config['start'])) {
                $result = $data;
                foreach ($this->config['start'] as $k => $v) {
                    $result = Dever::call($v, array($this->db, $result));
                }
            } else {
                $result = Dever::call($this->config['start'], array($this->db, $data));
            }
            if ($result) {
                if ($result == 'end') {
                    return $result;
                }
                if (is_object($result)) {
                    $this->db = $result;
                } else {
                    $data = $result;
                }
            }
        }
    }

    private function end($id, $data)
    {
        if (isset($this->config['end']) && $this->config['end']) {
            $data['id'] = $id;
            if (is_array($this->config['end'])) {
                foreach ($this->config['end'] as $k => $v) {
                    Dever::call($v, array($this->db, $data));
                }
            } else {
                Dever::call($this->config['end'], array($this->db, $data));
            }
        }
    }

    private function other($rid, $data, $other)
    {
        if ($other) {
            foreach ($other as $k => $v) {
                if (strpos($k, '#')) {
                    $k = str_replace('#', '', $k);
                }
                $set = new Update($k, false);
                $common = $v[0];
                $update = $v[1];
                $drag = $v[2];
                $input = $v[3];
                $value = array();
                foreach ($input as $k1 => $v1) {
                    if (isset($v1['id']) && $v1['id']) {
                        $value['id'] = $v1['id'];
                    }
                    foreach ($common as $k2 => $v2) {
                        if (!is_array($v2)) {
                            if ($v2 == 'id') {
                                $value[$k2] = $rid;
                            } elseif (isset($data[$v2])) {
                                $value[$k2] = $data[$v2];
                            } else {
                                $value[$k2] = $v2;
                            }
                        }
                    }
                    if ($update) {
                        foreach ($update as $k2 => $v2) {
                            if (isset($v1[$k2])) {
                                $this->doData($value, $k2, $v1[$k2], $set->config['field']);
                            } else {
                                $value[$k2] = '';
                            }
                        }
                    } else {
                        $value += $v1;
                    }
                    if ($drag) {
                        $value[$drag] = $k1+1;
                    }
                    if (isset($value['id']) && $value['id'] > 0) {
                        $id = $value['id'];
                        unset($value['id']);
                        Dever::db($k)->update($id, $value);
                    } else {
                        Dever::db($k)->insert($value);
                    }
                }
            }
        }
    }

    private function sku($rid, $data, $sku)
    {
        if ($sku) {
            if (isset($data['spec_type']) && $data['spec_type'] <= 2) {
                return;
            }
            foreach ($sku as $k => $v) {
                if (strpos($k, '#')) {
                    $k = str_replace('#', '', $k);
                }
                $common = $v[0];
                $update = $v[1];
                $spec_table = $v[2];
                $spec_value_table = $spec_table . '_value';
                $spec_field = $v[3];
                $spec = $v[4];
                $input = $v[5];
                $value = $spec_value = array();
                Dever::db($spec_table)->update(array($spec_field => $rid), array('state' => 2));
                Dever::db($spec_value_table)->update(array($spec_field => $rid), array('state' => 2));
                foreach ($spec as $k1 => &$v1) {
                    $spec_data = array();
                    $spec_data['state'] = 1;
                    $spec_data[$spec_field] = $rid;
                    $spec_data['name'] = $v1['name'];
                    $spec_data['sort'] = $k1+1;
                    if (isset($v1['id']) && $v1['id']) {
                        Dever::db($spec_table)->update($v1['id'], $spec_data);
                    } else {
                        $v1['id'] = Dever::db($spec_table)->insert($spec_data);
                    }
                    if ($v1['id']) {
                        foreach ($v1['value'] as $k2 => &$v2) {
                            $spec_value_data = array();
                            $spec_value_data['state'] = 1;
                            $spec_value_data[$spec_field] = $rid;
                            $spec_value_data['spec_id'] = $v1['id'];
                            $spec_value_data['value'] = $v2['value'] ?? $v2['name'];
                            $spec_value_data['sort'] = $k2+1;
                            $spec_value_data['is_checked'] = $v2['checked'] == 'true' ? 1 : 2;
                            if (isset($v2['id']) && $v2['id']) {
                                Dever::db($spec_value_table)->update($v2['id'], $spec_value_data);
                            } else {
                                $v2['id'] = Dever::db($spec_value_table)->insert($spec_value_data);
                            }
                            $spec_value[$v1['key']][$spec_value_data['value']] = array($v2['id'], $spec_data['sort']);
                        }
                    }
                }
                Dever::db($spec_table)->delete(array($spec_field => $rid, 'state' => 2));
                Dever::db($spec_value_table)->delete(array($spec_field => $rid, 'state' => 2));
                Dever::db($k)->update(array($spec_field => $rid), array('state' => 2));
                foreach ($input as $k1 => $v1) {
                    if (isset($v1['id'])) {
                        $value['id'] = $v1['id'];
                    }
                    foreach ($common as $k2 => $v2) {
                        if (!is_array($v2)) {
                            if ($v2 == 'id') {
                                $value[$k2] = $rid;
                            } elseif (isset($data[$v2])) {
                                $value[$k2] = $data[$v2];
                            } else {
                                $value[$k2] = $v2;
                            }
                        }
                    }
                    foreach ($update as $k2 => $v2) {
                        if (isset($v1[$k2])) {
                            $value[$k2] = $v1[$k2];
                        }
                    }
                    $value['key'] = array();
                    foreach ($v1 as $k2 => $v2) {
                        if (isset($spec_value[$k2]) && isset($spec_value[$k2][$v2])) {
                            $value['key'][$spec_value[$k2][$v2][1]] = $spec_value[$k2][$v2][0];
                        }
                    }
                    if ($value['key']) {
                        $value['key'] = implode(',' , $value['key']);
                    }
                    $value['state'] = 1;
                    if (isset($value['id']) && $value['id'] > 0) {
                        $id = $value['id'];
                        unset($value['id']);
                        Dever::db($k)->update($id, $value);
                    } else {
                        Dever::db($k)->insert($value);
                    }
                }
                Dever::db($k)->delete(array($spec_field => $rid, 'state' => 2));
            }
        }
    }
}