<?php
/**
 *
 * @author：lltyy
 * @DateTime：2022/9/20 13:00
 * @Description：
 *
 */

namespace Application\Service\DB\Dictionary;

use Interop\Container\ContainerInterface;

class Workset extends \Application\Service\DB\AbstractDb
{
    public function __construct($dbAdapter, ContainerInterface $container)
    {
        $this->tableName = 'yikeen_dictionary_workset';
        parent::__construct($dbAdapter, $container);
    }

    /**
     * Notes:依据指定条件获取工作重组表单结构信息
     * User: lltyy
     * DateTime: 2022/9/29 14:58
     *
     * @param $where
     */
    public function fetchWorkFormData($where){
        $datas = [];
        $cond = [
            'where'=>$where,
            'columns'=>['id','parent_id','work_name','work_code','write_type','content','is_more','work_type','is_required'],
            'order'=>['parent_id','work_order']
        ];
        $work_datas = $this->fetchAll($cond);
        if(!empty($work_datas)){
            $work_arr = [
                'data'=>[],
                'child'=>[],
                'name'=>[]
            ];
            $write_type_code = $this->LocalService()->dictionaryFormtype->fetchCol('type_code','is_del = 0');
            foreach($work_datas as $work_data){
                $workset = [
                    'type'=>"Input",
                    'label'=>$work_data['work_name'],
                    'placeholder'=>"请输入",
                    'write_code'=>"",
                    'work_id'=>$work_data['id']
                ];
                //获取输入类型code值
                $write_type = $work_data['write_type'];
                $writetype_code = isset($write_type_code[$write_type]) ? $write_type_code[$write_type] : "";
                $workset['write_code'] = $writetype_code;

                //多行文本
                if($writetype_code == 'textarea'){
                    $workset['type'] = "Textarea";
                }

                //数字
                if(in_array($writetype_code,['integer','decimal_one','decimal_two','decimal_three'])){
                    $workset['type'] = "Number";
                }

                //获取选项信息
                if(in_array($writetype_code,["radio","select","checkbox"])){
                    $workset['type'] = "Select";
                    $workset['placeholder'] = "请选择";
                    if($writetype_code == "checkbox"){
                        $work_form_data['multiple'] = true;
                    }
                    $workset['options'] = [];
                    $content_arr = json_decode($work_data['content']);
                    if(!empty($content_arr)){
                        $new_content_arr = [];
                        foreach($content_arr as $content){
                            $content = get_object_vars($content);
                            $content_order = $content['select_order'];
                            $new_content_arr[$content_order] = $content;
                        }
                        ksort($new_content_arr);
                        foreach($new_content_arr as $new_content){
                            $workset['options'][] = [
                                'label' => $new_content['select_name'],
                                'value' => $new_content['select_val'],
                                'code' => $new_content['select_code']
                            ];
                        }
                    }
                }

                $workset['prop'] = $work_data['work_code'];
                $workset['rules'] = [
                    [
                        'required' => false,
                        'message' => $workset['placeholder'],
                        'trigger' => 'blur'
                    ]
                ];
                if($work_data['is_required'] == 1){
                    $workset['rules'][0]['required'] = true;
                }
                if($work_data['is_more'] == 1 && $work_data['work_type'] == 3){
                    $workset['isExpand'] = true;
                }

                $id = $work_data['id'];
                $work_arr['name'][$id] = $work_data['work_name'];
                $parent_id = $work_data['parent_id'];
                if(!empty($parent_id)){
                    $work_arr['child'][$parent_id][] = $workset;
                }else{
                    $work_arr['data'][] = $workset;
                }

            }
            if(!empty($work_arr['data'])){
                foreach($work_arr['data'] as $work){
                    $work_id = $work['work_id'];
                    if(isset($work_arr['child'][$work_id]) && !empty($work_arr['child'][$work_id])){
                        foreach($work_arr['child'][$work_id] as $work_child){
                            $work_child_data = $work_child;
                            $work_child_data['label'] = '【'.$work['label'].'】'.$work_child['label'];
                            $work_child_data['placeholder'] = $work_child_data['placeholder'].$work_child_data['label'];
                            $work_child_data['rules'][0]['message'] = $work_child_data['placeholder'];
                            $work_child_prop = $work_child_data['prop'];
                            $datas[$work_child_prop] = $work_child_data;
                        }
                    }else{
                        $work_form_data = $work;
                        $work_form_data['placeholder'] = $work_form_data['placeholder'].$work['label'];
                        $work_form_data['rules'][0]['message'] = $work_form_data['placeholder'];
                        $work_form_prop = $work_form_data['prop'];
                        $datas[$work_form_prop] = $work_form_data;
                    }
                }
            }
        }
        return $datas;
    }

}