<script setup lang="ts">
import AForm from '@/components/AForm/index.vue'
import { FormConfigType } from '@/components/AForm/type'
import { ToolbarConfigType } from '@/components/AToolbar/type'
import { ColumnConfigType } from '@/components/ATable/type'
import { StrAnyObj, StrAnyObjArr } from '@/typings'
import { useHandleData } from '@/utils/useHandleData'
import { getPageApi, getDetailApi, addApi, editApi, deleteApi } from '@/api/system/dictData'

const visible = ref<boolean>(false)
let title = ''
let dictTypeId = ''

const queryRef = ref<InstanceType<typeof AForm>>()
const formRef = ref<InstanceType<typeof AForm>>()

const showQuery = ref<boolean>(true)
const selectKeys = ref<string[]>([])
const pageTotal = ref<number>(0)

const queryData = ref<StrAnyObj>({ pageNum: 1, pageSize: 10 })
const tableData = ref<StrAnyObjArr>([])
const formData = ref<StrAnyObj>({ cssStyle: 'default', status: 1, sort: 1 })

const dialogTitle = ref<string>('')
const dialogVisible = ref<boolean>(false)

const queryConfig: FormConfigType[] = [
  {
    type: 'input',
    prop: 'label',
    label: '字典标签'
  },
  {
    type: 'select',
    prop: 'status',
    label: '状态',
    dict: 'enable_disable'
  }
]

const toolbarConfig: ToolbarConfigType[] = [
  {
    common: 'search',
    click() {
      queryData.value.pageNum = 1
      getPage()
    }
  },
  {
    common: 'reset',
    click() {
      queryRef.value?.resetFields()
      getPage()
    }
  },
  {
    common: 'add',
    permissions: 'sysDictData:add',
    click() {
      dialogVisible.value = true
      dialogTitle.value = '新增'
    }
  },
  {
    common: 'delete',
    permissions: 'sysDictData:delete',
    disabled() {
      return selectKeys.value.length == 0
    },
    click() {
      handleRemove(selectKeys.value)
    }
  }
]

const columnConfig: ColumnConfigType[] = [
  {
    prop: 'label',
    label: '字典标签'
  },
  {
    prop: 'value',
    label: '字典值'
  },
  {
    slot: 'cssStyle',
    label: '回显样式'
  },
  {
    prop: 'status',
    label: '状态',
    dict: 'enable_disable'
  },
  {
    prop: 'sort',
    label: '排序'
  },
  {
    width: 200,
    handleConfig: [
      {
        common: 'update',
        permissions: 'sysDictData:edit',
        click(row) {
          dialogVisible.value = true
          dialogTitle.value = '编辑'
          getDetailApi({ id: row.id }).then((resp: StrAnyObj) => {
            formData.value = resp
          })
        }
      },
      {
        common: 'delete',
        permissions: 'sysDictData:delete',
        click(row) {
          handleRemove([row.id])
        }
      }
    ]
  }
]

const formConfig: FormConfigType[] = [
  {
    type: 'input',
    prop: 'label',
    label: '字典标签',
    rule: [{ required: true, message: '字典标签不能为空', trigger: 'blur' }]
  },
  {
    type: 'inputNumber',
    prop: 'value',
    label: '字典值',
    precision: 0,
    rule: [{ required: true, message: '字典值不能为空', trigger: 'blur' }]
  },
  {
    type: 'select',
    prop: 'cssStyle',
    label: '回显样式',
    option: [
      {
        key: 'default',
        label: '默认(default)'
      },
      {
        key: 'primary',
        label: '主要(primary)'
      },
      {
        key: 'success',
        label: '成功(success)'
      },
      {
        key: 'info',
        label: '信息(info)'
      },
      {
        key: 'warning',
        label: '警告(warning)'
      },
      {
        key: 'danger',
        label: '危险(danger)'
      }
    ],
    rule: [{ required: true, message: '回显样式不能为空', trigger: 'blur' }]
  },
  {
    type: 'radio',
    prop: 'status',
    label: '状态',
    dict: 'enable_disable',
    rule: [{ required: true, message: '状态不能为空', trigger: 'blur' }]
  },
  {
    type: 'inputNumber',
    prop: 'sort',
    label: '排序',
    min: 0,
    precision: 0,
    rule: [{ required: true, message: '排序不能为空', trigger: 'blur' }]
  }
]

defineExpose({ open })

function open(paramTitle: string, paramDictTypeId: string) {
  visible.value = true
  title = paramTitle
  dictTypeId = paramDictTypeId
  getPage()
}

function closed() {
  queryRef.value?.resetFields()
  tableData.value = []
}

function getPage() {
  getPageApi({ ...queryData.value, dictTypeId }).then((resp) => {
    tableData.value = resp.records
    pageTotal.value = resp.total
  })
}

function tableSelectionChange(item: StrAnyObjArr) {
  selectKeys.value = item.map((item) => item.id)
}

function formSubmit() {
  formRef.value?.validate(() => {
    if (formData.value.id) {
      editApi({ ...formData.value, dictTypeId }).then(() => {
        dialogVisible.value = false
        ElMessage.success('修改成功')
        getPage()
      })
    } else {
      addApi({ ...formData.value, dictTypeId }).then(() => {
        dialogVisible.value = false
        ElMessage.success('新增成功')
        getPage()
      })
    }
  })
}

function formClosed() {
  formRef.value?.resetFields()
}

function handleRemove(idList: string[]) {
  useHandleData('是否确认删除？', () => {
    deleteApi({ idList }).then(() => {
      ElMessage.success('删除成功')
      getPage()
    })
  })
}
</script>

<template>
  <a-dialog v-model="visible" :title="title" :footer="false" width="1200px" @closed="closed">
    <el-card v-if="showQuery">
      <a-form ref="queryRef" v-model="queryData" :config="queryConfig" :span="8"> </a-form>
    </el-card>

    <a-table
      selection
      :data="tableData"
      :page-total="pageTotal"
      :toolbar-config="toolbarConfig"
      :column-config="columnConfig"
      v-model:showQuery="showQuery"
      v-model:page-num="queryData.pageNum"
      v-model:page-size="queryData.pageSize"
      @page-num-change="getPage"
      @page-size-change="getPage"
      @selection-change="tableSelectionChange"
    >
      <template #cssStyle="scope">
        <span v-if="scope.row.cssStyle === 'default'">
          {{ scope.row.cssStyle }}
        </span>
        <el-tag v-else :type="scope.row.cssStyle" style="min-width: 40px">
          {{ scope.row.cssStyle }}
        </el-tag>
      </template>
    </a-table>

    <a-dialog
      v-model="dialogVisible"
      :title="dialogTitle"
      @submit="formSubmit"
      @closed="formClosed"
    >
      <a-form ref="formRef" v-model="formData" :config="formConfig" :span="24"> </a-form>
    </a-dialog>
  </a-dialog>
</template>
