<template>
  <div v-if="null !== props.log" class="h-full flex flex-col">
    <div class="border rounded p-3 whitespace-pre bg-white mb-3">
      执行动作 : {{ props.log.action }}
    </div>

    <div class="border rounded p-3 whitespace-pre bg-white mb-3">
      <div>参数列表：</div>
      <div v-for="(item, index) of props.log.params" :key="index" class="mr-2">
        {{ JSON.stringify(item, null, 2) }}
      </div>
    </div>

    <a-modal v-model:open="reultDisplay" title="结果" @ok="reultDisplay = false" :closable="false">
      <div class="border rounded p-3 whitespace-pre bg-white mb-3 grow" style="max-height: 80vh; overflow-y: auto;">
        <vue-json-pretty :data="resultContent" :deep="5" :show-double-quotes="false" :show-key-value-space="false"
          :show-icon="true" :item-height="18"></vue-json-pretty>
      </div>
    </a-modal>

    <a-modal v-model:open="chartEnable" title="曲线" width="90%" @ok="chartEnable = false">
      <div ref="chartContainer" class="w-full bg-white mb-3 border rounded" style="height:600px;"></div>
    </a-modal>

    <a-modal v-model:open="tableDisEnable" title="表单" width="90%" @ok="tableDisEnable = false">
      <a-table :pagination=false  :scroll="{ y: '50vh' }" :columns="columns" :data-source="tableData" />
    </a-modal>

  </div>
</template>
<script setup>
import * as echarts from 'echarts';
import { ref, nextTick, watch } from 'vue';
import VueJsonPretty from 'vue-json-pretty';
import 'vue-json-pretty/lib/styles.css';
/** @var {Object} */
const props = defineProps({
  log: Object,
});
/** @var {Element} */
const chartContainer = ref(null);
/** @var {Boolean} */
const chartEnable = ref(false);
const reultDisplay = ref(false);
const tableDisEnable = ref(false);
const resultContent = ref({});
const tableData = ref([]);
const columns = ref([]);
/** @var {echarts} */
let chart = null;
// watch log
watch(() => props.log, handleLogChange, { deep: true });


// handle log change
async function handleLogChange() {
  if (null !== chart) {
    chart.dispose();
    chart = null;
  }

  chartEnable.value = false;
  reultDisplay.value = false;
  tableDisEnable.value = false;

  if (null === props.log
    || undefined === props.log.response
    || null === props.log.response) {
    return;
  }


  if ('A8kScanCurve' === props.log.response.$dataType) {
    chartEnable.value = true;
    let minY = Math.min(...props.log.response.scanDataCurve) - 100;
    let maxY = Math.max(...props.log.response.scanDataCurve) + 100;

    let refLine = props.log.response.refLine.map((v, i) => [i, v]);
    let scanDataCurve = props.log.response.scanDataCurve.map((v, i) => [i, v]);
    let refCurve = props.log.response.refCurve || [];
    refCurve = refCurve.map(v => ({ xAxis: v }));

    await nextTick();
    chart = echarts.init(chartContainer.value);
    chart.setOption({
      xAxis: { type: 'value', axisLabel: { show: true } },
      yAxis: { type: 'value', axisLabel: { show: true }, min: minY, max: maxY },
      grid: { left: '3%', right: '4%', bottom: '3%', top: '3%', containLabel: true },
      dataZoom: [{ type: 'inside' }, { type: 'inside', orient: 'vertical' }],
      tooltip: { trigger: 'axis' },
      series: [{
        name: 'RefLine',
        type: 'line',
        itemStyle: { normal: { lineStyle: { width: 1 }, color: '#ffb2b3' } },
        showSymbol: false,
        data: refLine
      }, {
        name: 'ScanDataCurve',
        type: 'line',
        data: scanDataCurve,
        itemStyle: { normal: { lineStyle: { width: 1 }, color: '#4d90ff' } },
        showSymbol: false,
        markLine: {
          slient: true,
          symbol: 'none',
          data: refCurve,
          label: { show: true },
          lineStyle: { normal: { type: 'solid' } }
        },
      }]
    });
  }
  else if ('FileToBeDownload' === props.log.response.$dataType) {
    var url = process.env.NODE_ENV === 'production' ? `${props.log.response.url}` : `http://localhost:80${props.log.response.url}`;
    window.open(url);
  }
  else if ('ExtApiCurve' === props.log.response.$dataType) {
    let response = props.log.response;
    chartEnable.value = true;
    let curve = response.data;
    let markLine = response.markLine || [];
    markLine = markLine.map(v => ({ xAxis: v }));

    await nextTick();
    chart = echarts.init(chartContainer.value);
    chart.setOption({
      xAxis: { type: response.xtype, axisLabel: { show: true } },
      yAxis: { type: response.ytype, axisLabel: { show: true }, min: response.minY, max: response.maxY },
      grid: { left: '3%', right: '4%', bottom: '3%', top: '3%', containLabel: true },
      dataZoom: [{
        type: 'slider',  // 滑动条缩放
        show: true,
        xAxisIndex: [0],  // 只作用于x轴

      }, {
        type: 'slider',  // 滑动条缩放
        show: true,
        yAxisIndex: [0],  // 只作用于y轴
      }],
      tooltip: { trigger: 'axis' },
      series: [{
        name: response.name,
        type: 'line',
        data: curve,
        itemStyle: { normal: { lineStyle: { width: 1 }, color: '#4d90ff' } },
        showSymbol: false,
        markLine: {
          slient: true,
          symbol: 'none',
          data: markLine,
          label: { show: true },
          lineStyle: { normal: { type: 'solid' } }
        },
      }]
    });
  } else if ('ExtUiTable' === props.log.response.$dataType) {
    tableDisEnable.value = true;
    columns.value = props.log.response.columns;
    tableData.value = props.log.response.datas;
  }
  else {
    resultContent.value = props.log.response;
    reultDisplay.value = true;
  }
}
</script>