<script lang="ts" setup>
import * as DictDataApi from '@/api/system/dict/dict.data'
import type { DictDataVO } from '@/api/system/dict/dict.data'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import { useTableComponents } from '@/components/ZmTable/useTableComponents'

const emit = defineEmits<{
  choose: [row: DictDataVO]
}>()

const { ZmTable, ZmTableColumn } = useTableComponents<DictDataVO>()

const visible = ref(false)
const loading = ref(false)
const queryFormRef = ref()
const list = ref<DictDataVO[]>([])
const total = ref(0)
const selectedRow = ref<DictDataVO | null>(null)

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  label: '',
  status: undefined as number | undefined,
  dictType: 'pms_device_brand'
})

const getList = async () => {
  loading.value = true
  try {
    const data = await DictDataApi.getDictDataPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

const resetQuery = () => {
  queryFormRef.value?.resetFields()
  handleQuery()
}

const chooseRow = (row: DictDataVO) => {
  selectedRow.value = row
}

const confirmChoose = () => {
  if (!selectedRow.value) return
  emit('choose', selectedRow.value)
  visible.value = false
}

const cancelChoose = () => {
  visible.value = false
}

const open = async () => {
  visible.value = true
  selectedRow.value = null
  queryParams.pageNo = 1
  queryParams.label = ''
  queryParams.status = undefined
  await getList()
}

defineExpose({ open })
</script>

<template>
  <Dialog v-model="visible" title="选择品牌" style="width: 1100px" class="brand-select-dialog">
    <div class="brand-select">
      <el-form
        ref="queryFormRef"
        :model="queryParams"
        class="brand-select__toolbar"
        label-width="68px"
        size="default">
        <div class="brand-select__filters">
          <el-form-item label="字典标签" prop="label">
            <el-input
              v-model="queryParams.label"
              placeholder="请输入字典标签"
              clearable
              class="brand-select__control"
              @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select
              v-model="queryParams.status"
              placeholder="数据状态"
              clearable
              class="brand-select__control">
              <el-option
                v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value" />
            </el-select>
          </el-form-item>
        </div>
        <div class="brand-select__actions">
          <el-button type="primary" @click="handleQuery">
            <Icon icon="ep:search" class="mr-5px" />
            搜索
          </el-button>
          <el-button @click="resetQuery">
            <Icon icon="ep:refresh" class="mr-5px" />
            重置
          </el-button>
        </div>
      </el-form>

      <div class="brand-select__table-panel">
        <ZmTable
          :data="list"
          :loading="loading"
          :max-height="430"
          :show-border="true"
          align="center"
          @row-click="chooseRow">
          <ZmTableColumn label="选择" width="70" hide-in-column-settings>
            <template #default="{ row }">
              <el-radio
                :model-value="selectedRow?.id"
                :value="row.id"
                class="brand-select__radio"
                @click.stop="chooseRow(row)" />
            </template>
          </ZmTableColumn>
          <ZmTableColumn prop="id" label="字典编码" width="120" />
          <ZmTableColumn prop="label" label="字典标签" min-width="180" />
          <ZmTableColumn prop="value" label="字典键值" min-width="180" />
          <ZmTableColumn prop="status" label="状态" width="120">
            <template #default="{ row }">
              <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="row.status ?? 0" />
            </template>
          </ZmTableColumn>
          <ZmTableColumn prop="remark" label="备注" min-width="160" />
          <ZmTableColumn
            prop="createTime"
            label="创建时间"
            width="180"
            :formatter="dateFormatter" />
        </ZmTable>

        <div class="brand-select__pagination">
          <Pagination
            v-model:page="queryParams.pageNo"
            v-model:limit="queryParams.pageSize"
            :total="total"
            @pagination="getList" />
        </div>
      </div>

      <div class="brand-select__footer">
        <el-button size="default" @click="cancelChoose">取消</el-button>
        <el-button size="default" type="primary" :disabled="!selectedRow" @click="confirmChoose">
          确定
        </el-button>
      </div>
    </div>
  </Dialog>
</template>

<style scoped lang="scss">
.brand-select {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.brand-select__toolbar {
  display: flex;
  padding: 14px 16px;
  background: linear-gradient(135deg, rgb(64 158 255 / 6%) 0%, rgb(255 255 255 / 0%) 100%);
  border: 1px solid var(--el-border-color-lighter);
  border-radius: 8px;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.brand-select__filters {
  display: flex;
  min-width: 0;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px 24px;
}

.brand-select__toolbar :deep(.el-form-item) {
  margin-bottom: 0;
}

.brand-select__control {
  width: 240px;
}

.brand-select__actions {
  display: flex;
  align-items: center;
  flex-shrink: 0;
  gap: 8px;
}

.brand-select__table-panel {
  padding: 12px;
  background: var(--el-bg-color);
  border: 1px solid var(--el-border-color-lighter);
  border-radius: 8px;
}

.brand-select__radio {
  height: 20px;
}

.brand-select__radio :deep(.el-radio__label) {
  display: none;
}

.brand-select__pagination {
  display: flex;
  padding-top: 12px;
  justify-content: flex-end;
}

.brand-select__footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

@media (width <= 900px) {
  .brand-select__toolbar {
    align-items: stretch;
    flex-direction: column;
  }

  .brand-select__actions {
    justify-content: flex-end;
  }

  .brand-select__control {
    width: 100%;
  }
}
</style>
