<?php
declare (strict_types = 1);

namespace app\admin\controller\shop;

use app\common\controller\Backend;
use think\annotation\route\Group;
use think\annotation\route\Route;
use app\admin\traits\Actions;
use think\facade\Db;
use app\common\model\Customer as CustomerModel;

#[Group("shop/customer")]
class Customer extends Backend
{
    use Actions;

    protected function _initialize()
    {
        parent::_initialize();
        $this->model = new CustomerModel();
    }

    /**
     * 添加
     */
    #[Route('GET,POST','add')]
    public function add()
    {
        if (false === $this->request->isPost()) {
            return $this->fetch();
        }
        $params = array_merge($this->request->post("row/a"),$this->postParams);
        if (empty($params)) {
            $this->error(__('提交的参数不能为空'));
        }
        if(!$this->request->checkToken('__token__',['__token__'=>$this->request->post('__token__')])){
            $this->error(__('token错误，请刷新页面重试'));
        }
        foreach ($params as &$value){
            if(is_array($value)){
                $value=implode(',',$value);
            }
            if($value===''){
                $value=null;
            }
        }
        if($params['account_term'] == 2 && empty($params['cycle'])) $this->error(__('请选择周结日期'));
        $result = false;
        Db::startTrans();
        try {
            $result = $this->model->save($params);
            if($this->callback){
                $callback=$this->callback;
                $callback($this->model);
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if ($result === false) {
            $this->error(__('没有新增任何数据'));
        }
        $this->success();
    }

    /**
     * 编辑
     */
    #[Route('GET,POST','edit')]
    public function edit(mixed $row=null)
    {
        $ids = $this->request->get('ids');
        if(!$row || is_array($row)){
            $row = $this->model->find($ids);
        }
        if (!$row) {
            $this->error(__('没有找到记录'));
        }
        if(count($this->volidateFields)>0){
            foreach ($this->volidateFields as $field=>$value){
                if($row[$field]!=$value){
                    $this->error(__('没有操作权限'));
                }
            }
        }
        if (false === $this->request->isPost()) {
            $this->assign('row', $row);
            return $this->fetch();
        }
        $params = array_merge($this->request->post("row/a"),$this->postParams);
        if (empty($params)) {
            $this->error(__('提交的参数不能为空'));
        }
        if(!$this->request->checkToken('__token__',['__token__'=>$this->request->post('__token__')])){
            $this->error(__('token错误，请刷新页面重试'));
        }
        foreach ($params as &$value){
            if(is_array($value)){
                $value=implode(',',$value);
            }
            if($value===''){
                $value=null;
            }
        }
        if($params['account_term'] == 2 && empty($params['cycle'])) $this->error(__('请选择周结日期'));
        $result = false;
        Db::startTrans();
        try {
            $result = $row->save($params);
            if($this->callback){
                $callback=$this->callback;
                $callback($row);
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if (false === $result) {
            $this->error(__('没有数据被更新'));
        }
        $this->success();
    }


  
    
    //导入
    #[Route("GET","import")]
    public function import()
    {
        return "导入";
    }
    
    //下载
    #[Route("GET","download")]
    public function download()
    {
        return "下载";
    }
}
