<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" width="1200" :scroll="true" max-height="70vh" align-center>
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="auto">
      <el-form-item :label="t('EquipmentManagement.CapacityReport.dialogProductCode')" prop="productCode">
        <el-input
          v-model="queryParams.productCode"
          :placeholder="t('EquipmentManagement.CapacityReport.dialogProductCodePlaceholder')"
          clearable
          @keyup.enter="handleQuery"
          class="!w-200px"
        />
      </el-form-item>
      <el-form-item :label="t('EquipmentManagement.CapacityReport.dialogProductName')" prop="productName">
        <el-input
          v-model="queryParams.productName"
          :placeholder="t('EquipmentManagement.CapacityReport.dialogProductNamePlaceholder')"
          clearable
          @keyup.enter="handleQuery"
          class="!w-200px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          {{ t('EquipmentManagement.CapacityReport.dialogSearchButtonText') }}
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          {{ t('EquipmentManagement.CapacityReport.dialogResetButtonText') }}
        </el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true" style="margin-top: 16px">
      <el-table-column :label="t('EquipmentManagement.CapacityReport.dialogProductCode')" align="center" prop="productCode" />
      <el-table-column :label="t('EquipmentManagement.CapacityReport.dialogProductName')" align="center" prop="productName" />
      <el-table-column :label="t('EquipmentManagement.CapacityReport.dialogBaogongTotal')" align="center" prop="baogongTotal" />
      <el-table-column :label="t('EquipmentManagement.CapacityReport.dialogAvgCapacity')" align="center" prop="avgCapacity" />
    </el-table>
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </Dialog>
</template>

<script setup lang="ts">
import { PlanApi } from '@/api/mes/plan'

const { t } = useI18n()

const props = defineProps<{
  modelValue: boolean
  deviceId?: number
  deviceName?: string
}>()

const emit = defineEmits<{
  (e: 'update:modelValue', value: boolean): void
}>()

const dialogVisible = computed({
  get() {
    return props.modelValue
  },
  set(value: boolean) {
    emit('update:modelValue', value)
  }
})

const dialogTitle = computed(() => {
  const name = props.deviceName || '-'
  return `${t('EquipmentManagement.CapacityReport.dialogTitlePrefix')}${name}`
})

const loading = ref(false)
const list = ref<any[]>([])
const total = ref(0)
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  deviceId: undefined as number | undefined,
  productCode: undefined as string | undefined,
  productName: undefined as string | undefined
})
const queryFormRef = ref()

const getList = async () => {
  if (!props.deviceId) {
    list.value = []
    total.value = 0
    return
  }
  loading.value = true
  try {
    const data = await PlanApi.getProductCapacityPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

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

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

watch(
  () => [props.modelValue, props.deviceId],
  ([visible, deviceId]) => {
    if (!visible) return
    queryParams.deviceId = deviceId as number
    queryParams.pageNo = 1
    queryParams.productCode = undefined
    queryParams.productName = undefined
    queryParams.taskCode = undefined
    getList()
  }
)
</script>
