<template>
  <el-table ref="dragTable" :data="formData" :max-height="tableHeight" row-key="columnId">
    <el-table-column
      :show-overflow-tooltip="true"
      :label="t('infra.fieldColumnName')"
      min-width="10%"
      prop="columnName"
    />
    <el-table-column :label="t('infra.fieldDescription')" min-width="10%">
      <template #default="scope">
        <el-input v-model="scope.row.columnComment" />
      </template>
    </el-table-column>
    <el-table-column
      :show-overflow-tooltip="true"
      :label="t('infra.physicalType')"
      min-width="10%"
      prop="dataType"
    />
    <el-table-column :label="t('infra.javaType')" min-width="11%">
      <template #default="scope">
        <el-select v-model="scope.row.javaType">
          <el-option label="Long" value="Long" />
          <el-option label="String" value="String" />
          <el-option label="Integer" value="Integer" />
          <el-option label="Double" value="Double" />
          <el-option label="BigDecimal" value="BigDecimal" />
          <el-option label="LocalDateTime" value="LocalDateTime" />
          <el-option label="Boolean" value="Boolean" />
        </el-select>
      </template>
    </el-table-column>
    <el-table-column :label="t('infra.javaAttributes')" min-width="10%">
      <template #default="scope">
        <el-input v-model="scope.row.javaField" />
      </template>
    </el-table-column>
    <el-table-column :label="t('infra.insert')" min-width="4%">
      <template #default="scope">
        <el-checkbox v-model="scope.row.createOperation" false-label="false" true-label="true" />
      </template>
    </el-table-column>
    <el-table-column :label="t('infra.edit')" min-width="4%">
      <template #default="scope">
        <el-checkbox v-model="scope.row.updateOperation" false-label="false" true-label="true" />
      </template>
    </el-table-column>
    <el-table-column :label="t('infra.list')" min-width="4%">
      <template #default="scope">
        <el-checkbox
          v-model="scope.row.listOperationResult"
          false-label="false"
          true-label="true"
        />
      </template>
    </el-table-column>
    <el-table-column :label="t('infra.query')" min-width="4%">
      <template #default="scope">
        <el-checkbox v-model="scope.row.listOperation" false-label="false" true-label="true" />
      </template>
    </el-table-column>
    <el-table-column :label="t('infra.queryMethod')" min-width="10%">
      <template #default="scope">
        <el-select v-model="scope.row.listOperationCondition">
          <el-option label="=" value="=" />
          <el-option label="!=" value="!=" />
          <el-option label=">" value=">" />
          <el-option label=">=" value=">=" />
          <el-option label="<" value="<>" />
          <el-option label="<=" value="<=" />
          <el-option label="LIKE" value="LIKE" />
          <el-option label="BETWEEN" value="BETWEEN" />
        </el-select>
      </template>
    </el-table-column>
    <el-table-column :label="t('infra.allowNull')" min-width="5%">
      <template #default="scope">
        <el-checkbox v-model="scope.row.nullable" false-label="false" true-label="true" />
      </template>
    </el-table-column>
    <el-table-column :label="t('infra.displayType')" min-width="12%">
      <template #default="scope">
        <el-select v-model="scope.row.htmlType">
          <el-option :label="t('infra.textBox')" value="input" />
          <el-option :label="t('infra.textField')" value="textarea" />
          <el-option :label="t('infra.dropdownBox')" value="select" />
          <el-option :label="t('infra.radioBox')" value="radio" />
          <el-option :label="t('infra.checkbox')" value="checkbox" />
          <el-option :label="t('infra.dateControl')" value="datetime" />
          <el-option :label="t('infra.imageUpload')" value="imageUpload" />
          <el-option :label="t('infra.fileUpload')" value="fileUpload" />
          <el-option :label="t('infra.richTextControls')" value="editor" />
        </el-select>
      </template>
    </el-table-column>
    <el-table-column :label="t('infra.dictionaryTypes')" min-width="12%">
      <template #default="scope">
        <el-select v-model="scope.row.dictType" clearable filterable :placeholder="t('common.selectText')">
          <el-option
            v-for="dict in dictOptions"
            :key="dict.id"
            :label="dict.name"
            :value="dict.type"
          />
        </el-select>
      </template>
    </el-table-column>
    <el-table-column :label="t('infra.examples')" min-width="10%">
      <template #default="scope">
        <el-input v-model="scope.row.example" />
      </template>
    </el-table-column>
  </el-table>
</template>
<script lang="ts" setup>
import { PropType } from 'vue'
import * as CodegenApi from '@/api/infra/codegen'
import * as DictDataApi from '@/api/system/dict/dict.type'
const { t } = useI18n()

defineOptions({ name: 'InfraCodegenColumInfoForm' })

const props = defineProps({
  columns: {
    type: Array as unknown as PropType<CodegenApi.CodegenColumnVO[]>,
    default: () => null
  }
})

const formData = ref<CodegenApi.CodegenColumnVO[]>([])
const tableHeight = document.documentElement.scrollHeight - 350 + 'px'

/** 查询字典下拉列表 */
const dictOptions = ref<DictDataApi.DictTypeVO[]>()
const getDictOptions = async () => {
  dictOptions.value = await DictDataApi.getSimpleDictTypeList()
}

watch(
  () => props.columns,
  (columns) => {
    if (!columns) return
    formData.value = columns
  },
  {
    deep: true,
    immediate: true
  }
)

onMounted(async () => {
  await getDictOptions()
})
</script>
