<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.dibian" placeholder="请输入底边"/>
                </u-form-item>
                <u-form-item label="边高(cm)" label-width="160">
                    <u-input type="number" v-model="formData.biangao" placeholder="请输入边高"/>
                </u-form-item>
                <u-form-item label="长(cm)" label-width="160">
                    <u-input type="number" v-model="formData.length" 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}}
                    <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="qiwan top">{{rules.qiwan.value?rules.qiwan.value:'**'}}{{rules.qiwan.unit}}</text>
                <text class="xiebian top">{{rules.xiebian.value?rules.xiebian.value:'**'}}{{rules.xiebian.unit}}</text>
                <text class="slice1 bottom">{{rules.slice.value?rules.slice.value:'**'}}{{rules.slice.unit}}</text>
                <text class="slice2 bottom">{{rules.slice.value?rules.slice.value:'**'}}{{rules.slice.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="300rpx" :src="mathImgs[name].slice" mode="aspectFit"></u-image>

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

<script>
    import mathImgs from "@/core/math-imgs"
    // 万能公式
    export default {
        data() {
            return {
                name: 'rightAngle45',
                mathImgs: mathImgs,
                formData: {
                    dibian: '',
                    biangao: '',
                    length: '',
                },
                // 用来验证 输入
                rules: {
                    xiebian: {name:'斜边', value:'',unit:'cm',show: true},
                    slice: {name:'切口', value:'',unit:'cm',show: true,isHalf: true},
                    qiwan: {name:'起弯', value:'',unit:'cm',show: false},
                }
            }
        },
        methods: {
            handleBigImage(){
                uni.previewImage({
                    urls: [mathImgs[this.name].big],
                });
            },
            handelCalc(){
                this.initRules();
                /**
                 * 1、 斜边=边高×1.414
                 * 2、 切口=边高×0.828
                 */
                this.formData.dibian = parseFloat(this.formData.dibian)
                this.formData.biangao = parseFloat(this.formData.biangao)
                if(this.formData.dibian && this.formData.biangao){
                    this.rules.xiebian.value = this.formData.dibian * 1.414;
                    this.rules.slice.value = this.formData.biangao * 0.828
                }else{
                    this.$u.toast('请输入两个参数');
                }
                // 计算起弯 长-边高-底边
                if(this.formData.length){
                    this.rules.qiwan.value = this.formData.length - this.formData.biangao - this.formData.dibian
                }
                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 = {
                    dibian: '',
                    biangao: '',
                    length: '',
                };
            }
        },
        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";
    .qiwan{
        left: 100rpx;
    }
    .xiebian{
        left: 320rpx;
    }
    .slice1{
        left: 210rpx;
    }
    .slice2{
        left: 430rpx;
    }
</style>
