<?php

namespace app\admin\controller\ContactUs;

use app\BaseController;
use app\common\model\ContactUs\JoinApply as ModelJoinApply;
use app\Request;
use think\Validate;
use think\exception\ValidateException;
use think\facade\Filesystem;
use app\common\arw\adjfut\src\Excel;
use app\common\arw\adjfut\src\UploadFile;
use app\common\exception\Tool;
use think\facade\Db;
use think\facade\Env;


class JoinApply extends BaseController
{
    /**
     * 获取加盟申请列表
     */
    public function getJoinApplyList(Request $request): array
    {
        $params = $request->param();
        $con = [];

        $con = Tool::getOptionalQuery(['join_apply_user_name', 'LIKE'], ['join_apply_user_phone', 'LIKE'], ['join_apply_user_position', 'LIKE'], ['join_apply_status', '='],);

        $query = ModelJoinApply::where($con)
            ->field([
                'join_apply_id',
                'join_apply_guid',
                'join_apply_user_name',
                'join_apply_user_phone',
                'join_apply_user_position',
                'join_apply_user_area',
                'join_apply_user_org',
                'join_apply_status'
            ])
            ->order('join_apply_update_time', 'desc');

        return msg("获取加盟申请列表成功!", $query);
    }

    /**
     * 编辑加盟申请
     */
    public function editJoinApply(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'join_apply_user_name|加盟申请用户名字' => 'require',
            'join_apply_user_phone|加盟申请用户电话' => 'require',
            'join_apply_user_position|加盟申请用户职位' => 'require',
            'join_apply_user_area|加盟申请用户所在地区' => 'require',
            'join_apply_user_org|加盟申请用户所属机构名称' => 'require',
            'join_apply_status|加盟申请受理状态' => 'require'
        ]);
        $model = ModelJoinApply::where('join_apply_guid', $params['join_apply_guid'])->find();
        if (!$model) throwErrorMsg("该加盟申请不存在", 1);
        $model->allowField([
            'join_apply_update_user_guid',
            'join_apply_user_name',
            'join_apply_user_phone',
            'join_apply_user_position',
            'join_apply_user_area',
            'join_apply_user_org',
            'join_apply_status'
        ])->save($params);
        return msg('编辑成功!');
    }

    /**
     * 添加加盟申请
     */
    public function addJoinApply(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'join_apply_user_name|加盟申请用户名字' => 'require',
            'join_apply_user_phone|加盟申请用户电话' => 'require',
            'join_apply_user_position|加盟申请用户职位' => 'require',
            'join_apply_user_area|加盟申请用户所在地区' => 'require',
            'join_apply_user_org|加盟申请用户所属机构名称' => 'require',
            'join_apply_status|加盟申请受理状态' => 'require'
        ]);
        $model = ModelJoinApply::create($params, [
            'join_apply_guid',
            'join_apply_create_user_guid',
            'join_apply_update_user_guid',
            'join_apply_user_name',
            'join_apply_user_phone',
            'join_apply_user_position',
            'join_apply_user_area',
            'join_apply_user_org',
            'join_apply_status'
        ]);
        return msg('添加成功!');
    }

    /**
     * 删除加盟申请
     */
    public function deleteJoinApply(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'join_apply_guid' => 'require',
        ]);
        $join_apply = ModelJoinApply::where([
            'join_apply_guid' => explode(',', $params['join_apply_guid'])
        ])->select();
        $join_apply->delete();
        return msg('删除成功!');
    }

    /**
     * 导出Excel
     */
    public function exportExcel(Request $request)
    {
        $params = $request->param();
        $select = ModelJoinApply::field([
            'join_apply_user_name',
            'join_apply_user_phone',
            'join_apply_user_position',
            'join_apply_user_area',
            'join_apply_user_org',
            'join_apply_status'
        ])
            ->order('join_apply_update_time', 'desc')
            ->select();
        return ModelJoinApply::exportExcel($select);
    }
    /**
     *  合作加盟申请受理接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-20
     * @author xjh
     * @since 1.0.0
     */
    public function handlejoin_applyStatus(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'join_apply_guid|合作加盟申请guid' => 'require',
        ]);
        $msg_data = [];
        $join_apply_guids = explode(',', $params['join_apply_guid']);
        $join_apply_guids_count = count($join_apply_guids);
        foreach ($join_apply_guids  as $line => $join_apply_guid) {
            $prefix_text = ($join_apply_guids_count != 1) ? "第" . ($line + 1) . "行-> " : null;
            try {
                $join_apply = ModelJoinApply::where('join_apply_guid', $join_apply_guid)->find();
                if (!$join_apply) throwErrorMsg('该合作加盟申请数据不存在!');
                if ($join_apply->join_apply_status == 1)  throwErrorMsg('该合作加盟申请数据已处理!');
                $join_apply->join_apply_status = 1;
                $join_apply->save();
                $msg_data[] = $prefix_text . "用户名称【{$join_apply->join_apply_user_name}】: <span style='color:#27af49'>受理成功!</span>";
            } catch (\Throwable $th) {
                $msg_data[] = $prefix_text . "用户名称:【{$join_apply->join_apply_user_name}】: <span style='color:red'>{$th->getMessage()}</span>";
            }
        };
        return msg(0, implode('<br>', $msg_data));
    }
}
