<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()">{{ $t('allMy_convert') }}</Button>
                    <Button type="primary" @click="winError()"> winerror.h </Button>
                    <!-- <Button type="primary" @click="handle('str')">Hex -> String</Button> -->
                </ButtonGroup>
            </FormItem>
            <FormItem>
                <Checkbox v-model="current.isFormat">{{ $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 {
    data() {
        return {
            current: {
                input: `
123033	    31
123034	31
123035	32
                `,
                isFormat: false,
                output_beautiful: "",       // 一行，全字节内容，美化的内容
                output_str: "",             // 字符串格式
                output_bytes: "",           // 字节数组格式
                output: "",
                operation: ""
            },
            inputHeight: 100,
            outputHeight: 100
        }
    },
    components: {
        heightResize,
        autoHeightTextarea
    },
    created() {
        this.$initToolData('input')
    },
    methods: {
        codeGenJsonOld() {
            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)
        },
        codeGenJson() {
            console.log('enter...')
            var objJson = {}
            if (this.current.input) {
                var lines = this.current.input.split('\n');
                lines.forEach(line => {
                    console.log(line)
                    line = line.trim().replace(/\s+/g, ' ')
                    var items = line.split(' ')
                    if (items.length != 2) {
                      console.log(items)
                      return
                    }

                    objJson[items[0]] = items[1]
                })
            }
            console.log('leave...', objJson)
            this.current.output = JSON.stringify(objJson, null, 4)
        },
        winError() {
          var objJson = [];
          //   // (.*)\n//\n#define (\S+)\s+(\d+)L/g
          var r = /\/\/ (.*)\n\/\/\n#define (\S+)\s+(\d+)L/g;
          var a = null;
          while ((a = r.exec(this.current.input))) {  //循环执行匹配操作
            console.log("匹配文本 = " + a[0] + "    a.index = " + a.index + "    r.lastIndex = " + r.lastIndex);  //显示每次匹配后返回的数组信息
            var [_, desc, k, v] = a;
            objJson.push({desc: desc, k: k, v: v});
          }
          this.current.output = JSON.stringify(objJson);
          console.log(Object.keys(objJson).length);
        },
        resize(height) {
            this.inputHeight = Math.min(320, Math.ceil(height / 2))
            this.outputHeight = height - this.inputHeight
            // this.outputHeight = 180
        }
    },
}
</script>

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