<template>
    <view class="math-container">
        <view class="header">
            <u-image width="100%" height="300rpx" :src="mathImgs[name].big" mode="aspectFit" @click="handleBigImage"></u-image>
        </view>
        <view class="main dir-top-wrap cross-center">
            <view class="form">
                <u-form-item label="次边高(cm)" label-width="160">
                    <u-input type="number" v-model="formData.cibianheight" placeholder="请输入次底高"/>
                </u-form-item>
                <u-form-item label="次底宽(cm)" label-width="160">
                    <u-input type="number" v-model="formData.cidilength" placeholder="请输入次底宽"/>
                </u-form-item>
            </view>
            <view class="btn-group main-left cross-center">
                <u-button :ripple="true" @click="handelCalc" :custom-style="{backgroundColor: $u.color['mainBgColor'],color:'#ffffff',width: '260rpx',marginRight:'30rpx'}">计算</u-button>
                <u-button :ripple="true" @click="handleClear">清空</u-button>
            </view>
        </view>
        <div class="footer">
            <view class="result dir-top-wrap cross-center" v-if="showResult">
                <view v-for="item in rules" v-if="item.value && item.show">
                    <text>{{item.name}}</text>{{item.value}}{{item.unit}}
                </view>
            </view>
            <view class="title">计算图</view>
            <view class="calc-img">
                <text class="cibianheight1">{{formData.cibianheight?$util.round(formData.cibianheight,2):'**'}}cm</text>
                <text class="cidilength">{{formData.cidilength?$util.round(formData.cidilength,2):'**'}}cm</text>
                <text class="cibianheight2">{{formData.cibianheight?$util.round(formData.cibianheight,2):'**'}}cm</text>
                <text class="cibianheight3">{{formData.cibianheight?$util.round(formData.cibianheight,2):'**'}}cm</text>
                <text class="slice1">{{rules.result.value?rules.result.value:'**'}}{{rules.result.unit}}</text>
                <text class="slice2">{{rules.result.value?rules.result.value:'**'}}{{rules.result.unit}}</text>
                <u-image width="100%" height="800rpx" :src="mathImgs[name].calc" mode="widthFix"></u-image>
            </view>
            <view class="title">切割图</view>
            <u-image width="100%" height="800rpx" :src="mathImgs[name].slice" mode="widthFix"></u-image>

        </div>
    </view>
</template>

<script>
    import mathImgs from "@/core/math-imgs"
    // 万能公式
    export default {
        data() {
            return {
                name: 'santongwanHorizontal',
                mathImgs: mathImgs,
                formData: {
                    cibianheight: '',
                    cidilength: '',
                },
                // 用来验证 输入
                rules: {
                    result: {name:'', value:'',unit:'cm',show: true},
                }
            }
        },
        methods: {
            handleBigImage(){
                uni.previewImage({
                    urls: [mathImgs[this.name].big],
                });
            },
            handelCalc(){
                let validate = [this.formData.cibianheight,this.formData.cidilength]
                if(!this.$util.checkArrayNotNullNumber(validate)){
                    this.$u.toast('至少输入两个参数');
                    return
                }
                this.initRules();
                /**
                 结果=次边高×1.414
                 */
                this.rules.result.value = this.formData.cibianheight * 1.414
                this.roundRules();
                this.$u.toast("请参考计算示意图")
            },
            roundRules(){
                for (const itemKey in this.rules) {
                    this.rules[itemKey].value = this.$util.round(this.rules[itemKey].value,2);
                }
            },
            initRules(){
                for (const itemKey in this.rules) {
                    this.rules[itemKey].value = "";
                }
            },
            handleClear() {
                this.initRules();
                this.formData = {
                    cibianheight: '',
                    cidilength: '',
                };
            }
        },
        computed:{
            showResult(){
                let validate = [];
                for (const itemKey in this.rules) {
                    validate.push(this.rules[itemKey].value);
                }
                return this.$util.checkArrayNotNullNumber(validate,1)
            }
        }
    }
</script>

<style lang="scss" scoped>
    @import "@/static/css/math.scss";
    .cibianheight1{
        top: 210rpx;
        left: 10rpx;
    }
    .cidilength{
        top: 290rpx;
        left: 230rpx;
    }
    .cibianheight2{
        top: 290rpx;
        left: 340rpx;
    }
    .cibianheight3{
        top: 290rpx;
        left: 445rpx;
    }
    .slice1{
        top: 390rpx;
        left: 180rpx;
    }
    .slice2{
        top: 730rpx;
        left: 160rpx;
    }
</style>
