_service = 'ums'; $this->_module = 'contact'; $this->_viewname = 'contact'; $this->_title = 'Contact'; $this->_base_url = '/contact'; $this->_listtype = 'ajaxlist'; } public function list(Request $request) { $fields = $this->get_list_fields(); return $this->view($this->_listtype)->with([ 'title' => $this->_title, 'fields' => $fields, 'base_url' => $this->_base_url, //'items' => $response->data->items, 'buttons' => $this->get_list_buttons() ]); } protected function get_list_buttons() { return [ ['title' => 'Add New', 'link' => $this->_base_url.'/addnew' ], ['title' => 'Import Users', 'link' => $this->_base_url.'/import'] ]; } protected function get_import_fields($records){ $response = LambdaRequest::signedrequest('get', 'contact', 'contactattribute/get_all'); $hierar = $this->getlambda('get', 'ums', 'hierarchy', 'dropdown'); $hierar_dd = []; foreach($hierar->data->items as $hier){ $hierar_dd[$hier->code] = $hier->name . ' (' . $hier->code . ')'; } //echo '
'; print_r($response); exit;
        $fields = [
            'name' => ['title' => 'Name', 'type' => 'text', 'required' => true],
            'email' => ['title' => 'Email', 'type' => 'email', 'required' => true],
            'phone' => ['title' => 'Phone', 'type' => 'text', 'required' => true],
            'msl_code' => ['title' => 'MSL Code', 'type' => 'text'],
            'position' => ['title' => 'Position', 'type' => 'select', 'required' => true, 'values' => $hierar_dd ],
            'status' => ['title' => 'Status', 'type' => 'select', 'required' => true, 'values' => ['active' => 'Active', 'disabled' => 'Disabled'] ],
        ];

        foreach ($response->data->items as $item) {
            $values = [];
            if(in_array($item->type, ['select', 'radio', 'checkbox']) ) {
                foreach($item->options as $op) {
                    $values[$op->value] = $op->display;
                }
            }
            $fields[$item->code] = ['title' => $item->attribute, 'type' => $item->type, 'required' => $item->required=='yes'?true:false, 'values' => $values];
        }

        return $fields;
    }

    // public function list(Request $request) {
    //     $response = LambdaRequest::signedrequest('get', $this->_service, $this->_module.'/get_all');
    //     //echo '
'; print_r($items); exit;
    //     $items = [];
    //     foreach ($response->data->items as $item) {
    //         $item->position = $item->hierarchy_info->code;
    //         unset($item->hierarchy_info);
    //         $items[] = $item;
    //     }
        
    //     $fields = $this->get_list_fields();
    //     return $this->view('list')->with([
    //         'title' => $this->_title,
    //         'fields' => $fields,
    //         'base_url' => $this->_base_url,
    //         'items' => $items,
    //         'buttons' => $this->get_list_buttons()
    //     ]);
    // }

    protected function get_list_fields(){
        return [
            'actions' => ['title' => 'Actions', 'buttons' => [ 'edit', 'delete' ] ],
            'name' => ['title' => 'Name', 'type' => 'text'],
            'msl_code' => ['title' => 'MSL Code', 'type' => 'text'],
            'position_name' => ['title' => 'Position', 'type' => 'text'],
            'user_name' => ['title' => 'Rep Name', 'type' => 'text'],
            'status' => ['title' => 'Status', 'type' => 'select'],
        ];
    }

    protected function get_form_fields($type, $values){
        $response = LambdaRequest::signedrequest('get', 'contact', 'contactattribute/get_all');
        $hierar = $this->getlambda('get', 'ums', 'hierarchy', 'dropdown');
        $hierar_dd = [];
        foreach($hierar->data->items as $hier){
            $hierar_dd[$hier->code] = $hier->name . ' (' . $hier->code . ')';
        }

        //echo '
'; print_r($response); exit;
        $fields = [
            'name' => ['title' => 'Name', 'type' => 'text', 'required' => true],
            'email' => ['title' => 'Email', 'type' => 'email', 'required' => true],
            'phone' => ['title' => 'Phone', 'type' => 'text', 'required' => true],
            'msl_code' => ['title' => 'MSL Code', 'type' => 'text'],
            'position' => ['title' => 'Position', 'type' => 'select', 'required' => true, 'values' => $hierar_dd ],
            'status' => ['title' => 'Status', 'type' => 'select', 'required' => true, 'values' => ['active' => 'Active', 'disabled' => 'Disabled'] ],
        ];

        foreach ($response->data->items as $item) {
            $values = [];
            if(in_array($item->type, ['select', 'radio', 'checkbox']) ) {
                foreach($item->options as $op) {
                    $values[$op->value] = $op->display;
                }
            }
            $fields[$item->code] = ['title' => $item->attribute, 'type' => $item->type, 'required' => $item->required=='yes'?true:false, 'values' => $values];
        }

        return $fields;
    }

    protected function _beforesave($type, $request) {
        $data = $request->except('_token');
        $data['entity_id'] = $request->session()->get('entity_id');
        $options = [];
        if(isset($data['option_value'])) {
            foreach($data['option_value'] as $k => $ov) {
                $options[] = ['value' => $ov, 'display' => $data['option_name'][$k] ];
            }
            $data['options'] = $options;
            unset($data['option_value']);
            unset($data['option_name']);
        }
        return $data;
    }

    public function editall(Request $request) {
        $response = LambdaRequest::signedrequest('get', $this->_service, $this->_module.'/get_my_contacts');
        //echo '
'; print_r($response); exit;
        $fields = $this->get_list_fields();
        return $this->view('editall')->with([
            'title' => $this->_title,
            'fields' => $response->data->attributes,
            'base_url' => $this->_base_url,
            'items' => $response->data->items
        ]);
    }


    // public function postimport(Request $request) {
    //     if( $request->file('importfile') ) {
    //         $path = $request->file('importfile')->store('user_import');
    //         $request->session()->put('user_import_path', $path);

    //         $csv = Reader::createFromPath(storage_path('app') .'/' . $path, 'r');
    //         $csv->setHeaderOffset(0);

    //         $header = $csv->getHeader(); //returns the CSV header record
    //         $records = $csv->getRecords(); //returns all the CSV records as an Iterator 
    //         $contacts = [];
    //          echo '
';
    //         foreach($records as $record) {
    //             $contacts[] = (array)$record;
    //         }

    //         $created = 0;
    //         $updated = 0;
    //         $count = 0;
    //         $total = sizeof($contacts);

    //         while($count < $total) {
    //             $tempdata = array_slice($contacts, $count, 20);
    //             Log::debug(print_r($tempdata, true));
    //             $response = LambdaRequest::signedrequest('post', $this->_service, $this->_module.'/import', $tempdata);
    //             Log::debug(print_r($response, true));
    //             $count = $count + 20;
    //             $created = $created + $response->data->created_count;
    //             $updated = $updated + (isset($response->data->updated_count) ? $response->data->updated_count : 0);
    //             Log::debug(print_r(['$count' => $count, '$created' => $created, '$updated' => $updated], true));
    //         }

    //         //echo '
'; print_r($response); exit;
    //         if($response->statusCode == 200 && $response->status == "success") {
    //             return redirect($this->_base_url.'s')->withMessage('Successfully Imported ' .$created. ' records and Updated ' .$updated. ' records out of ' .$total. ' records!!!');
    //         } else {
    //             return \Redirect::back()->withMessage('Some Error occured!');
    //         }


    //         exit;
    //         return redirect($this->_base_url.'/importmap')->withMessage('Successfully Uploaded !!!');
    //     } else {
    //         return \Redirect::back()->withMessage('Some Error occured!');
    //     }
    // }
}