<?php
/**
 *
 * @author：llbjj
 * @DateTime：2022/5/6 10:57
 * @Description：
 *
 */

namespace Application\Service\DB\Dictionary;

use Interop\Container\ContainerInterface;

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

    public function formFieldData($where){
        $datas = [];
        $whereArr = [
            'where'=>$where,
            'columns'=>['id','attr_name', 'attr_filed', 'attr_type', 'attr_order', 'is_required', 'is_default', 'content', 'attr_type_id'],
            'order'=>['attr_order']
        ];
        $patientattrData = $this->fetchAll($whereArr);
        if(!empty($patientattrData)){
            $write_type_code = $this->LocalService()->dictionaryFormtype->fetchCol('type_code','is_del = 0');
            foreach($patientattrData as $patientattrDataKey=>&$patientattrDataValue){
                $patientattrDataValue['type'] = "Input";
                $patientattrDataValue['write_code'] = "";
                $patientattrDataValue['label'] = $patientattrDataValue['attr_name'];
                $patientattrDataValue['placeholder'] = "请输入".$patientattrDataValue['attr_name'];



                $formFieldModel = [
                    'type'=>"Input",
                    'label'=>$patientattrDataValue['attr_name'],
                    'placeholder'=>"请输入".$patientattrDataValue['attr_name'],
                    'write_code'=>"",
                    'id'=>$patientattrDataValue['id']
                ];
                //获取输入类型code值
                $write_type = $patientattrDataValue['attr_type_id'];
                $writetype_code = isset($write_type_code[$write_type]) ? $write_type_code[$write_type] : "";
                $formFieldModel['write_code'] = $writetype_code;


                $patientattrDataValue['write_code'] = $writetype_code;

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

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

                }

                //获取选项信息
                if(in_array($writetype_code,["radio","select","checkbox"])){
                    $formFieldModel['type'] = "Select";
                    $formFieldModel['placeholder'] = "请选择";


                    $patientattrDataValue['type'] = "Select";
                    $patientattrDataValue['placeholder'] = "请选择";
                    if($writetype_code == "checkbox"){
                        $formData['multiple'] = true;
                        $patientattrDataValue['multiple'] = true;
                    }
                    $patientattrDataValue['options'] = [];
//                    $formFieldModel['options'] = [];
                    $content_arr = json_decode($patientattrDataValue['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){
                            $formFieldModel['options'][] = [
                                'label' => $new_content['select_name'],
                                'value' => $new_content['select_val'],
                                'code' => $new_content['select_code']
                            ];
                            $patientattrDataValue['options'][] = [
                                'label' => $new_content['select_name'],
                                'value' => $new_content['select_val'],
                                'code' => $new_content['select_code']
                            ];
                        }
                    }
                }

                $formFieldModel['prop'] = $patientattrDataValue['attr_filed'];
                $patientattrDataValue['prop'] = $patientattrDataValue['attr_filed'];
                $formFieldModel['rules'] = [
                    [
                        'required' => !empty($patientattrDataValue['attr_filed'])?true:false,
                        'message' => $formFieldModel['placeholder'],
                        'trigger' => 'blur'
                    ]
                ];
                $patientattrDataValue['rules'] = [
                    [
                        'required' => !empty($patientattrDataValue['attr_filed'])?true:false,
                        'message' => $formFieldModel['placeholder'],
                        'trigger' => 'blur'
                    ]
                ];
                $datas[$patientattrDataValue['attr_filed']] = $formFieldModel;

            }
        }
//        return $patientattrData;

        return $datas;
    }


}