<?php
/**
 * 供应商管理模块
 * 
 * @author Daniel Chen
 * @date 2014.05.15
 */
class Merchant_DistributorController extends Ec_Controller_Action {
  public function preDispatch() {
    $this->tplDirectory = 'merchant/views/distributor/';
  }

  /**
   * 供应商列表Action
   */
  public function listAction() {
    $this->view->code = '';
    $this->view->name = '';
    $this->view->status = -1;
    $this->view->total = 0;
    $page = intval($this->_request->getParam('page', 1));
    $size = intval($this->_request->getParam('size', 20));
    $size = ($size < 1) ? 20 : (($size > 1000) ? 1000 : $size);

    if ($this->_request->isPost()) {
      $params = $this->_request->getPost();
      $code = trim($params['distributor_code']);
      $name = trim($params['distributor_name']);
      $status = intval($params['distributor_status']);
      $condition = array();

      if (!empty($code)) {
        $condition['distributor_code'] = $code;
        $this->view->code = $code;
      }

      if (!empty($name)) {
        $condition['distributor_name'] = $name;
        $this->view->name = $name;
      }

      if (in_array($status, array(0, 1), TRUE)) {
        $condition['distributor_status'] = $status;
        $this->view->status = $status;
      }

      $total = Service_Distributor::getCountByCustomerCode($this->_customerAuth['code'], $condition);
      $maxPage = ceil($total / $size);
      $data = Service_Distributor::getByCustomerCode($this->_customerAuth['code'], $condition, $page, $size);
    }
    else {
      $total = Service_Distributor::getCountByCustomerCode($this->_customerAuth['code']);
      $maxPage = ceil($total / $size);
      $data = Service_Distributor::getByCustomerCode($this->_customerAuth['code'], array(), $page, $size);
    }

    $page = ($page < 1) ? 1 : (($page > $maxPage) ? $maxPage : $page);
    $this->view->total = $total;
    $this->view->page = $page;
    $this->view->size = $size;
    $this->view->data = $data;

    echo Ec::renderTpl($this->tplDirectory . 'list.tpl', 'noleftlayout');
  }

  /**
   * 供应商数据添加Action
   */
  public function addAction() {
    $this->view->tip = '';
    $this->view->message = '';
    $this->view->code = '';
    $this->view->name = '';
    $this->view->status = 1;

    if ($this->_request->isPost()) {
      $params = $this->_request->getPost();
      $code = trim($params['distributor_code']);
      $name = trim($params['distributor_name']);
      $status = intval(trim($params['distributor_status']));
      $message = array();

      if (empty($code)) {
        array_push($message, '供应商代码不能为空');
      }
      elseif (Service_Distributor::isRepeatCode($code, $this->_customerAuth['code'])) {
        array_push($message, '供应商代码【' . $code . '】已经存在');
      }

      if (empty($name)) {
        array_push($message, '供应商名称不能为空');
      }

      if (!in_array($status, array(0, 1), TRUE)) {
        $status = 1;
      }

      if (!empty($message)) {
        $this->view->code = $code;
        $this->view->name = $name;
        $this->view->status = $status;
        $this->view->tip = 'error';
        $this->view->message = implode('；', $message);
      }
      else {
        $data = array(
          'distributor_code' => $code,
          'distributor_name' => $name,
          'distributor_status' => $status,
          /* 好吧，增加了子账号，这里只能改变策略，用0表示主账号，否则为子账号的ID */
          'distributor_creator' => $this->_customerAuth['is_master'] ? 0 : $this->_customerAuth['account_id'],
          'distributor_created' => date('Y-m-d H:i:s'),
          'customer_code' => $this->_customerAuth['code']
        );

        if (Service_Distributor::insert($data)) {
          $this->view->tip = 'info';
        }
        else {
          $this->view->tip = 'error';
          $this->view->message = '数据无法保存，请联系系统管理员或稍后再试';
        }
      }
    }

    echo Ec::renderTpl($this->tplDirectory . 'add.tpl', 'noleftlayout');
  }

  /**
   * 供应商数据编辑Action
   */
  public function editAction() {
    $distributorId = intval($this->_request->getParam('id', 0));
    $data = Service_Distributor::getByPrimaryKey($distributorId);

    if (!$distributorId || empty($data)) {
      $this->_redirect('/merchant/distributor/list');
      exit();
    }

    $this->view->tip = '';
    $this->view->message = '';
    $this->view->distributorId = $distributorId;
    $this->view->code = $data['distributor_code'];
    $this->view->name = $data['distributor_name'];
    $this->view->status = $data['distributor_status'];

    if ($this->_request->isPost()) {
      $params = $this->_request->getPost();
      $code = trim($params['distributor_code']);
      $name = trim($params['distributor_name']);
      $status = intval(trim($params['distributor_status']));
      $message = array();

      if (empty($code)) {
        array_push($message, '供应商代码不能为空');
      }
      elseif (Service_Distributor::isRepeatCode($code, $this->_customerAuth['code'], $distributorId)) {
        array_push($message, '供应商代码【' . $code . '】已经存在');
      }

      if (empty($name)) {
        array_push($message, '供应商名称不能为空');
      }

      if (!in_array($status, array(0, 1), TRUE)) {
        $status = 1;
      }

      $this->view->code = $code;
      $this->view->name = $name;
      $this->view->status = $status;

      if (!empty($message)) {
        $this->view->tip = 'error';
        $this->view->message = implode('；', $message);
      }
      else {
        $data = array(
          'distributor_code' => $code,
          'distributor_name' => $name,
          'distributor_status' => $status,
          /* 好吧，增加了子账号，这里只能改变策略，用0表示主账号，否则为子账号的ID */
          'distributor_modifier' => $this->_customerAuth['is_master'] ? 0 : $this->_customerAuth['account_id'],
          'distributor_modified' => date('Y-m-d H:i:s')
        );

        if (Service_Distributor::update($data, $distributorId)) {
          $this->view->tip = 'info';
        }
        else {
          $this->view->tip = 'error';
          $this->view->message = '数据无法保存，请联系系统管理员或稍后再试';
        }
      }
    }

    echo Ec::renderTpl($this->tplDirectory . 'edit.tpl', 'noleftlayout');
  }

  /**
   * 供应商详情Action
   */
  public function viewAction() {
    $distributorId = intval($this->_request->getParam('id', 0));
    $data = Service_Distributor::getByPrimaryKey($distributorId);

    if (!$distributorId || empty($data)) {
      $this->_redirect('/merchant/distributor/list');
      exit();
    }

    $this->view->distributorId = $data['distributor_id'];
    $this->view->code = $data['distributor_code'];
    $this->view->customerCode = $data['customer_code'];
    $this->view->name = $data['distributor_name'];
    $this->view->status = $data['distributor_status'];
    $this->view->creator = '';
    $this->view->created = $data['distributor_created'];
    $this->view->modifier = '';
    $this->view->modified = '';

    if ($data['distributor_creator']) { // 这是子账号创建的
      $account = Service_Account::getByCondition(array('account_id' => $data['distributor_creator']));
      $this->view->creator = empty($account) ? '' : $account['account_name'];
    }
    else { // 主账号就直接提取customer_code就可以了
      $this->view->creator = $data['customer_code'];
    }

    if ($data['distributor_modifier']) { // 这是子账号修改的
      $account = Service_Account::getByCondition(array('account_id' => $data['distributor_modifier']));
      $this->view->modifier = empty($account) ? '' : $account['account_name'];
    }
    elseif ('0000-00-00 00:00:00' !== $data['distributor_modified']) { // 竟然存在修改时间，那肯定是主账号干的
      $this->view->modifier = $data['customer_code'];
    }

    if ('0000-00-00 00:00:00' !== $data['distributor_modified']) {
      $this->view->modified = $data['distributor_modified'];
    }

    echo Ec::renderTpl($this->tplDirectory . 'detail.tpl', 'noleftlayout');
  }

  /**
   * 查找供应商相关数据AJAX请求，用于【产品管理】“产品新增”
   */
  public function ajaxAction() {
    $json = array('status' => FALSE, 'data' => array(), 'message' => 'ERROR_INVALID_REQUEST');

    if ($this->_request->isPost()) {
      $type = trim($this->_request->getPost('type', 0));

      if (1 == $type) {
        $code = trim($this->_request->getPost('code', ''));
        $data = array();

        if (!empty($code)) {
          $data = Service_Distributor::getByCode($code, $this->_customerAuth['code']);
        }

        if (!empty($data)) {
          $json = array(
            'status' => TRUE,
            'data' => array(
              'distributor_id' => $data['distributor_id'],
              'distributor_name' => $data['distributor_name'],
              'distributor_code' => $data['distributor_code']
            ),
            'message' => 'INFO_SUCCESS'
          );
        }
        else {
          $json['message'] = 'INFO_NO_DATA';
        }
      }
      elseif (2 == $type) {
        $code = trim($this->_request->getPost('code', ''));
        $name = trim($this->_request->getPost('name', ''));
        $page = intval($this->_request->getPost('page', 1));
        $size = intval($this->_request->getPost('size', 20));
        $total = 0;
        $condition = array(
          'distributor_code' => $code,
          'distributor_name' => $name,
          'distributor_status' => 1
        );
        $table = '<tr><th style="width:30%;">供应商代码</th><th style="width:50%;">供应商名称</th><th style="width:20%;">使用</th></tr>';
        $total = Service_Distributor::getCountByCustomerCode($this->_customerAuth['code'], $condition);
        $page = ($page < 0) ? 1 : ($page > ceil($total / $size) ? ceil($total / $size) : $page);
        $data = Service_Distributor::getByCustomerCode($this->_customerAuth['code'], $condition, $page, $size);

        if (empty($data)) {
          $table .= '<tr class="even"><td colspan="3" style="text-align:center;">没有查找到与条件相匹配的数据</td></tr>';
        }
        else {
          foreach ($data as $index => $item) {
            if (0 == $index % 2) {
              $table .= '<tr class="even">';
            }
            else {
              $table .= '<tr class="odd">';
            }

            $table .= '<td style="text-align:center;">' . $item['distributor_code'] . '</td>';
            $table .= '<td>' . $item['distributor_name'] . '</td>';
            $table .= '<td style="text-align:center;"><input type="button" class="button" value="选择"';
            $table .= ' onclick="selectDistributor(' . $item['distributor_id'];
            $table .= ', \'' . htmlspecialchars($item['distributor_code']) . '\', \'';
            $table .= htmlspecialchars($item['distributor_name']) . '\');" />';
          }
        }

        $json = array(
          'status' => TRUE,
          'data' => array(
            'total' => $total,
            'page' => $page,
            'size' => $size,
            'html' => $table
          ),
          'message' => 'INFO_REQUEST_SUCCESS'
        );
      }
    }

    exit(Zend_Json::encode($json));
  }
}
