
import common from "@/utils/common"
import api from "@/api"
import ultrasoundImageInfo from '@/config/designer/ultrasoundImage';
import tableCell from '@/config/designer/tableCell';
import labelInfo from '@/config/designer/label';
import tool from '@/utils/tool';
import pageInfo from '@/config/designer/page';

const label = 'reportDesigner.scanTable';
const name = 'scanTable';
const category = 2;//0-表示默认基础组件，1-表示报告组件；2-布局组件
export default {
    initData: function(flag){
        var guid = common.guid();
        //组件一级对象
        var obj = {
            icon: 'icon-space',
            label,
            id: guid,
            name,
            category: category,
            disabledMove: false,
            displayContentList: [],
            //二级对象,用来封装属性和样式
            data: {},
            result: {}
        };
        if (flag)
        {
            obj.data = this.props(false);
            obj.result = this.finals(guid, name);
        }
        return obj;
    },
    props: function(type) {
        if (type) {
            return {
                //样式
                pattern: [   
                    {
                        type: 'input', field: 'colNumPerRow', title: 'reportDesigner.ImageColumnsPerRow', require: true, value: '2', unit: "reportDesigner.Num", labelWidth:"110px",
                        rules: [{required: true, message: "Please input image number", trigger: "blur"}],
                        children: [
                            { num: 1, width: 88, height: 53.7 },
                            { num: 2, width: 83, height: 53 },
                            { num: 3, width: 55.6, height: 35.1 },
                            { num: 4, width: 41.5, height: 26.1 },
                        ]
                    },       
                    {
                        type: 'input', field: 'totalCount', title: 'reportDesigner.TotoalImagesCount', require: true, value: '2', unit: "reportDesigner.Num", labelWidth:"110px",
                        rules: [{required: true, message: "Please input image number", trigger: "blur"}],
                        children: [
                            { num: 1, width: 88, height: 53.7 },
                            { num: 2, width: 83, height: 53 },
                            { num: 3, width: 55.6, height: 35.1 },
                            { num: 4, width: 41.5, height: 26.1 },
                        ]
                    },                  
                    {
                        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: '1', unit: "", rules:[], labelWidth:"270px"
                    }, 
                    {
                        type: 'switch', field: 'showTableCellBorder', title: 'reportDesigner.showTableCellBorder', require: false, value: '1', 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: '1', 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: ""
                        }, 
                    ],
                    formatSet:[
                        {type: 'switch', field: 'showImageAndDesc', title: 'reportDesigner.ImageWithComment', require: false, value: '0', unit: "", rules:[], labelWidth:"270px"}, 
                    ]
                },
            }
        }
        else {
            return {
                //样式
                pattern: [
                    {
                        type: 'input', field: 'colNum', title: 'reportDesigner.ImageColumnsPerRow', require: true, value: '2', unit: "reportDesigner.Num", labelWidth:"110px",
                        rules: [{required: true, message: "Please input image number", trigger: "blur"}],
                        children: [
                            { num: 1, width: 88, height: 53.7 },
                            { num: 2, width: 83, height: 53 },
                            { num: 3, width: 55.6, height: 35.1 },
                            { num: 4, width: 41.5, height: 26.1 },
                        ]
                    }, 
                    // {
                    //     type: 'input', field: 'gridWidth', title: 'reportDesigner.TextLengthPerLine', require: true, value: '83', unit: "mm"
                    // }, 
                    // {
                    //     type: 'input', field: 'gridHeight', title: '高度', require: true, value: '53', unit: "mm"
                    // }, 
                    {
                        type: 'switch', field: 'showImageBorder', title: 'reportDesigner.HasBorder', require: false, value: '1', unit: "", rules:[], labelWidth:"270px"
                    }, 
                    {
                        type: 'switch', field: 'backgroundShadow', title: 'reportDesigner.TransparentBackground', require: false, value: '1', unit: "", rules:[], labelWidth:"270px"
                    }, 
                    {
                        type: 'switch', field: 'showImageInterval', title: 'reportDesigner.HasGap', require: false, value: '1', 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: true, value: '', unit: "",
                            rules: [{required: true, message: "Please select bind content", trigger: "blur"}]
                        }, 
                    ],
                    formatSet:[
                        {type: 'switch', field: 'showImageAndDesc', title: 'reportDesigner.ImageWithComment', require: false, value: '0', unit: "", rules:[], labelWidth:"270px"}, 
                    ]
                }
            }
        }
    },
    //添加默认单元格
    generalCells: function(tableId, totalCount, defaultCols, tName) {//, border, backgroundShadow
        var list = [];
        var sizeInfo = this.getWidthHeight(defaultCols);
        var defaultRows = parseInt(totalCount / defaultCols) * 2;
        var surplusCols = totalCount % defaultCols;
        for (var i = 1; i <= defaultRows; i++) {
            for (var j = 1; j <= defaultCols; j++) {
                var cellInfo = tableCell.initData(tableId, i, j, tName);
                cellInfo.result.pattern.horizontalAlignment = "Center";
                if (i % 2 === 0) {
                    //添加内容
                    var tempLabel = labelInfo.initData(true);
                    tempLabel.result.parentId = cellInfo.id;
                    tempLabel.result.parentName = cellInfo.name;
                    tempLabel.result.pattern.horizontalAlignment = "Center";
                    tempLabel.result.pattern.width = 0;
                    tempLabel.result.pattern.height = 8;
                    tempLabel.result.property.placeholder = 'reportDesigner.EnterImageTotalCount';
                    cellInfo.result.tasks.push(tempLabel);
                } 
                else {
                    //添加内容
                    var temp = ultrasoundImageInfo.initData(true, cellInfo.id, cellInfo.name);
                    temp.result.pattern.horizontalAlignment = "Center";
                    temp.result.pattern.width = sizeInfo.perWidth;
                    temp.result.pattern.height = sizeInfo.perHeight;
                    temp.result.property.bindType = "General";
                    temp.result.property.bindField = "ReportImages";
                    // temp.result.pattern.border = border;
                    // temp.result.pattern.backgroundShadow = backgroundShadow;
                    cellInfo.result.tasks.push(temp);
                }
                list.push(cellInfo);
            }
        }
        if (surplusCols > 0) {
            for (var m = 1; m <= 2; m++) { 
                for (var n = 1; n <= defaultCols; n++) { 
                    var surplusCellInfo = tableCell.initData(tableId, (defaultRows + m), n, tName);
                    surplusCellInfo.result.pattern.horizontalAlignment = "Center";
                    if (n <= surplusCols) {
                        if (m % 2 === 0) {
                            //添加内容
                            var tempLabels = labelInfo.initData(true);
                            tempLabels.result.parentId = surplusCellInfo.id;
                            tempLabels.result.parentName = surplusCellInfo.name;
                            tempLabels.result.pattern.horizontalAlignment = "Center";
                            tempLabels.result.pattern.width = 40;
                            tempLabels.result.pattern.height = 8;
                            tempLabels.result.property.placeholder = 'reportDesigner.EnterImageTotalCount';
                            surplusCellInfo.result.tasks.push(tempLabels);
                        } 
                        else {
                            //添加内容
                            var temps = ultrasoundImageInfo.initData(true, surplusCellInfo.id, surplusCellInfo.name);
                            temps.result.pattern.horizontalAlignment = "Center";
                            temps.result.pattern.width = sizeInfo.perWidth;
                            temps.result.pattern.height = sizeInfo.perHeight;
                            temps.result.property.bindType = "General";
                            temps.result.property.bindField = "ReportImages";
                            // temps.result.pattern.border = border;
                            // temps.result.pattern.backgroundShadow = backgroundShadow;
                            surplusCellInfo.result.tasks.push(temps);
                        }
                    }
                    list.push(surplusCellInfo);
                }
            }
        }
        return list;
    },
    addPropContent: function (tableId, defaultRows, defaultCols, border, backgroundShadow, tName) {
        var list = [];
        var perWidth = 0;
        var perHeight = 0;
        switch (defaultCols) {
            case 1:
                perWidth = 88;
                perHeight = 53.7;
                break;
            case 2:
                perWidth = 84;
                perHeight = 53.5;
                break;
            case 3:
                perWidth = 55.6;
                perHeight = 35.1;
                break;
            case 4:
                perWidth = 41.5;
                perHeight = 26.1;
                break;
            default:
                break;
        }
        for (var i = 1; i <= defaultRows; i++) {
            for (var j = 1; j <= defaultCols; j++) {
                var cellInfo = tableCell.initData(tableId, i, j, tName);
                cellInfo.result.pattern.horizontalAlignment = "Center";
                cellInfo.result.pattern.cellBorderWeight = 0;
                //添加内容
                var temp = ultrasoundImageInfo.initData(true, cellInfo.id, cellInfo.name);
                temp.result.pattern.horizontalAlignment = "Center";
                temp.result.pattern.width = perWidth;
                temp.result.pattern.height = perHeight;
                temp.result.property.bindType = "General";
                temp.result.property.bindField = "ReportImages";
                temp.result.pattern.border = border;
                temp.result.pattern.backgroundShadow = backgroundShadow;
                cellInfo.result.tasks.push(temp);
                list.push(cellInfo);
            }
        }
        return list;
    },    
    getWidthHeight: function (defaultCols) {
        var perWidth = 0;
        var perHeight = 0;
        switch (defaultCols) {
            case 1:
                perWidth = 88;
                perHeight = 53.7;
                break;
            case 2:
                perWidth = 84;
                perHeight = 53.5;
                break;
            case 3:
                perWidth = 55.6;
                perHeight = 35.1;
                break;
            case 4:
                perWidth = 41.5;
                perHeight = 26.1;
                break;
            default:
                break;
        }
        return { perWidth, perHeight };
    },  
    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 };
    },  
    //结果
    finals: function(guid, tName){ 
        var defaultRows = 4;
        var defaultCols = 2;
        var defaultRowNum = 1;
        var defaultColNum = 2;
        var caculateInfo = this.caculateCellInfo(defaultRows, defaultCols)
        return {
            base:{
                rowCount: defaultRows,
                columnCount: defaultCols,
                rowNum: defaultRowNum,
                colNum: defaultColNum,
                columnWidth: caculateInfo.columnWidthList,
                rowHeight: caculateInfo.rowHeightList,
                gridWidth: 83,
                gridHeight: 53,
                colNumPerRow: 2,
                totalCount: 4
            },
            //父组件id
            parentId: "",
            parentName: "",
            offsetWidth: 0,
            offsetHeight: 205,
            tasks: this.addPropContent(guid, defaultRowNum, defaultColNum, tName),
            //占位符
            space: "",
            //样式，每个组件可根据自身特点进行增减
            pattern: {
                //Left,Right,Center,Bothends
                horizontalAlignment:"Center",
                borderStyle: "Solid",
                borderColor: "#747474",
                borderWeight: 1,
                backgroundColor: "",
                showTableBorder: true,
                avgColumnWidth: true,
                allowSpanPage: true,
                showTableCellBorder: false,
                showTableCellMargin: false,
                showImageInterval: false,
                showImageBorder: false,
                backgroundShadow: false,
            },
            //属性，每个组件可根据自身特点进行增减
            property: {
                bindId: "",
                //绑定类型
                bindType: "General",
                //绑定类型名称
                bindTypeField: "bindType",
                showImageAndDesc : false,
                //绑定内容
                bindField: "ReportImages",
                isDefault: false,
                //所属组
                tempStatus: 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,
            }
        }
    }
}