import common from "@/utils/common";
import api from "@/api";
import labelInfo from '@/config/designer/label';
import inputInfo from '@/config/designer/input';
import dateInfo from '@/config/designer/datetime';
import tableCell from '../tableCell';
import tool from '@/utils/tool';
import pageInfo from '@/config/designer/page';
import select from '@/config/designer/select';

const label = 'reportDesigner.patientInfo';
const name = 'patientInfo';
const category = 2;//0-表示默认基础组件2-布局组件
var obj;
export default {
    initData: function (flag, isAnimal) {
        var commonId = common.guid();
        obj = {
            icon: 'icon-space',
            label,
            id: commonId,
            name,
            category: category,
            disabledMove: false,
            patientModuleList: [],
            displayContentList: [],
            //二级对象,用来封装属性和样式
            data: {},
            result: {}
        };
        if (flag) {
            obj.patientModuleList = [
                { sort: 5, key: "PatientId", label: "reportDesigner.PatientId", value: false, type: "input", group: "human" },
                { sort: 1, key: "PatientName", label: "reportDesigner.PatientName", value: false, type: "input", group: "human" },
                { sort: 4, key: "PatientAge", label: "reportDesigner.PatientAge", value: false, type: "input", group: "human" },

                { sort: 27, key: "PatientBirthday", label: "reportDesigner.PatientBirthday", value: false, type: "date", group: "human" },
                { sort: 3, key: "PatientGender", label: "reportDesigner.PatientGender", value: false, type: "singeSelect", group: "human", options: ['reportDesigner.SexUnSelect','reportDesigner.Male', 'reportDesigner.Female'] },
                { sort: 7, key: "PatientPhoneNumber", label: "reportDesigner.PatientPhoneNumber", value: false, type: "input", group: "human" },

                { sort: 8, key: "PatientExamDate", label: "reportDesigner.PatientExamDate", value: false, type: "date", group: "item" },
                { sort: 9, key: "PatientLMP", label: "reportDesigner.PatientLMP", value: false, type: "date", group: "item" },
                { sort: 10, key: "PatientGA", label: "reportDesigner.PatientGA", value: false, type: "input", group: "item" },

                { sort: 11, key: "PatientEDD", label: "reportDesigner.PatientEDD", value: false, type: "date", group: "item" },
                { sort: 12, key: "PatientPerfPhysician", label: "reportDesigner.PatientPerfPhysician", value: false, type: "input", group: "item" },
                { sort: 13, key: "PatientInspectionItems", label: "reportDesigner.PatientInspectionItems", value: false, type: "mulitSelect", group: "item", options: ['reportDesigner.ObstetricRoutine','reportDesigner.GynecologicalRoutine', 'reportDesigner.AbdominalRoutine', 'reportDesigner.FemaleUrologicalRoutine', 'reportDesigner.MaleUrologicalRoutine'] },

                { sort: 6, key: "PatientInspectionSite", label: "reportDesigner.PatientInspectionSite", value: false, type: "mulitSelect", group: "item", options: ['reportDesigner.Fetu','reportDesigner.Liver','reportDesigner.Cardiac','reportDesigner.Uterus','reportDesigner.Appendages','reportDesigner.PelvicCavity','reportDesigner.GB','reportDesigner.BileDuct','reportDesigner.Pancreas','reportDesigner.Spleen','reportDesigner.DoubleKidney','reportDesigner.Ureter','reportDesigner.Bladder','reportDesigner.Prostate','reportDesigner.Breast','reportDesigner.MesentericLymphNode','reportDesigner.Thyroid','reportDesigner.PeripheralSuperficial'] },
                { sort: 15, key: "PatientExaminationWay", label: "reportDesigner.PatientExaminationWay", value: false, type: "singeSelect", group: "item", options: ['reportDesigner.BodySurface','reportDesigner.Transabdominal', 'reportDesigner.Transvaginal'] },
                { sort: 16, key: "PatientCheckNumber", label: "reportDesigner.PatientCheckNumber", value: false, type: "input", group: "item" },

                { sort: 2, key: "PatientApplicationDepartment", label: "reportDesigner.PatientApplicationDepartment", value: false, type: "singeSelect", group: "item", options: ['reportDesigner.OB','reportDesigner.Outpatient', 'reportDesigner.PublicHealthDivision'] },
                { sort: 18, key: "PatientDeviceNumber", label: "reportDesigner.PatientDeviceNumber", value: false, type: "singeSelect", group: "item", options: ['Vinno M80', 'Vinno E35'] },
                { sort: 14, key: "PatientOutpatientNumber", label: "reportDesigner.PatientOutpatientNumber", value: false, type: "input", group: "item" },

                { sort: 19, key: "PatientUltrasonicNumber", label: "reportDesigner.PatientUltrasonicNumber", value: false, type: "input", group: "item" },
                { sort: 20, key: "PerformingPhysician", label: "reportDesigner.PerformingPhysician", value: false, type: "input", group: "item" },
                { sort: 21, key: "ReferringPhysician", label: "reportDesigner.ReferringPhysician", value: false, type: "input", group: "item" },

                { sort: 22, key: "InspectionPhysician", label: "reportDesigner.InspectionPhysician", value: false, type: "input", group: "item" },
                { sort: 23, key: "Owner", label: "reportDesigner.Owner", value: false, type: "input", group: "animal" },
                { sort: 24, key: "Species", label: "reportDesigner.Species", value: false, type: "input", group: "animal" },

                { sort: 25, key: "Breed", label: "reportDesigner.Breed", value: false, type: "input", group: "animal" },
                { sort: 26, key: "Operator", label: "reportDesigner.Operator", value: false, type: "input", group: "item" },
                { sort: 17, key: "ScanPhysician", label: "reportDesigner.ScanPhysician", value: false, type: "input", group: "item" }
            ];
            obj.data = this.props();
            obj.caculateRowsAndCols = function (moduleCount, showNumPerRow) {
                var rows = parseInt(moduleCount / showNumPerRow) + parseInt((moduleCount % showNumPerRow) > 0 ? 1: 0);
                var bindWidth = 32;
                if (showNumPerRow === 4) {
                    bindWidth = 20;
                }
                return { rows, showNumPerRow, bindWidth};
            };
            obj.caculateCellInfo = function(defaultRows, defaultCols) {
                var width = tool.mmToPx(pageInfo.defaultPageType.width) - tool.mmToPx(pageInfo.defaultPagePadding.left) - tool.mmToPx(pageInfo.defaultPagePadding.right)
                var avgWidth = parseInt(width / defaultCols);
                var tempVal = 0.0;
                var columnWidthList = [];
                var rowHeightList = [];
                for (var i = 1; i < defaultCols; i++) {
                    tempVal += avgWidth;
                    columnWidthList.push(tool.pxToMM(avgWidth));
                }
                var lastColumnWidth = width - tempVal;
                columnWidthList.push(tool.pxToMM(lastColumnWidth));
                for (var j = 1; j <= defaultRows; j++) {
                    var tempMinH = { height: 0.0, type: "MinValue", minHeight: 6.34 };
                    rowHeightList.push(tempMinH);
                }
                return { columnWidthList, rowHeightList };
            };
            obj.generalCells = function(tableId, defaultRows, defaultCols, tName, showControls, bindWidth) {
                var list = [];
                var tempShowControls = tool.objCopy(showControls);
                for (var i = 1; i <= defaultRows; i++) {
                    for (var j = 1; j <= defaultCols; j++) {
                        if (tempShowControls.length <= 0) {
                            list.push(tableCell.initData(tableId, i, j, tName, 1, 1));
                            continue;
                        }
                        var moduleInfo = tempShowControls[0];
                        let cell = tableCell.initData(tableId, i, j, tName, 1, 1);
                        cell.groupField = moduleInfo.key;
                        switch (moduleInfo.type) {
                            case "input":
                                var inputLableControl = labelInfo.initData(true);
                                inputLableControl.result.parentId = cell.id;
                                inputLableControl.result.parentName = cell.name;
                                inputLableControl.result.pattern.horizontalAlignment = "Center";
                                inputLableControl.result.pattern.width = 0;
                                inputLableControl.result.property.placeholder = moduleInfo.label;
                                inputLableControl.result.property.group = moduleInfo.key;
                                cell.result.tasks.push(inputLableControl);

                                var inputValueControl = inputInfo.initData(true);
                                inputValueControl.result.parentId = cell.id;
                                inputValueControl.result.parentName = cell.name;
                                inputValueControl.result.pattern.horizontalAlignment = "Left";
                                inputValueControl.result.pattern.width = parseFloat(bindWidth);
                                inputValueControl.result.property.bindType = "General";
                                inputValueControl.result.property.bindField = moduleInfo.key;
                                inputValueControl.result.property.group = moduleInfo.key;
                                cell.result.tasks.push(inputValueControl);
                                break;
                            case "date":
                                var dateLableControl = labelInfo.initData(true);
                                dateLableControl.result.parentId = cell.id;
                                dateLableControl.result.parentName = cell.name;
                                dateLableControl.result.pattern.horizontalAlignment = "Center";
                                dateLableControl.result.pattern.width = 0;
                                dateLableControl.result.property.placeholder = moduleInfo.label;
                                dateLableControl.result.property.group = moduleInfo.key;
                                cell.result.tasks.push(dateLableControl);

                                var dateControl = dateInfo.initData(true);
                                dateControl.result.parentId = cell.id;
                                dateControl.result.parentName = cell.name;
                                dateControl.result.pattern.horizontalAlignment = "Left";
                                dateControl.result.pattern.width = parseFloat(bindWidth);
                                dateControl.result.property.bindType = "General";
                                dateControl.result.property.bindField = moduleInfo.key;
                                dateControl.result.property.group = moduleInfo.key;
                                cell.result.tasks.push(dateControl);
                                break;
                            case "singeSelect":
                                var singeSelectLableControl = labelInfo.initData(true);
                                singeSelectLableControl.result.parentId = cell.id;
                                singeSelectLableControl.result.parentName = cell.name;
                                singeSelectLableControl.result.pattern.horizontalAlignment = "Center";
                                singeSelectLableControl.result.pattern.width = 0;
                                singeSelectLableControl.result.property.placeholder = moduleInfo.label;
                                singeSelectLableControl.result.property.group = moduleInfo.key;
                                cell.result.tasks.push(singeSelectLableControl);

                                var singeSelectControl = select.initData(true);
                                singeSelectControl.result.parentId = cell.id;
                                singeSelectControl.result.parentName = cell.name;
                                singeSelectControl.result.pattern.horizontalAlignment = "Left";
                                singeSelectControl.result.pattern.width = parseFloat(bindWidth);
                                singeSelectControl.result.property.bindType = "General";
                                singeSelectControl.result.property.bindField = moduleInfo.key;
                                singeSelectControl.result.property.group = moduleInfo.key;
                                singeSelectControl.result.property.staticOptions = moduleInfo.options;
                                singeSelectControl.result.property.placeholder = moduleInfo.options[0];
                                cell.result.tasks.push(singeSelectControl);
                                break;
                            case "mulitSelect":
                                var mulitSelectLableControl = labelInfo.initData(true);
                                mulitSelectLableControl.result.parentId = cell.id;
                                mulitSelectLableControl.result.parentName = cell.name;
                                mulitSelectLableControl.result.pattern.horizontalAlignment = "Center";
                                mulitSelectLableControl.result.pattern.width = 0;
                                mulitSelectLableControl.result.property.placeholder = moduleInfo.label;
                                mulitSelectLableControl.result.property.group = moduleInfo.key;
                                cell.result.tasks.push(mulitSelectLableControl);

                                var mulitSelectControl = select.initData(true);
                                mulitSelectControl.result.parentId = cell.id;
                                mulitSelectControl.result.parentName = cell.name;
                                mulitSelectControl.result.pattern.horizontalAlignment = "Left";
                                mulitSelectControl.result.pattern.width = parseFloat(bindWidth);
                                mulitSelectControl.result.property.bindType = "General";
                                mulitSelectControl.result.property.bindField = moduleInfo.key;
                                mulitSelectControl.result.property.group = moduleInfo.key;
                                mulitSelectControl.result.property.staticOptions = moduleInfo.options;
                                mulitSelectControl.result.property.placeholder = moduleInfo.options[0];
                                mulitSelectControl.result.property.multiple = true;
                                cell.result.tasks.push(mulitSelectControl);
                                break;
                        }
                        list.push(cell);
                        tempShowControls.splice(0, 1);
                    }
                }
                return list;
            };
            obj.result = this.finals(obj, isAnimal);
        }
        return obj;
    },
    props: function () {
        return {
            //样式
            pattern: [
                {
                    type: 'borderStyleSelect', field: 'borderStyle', title: 'reportDesigner.LineType', require: false, value: 'solid', unit: "",
                    options: [
                        { key: 'Solid', value: 'reportDesigner.solid' },
                        { key: 'Dotted', value: 'reportDesigner.dotted' },
                        { key: 'Dashed', value: 'reportDesigner.dashed' }
                    ]
                },
                {
                    type: 'colorPicker', field: 'borderColor', title: 'reportDesigner.borderColor', require: false, value: '#888', unit: ""
                },
                {
                    type: 'borderWeightSelect', field: 'borderWeight', title: 'reportDesigner.Thickness', require: false, value: 1, unit: "",
                    options: [0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2, 2.25, 3, 4.5, 6]
                },
                {
                    type: 'colorPicker', field: 'backgroundColor', title: 'reportDesigner.Fill', require: false, value: '#FFF', unit: ""
                },
                {
                    type: 'switch', field: 'showTableBorder', title: 'reportDesigner.showTableBorder', require: false, value: '0', unit: "", rules: [], labelWidth: "270px"
                },
                {
                    type: 'switch', field: 'showTableCellBorder', title: 'reportDesigner.showTableCellBorder', require: false, value: '0', unit: "", rules: [], labelWidth: "270px"
                },
                {
                    type: 'switch', field: 'avgColumnWidth', title: 'reportDesigner.AverageColumnWidth', require: false, value: '1', unit: "", rules: [], labelWidth: "270px"
                },
                {
                    type: 'switch', field: 'allowSpanPage', title: 'reportDesigner.allowSpanPage', require: false, value: '1', unit: "", rules: [], labelWidth: "270px"
                },
                {
                    type: 'switch', field: 'showTableCellMargin', title: 'reportDesigner.showTableCellMargin', require: false, value: '0', unit: "", rules: [], labelWidth: "270px"
                },
            ],
            //属性
            property: {
                contentSet: [
                    {
                        type: 'select', field: 'bindType', title: 'reportDesigner.bindType', require: false, value: '', unit: "", rules: [],
                        dataSource: api.template.findContentConfigAsync,
                        tiedDataSource: api.template.findContentConfigAsync,
                        tiedBack: "bindField", changeStatus: true
                    },
                    {
                        type: 'elSelect', field: 'bindField', title: 'reportDesigner.bindField', require: false, value: '', unit: ""
                    },
                    {
                        type: 'addButtom', field: 'addControlButton', title: 'reportDesigner.choosePatientInfo', require: false, value: '', unit: "1",
                        labelWidth: "295px"
                    }
                ],
                formatSet: [
                    { type: 'switch', field: 'isHidden', title: 'reportDesigner.hidden', require: false, value: '0', unit: "", rules: [], labelWidth: "295px" },
                ]
            },
        }
    },
    //加载初始数据
    loadDefaultData: function(moduleList, isAnimal) {
        var list = [];
        if (isAnimal)
        {
            list = ["Owner", "Species", "Breed", "PatientApplicationDepartment", "PatientCheckNumber", "PatientOutpatientNumber", "PatientInspectionSite", "PatientDeviceNumber"];
        }
        else {
            list = ["PatientName", "PatientGender", "PatientAge", "PatientApplicationDepartment", "PatientCheckNumber", "PatientOutpatientNumber", "PatientInspectionSite", "PatientDeviceNumber"];
        }

        var result = [];
        list.forEach(c => {
            moduleList.some(d => {
                if (d.key === c) {
                    result.push(d);
                    return true;
                }
            });
        });
        return result;
    },
    //结果
    finals: function (curObj, isAnimal) {
        var defaultRows = 3;
        var defaultCols = 3;
        var bindWidth = 32;
        var caculateInfo = curObj.caculateCellInfo(defaultRows, defaultCols);
        var selectedItem = this.loadDefaultData(curObj.patientModuleList, isAnimal);
        return {
            base: {
                rowCount: defaultRows,//表格行数
                columnCount: defaultCols,//表格列数
                columnWidth: caculateInfo.columnWidthList,//表格每列的宽度列表
                rowHeight: caculateInfo.rowHeightList,//表格每行的高度列表
                columnCountPerRow: defaultCols,//每行显示几列
                moduleBindWidth: bindWidth
            },
            //父组件id
            parentId: "",
            parentName: "",
            offsetWidth: 0,
            offsetHeight: 65,
            tasks: curObj.generalCells(curObj.id, defaultRows, defaultCols, curObj.name, selectedItem, bindWidth),//如果每行列的数量有变化，请重置表格
            selectedItems: selectedItem,//选中项，表示
            //占位符
            space: "",
            //样式，每个组件可根据自身特点进行增减
            pattern: {
                //Left,Right,Center,Bothends
                horizontalAlignment: "",
                borderStyle: "Solid",
                borderColor: "#747474",
                borderWeight: 1,
                backgroundColor: "",
                showTableBorder: false,
                avgColumnWidth: true,
                allowSpanPage: true,
                showTableCellBorder: false,
                showTableCellMargin: false
            },
            //属性，每个组件可根据自身特点进行增�
            property: {
                bindId: "",
                //绑定类型
                bindType: "",
                //绑定类型名称
                bindTypeField: "bindType",
                //绑定内容
                bindField: "",
                isDefault: false,
                isHidden: false,
                measureItem: {
                    AvailableMethods: [],
                    BaseType: "",
                    CalculationId: "",
                    MatchId: "",
                    Method: "",
                    Mode: "",
                    Output: "",
                    OutputV2: "",
                    Unit: "",
                    UserId: ""
                }
            },
            //位置坐标
            margin: {
                sort: 0,
                //0-表示absolute;1-relative
                position: 0,
                //如果采用相对定位，则需要填写上一个组件的组件id
                previousId: "",
                top: 0.0,
                bottom: 0.0,
                left: 0.0,
                right: 0.0,
            }
        }
    }
}
