
import common from "@/utils/common"
import api from "@/api"
import tableCell from './tableCell';
import tool from '@/utils/tool';
import pageInfo from '@/config/designer/page';
import i18n from '@/locales'

const label = 'reportDesigner.table';
const name = 'table';
const category = 2;//0-表示默认基础组件，1-表示报告组件；2-布局组件
export default {
    initData: function(flag){
        var tableId = common.guid();
        //组件一级对象
        var obj = {
            icon: 'icon-space',
            label,
            id: tableId,
            name,
            category: category,
            disabledMove: false,
            //二级对象,用来封装属性和样式
            data: {},
            result: {},
            displayContentList: [{
                rowIndex: 0,
                offsetWidth: 0,
                offsetHeight: 20,
                moduleIdList: []
              },
              {
                rowIndex: 1,
                offsetWidth: 0,
                offsetHeight: 20,
                moduleIdList: []
              },
              {
                rowIndex: 1,
                offsetWidth: 0,
                offsetHeight: 20,
                moduleIdList: []
              }
            ]
        };
        if (flag)
        {
            obj.data = this.props();
            obj.result = this.finals(tableId, name);
        }
        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: '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: '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: 'inputNumber', field: 'rowCount', title: 'reportDesigner.rowCount', require: true, value: '0', unit: "", 
                    rules: [{required: true, message: i18n.global.tc("reportDesigner.rownumberTypeRequireMessage"), trigger: "blur"}]},
                    {type: 'inputNumber', field: 'columnCount', title: 'reportDesigner.columnCount', require: true, value: '0', unit: "", 
                    rules: [{required: true, message: i18n.global.tc("reportDesigner.columnNumberRequireMessage"), trigger: "blur"}]}
                ],
                formatSet:[
                    {type: 'switch', field: 'isHidden', title: 'reportDesigner.hidden', require: false, value: '0', unit: "", rules:[], labelWidth:"270px"}, 
                ]
            },
        }
    },
    //添加默认单元格，每次单元格数量变化，都要重新调用
    generalCells: function(tableId, defaultRows, defaultCols, tName) {
        var list = [];
        for (var i = 1; i <= defaultRows; i++) {
            for (var j = 1; j <= defaultCols; j++) {
                var cellInfo = tableCell.initData(tableId, i, j, tName);
                //此处默认加载了空的单元格，如果是有信息，需要在单元格内加载基础组件
                list.push(cellInfo);
            }
        }
        return list;
    },
    //计算单元格宽高，每次单元格重绘，需要重新计算
    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(tableId, tName){
        var defaultRows = 3;
        var defaultCols = 3;
        var caculateInfo = this.caculateCellInfo(defaultRows, defaultCols)
        return {
            base:{
                rowCount: defaultRows,//表格行数
                columnCount: defaultCols,//表格列数
                columnWidth: caculateInfo.columnWidthList,//表格每列的宽度列表
                rowHeight: caculateInfo.rowHeightList,//表格每行的高度列表
                isRedraw: false
            },
            //父组件id
            parentId: "",
            parentName: "",
            offsetWidth: 0,
            offsetHeight: 60,
            tasks: this.generalCells(tableId, defaultRows, defaultCols, tName),//如果每行列的数量有变化，请重置表格
            //占位符
            space: "",
            //样式，每个组件可根据自身特点进行增减
            pattern: {
                //Left,Right,Center,Bothends
                horizontalAlignment:"",
                borderStyle: "Solid",
                borderColor: "#747474",
                borderWeight: 1,
                backgroundColor: "",
                showTableBorder: true,
                avgColumnWidth: true,
                allowSpanPage: true,
                showTableCellBorder: true,
                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,
            }
        }
    }
}