<template>
    <el-dialog class="transferItems" :title="$t('reportDesigner.CustomizeCalculation')" width="1100px" v-model="visible" destroy-on-close
        @closed="confirm(); $emit('closed')">
        <el-row>
            <el-col :span="7">
                <el-card class="box-card">
                    <template #header>
                        <div class="card-header">
                            <div>
                                <span>{{ $t('reportDesigner.MeasureCalculation') }}</span>
                            </div>
                            <div style="display:flex;justify-content:left;">
                                <el-input type="text" class="searchInput" v-model="newItemText" :placeholder="$t('reportDesigner.MeasureTip1')" size="default"></el-input>
                                <el-button class="searchButton" type="primary" plain @click="addInputData()">{{ $t('reportLabel.addItem') }}</el-button>
                            </div>
                        </div>
                    </template>
                    <div class="contentList">
                        <div class="item" v-for="(item, itemIndex) in items" :key="item" :style="showActive(item)" @click="setItemActive(itemIndex);">
                            <div>
                                {{ item.title }}
                            </div>
                            <div>
                                <el-button class="itemButton" type="danger" icon="el-icon-delete" size="small" round @click="removeItem(item.id)"></el-button>
                            </div>
                        </div>
                    </div>
                </el-card>
            </el-col>
            <el-col :span="17">
                <el-row>
                    <el-col>
                        <el-card class="box-card formulaCard">
                            <template #header>
                                <div class="card-header">
                                    <div>
                                        <span>{{ $t('reportDesigner.MeasureFormula') }}</span>
                                    </div>
                                    <div class="formulaContent">
                                        <el-input class="formulaInput" type="text" v-model="currentActiveItem.formula" :placeholder="$t('reportDesigner.MeasureTip2')" size="default" style="width:75%;" @change="changeInput"></el-input>
                                        <el-button class="formulaButton" type="primary" plain @click="validateFormulaData()">{{ $t('reportDesigner.MeasureVerify') }}</el-button>
                                        <el-select v-model="currentActiveItem.additionUnitTypeText" style="width:20%;" :placeholder="$t('reportDesigner.MeasureTip3')" filterable @change="additionUnitTypeClick">
                                            <el-option v-for="item in selectItems['unitOptions']" :key="item.value" :label="item.name" :value="item.value">
                                            </el-option>
                                        </el-select>
                                    </div>
                                </div>
                            </template>
                        </el-card>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="10">
                        <el-card class="box-card">
                            <el-form :model="form" ref="dialogForm" label-width="70px" label-position="left">
                                <el-row :gutter="15" >
                                    <el-col :span="24">
                                        <el-form-item :label="$t('reportDesigner.bindType')" prop="bindType">
                                            <el-select v-model="form['bindType']" filterable style="width: 100%" @change="bindTypeChange">
                                                <el-option v-for="item in selectItems['bindType']" :key="item.value" :label="$t(item.name)" :value="item.value"></el-option>
                                            </el-select>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row :gutter="15" v-if="isShowContent" >
                                    <el-col :span="24">
                                        <el-form-item :label="$t('reportDesigner.bindField')" prop="bindField" >
                                            <el-select v-model="form['bindField']" filterable style="width: 100%" >
                                                <el-option v-for="item in selectItems['bindField']" :key="item.value" :label="$t(item.name)" :value="item.value"></el-option>
                                            </el-select>
                                        </el-form-item>
                                    </el-col>
                                </el-row>

                                <el-row :gutter="15" v-if="isShowCommonItem">
                                    <el-col :span="24">
                                        <el-form-item :label="$t('reportDesigner.ScanMode')" :prop="ScanModeId" >
                                            <el-select v-model="form['ScanModeId']" clearable style="width: 100%" @change="commonChange(0)">
                                                <el-option v-for="item in selectItems.modeList" :key="item.Id" :label="item.Value" :value="item.Id"></el-option>
                                            </el-select>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row :gutter="15" v-if="isShowCommonItem">
                                    <el-col :span="24">
                                        <el-form-item :label="$t('reportDesigner.MeasureItem')" :prop="MeasureItemId" >
                                            <el-select v-model="form['MeasureItemId']" clearable style="width: 100%" @change="commonChange(1)" :disabled="selectItems.measureItemList.length<=1?true:false">
                                                <el-option v-for="item in selectItems.measureItemList" :key="item.Id" :label="item.Value" :value="item.Id"></el-option>
                                            </el-select>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row :gutter="15" v-if="isShowCommonItem">
                                    <el-col :span="24">
                                        <el-form-item :label="$t('reportDesigner.MeasureWay')" :prop="MeasureWayId" >
                                            <el-select v-model="form['MeasureWayId']" clearable style="width: 100%" @change="commonChange(2)" :disabled="selectItems.measureWayList.length<=1?true:false">
                                                <el-option v-for="item in selectItems.measureWayList" :key="item.Id" :label="item.Value" :value="item.Id"></el-option>
                                            </el-select>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row :gutter="15" v-if="isShowCommonItem">
                                    <el-col :span="24">
                                        <el-form-item :label="$t('reportDesigner.OutputItem')" :prop="OutputItemId" >
                                            <el-select v-model="form['OutputItemId']" clearable style="width: 100%" @change="commonChange(3)" :disabled="selectItems.outputList.length<=1?true:false">
                                                <el-option v-for="item in selectItems.outputList" :key="item.Id" :label="item.Value" :value="item.Id"></el-option>
                                            </el-select>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row :gutter="15" v-if="isShowCommonItem">
                                    <el-col :span="24">
                                        <el-form-item :label="$t('reportDesigner.UnitType')" :prop="UnitTypeId" >
                                            <el-select v-model="form['UnitTypeId']" clearable style="width: 100%" @change="commonChange(4)" :disabled="selectItems.unitList.length<=1?true:false">
                                                <el-option v-for="item in selectItems.unitList" :key="item.Id" :label="item.Value" :value="item.Id"></el-option>
                                            </el-select>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row :gutter="15" v-if="isShowCommonItem">
                                    <el-col :span="24">
                                        <el-form-item :label="$t('reportDesigner.UserDefineName')" :prop="UserDefineNameId" label-width="140px" >
                                            <el-select v-model="form['UserDefineNameId']" clearable style="width: 100%" @change="commonChange(5)" :disabled="selectItems.customNameList.length<=1?true:false">
                                                <el-option v-for="item in selectItems.customNameList" :key="item.Id" :label="item.Value" :value="item.Id"></el-option>
                                            </el-select>
                                        </el-form-item>
                                    </el-col>
                                </el-row>


                                <el-row :gutter="15" v-if="isShowOtherItem">
                                    <el-col :span="24">
                                        <el-form-item :label="$t('reportDesigner.ScanMode')" :prop="ScanModeId" >
                                            <el-select v-model="form['ScanModeId']" clearable style="width: 100%" @change="otherChange(0)">
                                                <el-option v-for="item in selectItems.modeList" :key="item.Id" :label="item.Value" :value="item.Id"></el-option>
                                            </el-select>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row :gutter="15" v-if="isShowOtherItem">
                                    <el-col :span="24">
                                        <el-form-item :label="$t('reportDesigner.MeasureCategory')" :prop="MeasureCategoryId" >
                                            <el-select v-model="form['MeasureCategoryId']" clearable style="width: 100%" @change="otherChange(1)" :disabled="selectItems.categoryList.length<=1?true:false">
                                                <el-option v-for="item in selectItems.categoryList" :key="item.Id" :label="item.Value" :value="item.Id"></el-option>
                                            </el-select>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row :gutter="15" v-if="isShowOtherItem">
                                    <el-col :span="24">
                                        <el-form-item :label="$t('reportDesigner.MeasureItem')" :prop="MeasureItemId" >
                                            <el-select v-model="form['MeasureItemId']" clearable style="width: 100%" @change="otherChange(2)" :disabled="selectItems.measureItemList.length<=1?true:false">
                                                <el-option v-for="item in selectItems.measureItemList" :key="item.Id" :label="item.Value" :value="item.Id"></el-option>
                                            </el-select>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row :gutter="15" v-if="isShowOtherItem">
                                    <el-col :span="24">
                                        <el-form-item :label="$t('reportDesigner.MeasureWay')" :prop="MeasureWayId" >
                                            <el-select v-model="form['MeasureWayId']" clearable style="width: 100%" @change="otherChange(3)" :disabled="selectItems.measureWayList.length<=1?true:false">
                                                <el-option v-for="item in selectItems.measureWayList" :key="item.Id" :label="item.Value" :value="item.Id"></el-option>
                                            </el-select>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row :gutter="15" v-if="isShowOtherItem">
                                    <el-col :span="24">
                                        <el-form-item :label="$t('reportDesigner.OutputItem')" :prop="OutputItemId" >
                                            <el-select v-model="form['OutputItemId']" clearable style="width: 100%" @change="otherChange(4)" :disabled="selectItems.outputList.length<=1?true:false">
                                                <el-option v-for="item in selectItems.outputList" :key="item.Id" :label="item.Value" :value="item.Id"></el-option>
                                            </el-select>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row :gutter="15" v-if="isShowOtherItem">
                                    <el-col :span="24">
                                        <el-form-item :label="$t('reportDesigner.MeasureCalculation')" :prop="MeasureCalculationId" >
                                            <el-select v-model="form['MeasureCalculationId']" clearable style="width: 100%" @change="otherChange(5)" :disabled="selectItems.caculateItemList.length<=1?true:false">
                                                <el-option v-for="item in selectItems.caculateItemList" :key="item.Id" :label="item.Value" :value="item.Id"></el-option>
                                            </el-select>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row :gutter="15" v-if="isShowOtherItem">
                                    <el-col :span="24">
                                        <el-form-item :label="$t('reportDesigner.UnitType')" :prop="UnitTypeId" >
                                            <el-select v-model="form['UnitTypeId']" clearable style="width: 100%" @change="otherChange(6)" :disabled="selectItems.unitList.length<=1?true:false">
                                                <el-option v-for="item in selectItems.unitList" :key="item.Id" :label="item.Value" :value="item.Id"></el-option>
                                            </el-select>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row :gutter="15">
                                    <el-col :span="24" style="padding-left:40%;">
                                        <el-button type="primary" size="default" v-show="(currentActiveItem && currentActiveItem.id && currentActiveItem.id.length > 0 ? true : false)" @click="addMeasure">{{ $t('reportLabel.addItem') }}</el-button>
                                    </el-col>
                                </el-row>
                            </el-form>
                        </el-card>
                    </el-col>
                    <el-col :span="14">
                        <el-card class="box-card">
                            <template #header>
                                <div class="card-header">
                                    <div>
                                        <span>{{ $t('reportDesigner.CalculationOperator') }}</span>
                                    </div>
                                    <div>
                                        <el-row>
                                            <el-col :span="6" v-for="btn in operators" :key="btn">
                                                <el-button class="operatorBtn" @click="addOperator(btn.name)">{{btn.name}}</el-button>
                                            </el-col>
                                        </el-row>
                                    </div>
                                </div>
                            </template>
                        </el-card>
                    </el-col>
                </el-row>
            </el-col>
        </el-row>
    </el-dialog>
</template>
<script>
import save from "./saveContentConfig";
import common from '@/utils/common';

export default {
    components: {
        save,
    },
    data() {
        return {
            form: {
                bindType: "",
                bindField: "",

                UnitType: "",
                MeasureCalculation: "",
                OutputItem: "",
                MeasureWay: "",
                MeasureItem: "",
                MeasureCategory: "",
                ScanMode: "",
                UserDefineName: "",
                
                UnitTypeId: 0,
                MeasureCalculationId: 0,
                OutputItemId: 0,
                MeasureWayId: 0,
                MeasureItemId: 0,
                MeasureCategoryId: 0,
                ScanModeId: 0,
                UserDefineNameId: 0,
            },
            selectItems: {
                bindType: [
                    {
                        name:"measurement.Default",
                        value:"General"
                    },
                    {
                        name:"measurement.Custom",
                        value:"CommonMeasure"
                    },
                    {
                        name:"measurement.Common",
                        value:"OtherMeasure"
                    }
                ],
                bindField: [
                    {
                        name:"measurement.PSAName",
                        value:"[PSA\"ng;PatientInfo\"]",
                        key:"3969C8BE-7715-4423-919E-51D6EBEBFA8E"
                    },
                    {
                        name:"measurement.PPSAName",
                        value:"[PPSA Coefficient\";PatientInfo\"]",
                        key:"C64CC111-3557-4B29-ACBF-678816235694"
                    },
                    {
                        name:"measurement.Height",
                        value:"[Height\"cm;PatientInfo\"]",
                        key:"9556EA12-CFB3-4AD5-8717-1057D1D5A3EE"
                    },
                    {
                        name:"measurement.BSA",
                        value:"[BSA\"m2;PatientInfo\"]",
                        key:"5AAC30D9-FCE3-4FD0-998C-3A9E2D54F312"
                    },
                    {
                        name:"measurement.Weight",
                        value:"[Weight\"kg;PatientInfo\"]",
                        key:"0003B8C3-397C-4688-985D-55B757E3855D"
                    },
                    {
                        name:"measurement.HR",
                        value:"[HR\"HR;PatientInfo\"]",
                        key:"33D46E3F-A089-4B0A-8D1D-F4C7556BD62D"
                    },
                    {
                        name:"measurement.GA",
                        value:"[GA\"day;PatientInfo\"]",
                        key:"98bbaa61-a91d-4399-a3f4-df29117d2d59"
                    }
                ],
                modeList:[],
                categoryList:[],
                measureItemList:[],
                measureWayList:[],
                outputList:[],
                unitList:[],
                customNameList:[],
                caculateItemList:[],

                unitOptions: []
            },
            isShowCommonItem: false,
            isShowOtherItem: false,
            isShowContent:true,
            visible: false,
            newItemText: "",
            unitOptions: [],
            measureDataJson: {},
            operators:[
                {
                    name:"sin(x)",
                },{
                    name:"cos(x)",
                },{
                    name:"tan(x)",
                },{
                    name:"atan(x)",
                },{
                    name:"min(x,y,...)",
                },{
                    name:"max(x,y,...)",
                },{
                    name:"sqrt(x)",
                },{
                    name:"pow(x,y)",
                },{
                    name:"exp(x)",
                },{
                    name:"In(x)",
                },{
                    name:"log(x,y)",
                },{
                    name:"avg(x,y,...)",
                },{
                    name:"3.1415926",
                },{
                    name:"abs(x)",
                }
            ],
            items: [],
            currentActiveItem: {

            },
            objOperators: {
                'sin': 1,
                'cos': 1,
                'tan': 1,
                'atan': 1,
                'min': 1,
                'max': 1,
                'sqrt': 1,
                'pow': 1,
                'exp': 1,
                'In': 1,
                'log': 1,
                'avg': 1,
                '3.1415926': 1,
                'abs': 1,
            }
        }
    },
    methods: {
        changeInput() {
            var newText = this.currentActiveItem.formula.replaceAll("　","");
            this.currentActiveItem.formula = newText;
        },
        //附加单位
        additionUnitTypeClick() {
            this.selectItems.unitOptions.some(d => {
                if (this.currentActiveItem.additionUnitTypeText === d.value) {
                    this.currentActiveItem.additionUnitType = d;
                    return true;
                }
            });
        },
        //添加
        addMeasure() {
            //当前选中项
            if (this.form['bindType'] === "CommonMeasure") {
                this.selectItems.modeList.some(c => {
                    if (this.form.ScanModeId === c.Id)
                    {
                        this.currentActiveItem.bindContentItem.ScanMode = c;
                        return true;
                    }
                });
                this.selectItems.measureItemList.some(c => {
                    if (this.form.MeasureItemId === c.Id)
                    {
                        this.currentActiveItem.bindContentItem.MeasureItem = c;
                        return true;
                    }
                });
                this.selectItems.measureWayList.some(c => {
                    if (this.form.MeasureWayId === c.Id)
                    {
                        this.currentActiveItem.bindContentItem.MeasureWay = c;
                        return true;
                    }
                });
                this.selectItems.outputList.some(c => {
                    if (this.form.OutputItemId === c.Id)
                    {
                        this.currentActiveItem.bindContentItem.OutputItem = c;
                        return true;
                    }
                });
                this.selectItems.unitList.some(c => {
                    if (this.form.UnitTypeId === c.Id)
                    {
                        this.currentActiveItem.bindContentItem.UnitTypeItem = c;
                        return true;
                    }
                });
                this.selectItems.customNameList.some(c => {
                    if (this.form.UserDefineNameId === c.Id)
                    {
                        this.currentActiveItem.bindContentItem.UserDefineName = c;
                        return true;
                    }
                });
                this.currentActiveItem.generalContentItem = {};
            }
            else if (this.form['bindType'] === "OtherMeasure") {
                this.selectItems.outputList.some(c => {
                    if (this.form.OutputItemId === c.Id)
                    {
                        this.currentActiveItem.bindContentItem.OutputItem = c;
                        return true;
                    }
                });
                this.selectItems.modeList.some(c => {
                    if (this.form.ScanModeId === c.Id)
                    {
                        this.currentActiveItem.bindContentItem.ScanMode = c;
                        return true;
                    }
                });
                this.selectItems.caculateItemList.some(c => {
                    if (this.form.MeasureCalculationId === c.Id)
                    {
                        this.currentActiveItem.bindContentItem.MeasureCalculation = c;
                        return true;
                    }
                });
                this.selectItems.unitList.some(c => {
                    if (this.form.UnitTypeId === c.Id)
                    {
                        this.currentActiveItem.bindContentItem.UnitTypeItem = c;
                        return true;
                    }
                });
                this.selectItems.categoryList.some(c => {
                    if (this.form.MeasureCategoryId === c.Id)
                    {
                        this.currentActiveItem.bindContentItem.MeasureCategory = c;
                        return true;
                    }
                });
                this.selectItems.measureItemList.some(c => {
                    if (this.form.MeasureItemId === c.Id)
                    {
                        this.currentActiveItem.bindContentItem.MeasureItem = c;
                        return true;
                    }
                });
                this.selectItems.measureWayList.some(c => {
                    if (this.form.MeasureWayId === c.Id)
                    {
                        this.currentActiveItem.bindContentItem.MeasureWay = c;
                        return true;
                    }
                });
                this.currentActiveItem.generalContentItem = {};
            }
            else {
                //general
                this.selectItems.bindField.some(c => {
                    if (this.form.bindField === c.value)
                    {
                        this.currentActiveItem.generalContentItem = c;
                        return true;
                    }
                });
                this.currentActiveItem.bindContentItem = {
                    UnitTypeItem: {},
                    MeasureCalculation: {},
                    OutputItem: {},
                    MeasureWay: {},
                    MeasureItem: {},
                    MeasureCategory: {},
                    ScanMode: {},
                    UserDefineName: {},
                };
            }
            if (this.form['bindType'] === "CommonMeasure" || this.form['bindType'] === "OtherMeasure") {
                if (this.currentActiveItem.bindContentItem) {
                    var curTitle = "[";
                    if (this.currentActiveItem.bindContentItem.MeasureItem) {
                        curTitle += this.currentActiveItem.bindContentItem.MeasureItem.Key;
                    }
                    if (this.currentActiveItem.bindContentItem.OutputItem) {
                        curTitle += "|" + this.currentActiveItem.bindContentItem.OutputItem.Key;
                    }
                    if (this.currentActiveItem.bindContentItem.UnitTypeItem) {
                        curTitle += '"' + this.currentActiveItem.bindContentItem.UnitTypeItem.Key;
                    }
                    if (this.currentActiveItem.bindContentItem.ScanMode) {
                        curTitle += ';' + this.currentActiveItem.bindContentItem.ScanMode.Key + '"';
                    }
                    curTitle += "]";
                    this.currentActiveItem.formula += curTitle;
                }
            }
            else {
                if (this.currentActiveItem.generalContentItem) {
                    this.currentActiveItem.formula += this.currentActiveItem.generalContentItem.value;
                }
            }
        },
        //验算公式
        validateFormulaData() {
            this.validateFormulaDetail(this.currentActiveItem.formula, true);
        },
        validateFormulaDetail(formulaContent, isShowHint) {
            var formulaText = formulaContent;
            if (formulaText === "" || /\[\]/.test(formulaText)) {
                if (isShowHint) {
                    this.$message.warning(this.$t("reportDesigner.MeasureVerifyFail"));
                }
                return false;
            }

            //验证[]是否配对
            var stack2 = [];
            var stackTextList = [];
            var tempStackStr = "";
            for(var i = 0, item; i < formulaText.length; i++) {
                item = formulaText.charAt(i);
                if ('[' === item) {
                    tempStackStr = "";
                    stack2.push('[');
                }
                else if (']' === item) {
                    if (stack2.length > 0){
                        stack2.pop();
                        tempStackStr += "]";
                        stackTextList.push(tempStackStr);
                    }
                    else {
                        return false;
                    }
                }
                tempStackStr += item;
            }
            if (0 !== stack2.length) {
                if (isShowHint) {
                    this.$message.warning(this.$t("reportDesigner.MeasureVerifyFail"));
                }
                return false;
            }
            //验证[]公式是否正确
            if (stackTextList && stackTextList.length > 0) {
                var tempResult = true;
                stackTextList.some(c => {
                    var data = c.replace("[","").replace("]","");
                    var resultTempValue = this.validateFormulaContent(data);
                    if (!resultTempValue) {
                        tempResult = tempResult & false;
                    }
                })
                if (!tempResult) {
                    if (isShowHint) {
                        this.$message.warning(this.$t("reportDesigner.MeasureVerifyFail"));
                    }
                    return false;
                }
                stackTextList.some(c => {
                    formulaText = formulaText.replace(c, '`');
                });
                //去掉空格
                formulaText = formulaText.replace(/\s/g, '');
                // 错误情况，括号不配对
                var stack1 = [];
                for(var j = 0, item1; j < formulaText.length; j++){
                    item1 = formulaText.charAt(j);
                    if ('(' === item1) {
                        stack1.push('(');
                    }
                    else if (')' === item1) {
                        if (stack1.length > 0) {
                            stack1.pop();
                        }
                        else {
                            return false;
                        }
                    }
                }
                if(0 !== stack1.length) {
                    if (isShowHint) {
                        this.$message.warning(this.$t("reportDesigner.MeasureVerifyFail"));
                    }
                    return false;
                }
                //u002B +
                //u002D -
                //u002A *
                //u002F /
                //u002C ,
                //u0028 (
                //u0029 )
                //u002E .
                // 错误情况，运算符连续  ||  空括号  || 错误情况，(后面是运算符  || 错误情况，)前面是运算符
                // 错误情况，运算符号不能在首末位 || 错误情况，中括号结尾到下一个开始之间没有运算符  || 错误情况，括号结尾到下一个开始之间没有运算符
                if( /[\u002B\u002D\u002A\u002F]{2,}/.test(formulaText) || /\(\)/.test(formulaText) || /\([\u002B\u002D\u002A\u002F]/.test(formulaText) || /[\u002B\u002D\u002A\u002F]\)/.test(formulaText) 
                   || /^[\u002B\u002D\u002A\u002F.]|[\u002B\u002D\u002A\u002F.]$/.test(formulaText) || /\]\[/.test(formulaText) || /\)\(/.test(formulaText)) {
                    if (isShowHint) {
                        this.$message.warning(this.$t("reportDesigner.MeasureVerifyFail"));
                    }
                    return false;
                }
                // if( /[\+\-\*\/]{2,}/.test(formulaText) || /\(\)/.test(formulaText) || /\([\+\-\*\/]/.test(formulaText) || /[\+\-\*\/]\)/.test(formulaText) 
                //   || /^[\+\-\*\/.]|[\+\-\*\/.]$/.test(formulaText) || /\]\[/.test(formulaText) || /\)\(/.test(formulaText)) {
                //     this.$message.warning(this.$t("reportDesigner.MeasureVerifyFail"));
                // }
                // 错误情况，变量没有来自“待选公式变量”
                var tmpStr = formulaText.replace(/[\u002C\u0028\u0029\u002B\u002D\u002A\u002F]{1,}/g, '`');
                var array = tmpStr.split('`');
                var arrayResult = true;
                for (var l = 0, item2; l < array.length; l++) {
                    item2 = array[l];
                    if (item2 === "") {
                        arrayResult = arrayResult & true;
                    }
                    else if((/[A-Z]/i.test(item2) && 1 === this.objOperators[item2]) ){
                        arrayResult = arrayResult & true;
                    }
                    else if(/^[\d|\u002E]*$/.test(item2)) {
                        arrayResult = arrayResult & true;
                    }
                    else {
                        arrayResult = arrayResult & false;
                    }
                }
                if (arrayResult) {
                    if (isShowHint) {
                        this.$message.warning(this.$t("reportDesigner.MeasureVerifySuccess"));
                    }
                    return true;
                }
                else {
                    if (isShowHint) {
                        this.$message.warning(this.$t("reportDesigner.MeasureVerifyFail"));
                    }
                    return false;
                }
            }
            else {
                if (isShowHint) {
                    this.$message.warning(this.$t("reportDesigner.MeasureVerifyFail"));
                }
                return false;
            }
        },
        // oldMethod() {
        //     var curTitle = "";
        //     if (this.currentActiveItem.bindContentItem || this.currentActiveItem.generalContentItem) {
        //         if (this.currentActiveItem.generalContentItem) {
        //             this.selectItems.bindField.some(c => {
        //                 if (this.currentActiveItem.generalContentItem.value === c.value)
        //                 {
        //                     curTitle = c.value;
        //                     return true;
        //                 }
        //             });
        //         }
        //         else {
        //             curTitle = "[";
        //             if (this.currentActiveItem.bindContentItem.MeasureItem) {
        //                 if (this.currentActiveItem.bindContentItem.MeasureItem.Key) {
        //                     curTitle += this.currentActiveItem.bindContentItem.MeasureItem.Key;
        //                 }
        //             }
        //             if (this.currentActiveItem.bindContentItem.OutputItem) {
        //                 if (this.currentActiveItem.bindContentItem.OutputItem.Key) {
        //                     curTitle += "|" + this.currentActiveItem.bindContentItem.OutputItem.Key;
        //                 }
        //             }
        //             if (this.currentActiveItem.bindContentItem.UnitTypeItem) {
        //                 if (this.currentActiveItem.bindContentItem.UnitTypeItem.Key) {
        //                     curTitle += '"' + this.currentActiveItem.bindContentItem.UnitTypeItem.Key;
        //                 }
        //             }
        //             if (this.currentActiveItem.bindContentItem.ScanMode) {
        //                 if (this.currentActiveItem.bindContentItem.ScanMode.Key) {
        //                     curTitle += ';' + this.currentActiveItem.bindContentItem.ScanMode.Key + '"';
        //                 }
        //             }
        //             curTitle += "]";
        //         }
        //     }
        //     if (curTitle === this.currentActiveItem.formula) {
        //         this.$message.warning(this.$t("reportDesigner.MeasureVerifySuccess"));
        //     }
        //     else if (this.currentActiveItem.formula.indexOf(curTitle) > -1) {
        //         var result = this.validateOperatorFormula(this.currentActiveItem.formula, curTitle);
        //         if (result) {
        //             this.$message.warning(this.$t("reportDesigner.MeasureVerifySuccess"));
        //         }
        //         else {
        //             this.$message.warning(this.$t("reportDesigner.MeasureVerifyFail"));
        //         }
        //     }
        //     else {
        //         this.$message.warning(this.$t("reportDesigner.MeasureVerifyFail"));
        //     }
        // },
        // //验证运算函数
        // validateOperatorFormula(formulaContent, title) {
        //     if (this.operators && this.operators.length > 0) {
        //         var res = this.operators.some(c => {
        //             if (c.name === "sin(x)" || c.name === "cos(x)" || c.name === "tan(x)" || c.name === "atan(x)" || c.name === "sqrt(x)"
        //              || c.name === "exp(x)" || c.name === "In(x)" || c.name === "abs(x)") {
        //                 var operateContent = c.name.replace("(x)","(" + title + ")");
        //                 if (operateContent === formulaContent) {
        //                     return true;
        //                 }
        //                 else if (formulaContent.indexOf(operateContent) > -1){
        //                     var rCResult = this.validateOperatorFormula(formulaContent, operateContent);
        //                     return rCResult;
        //                 }
        //             }
        //             else if (c.name === "min(x,y,...)" || c.name === "max(x,y,...)" || c.name === "avg(x,y,...)" || 
        //                     c.name === "pow(x,y)" || c.name === "log(x,y)") {
        //                 if ((formulaContent.indexOf('pow(') === 0 && formulaContent.indexOf(')') === (formulaContent.length - 1))
        //                     || (formulaContent.indexOf('log(') === 0 && formulaContent.indexOf(')') === (formulaContent.length - 1))
        //                     || (formulaContent.indexOf('min(') === 0 && formulaContent.indexOf(')') === (formulaContent.length - 1))
        //                     || (formulaContent.indexOf('max(') === 0 && formulaContent.indexOf(')') === (formulaContent.length - 1))
        //                     || (formulaContent.indexOf('avg(') === 0 && formulaContent.indexOf(')') === (formulaContent.length - 1)))
        //                 {
        //                     var contentListStr = formulaContent.substring(4, (formulaContent.length - 2));
        //                     if (contentListStr && contentListStr.indexOf(',') > 0) {
        //                         var contentList = contentListStr.splice(',');
        //                         var tempResult = true;
        //                         contentList.some(d => {
        //                             var data = d.replace("[").replace("]");
        //                             if (!this.validateFormulaContent(data)) {
        //                                 tempResult = tempResult & false;
        //                             }
        //                         });
        //                         if (tempResult) {
        //                             return true;
        //                         }
        //                     }
        //                 }
        //             }
        //             else if (c.name === "3.1415926") {
        //                 //3.1415926
        //                 var newFormulaContent = formulaContent.replaceAll("+", ' ').replaceAll("-", ' ').replaceAll("*", ' ').replaceAll("/", ' ');
        //                 var piList = newFormulaContent.split(' ');
        //                 if (piList && piList.length > 0) {
        //                     var resTemp = true;
        //                     piList.some(f => {
        //                         if (f.indexOf(']') > 0) {
        //                             if (f.replace(/^\s*|\s*$/g, "") === title) {
        //                                 resTemp = resTemp & true;
        //                             }
        //                             else {
        //                                 resTemp = resTemp & false;
        //                             }
        //                         }
        //                         else {
        //                             if (f.replace(/^\s*|\s*$/g, "") === c.name) {
        //                                 resTemp = resTemp & true;
        //                             }
        //                             else if (this.validateFormulaContent(f.replace(/^\s*|\s*$/g, ""))) {
        //                                 resTemp = resTemp & true;
        //                             }
        //                             else {
        //                                 resTemp = resTemp & false;
        //                             }
        //                         }
        //                     });
        //                     return resTemp;
        //                 }
        //             }
        //             return false;
        //         });
        //         return res;
        //     }
        //     return false;
        // },
        //公式计算
        validateFormulaContent(data) {
            if (data.indexOf('|') > 0) {
                var measureItemStr = data.split('|')[0];
                var outputStr = "";
                var unitStr = "";
                var modeStr = "";
                var otherData = "";
                var measureItemId = 0;
                var outputId = 0;
                var unitId = 0;
                var modeId = 0;
                if (data.split('|')[1].indexOf('"') > 0) {
                    outputStr = data.split('|')[1].split('"')[0];
                    otherData = data.split('|')[1].split('"')[1];
                }
                if (otherData.indexOf(';') > 0) {
                    modeStr = otherData.split(';')[1];
                    unitStr = otherData.split(';')[0];
                }
                
                //CommonMeasure
                this.measureDataJson.model.some(c => {
                    if (modeStr === c.Key)
                    {
                        modeId = c.Id;
                        return true;
                    }
                });
                if (modeId > 0) {
                    this.measureDataJson.measureItem.some(c => {
                        if (c.PId === modeId && c.Key === measureItemStr)
                        {
                            measureItemId = c.Id;
                            return true;
                        }
                    });
                    if (measureItemId > 0) {
                        this.measureDataJson.measureMethod.some(c => {
                            if (c.PId === measureItemId)
                            {
                                this.measureDataJson.output.some(d => {
                                    if (d.PId === c.Id && d.Key === outputStr)
                                    {
                                        outputId = d.Id;
                                        return true;
                                    }
                                });
                                if (outputId > 0) {
                                    return true;
                                }
                            }
                        });
                        if (outputId > 0) {
                            this.measureDataJson.unit.some(c => {
                                if (c.PId === outputId && c.Key === unitStr)
                                {
                                    unitId = c.Id;
                                    return true;
                                }
                            });
                        }
                    }
                }
                if (measureItemId > 0 && outputId > 0 && unitId > 0 && modeId > 0) {
                    return true;
                }

                //OtherMeasure
                this.measureDataJson.model.some(c => {
                    if (modeStr === c.Key)
                    {
                        modeId = c.Id;
                        return true;
                    }
                });
                if (modeId > 0) {
                    this.measureDataJson.category.some(c => {
                        if (c.PId === modeId)
                        {
                            this.measureDataJson.measureItem.some(d => {
                                if (d.PId === c.Id && d.Key === measureItemStr)
                                {
                                    measureItemId = d.Id;
                                    return true;
                                }
                            });
                            if (measureItemId > 0) {
                                return true;
                            }
                        }
                    });
                    
                    if (measureItemId > 0) {
                        this.measureDataJson.measureMethod.some(c => {
                            if (c.PId === measureItemId)
                            {
                                this.measureDataJson.output.some(d => {
                                    if (d.PId === c.Id && d.Key === outputStr)
                                    {
                                        outputId = d.Id
                                        return true;
                                    }
                                });
                                if (outputId > 0) {
                                    return true;
                                }
                            }
                        });
                        if (outputId > 0) {
                            this.measureDataJson.caculateItem.some(c => {
                                if (c.PId === outputId)
                                {
                                    this.measureDataJson.unit.some(d => {
                                        if (d.PId === c.Id && d.Key === unitStr)
                                        {
                                            unitId = d.Id;
                                            return true;
                                        }
                                    });
                                }
                            });
                        }
                    }
                }
                if (measureItemId > 0 && outputId > 0 && unitId > 0 && modeId > 0) {
                    return true;
                }
            }
            else {
                //general
                var r = this.selectItems.bindField.some(c => {
                    if (data === c.value.replace("[","").replace("]",""))
                    {
                        return true;
                    }
                    return false;
                });
                if (r) {
                    return true;
                }
            }
            return false;
        },
        //设置选中，不选中
        setItemActive(indexItem) {
            this.items.some((c, index) => {
                if (index === indexItem) {
                    c.active = true;
                    this.currentActiveItem = c;
                }
                else {
                    c.active = false;
                }
            })
        },
        showActive(item) {
            var styleInfo = {
                background: "white"
            };
            if (item.active) {
                styleInfo.background = "#ECF5FF";
            }
            return styleInfo;
        },
        bindCommonData(type, jsonData) {
            if (type === 0) {
                if (this.measureDataJson.measureItem) 
                {
                    this.selectItems.measureItemList = [];
                    var comMeasureItemList = this.measureDataJson.measureItem.filter(c => c.Type === 2 && c.PId === this.form.ScanModeId);
                    comMeasureItemList.forEach((b, index2) => {
                        this.selectItems.measureItemList.push({
                            Id: b.Id, Key: b.Key, Value: (b.Value.length > 0 ? b.Value : " "), PId: b.PId
                        });
                        if (index2 === 0) {
                            this.form.MeasureItemId = b.Id;
                        }
                        if (jsonData && jsonData.BaseType) {
                            if (jsonData.BaseType === b.Key) 
                            {
                                this.form.MeasureItemId = b.Id;
                            }
                        }
                    });
                }
            }
            if (type === 0 || type === 1) {
                if (this.measureDataJson.measureMethod) 
                {
                    this.selectItems.measureWayList = [];
                    var comMeasureMethodList = this.measureDataJson.measureMethod.filter(c => c.Type === 2 && c.PId === this.form.MeasureItemId);
                    comMeasureMethodList.forEach((c, index3) => {
                        this.selectItems.measureWayList.push({
                            Id: c.Id, Key: c.Key, Value: (c.Value.length > 0 ? c.Value : " "), PId: c.PId
                        });
                        //this.selectItems.measureItemList.push(c.Key);
                        if (index3 === 0) {
                            this.form.MeasureWayId = c.Id;
                        }
                        if (jsonData && jsonData.Method) {
                            if (jsonData.Method === c.Key) 
                            {
                                this.form.MeasureWayId = c.Id;
                            }
                        }
                    });
                }
            }
            if (type === 0 || type === 1 || type === 2) {
                if (this.measureDataJson.output) 
                {
                    this.selectItems.outputList = [];
                    var comOutputList = this.measureDataJson.output.filter(c => c.Type === 2 && c.PId === this.form.MeasureWayId);
                    comOutputList.forEach((d, index4) => {
                        this.selectItems.outputList.push({
                            Id: d.Id, Key: d.Key, Value: (d.Value.length > 0 ? d.Value : " "), PId: d.PId
                        });
                        if (index4 === 0) {
                            this.form.OutputItemId = d.Id;
                        }
                        if (jsonData && jsonData.Output) {
                            if (jsonData.Output === d.Key) 
                            {
                                this.form.OutputItemId = d.Id;
                            }
                        }
                    });
                }
            }
            if (type === 0 || type === 1 || type === 2 || type === 3) {
                if (this.measureDataJson.unit) 
                {
                    this.selectItems.unitList = [];
                    var comUnitList = this.measureDataJson.unit.filter(c => c.Type === 2 && c.PId === this.form.OutputItemId);
                    comUnitList.forEach((e, index5) => {
                        this.selectItems.unitList.push({
                            Id: e.Id, Key: e.Key, Value: (e.Value.length > 0 ? e.Value : " "), PId: e.PId
                        });
                        if (index5 === 0) {
                            this.form.UnitTypeId = e.Id;
                        }
                        if (jsonData && jsonData.Unit) {
                            if (jsonData.Unit === e.Key) 
                            {
                                this.form.UnitTypeId = e.Id;
                            }
                        }
                    });
                }
            }
            if (type === 0 || type === 1 || type === 2 || type === 3 || type === 4) {
                if (this.measureDataJson.type) 
                {
                    this.selectItems.customNameList = [];
                    var comTypeList = this.measureDataJson.type.filter(c => c.Type === 2 && c.PId === this.form.UnitTypeId);
                    comTypeList.forEach((f, index6) => {
                        this.selectItems.customNameList.push({
                            Id: f.Id, Key: f.Key, Value: (f.Value.length > 0 ? f.Value : " "), PId: f.PId
                        });
                        if (index6 === 0) {
                            this.form.UserDefineNameId = f.Id;
                        }
                        if (jsonData && jsonData.UserId) {
                            if (jsonData.UserId.indexOf(f.Key) > 0) 
                            {
                                this.form.UserDefineNameId = f.Id;
                            }
                        }
                    });
                }
            }
        },
        commonChange(type) {
            this.bindCommonData(type);
            this.selectItems.modeList.some(c => {
                if (this.form.ScanModeId === c.Id)
                {
                    this.form.ScanMode = c.Key;
                    return true;
                }
            });
            this.selectItems.measureItemList.some(c => {
                if (this.form.MeasureItemId === c.Id)
                {
                    this.form.MeasureItem = c.Key;
                    return true;
                }
            });
            this.selectItems.measureWayList.some(c => {
                if (this.form.MeasureWayId === c.Id)
                {
                    this.form.MeasureWay = c.Key;
                    return true;
                }
            });
            this.selectItems.outputList.some(c => {
                if (this.form.OutputItemId === c.Id)
                {
                    this.form.OutputItem = c.Key;
                    return true;
                }
            });
            this.selectItems.unitList.some(c => {
                if (this.form.UnitTypeId === c.Id)
                {
                    this.form.UnitType = c.Key;
                    return true;
                }
            });
            this.selectItems.customNameList.some(c => {
                if (this.form.UserDefineNameId === c.Id)
                {
                    this.form.UserDefineName = c.Key;
                    return true;
                }
            });
        },
        otherChange(type) {
            this.bindOtherData(type);
            this.selectItems.outputList.some(c => {
                if (this.form.OutputItemId === c.Id)
                {
                    this.form.OutputItem = c.Key;
                    return true;
                }
            });
            this.selectItems.modeList.some(c => {
                if (this.form.ScanModeId === c.Id)
                {
                    this.form.ScanMode = c.Key;
                    return true;
                }
            });
            this.selectItems.caculateItemList.some(c => {
                if (this.form.MeasureCalculationId === c.Id)
                {
                    this.form.MeasureCalculation = c.Key;
                    return true;
                }
            });
            this.selectItems.unitList.some(c => {
                if (this.form.UnitTypeId === c.Id)
                {
                    this.form.UnitType = c.Key;
                    return true;
                }
            });
            this.selectItems.categoryList.some(c => {
                if (this.form.MeasureCategoryId === c.Id)
                {
                    this.form.MeasureCategory = c.Key;
                    return true;
                }
            });
            this.selectItems.measureItemList.some(c => {
                if (this.form.MeasureItemId === c.Id)
                {
                    this.form.MeasureItem = c.Key;
                    return true;
                }
            });
            this.selectItems.measureWayList.some(c => {
                if (this.form.MeasureWayId === c.Id)
                {
                    this.form.MeasureWay = c.Key;
                    return true;
                }
            });
            // if (this.measureDataJson.type) 
            // {
            //     var typeTempList = this.measureDataJson.type.filter(c => c.Type === 3 && c.PId === this.form.UnitTypeId);
            //     if (typeTempList && typeTempList.length > 0) {
            //         this.form.UserDefineNameId = typeTempList[0].Id;
            //         this.form.UserDefineName = typeTempList[0].Key;
            //     }
            // }
        },
        bindOtherData(type, jsonData) {
            if (type === 0) {
                if (this.measureDataJson.category) 
                {
                    this.selectItems.categoryList = [];
                    var comCategoryList = this.measureDataJson.category.filter(c => c.Type === 3 && c.PId === this.form.ScanModeId);
                    comCategoryList.forEach((m, index7) => {
                        this.selectItems.categoryList.push({
                            Id: m.Id, Key: m.Key, Value: (m.Value.length > 0 ? m.Value : " "), PId: m.PId
                        });
                        if (index7 === 0) {
                            this.form.MeasureCategoryId = m.Id;
                        }
                    });
                }
            }
            if (type === 0 || type === 1) {
                if (this.measureDataJson.measureItem) 
                {
                    this.selectItems.measureItemList = [];
                    var comMeasureItemList = this.measureDataJson.measureItem.filter(c => c.Type === 3 && c.PId === this.form.MeasureCategoryId);
                    comMeasureItemList.forEach((b, index2) => {
                        this.selectItems.measureItemList.push({
                            Id: b.Id, Key: b.Key, Value: (b.Value.length > 0 ? b.Value : " "), PId: b.PId
                        });
                        if (index2 === 0) {
                            this.form.MeasureItemId = b.Id;
                        }
                        if (jsonData && jsonData.BaseType) {
                            if (jsonData.BaseType === b.Key) 
                            {
                                this.form.MeasureItemId = b.Id;
                                this.form.MeasureCategoryId = b.PId;
                            }
                        }
                    });
                }
            }
            if (type === 0 || type === 1 || type === 2) {
                if (this.measureDataJson.measureMethod) 
                {
                    this.selectItems.measureWayList = [];
                    var comMeasureMethodList = this.measureDataJson.measureMethod.filter(c => c.Type === 3 && c.PId === this.form.MeasureItemId);
                    comMeasureMethodList.forEach((c, index3) => {
                        this.selectItems.measureWayList.push({
                            Id: c.Id, Key: c.Key, Value: (c.Value.length > 0 ? c.Value : " "), PId: c.PId
                        });
                        //this.selectItems.measureItemList.push(c.Key);
                        if (index3 === 0) {
                            this.form.MeasureWayId = c.Id;
                        }
                        if (jsonData && jsonData.Method) {
                            if (jsonData.Method === c.Key) 
                            {
                                this.form.MeasureWayId = c.Id;
                            }
                        }
                    });
                }
            }
            if (type === 0 || type === 1 || type === 2 || type === 3) {
                if (this.measureDataJson.output) 
                {
                    this.selectItems.outputList = [];
                    var comOutputList = this.measureDataJson.output.filter(c => c.Type === 3 && c.PId === this.form.MeasureWayId);
                    comOutputList.forEach((d, index4) => {
                        this.selectItems.outputList.push({
                            Id: d.Id, Key: d.Key, Value: (d.Value.length > 0 ? d.Value : " "), PId: d.PId
                        });
                        if (index4 === 0) {
                            this.form.OutputItemId = d.Id;
                        }
                        if (jsonData && jsonData.Output) {
                            if (jsonData.Output === d.Key) 
                            {
                                this.form.OutputItemId = d.Id;
                            }
                        }
                    });
                }
            }
            if (type === 0 || type === 1 || type === 2 || type === 3 || type === 4) {
                if (this.measureDataJson.caculateItem) 
                {
                    this.selectItems.caculateItemList = [];
                    var comCaculateItemList = this.measureDataJson.caculateItem.filter(c => c.Type === 3 && c.PId === this.form.OutputItemId);
                    comCaculateItemList.forEach((f, index6) => {
                        this.selectItems.caculateItemList.push({
                            Id: f.Id, Key: f.Key, Value: (f.Value.length > 0 ? f.Value : " "), PId: f.PId
                        });
                        if (index6 === 0) {
                            this.form.MeasureCalculationId = f.Id;
                        }
                        if (jsonData && jsonData.CalculationId) {
                            if (jsonData.CalculationId === f.Key) 
                            {
                                this.form.MeasureCalculationId = f.Id;
                            }
                        }
                    });
                }
            }
            if (type === 0 || type === 1 || type === 2 || type === 3 || type === 4 || type === 5) {
                if (this.measureDataJson.unit) 
                {
                    this.selectItems.unitList = [];
                    var comUnitList = this.measureDataJson.unit.filter(c => c.Type === 3 && c.PId === this.form.MeasureCalculationId);
                    comUnitList.forEach((e, index5) => {
                        this.selectItems.unitList.push({
                            Id: e.Id, Key: e.Key, Value: (e.Value.length > 0 ? e.Value : " "), PId: e.PId
                        });
                        if (index5 === 0) {
                            this.form.UnitTypeId = e.Id;
                            if (jsonData && jsonData.Unit) {
                                if (jsonData.Unit === e.Key) 
                                {
                                    this.form.UnitTypeId = e.Id;
                                }
                            }
                            //加载type信息用于绑定到json数据中
                            if (this.measureDataJson.type) 
                            {
                                var typeTempList = this.measureDataJson.type.filter(c => c.Type === 3 && c.PId === this.form.UnitTypeId);
                                if (typeTempList && typeTempList.length > 0) {
                                    this.form.UserDefineNameId = typeTempList[0].Id;
                                }
                                if (jsonData && jsonData.UserId) {
                                    typeTempList = this.measureDataJson.type.filter(c => c.Type === 3 && c.PId === this.form.UnitTypeId && c.Key === jsonData.UserId);
                                    if (typeTempList && typeTempList.length > 0) {
                                        this.form.UserDefineNameId = typeTempList[0].Id;
                                    }
                                }
                            }
                        }
                    });
                }
            }
        },
        //设置样式
        setFormItemStyle(index, length) {
            var styleInfo = "";
            if (index === (length - 1))
            {
                styleInfo += "margin-bottom: 5px;";
            }
            return styleInfo;
        },
        //赋值--初始化
        open(data, itemsData, unitOptionList) {
            this.visible = true;
            if (data) {
                this.measureDataJson = data;
                this.items = itemsData;
                this.form['bindType'] = "General";
                this.form['bindField'] = "[PSA\"ng;PatientInfo\"]";
                this.items.some(c => {
                    if (c.active) {
                        this.currentActiveItem = c;
                    }
                });
            }
            if (unitOptionList && unitOptionList.length > 0) {
                this.selectItems.unitOptions = unitOptionList;
            }
        },
        async getCommonData(jsonData) {
            if (this.measureDataJson) {
                this.selectItems.modeList = [];
                this.selectItems.measureItemList = [];
                this.selectItems.measureWayList = [];
                this.selectItems.outputList = [];
                this.selectItems.unitList = [];
                this.selectItems.customNameList = [];
            
                if (this.measureDataJson.model) 
                {
                    var comModelList = this.measureDataJson.model.filter(c => c.Type === 2);
                    comModelList.forEach((a, index1) => {
                        if (a.Value === "2D" || a.Value === "Doppler" || a.Value === "M") {
                            this.selectItems.modeList.push({
                                Id: a.Id, Key: a.Key, Value: a.Value, PId: a.PId
                            });
                            if (index1 === 0) {
                                this.form.ScanModeId = a.Id;
                            }
                            if (jsonData && jsonData.Mode && jsonData.Mode.length > 0) {
                                if (jsonData.Mode === a.Key) 
                                {
                                    this.form.ScanModeId = a.Id;
                                }
                            }
                        }
                    });
                }
                this.bindCommonData(0, jsonData);
            }
        },
        async getOtherData(jsonData) {
            if (this.measureDataJson) {
                this.selectItems.outputList = [];
                this.selectItems.modeList = [];
                this.selectItems.caculateItemList= [];
                this.selectItems.unitList = [];
                this.selectItems.categoryList = [];
                this.selectItems.measureItemList = [];
                this.selectItems.measureWayList = [];
                if (this.measureDataJson.model) 
                {
                    var comModelList = this.measureDataJson.model.filter(c => c.Type === 3);
                    comModelList.forEach((a, index1) => {
                        if (a.Value === "2D" || a.Value === "Doppler" || a.Value === "M") {
                            this.selectItems.modeList.push({
                                Id: a.Id, Key: a.Key, Value: a.Value, PId: a.PId
                            });
                            if (index1 === 0) {
                                this.form.ScanModeId = a.Id;
                            }
                            if (jsonData && jsonData.Mode && jsonData.Mode.length > 0) {
                                if (jsonData.Mode === a.Key) 
                                {
                                    this.form.ScanModeId = a.Id;
                                }
                            }
                        }
                    });
                }
                this.bindOtherData(0, jsonData);    
            }
        },
        //提交
        bindTypeChange() {
            if (this.form['bindType'] === "CommonMeasure" ) {
                this.isShowCommonItem = true;
                this.isShowOtherItem = false;
                this.isShowContent = false;
                this.getCommonData();
            }
            else if (this.form['bindType'] === "OtherMeasure") {
                this.isShowCommonItem = false;
                this.isShowOtherItem = true;
                this.isShowContent = false;
                this.getOtherData();
            }
            else {
                this.isShowCommonItem = false;
                this.isShowOtherItem = false;
                this.isShowContent = true;
                this.form["bindField"] = "[PSA\"ng;PatientInfo\"]";
            }
            
        },
        confirm() {
            var res = true;
            if (this.items && this.items.length > 0) {
                this.items.some(c => {
                    if (c.formula && c.formula.length > 0) {
                        var resTemp = this.validateFormulaDetail(c.formula, false);
                        res = res & resTemp;
                    }
                    else {
                        res = res & false;
                    }
                });
            }
            if (!res) {
                this.$message.warning(this.$t("reportDesigner.CustomMeasureItemVerifyFail"));
                return;
            }
            this.$emit("setCalculationItem", this.items);
        },
        //添加自定义计算项名称
        addInputData() {
            if (this.newItemText && this.newItemText.length > 0) {
                if (!this.items.some(c => c.title === this.newItemText)) {
                    this.items.push({
                        id: common.guid(),
                        active: false,
                        title: this.newItemText,
                        formula: "",
                        additionUnitTypeText: "",
                        bindContentItem: {
                            UnitTypeItem: {},
                            MeasureCalculation: {},
                            OutputItem: {},
                            MeasureWay: {},
                            MeasureItem: {},
                            MeasureCategory: {},
                            ScanMode: {},
                            UserDefineName: {},
                        },
                        additionUnitType: {},
                        generalContentItem: {}
                    });
                }
            }
        },
        sortAsc(x, y) {
            if (x < y) {
                return -1;
            } else if (x > y) {
                return 1;
            }
            else {
                return 0;
            }
        },
        sortDesc(x, y) {
            return this.sortAsc(y, x);
        },
        //删除左边计算项
        removeItem(id) {
            var warningMsg = this.$t("dialogBox.confirmDeleteText");
            this.$confirm(warningMsg, this.$t('dialogBox.confirmWarningTitle'), {
                type: 'warning',
                confirmButtonText: this.$t('dialogBox.confirmButtonText'),
                cancelButtonText: this.$t('dialogBox.cancelButtonText'),
                confirmButtonClass: 'el-button--danger'
            }).then(() => {
                var filterList = this.items.filter(c => c.id === id);
                if (filterList && filterList.length > 0) {
                    var filterIndex = this.items.indexOf(filterList[0]);
                    if (filterIndex > -1) {
                        if (id === this.currentActiveItem.id) {
                            this.currentActiveItem = {};
                        }
                        this.items.splice(filterIndex, 1);
                    }
                }
            }).catch(() => {
                console.log("Detele Report Calculateion Exception")
            });
        },
        //添加运算符公式
        addOperator(operator){
            if(this.currentActiveItem && this.currentActiveItem.formula && operator.indexOf("(x)") > -1){
                this.currentActiveItem.formula = operator.replace("(x)","("+ this.currentActiveItem.formula +")");
            }
            else
            {
                this.currentActiveItem.formula += operator;
            }
        }
    },
    computed: {
    }
}
</script>

<style>
.box-card, .card-header {
    margin: 5px;
    height: 100%;
}

.formulaCard {
    height:120px;
}

.searchInput {
    margin: 2px;
    width: 200px;
}

.searchButton {
    margin: 2px;
    display: inline;
}

.contentList {
    border: 1px solid slategray;
    overflow: auto;
    height: 440px;
}

.contentList>.item {
    margin: 2px;
    display:flex;
    justify-content:space-between;
    cursor:pointer;
}

.formulaContent {
    margin: 2px;
    text-align: center;
    justify-content: left;
    align-items: center;
    display: flex;
}

.formulaInput {
    margin: 5px;
    width: 400px;
}

.formulaButton {
    margin: 5px;
    display: inline-block;
}

.operatorBtn {
    height: 80px;
    width: 80px;
    margin: 2px;
    font-style: italic;
    background-color: 	#87CEFF;
}

.el-form-item__label {
    flex: 0 0 auto;
    text-align: right;
    font-size: var(--el-form-label-font-size);
    color: var(--el-text-color-regular);
    line-height: 32px;
    padding: 0 12px 0 0;
    box-sizing: border-box;
    font-weight: bold;
}
</style>