<template>
    <heightResize ignore :append="['.page-option-block']" @resize="resize">
        <autoHeightTextarea v-model="current.input" :height="inputHeight" :placeholder="$t('hexString_input')"/>
        <option-block class="page-option-block">
            <FormItem>
                <ButtonGroup>
                    <Button type="primary" @click="codeGenJson()">Convert</Button>
                    <!-- <Button type="primary" @click="handle('str')">Hex -> String</Button> -->
                </ButtonGroup>
            </FormItem>
            <FormItem>
                <Checkbox v-model="current.isUppercase">{{ $t('hexString_uppercase') }}</Checkbox>
            </FormItem>
        </option-block>
        <!-- <span>美化的内容&nbsp;&nbsp;&nbsp;&nbsp;：</span><input class="input_width_100" type="text" v-model="current.output_beautiful">
        <br>
        <span>字符串格式&nbsp;&nbsp;&nbsp;&nbsp;：</span><input class="input_width_100" type="text" v-model="current.output_str">
        <br>
        <span>字节数组格式：</span><input class="input_width_100" type="text" v-model="current.output_bytes"> -->
        <autoHeightTextarea :value="current.output" :height="outputHeight" :placeholder="$t('hexString_output')"/>
    </heightResize>
</template>
<script>
import heightResize from "../../tool/components/heightResize";
import autoHeightTextarea from "../../tool/components/autoHeightTextarea";

export default {
    components: {
        heightResize,
        autoHeightTextarea
    },
    created() {
        this.$initToolData('input')
    },
    methods: {
        codeGenJson() {
            console.log('enter...')
            var objJson = {}
            if (this.current.input) {
                var lines = this.current.input.split('\n');
                lines.forEach(line => {
                    console.log(line)
                    var items = line.split(' ')
                    function del_space(s) {
                      return (s && s.trim());
                    }
                    // 去除空字符串
                    items = items.filter(del_space)
                    if (items.length != 2) {
                      console.log(items)
                    }

                    objJson[items[0]] = items[1]
                })
            }
            console.log('leave...', objJson)
            this.current.output = JSON.stringify(objJson)
        },
        resize(height) {
            this.inputHeight = Math.min(320, Math.ceil(height / 2))
            this.outputHeight = height - this.inputHeight
            // this.outputHeight = 180
        }
    },
    data() {
        return {
            current: {
                input: "",
                isUppercase: false,
                output_beautiful: "",       // 一行，全字节内容，美化的内容
                output_str: "",             // 字符串格式
                output_bytes: "",           // 字节数组格式
                output: "",
                operation: ""
            },
            inputHeight: 100,
            outputHeight: 100
        }
    },
}
</script>

<style scoped>
.input_width_100 {
    width: 80%;
    margin: 8px 0;
}
</style>