<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.height" placeholder="请输入高"/>
                </u-form-item>
                <u-form-item label="底边(cm)" label-width="160">
                    <u-input type="digit" v-model="formData.dibian" placeholder="请输入底边"/>
                </u-form-item>
                <u-form-item label="边高(cm)" label-width="160">
                    <u-input type="digit" v-model="formData.biangao" 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>{{$util.round(item.value,2)}}{{item.unit}}
                    <text v-if="item.isHalf">{{$util.round(item.value/2,2)}}{{item.unit}}</text>
                </view>
            </view>
            <view class="title">计算图</view>
            <view class="calc-img">
                <text class="height">{{formData.height?parseFloat(formData.height):'**'}}cm</text>
                <text class="xiebian">{{rules.xiebian.value?$util.round(rules.xiebian.value,2):'**'}}{{rules.xiebian.unit}}</text>
                <text class="angle">{{rules.angle.value?$util.round(rules.angle.value,2):'**'}}{{rules.angle.unit}}</text>
                <text class="dibian">{{formData.dibian?parseFloat(formData.dibian):'**'}}cm</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="300rpx" :src="mathImgs[name].slice" mode="aspectFit"></u-image>

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

<script>
    import mathImgs from "@/core/math-imgs"
    // 万能公式
    export default {
        data() {
            return {
                name: 'angle',
                mathImgs: mathImgs,
                formData: {
                    height: '',
                    dibian: '',
                    biangao: '',
                },
                // 用来验证 输入
                rules: {
                    xiebian:{name:'斜边', value:'',unit:'cm',show: true},
                    angle: {name:'角度', value:'',unit:'°',show: true},
                    slice:{name:'切口', value:'',unit:'cm',show: true,isHalf: true},
                }
            }
        },
        methods: {
            handleBigImage(){
                uni.previewImage({
                    urls: [mathImgs[this.name].big],
                });
            },
            handelCalc(){
                this.initRules();
                /*1：斜边²=高²+底边²*/
                /*2、tan角度°=高÷底边*/
                if(this.formData.height && this.formData.dibian){
                    this.rules.xiebian.value = Math.sqrt(Math.pow(this.formData.height,2) + Math.pow(this.formData.dibian,2));
                    this.rules.angle.value = this.$util.atan(this.formData.height / this.formData.dibian);
                }
                /*1、 切口=边高×tan（角度°÷2）×2*/
                if(this.formData.biangao){
                    this.rules.slice.value = this.formData.biangao * this.$util.tan(this.rules.angle.value / 2) * 2;
                }
                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 = {
                    height: '',
                    dibian: '',
                    biangao: '',
                };
            }
        },
        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";
    .height{
        top: 210rpx;
        left: 410rpx;
    }
    .xiebian{
        left: 320rpx;
        top: 180rpx;
    }
    .angle{
        left: 320rpx;
        top: 246rpx;
    }
    .dibian{
        left: 350rpx;
        top: 280rpx;
    }
</style>
