<script lang="ts" setup>
import { Api, type SupplierVO } from '@/api/supplier/base'
import { DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import { useTableComponents } from '@/components/ZmTable/useTableComponents'

interface SupplierRow extends SupplierVO {
  createTime?: string | number | Date
}

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

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

const visible = ref(false)
const loading = ref(false)
const title = ref('选择客商')
const queryFormRef = ref()
const list = ref<SupplierRow[]>([])
const total = ref(0)
const selectedRow = ref<SupplierRow | null>(null)

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined as string | undefined,
  code: undefined as string | undefined
})

const getList = async () => {
  loading.value = true
  try {
    const data = await Api.getPage(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: SupplierRow) => {
  selectedRow.value = row
}

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

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

const open = async (dialogTitle = '选择客商') => {
  title.value = dialogTitle
  visible.value = true
  selectedRow.value = null
  queryParams.pageNo = 1
  queryParams.name = undefined
  queryParams.code = undefined
  await getList()
}

defineExpose({ open })
</script>

<template>
  <Dialog v-model="visible" :title="title" style="width: 1100px" class="customer-select-dialog">
    <div class="customer-select">
      <el-form
        ref="queryFormRef"
        :model="queryParams"
        class="customer-select__toolbar"
        label-width="68px"
        size="default">
        <div class="customer-select__filters">
          <el-form-item label="客商名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入客商名称"
              clearable
              class="customer-select__control"
              @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="客商编号" prop="code">
            <el-input
              v-model="queryParams.code"
              placeholder="请输入客商编号"
              clearable
              class="customer-select__control"
              @keyup.enter="handleQuery" />
          </el-form-item>
        </div>
        <div class="customer-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="customer-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="customer-select__radio"
                @click.stop="chooseRow(row)" />
            </template>
          </ZmTableColumn>
          <ZmTableColumn prop="name" label="客商名称" min-width="180" />
          <ZmTableColumn prop="code" label="客商编号" min-width="150" />
          <ZmTableColumn prop="classification" label="客商分类" width="140">
            <template #default="{ row }">
              <dict-tag :type="DICT_TYPE.SUPPLIER_TYPE" :value="row.classification ?? 0" />
            </template>
          </ZmTableColumn>
          <ZmTableColumn prop="status" label="状态" width="120">
            <template #default="{ row }">
              <dict-tag :type="DICT_TYPE.SUPPLIER_STATUS" :value="row.status ?? 0" />
            </template>
          </ZmTableColumn>
          <ZmTableColumn
            prop="createTime"
            label="创建时间"
            width="180"
            :formatter="dateFormatter" />
        </ZmTable>

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

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

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

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

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

.customer-select__control {
  width: 220px;
}

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

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

.customer-select__radio {
  height: 20px;
}

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

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

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

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

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

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