import common from "@/utils/common"
import api from "@/api"
import tableCell from '../tableCell';
import labelInfo from '@/config/designer/label';
import imageInfo from '@/config/designer/image';

const label = 'reportDesigner.reportHeader';
const name = 'reportHeader';
const category = 2;//0-表示默认基础组件，1-表示报告组件；2-布局组件
var obj;
export default {
    initData: function (flag) {
        var commonId = common.guid();
        //组件一级对象
        obj = {
            icon: 'icon-space',
            label,
            id: commonId,
            name,
            category: category,
            disabledMove: false,
            displayContentList: [],
            //二级对象,用来封装属性和样式
            data: {},
            result: {}
        };
        if (flag) {
            obj.data = this.props();
            obj.caculateCellInfo = function(defaultRows, defaultCols) {
                var columnWidthList = [];
                var rowHeightList = [];
                if (defaultRows > 1) {
                    rowHeightList = [
                        {height: 14.58, type: "MinValue", minHeight: 6.34 },
                        {height: 8.13, type: "MinValue", minHeight: 6.34 }
                    ];
                }
                else {
                    rowHeightList = [{height: 14.58, type: "MinValue", minHeight: 6.34 }];
                }
                if (defaultCols > 1) {
                    columnWidthList = [66.88, 100.67];
                }
                else {
                    columnWidthList = [168.48];
                }
                return { columnWidthList, rowHeightList };
            };
            obj.generalCells = function(tableId, defaultRows, defaultCols, tName, showControls) {
                var list = [];
                if (defaultRows === 1 && defaultCols === 1) {
                    //一行一列
                    showControls.some((item, index) => {
                        if (item.value) {
                            let cell = tableCell.initData(tableId, 1, 1, tName, 1, 1);
                            cell.groupField = item.key; 
                            obj.AddControls(index, cell);
                            list.push(cell);
                            return true;
                        }
                    })
                }
                else if (defaultRows === 1 && defaultCols === 2) {
                    //一行两列
                    showControls.some((item, index) => {
                        if (list.length < 2) {
                            if (item.value) {
                                let cellTemp = tableCell.initData(tableId, 1, (list.length + 1), tName, 1, 1);
                                cellTemp.groupField = item.key;
                                if (list.length === 0) {
                                    cellTemp.result.pattern.horizontalAlignment = "Right";
                                }
                                obj.AddControls(index, cellTemp);
                                list.push(cellTemp);
                            }
                        }                        
                        else {
                            return true;
                        }
                    });
                }
                else {
                    //2行2列，其中第二行跨列
                    let cellTemp1 = tableCell.initData(tableId, 1, 1, tName, 1, 1);
                    cellTemp1.groupField = showControls[0].key;
                    cellTemp1.result.pattern.horizontalAlignment = "Right";
                    obj.AddControls(0, cellTemp1);
                    list.push(cellTemp1);

                    let cellTemp2 = tableCell.initData(tableId, 1, 2, tName, 1, 1);
                    cellTemp2.groupField = showControls[1].key;
                    obj.AddControls(1, cellTemp2);
                    list.push(cellTemp2);

                    let cellTemp3 = tableCell.initData(tableId, 2, 1, tName, 1, 2);
                    cellTemp3.groupField = showControls[2].key;
                    cellTemp3.result.pattern.horizontalAlignment = "Center";
                    obj.AddControls(2, cellTemp3);
                    list.push(cellTemp3);
                }
                return list;
            };
            obj.AddControls = function(type, cell) {
                switch (type) {
                    case 0://"image":
                        var imageInfoTitle = imageInfo.initData(true);
                        imageInfoTitle.result.parentId = cell.id;
                        imageInfoTitle.result.parentName = cell.name;
                        imageInfoTitle.result.pattern.horizontalAlignment = "Center";
                        imageInfoTitle.result.pattern.verticalAlignment = "Middle";
                        imageInfoTitle.result.pattern.width = 10;
                        imageInfoTitle.result.pattern.height = 8;
                        imageInfoTitle.result.property.bindType = "General";
                        imageInfoTitle.result.property.bindField = "HospitalIcon";
                        imageInfoTitle.result.property.imageFile = "data:image/png;base64,[encoded data, 22176 characters]";
                        cell.result.tasks.push(imageInfoTitle);
                        break;
                    case 1://"main":
                        var hospitalNameTitle = labelInfo.initData(true);
                        hospitalNameTitle.result.parentId = cell.id;
                        hospitalNameTitle.result.parentName = cell.name;
                        hospitalNameTitle.result.pattern.horizontalAlignment = "Center";
                        hospitalNameTitle.result.pattern.verticalAlignment = "Middle";
                        hospitalNameTitle.result.pattern.fontSize = 28;
                        hospitalNameTitle.result.pattern.fontWeight = 600;
                        hospitalNameTitle.result.pattern.width = 0;
                        hospitalNameTitle.result.property.bindType = "General";
                        hospitalNameTitle.result.property.bindField = "HospitalName";
                        hospitalNameTitle.result.property.placeholder = "reportDesigner.HeaderMainContent";
                        cell.result.tasks.push(hospitalNameTitle);
                        break;
                    case 2://"sub":
                        var reportName = labelInfo.initData(true);
                        reportName.result.parentId = cell.id;
                        reportName.result.parentName = cell.name;
                        reportName.result.pattern.width = 0;
                        reportName.result.pattern.horizontalAlignment = "Center";
                        reportName.result.pattern.verticalAlignment = "Middle";
                        reportName.result.property.bindType = "General";
                        reportName.result.property.bindField = "ReportTitleName";
                        reportName.result.property.placeholder = "reportDesigner.HeaderSubContent";
                        cell.result.tasks.push(reportName);
                        break;
                }
            }
            obj.result = this.finals(obj);
        }
        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: 0, 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: ""
                    },
                ],
                formatSet: [
                    {type: 'switch', field: 'HospitalIcon', title: 'reportDesigner.HospitalLogo', require: false, value: '1', unit: "", rules:[], labelWidth:"270px"}, 
                    {type: 'switch', field: 'HospitalName', title: 'reportDesigner.HeaderMainTitle', require: false, value: '1', unit: "", rules:[], labelWidth:"270px"},
                    {type: 'switch', field: 'ReportTitleName', title: 'reportDesigner.HeaderSubTitle', require: false, value: '1', unit: "", rules:[], labelWidth:"270px"},
                    { type: 'switch', field: 'isHidden', title: 'reportDesigner.hidden', require: false, value: '0', unit: "", rules: [], labelWidth: "270px" },
                ]
            },
        }
    },
    //结果
    finals: function (curObj) {
        var defaultRows = 2;
        var defaultCols = 2;
        var caculateInfo = curObj.caculateCellInfo(defaultRows, defaultCols); 
        var selectedItem = [
            { key: "HospitalIcon", value: true },
            { key: "HospitalName", value: true },
            { key: "ReportTitleName", value: true }
        ];
        return {
            base: {
                rowCount: defaultRows,//表格行数
                columnCount: defaultCols,//表格列数
                columnWidth: caculateInfo.columnWidthList,//表格每列的宽度列表
                rowHeight: caculateInfo.rowHeightList//表格每行的高度列表
            },
            //父组件id
            parentId: "",
            parentName: "",
            selectedItems: selectedItem,
            tasks: curObj.generalCells(curObj.id, defaultRows, defaultCols, curObj.name, selectedItem),//如果每行列的数量有变化，请重置表格
            //占位符
            space: "",
            offsetWidth: 0,
            offsetHeight: 60,
            //样式，每个组件可根据自身特点进行增减
            pattern: {
                //Left,Right,Center,Bothends
                horizontalAlignment: "Center",
                borderStyle: "Solid",
                borderColor: "#747474",
                borderWeight: 1,
                backgroundColor: "",
                showTableBorder: false,
                avgColumnWidth: false,
                allowSpanPage: true,
                showTableCellBorder: false,
                showTableCellMargin: false
            },
            //属性，每个组件可根据自身特点进行增减
            property: {
                bindId: "",
                //绑定类型
                bindType: "",
                //绑定类型名称
                bindTypeField: "bindType",
                isDefault: false,
                //绑定内容
                bindField: "",
                HospitalIcon: true,
                HospitalName: true,
                ReportTitleName: true,
                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,
            }
        }
    }
}