<script setup lang="ts">
import {
  Cell as vanCell,
  CellGroup as vanCellGroup,
  Field as vanField,
  Form as vanForm,
  Image as vanImage,
} from 'vant'
import { reactive, ref } from 'vue'

const props = defineProps({
  jsonConfig: Object,
})

const receivedFunction = reactive([])
const contentForMobile = ref([])

function formatConfig() {
  for (let i = 0; i < props.jsonConfig.content.length; i++) {
    let tempObj = {
      label: undefined,
      value: undefined,
      valueType: 'str',
    }
    const item = props.jsonConfig.content[i]
    if (item.type === 'jsonKey') {
      if (receivedFunction[i].labelFunction(props.jsonConfig).type === 'key')
        tempObj.label = props.jsonConfig.data[receivedFunction[i].labelFunction(props.jsonConfig).content]
      else if (receivedFunction[i].labelFunction(props.jsonConfig).type === 'value')
        tempObj.label = receivedFunction[i].labelFunction(props.jsonConfig).content

      if (receivedFunction[i].valueFunction(props.jsonConfig).type === 'key') {
        tempObj.value = props.jsonConfig.data[receivedFunction[i].valueFunction(props.jsonConfig).content]
      }
      else if (receivedFunction[i].valueFunction(props.jsonConfig).type === 'value') {
        tempObj.value = receivedFunction[i].valueFunction(props.jsonConfig).content
      }
      else if (receivedFunction[i].valueFunction(props.jsonConfig).type === 'img') {
        tempObj.valueType = 'img'
        tempObj.value = receivedFunction[i].valueFunction(props.jsonConfig).content
      }
      contentForMobile.value.push(tempObj)
      tempObj = {
        label: undefined,
        value: undefined,
        valueType: 'str',
      }
    }
    else if (item.type === 'jsonArray') {
      for (let j = 0; j < props.jsonConfig.data[item.jsonArrayDataIndex].length; j++) {
        const jsonArrayItem = props.jsonConfig.data[item.jsonArrayDataIndex][j]
        if (receivedFunction[i].labelFunction(props.jsonConfig, jsonArrayItem).type === 'key')
          tempObj.label = jsonArrayItem[receivedFunction[i].labelFunction(props.jsonConfig, jsonArrayItem).content]
        else if (receivedFunction[i].labelFunction(props.jsonConfig, jsonArrayItem).type === 'value')
          tempObj.label = receivedFunction[i].labelFunction(props.jsonConfig, jsonArrayItem).content

        if (receivedFunction[i].valueFunction(props.jsonConfig, jsonArrayItem).type === 'key') {
          tempObj.value = jsonArrayItem[receivedFunction[i].valueFunction(props.jsonConfig, jsonArrayItem).content]
        }
        else if (receivedFunction[i].valueFunction(props.jsonConfig, jsonArrayItem).type === 'value') {
          tempObj.value = receivedFunction[i].valueFunction(props.jsonConfig, jsonArrayItem).content
        }
        else if (receivedFunction[i].valueFunction(props.jsonConfig, jsonArrayItem).type === 'img') {
          tempObj.valueType = 'img'
          tempObj.value = receivedFunction[i].valueFunction(props.jsonConfig, jsonArrayItem).content
        }

        contentForMobile.value.push(tempObj)
        tempObj = {
          label: undefined,
          value: undefined,
          valueType: 'str',
        }
      }
    }
  }
}
// -------------------------  初始化  -------------------------
initComponent()

function initComponent() {
  for (let i = 0; i < props.jsonConfig.content.length; i++) {
    receivedFunction.push({
      // eslint-disable-next-line no-new-func
      labelFunction: new Function(`return ${props.jsonConfig.content[i].customFunctionForLabel}`)(),
      // eslint-disable-next-line no-new-func
      valueFunction: new Function(`return ${props.jsonConfig.content[i].customFunctionForValue}`)(),
    })
  }
  formatConfig()
}
</script>

<template>
  <div class="main">
    <!--  标题  -->
    <template v-if="jsonConfig.title">
      <template v-if="jsonConfig.title.type === 'titleKey'">
        <h2 class="title" v-html="jsonConfig.data[jsonConfig.title.value]" />
      </template>
      <template v-else-if="jsonConfig.title.type === 'titleValue'">
        <h2 class="title" v-html="jsonConfig.title.value" />
      </template>
    </template>
    <!--  内容  -->
    <van-form>
      <van-cell-group inset class="cell_group">
        <template v-for="(row, rowIndex) in contentForMobile" :key="rowIndex">
          <template v-if="row.valueType === 'str'">
            <van-cell center :title="row.label" :value="row.value ? row.value : '无'" />
          </template>
          <template v-else-if="row.valueType === 'img'">
            <template v-for="(img, imgIndex) in row.value" :key="imgIndex">
              <van-field :name="row.label" :label="row.label">
                <template #input>
                  <div style="width: 100%;text-align: right">
                    <van-image :src="img" width="100" height="100" fit="cover" />
                  </div>
                </template>
              </van-field>
            </template>
          </template>
        </template>
      </van-cell-group>
    </van-form>
  </div>
</template>

<style scoped lang="less">
.main {
  padding-top: 4vh;
  width: 100vw;
  height: 100vh;
  background-color: #eff2f5;

  .title {
    padding-bottom: 2vh;
    color: rgb(50, 50, 51);
    text-align: center;
    margin: 0 0 3vh;
  }

  .text_box {
    margin-top: 2vh;
    margin-bottom: 2vh;
  }

  .main_text {
    padding-left: 16px;
    font-weight: 400;
    line-height: 1.6;
    margin: 0 0 40px;
    color: #969799;
    font-size: 14px;
  }

  .show_value_item {
    text-align: center;
    font-size: 1.2em;
  }

  .cell_group {
    margin-top: 2vh;
    margin-bottom: 2vh;
  }

  .form_item {
    margin-top: 2vh;
  }

  .button_group {
    text-align: center;
    margin-top: 3vh;
    margin-bottom: 3vh;
  }

  .button_group > :first-child {
    margin-right: 3vw;
  }

  .divider {
    color: #1989fa;
    border-color: #1989fa;
    padding: 0 16px;
  }

  .submit_button {
    background-color: #eff2f5;
    padding: 5vh;
  }
}

.skeleton {
  margin-top: 5vh;
}
.my-cell-group {
  margin: 0 0 10px 0;
}
:deep(.van-collapse-item__content) {
  background-color: #eff2f5;
  padding: 10px 0;
}
:deep(.van-cell-group__title) {
  padding-top: 10px;
  padding-bottom: 10px;
}
:deep(.van-field__label) {
  font-weight: 600;
}
:deep(.van-cell__title) {
  font-weight: 600;
}
:deep(.van-uploader__wrapper) {
  padding: 10px;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between; /* 水平方向上左右分散对齐 */
  align-items: center; /* 垂直方向上上下中心对齐 */
}
</style>
