<template>
  <x-form-item
    v-if="isEditMode && getFromItem(item.dataIndex)"
    class="innerTable"
    :form="record"
    :attr="getFromItem(item.dataIndex)"
    :service-name="serviceName"
    mode="新增/修改"
    :env="env"
    :showLabel="false"
  />
  <!-- 序号列 -->
  <span v-else-if="item.slotType === 'index'">
    {{ index + 1 }}
  </span>
  <!-- 行链接 -->
  <span v-else-if="item.slotType === 'link'">
    <a @click="columnClick(item.dataIndex, text, record)">{{ text }}</a>
  </span>
  <!-- 文本溢出省略 -->
  <span v-else-if="item.slotType === 'ellipsis'">
    <ellipsis :length="item.slotValue" tooltip>{{ text === '' ? '--' : text }}</ellipsis>
  </span>
  <!-- 徽标 -->
  <span v-else-if="item.slotType === 'badge'">
    <x-badge
      :service-name="serviceName"
      :env="env"
      v-if="text !== null && text !== undefined"
      :badge-key="item.slotKeyMap"
      :value="text"
    />
  </span>
  <!-- 日期 -->
  <span v-else-if="item.slotType === 'date'">
    {{ format(text, 'yyyy-MM-dd') }}
  </span>
  <!-- 日期时间 -->
  <span v-else-if="item.slotType === 'dateTime'">
    {{ format(text, 'yyyy-MM-dd hh:mm:ss') }}
  </span>
  <!-- 两位小数 -->
  <span v-else-if="item.slotType === 'towDecimal'">
    {{ numberFormat(text, 2) }}
  </span>
  <!-- 四位小数 -->
  <span v-else-if="item.slotType === 'fourDecimal'">
    {{ numberFormat(text, 4) }}
  </span>
  <!-- 整数 -->
  <span v-else-if="item.slotType === 'int'">
    {{ numberFormat(text, 0) }}
  </span>
  <!-- 操作列 -->
  <span v-else-if="item.slotType === 'action'">
    <template v-if="item.actionArr && item.actionArr.length > 0">
      <a-dropdown>
        <a class="ant-dropdown-link" @click="e => e.preventDefault()">
          {{ item.scopedSlots?.customRender || item.slotValue }} <a-icon type="down"/>
        </a>
        <a-menu slot="overlay" style="min-width: 60px">
          <a-menu-item
            v-for="(action_item, actionIndex) in item.actionArr"
            :key="actionIndex"
            v-if="!action_item.customFunction || executeStrFunction(action_item.customFunction, [record, c_index])"
          >
            <a style="text-align: center" @click="action(record, item.dataIndex, action_item.func)">
              {{ action_item.text }}
            </a>
          </a-menu-item>
        </a-menu>
      </a-dropdown>
    </template>
    <template v-if="!item.actionArr || item.actionArr.length === 0">
      <a @click="action(record, item.dataIndex)">{{ item.slotValue }}</a>
    </template>
  </span>
</template>
<!--表格列组件抽取失败 暂时没用到这个组件-->
<script>
import XFormItem from '@vue2-client/base-client/components/common/XForm/XFormItem.vue'
import Ellipsis from '@vue2-client/components/Ellipsis/Ellipsis.vue'
import XBadge from '@vue2-client/base-client/components/common/XBadge/XBadge.vue'
import { executeStrFunction } from '@vue2-client/utils/runEvalFunction'
import { formatDate } from '@vue2-client/utils/util'

export default {
  components: { XBadge, Ellipsis, XFormItem },
  props: {
    item: {
      type: Object,
      required: true,
    },
    record: {
      type: Object,
      required: true,
    },
    index: {
      type: Number,
      required: true,
    },
    text: {
      type: String,
      required: true,
    },
    serviceName: {
      type: String,
      default: undefined
    },
    env: {
      type: String,
      default: undefined
    },
    isEditMode: {
      type: Boolean,
      default: false
    },
    formItems: {
      type: Array,
      required: true,
    },
  },
  methods: {
    executeStrFunction,
    getFromItem (model) {
      const aa = this.formItems.find(item => item.model === model && item.editRow)
      if (aa) {
        return JSON.parse(JSON.stringify(aa))
      }
      return false
    },
    columnClick () {
      this.$emit('columnClick', ...arguments)
    },
    action () {
      this.$emit('action', ...arguments)
    },
    /**
     * 格式化日期
     * @param date 日期字符串
     * @param format 格式化方式
     */
    format (date, format) {
      return formatDate(date, format)
    },
    /**
     * 格式化数字
     * @param number string 或者 number
     * @param decimalPlaces 小数位数
     */
    numberFormat (number, decimalPlaces = 2) {
      const value = parseFloat(number)
      if (!isNaN(value)) {
        return value.toFixed(decimalPlaces)
      } else {
        return ''
      }
    },
  },
}
</script>
