<?php
defined('BASEPATH') or exit('No direct script access allowed');

$aColumns = [
    'property_id'
];

$sIndexColumn = 'id';
$sTable = db_prefix() . 'propertybase_properties_leads';
$join = [];
$where = [];

array_push($where, 'AND lead_id=' . $leadid);

$result = data_tables_init($aColumns, $sIndexColumn, $sTable, $join, $where, [
    db_prefix() . 'propertybase_properties_leads.id'
]);

$output = $result['output'];
$rResult = $result['rResult'];


foreach ($rResult as $aRow) {
    $row = [];
    for ($i = 0; $i < count($aColumns); $i++) {

        $CI = &get_instance();
        $CI->load->model('propertybase_model');

        $getPropertyData = $CI->propertybase_model->get($aRow['property_id']);
        $assetMainImages = $CI->propertybase_model->get_property_main_image_attachment($aRow['property_id']);

        $row[] = $aRow['id'];

        $url = admin_url(PROPERTYBASE_MODULE_NAME . '/view/' . $aRow['property_id']);
        $imageCss = "max-height: 90px;max-width: 90px;";

        $assetImage = '<img style="'.$imageCss.'" class="mtop5 img img-responsive text-center" src="' . substr(module_dir_url(PROPERTYBASE_MODULE_NAME . '/uploads/default_image.jpg'), 0, -1) . '">';
        if (!empty($assetMainImages)) {
            $renderedImage = substr(module_dir_url(PROPERTYBASE_MODULE_NAME . '/uploads/property_images/main_image/' . $aRow['property_id'] . '/' . $assetMainImages[0]['file_name']), 0, -1);

            $assetImage = '<img style="'.$imageCss.'" class="mtop5 img img-responsive text-center" src="' . $renderedImage . '">';
        }
        $row[] = $assetImage;

        $asset = '<a href="' . $url . '">' . $getPropertyData->name . '</a>';

        $asset .= '<div class="row-options">';
        $asset .= '<a href="' . admin_url(PROPERTYBASE_MODULE_NAME . '/view/' . $aRow['property_id']) . '">' . _l('view') . '</a>';
        $asset .= '<a href="' . admin_url(PROPERTYBASE_MODULE_NAME . '/create/' . $aRow['property_id']) . '"> | ' . _l('edit') . '</a>';

        $asset .= '</div>';

        $row[] = $asset;

        if (!empty($getPropertyData->selected_agent)) {
            $row[] = '<a href="' . admin_url('staff/profile/' . $getPropertyData->selected_agent) . '">' . staff_profile_image($getPropertyData->selected_agent, [
                    'staff-profile-image-small',
                ]) . ' ' . get_staff_full_name($getPropertyData->selected_agent) . '</a>';
        } else {
            $row[] = '';
        }
        $row[] = $getPropertyData->created_at;
    }

    $output['aaData'][] = $row;
}
