<script lang="ts" setup>
import { IotModelApi, type IotModelVO } from '@/api/pms/model'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import { useTableComponents } from '@/components/ZmTable/useTableComponents'

type BrandValue = number | string | Array<number | string>

interface ModelRow extends IotModelVO {
  createTime?: string | number | Date
}

const props = defineProps<{
  brand?: BrandValue
}>()

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

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

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

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  brand: undefined as BrandValue | undefined,
  name: '',
  status: undefined as number | undefined
})

const getList = async () => {
  loading.value = true
  try {
    const data = await IotModelApi.getIotModelPage(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: ModelRow) => {
  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.name = ''
  queryParams.status = undefined
  queryParams.brand = props.brand
  await getList()
}

defineExpose({ open })
</script>

<template>
  <Dialog v-model="visible" title="选择规格型号" style="width: 1100px" class="model-select-dialog">
    <div class="model-select">
      <el-form
        ref="queryFormRef"
        :model="queryParams"
        class="model-select__toolbar"
        label-width="68px"
        size="default">
        <div class="model-select__filters">
          <el-form-item label="型号名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入型号名称"
              clearable
              class="model-select__control"
              @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select
              v-model="queryParams.status"
              placeholder="数据状态"
              clearable
              class="model-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="model-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="model-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="model-select__radio"
                @click.stop="chooseRow(row)" />
            </template>
          </ZmTableColumn>
          <ZmTableColumn prop="name" label="型号名称" min-width="180" />
          <ZmTableColumn prop="standard" 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="createTime"
            label="创建时间"
            width="180"
            :formatter="dateFormatter" />
        </ZmTable>

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

      <div class="model-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">
.model-select {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.model-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;
}

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

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

.model-select__control {
  width: 240px;
}

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

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

.model-select__radio {
  height: 20px;
}

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

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

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

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

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

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