<?php


namespace app\admin\controller\family;


use app\common\controller\Backend;
use fast\Pinyin;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;

class Surname extends Backend
{

    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\family\Surname();
    }



    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->param("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);

                if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
                    $params[$this->dataLimitField] = $this->auth->id;
                }
                $result = false;
                Db::startTrans();
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
                        $this->model->validateFailException(true)->validate($validate);
                    }
                    $pinyin = new \Overtrue\Pinyin\Pinyin();

                    if(empty($params['SpellCode'])){
                        $SpellCode=$pinyin->convert($params['Surname'],PINYIN_UNICODE);
                        $params['SpellCode']=implode(" ", $SpellCode);
                    }

                    if(empty($params['EnglishName'])){
                        $EnglishName=$pinyin->convert($params['Surname']);
                        $params['EnglishName']=implode("", array_map('ucfirst', $EnglishName));
                    }

                    if(empty($params['EnglishAbb'])){
                        $EnglishAbb=$pinyin->abbr($params['Surname']);
                        $params['EnglishAbb']=strtoupper($EnglishAbb);
                    }

                    $Surname=$this->model->where(['Surname'=>$params['Surname']])->find();
                    if($Surname){
                        $this->error("该姓氏已存在！");
                    }
                    $result = $this->model->allowField(true)->save($params);
                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error(__('No rows were inserted'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            $params = $this->request->param("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);
                $result = false;
                Db::startTrans();
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
                        $row->validateFailException(true)->validate($validate);
                    }
                    $pinyin = new \Overtrue\Pinyin\Pinyin();

                    if(empty($params['SpellCode'])){
                        $SpellCode=$pinyin->convert($params['Surname'],PINYIN_UNICODE);
                        $params['SpellCode']=implode(" ", $SpellCode);
                    }

                    if(empty($params['EnglishName'])){
                        $EnglishName=$pinyin->convert($params['Surname']);
                        $params['EnglishName']=implode("", array_map('ucfirst', $EnglishName));
                    }

                    if(empty($params['EnglishAbb'])){
                        $EnglishAbb=$pinyin->abbr($params['Surname']);
                        $params['EnglishAbb']=strtoupper($EnglishAbb);
                    }

                    $Surname=$this->model->where(['ID'=>['<>',$ids],'Surname'=>$params['Surname']])->find();
                    if($Surname){
                        $this->error("该姓氏已存在！");
                    }
                    $result = $row->allowField(true)->save($params);
                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error(__('No rows were updated'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }
}