<template>
  <a-modal v-model="visible" width="100%" title="统计列表" destroy-on-close>
    <sd-data-table-ex
      ref="dataTable"
      :form-id="formId"
      :page-id="pageId"
      :columns="columns"
      :filter-expressions="expressions"
      :actions="actions"
    >
    </sd-data-table-ex>
  </a-modal>
</template>

<script>
import moment from 'moment'
import components from './_import-components/sd-statis-data-list-import'

export default {
  name: 'SdStatisDataList',
  metaInfo: {
    title: 'SdStatisDataList',
  },
  components,
  data() {
    return {
      visible: false,
      columns: [],
      formId: '',
      pageId: '',
      expressions: [],
      actions: [
        {
          label: '导出',
          id: 'export',
          permission: 'export',
          callback: (keys) => {
            this.$refs.dataTable.exportExcel()
          },
        },
      ],
    }
  },
  methods: {
    show({ record, formContent, formId, pageId, columns, onlListColumnDefs }) {
      this.pageId = pageId
      this.columns = columns
      this.formId = formId
      const groupColumn = onlListColumnDefs.find((c) => c.fieldId === formContent.groupField)
      this.expressions = [
        ...formContent.statis?.expressions,
        ...this.getExpressions(groupColumn, record, formContent),
      ]
      this.visible = true
    },
    getExpressions(item, record, formContent) {
      let expression = []
      if (record.attrcode) {
        switch (item.fieldType) {
          case 'string':
          case 'title':
          case 'selectlist':
          case 'selectperson':
          case 'selectgroup':
          case 'selecttree':
            expression.push({
              dataType: 'str',
              name: item.fieldId,
              op: 'eq',
              stringValue: record.attrcode,
            })
            break
          case 'date':
            expression = [
              ...this.getDateExpressions(formContent.statis?.groupByColumns, item, record),
            ]

            break
          case 'number':
            expression.push({
              dataType: 'long',
              name: item.fieldId,
              op: 'eq',
              longValue: record.attrcode,
            })
            break
        }
      }

      return expression
    },
    getDateExpressions(type = 'day', item, record) {
      let minValue = ''
      let maxValue = ''
      if (type.includes('day')) {
        minValue = new Date(record.attrcode + ' 00:00:00').valueOf()
        maxValue = new Date(record.attrcode + ' 23:59:59').valueOf()
      } else if (type.includes('month')) {
        minValue = moment(record.attrcode)
          .startOf('month')
          .valueOf()
        maxValue = moment(record.attrcode)
          .endOf('month')
          .valueOf()
      } else if (type.includes('year')) {
        minValue = moment(record.attrcode)
          .startOf('year')
          .valueOf()
        maxValue = moment(record.attrcode)
          .endOf('year')
          .valueOf()
      }
      const expression = [
        {
          dataType: 'long',
          name: item.fieldId,
          op: 'ge',
          longValue: minValue,
        },
        {
          dataType: 'long',
          name: item.fieldId,
          op: 'le',
          longValue: maxValue,
        },
      ]
      return expression
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
</style>
