<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="digit" v-model="formData.biangao" placeholder="请输入边高"/>
                </u-form-item>
                <u-form-item label="底宽(cm)" label-width="160">
                    <u-input type="digit" v-model="formData.dikuan" 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="result1 top">{{rules.result1.value?rules.result1.value:'**'}}{{rules.result1.unit}}</text>
                <text class="result2 top">{{rules.result1.value?rules.result1.value:'**'}}{{rules.result1.unit}}</text>
                <text class="biangao">{{formData.biangao?parseFloat(formData.biangao):'**'}}cm</text>
                <text class="result3">{{rules.result2.value?rules.result2.value:'**'}}{{rules.result2.unit}}</text>
                <u-image width="100%" height="300rpx" :src="mathImgs[name].calc" mode="aspectFit"></u-image>
            </view>
            <view class="title">切割图</view>
            <u-image width="100%" height="600rpx" :src="mathImgs[name].slice" mode="aspectFit"></u-image>

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

<script>
    import mathImgs from "@/core/math-imgs"
    // 万能公式
    export default {
        data() {
            return {
                name: 'rightAngleVertical',
                mathImgs: mathImgs,
                formData: {
                    biangao: '',
                    dikuan: '',
                },
                // 用来验证 输入
                rules: {
                    result1: {name:'', value:'',unit:'cm',show: true},
                    result2: {name:'', value:'',unit:'cm',show: true},
                }
            }
        },
        methods: {
            handleBigImage(){
                uni.previewImage({
                    urls: [mathImgs[this.name].big],
                });
            },
            handelCalc(){
                this.initRules();
                /**
                 * 1、结果一=边高+底宽×0.5
                 * 2、结果二=底宽×0.707
                 */
                this.formData.biangao = parseFloat(this.formData.biangao)
                this.formData.dikuan = parseFloat(this.formData.dikuan)
                if(this.formData.biangao && this.formData.dikuan){
                    this.rules.result1.value = this.formData.biangao + this.formData.dikuan * 0.5;
                    this.rules.result2.value = this.formData.dikuan * this.$util.sin(45);
                }else{
                    this.$u.toast('请输入两个参数');
                }
                this.roundRules();
                this.$u.toast("请参考计算示意图")
            },
            roundRules(){
                for (const itemKey in this.rules) {
                    this.rules[itemKey].value = this.$util.round(this.$util.round(this.rules[itemKey].value,3),2);
                }
            },
            initRules(){
                for (const itemKey in this.rules) {
                    this.rules[itemKey].value = "";
                }
            },
            handleClear() {
                this.initRules();
                this.formData = {
                    biangao: '',
                    dikuan: '',
                };
            }
        },
        computed:{
            showResult(){
                let validate = [];
                for (const itemKey in this.rules) {
                    validate.push(this.rules[itemKey].value);
                }
                return this.$util.checkArrayNotNullNumber(validate,2)
            }
        }
    }
</script>

<style lang="scss" scoped>
    @import "@/static/css/math.scss";
    .result1{
        left: 240rpx;
    }
    .result2{
        left: 360rpx;
    }
    .biangao,.result3{
        top: 82rpx;
    }
    .biangao{
        left: 275rpx;
    }
    .result3{
        left: 405rpx;
    }
</style>
