<template>
  <Dialog :title="'产品信息'" v-model="dialogVisible" width="1000px">
    <!-- 搜索栏 -->
    <el-form :model="queryParams" ref="queryFormRef" :inline="true" class="-mb-15px">
      <el-form-item label="供应商" prop="supplierName">
        <el-input v-model="queryParams.supplierName" placeholder="请输入" clearable class="!w-180px"  disabled/>
      </el-form-item>
      <el-form-item label="采购单号" prop="purOrdNo">
        <el-input v-model="queryParams.purOrdNo" placeholder="请输入" clearable class="!w-180px" />
      </el-form-item>
      <el-form-item>
        <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>
      </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table
      v-loading="loading"
      :data="list"
      :stripe="true"
      ref="tableRef"
      @row-dblclick="handleRowDblclick"
    >
      <el-table-column  width="60" align="center">
        <template #default="scope">
          <el-radio
            :model-value="selectedRow?.id"
            :label="scope.row.id"
            @change="handleRadioChange(scope.row)"
          >
            <span></span>
          </el-radio>
        </template>
      </el-table-column>
      <el-table-column type="index" label="序号" width="60" align="center" />
      <el-table-column label="采购单号" prop="purOrdNo" align="center" min-width="120" />
      <el-table-column label="单据类型" prop="billType" align="center" width="120" >
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.BILL_TYPE" :value="scope.row.billType"/>
        </template>
      </el-table-column>
      <el-table-column label="订单日期" prop="purDate" align="center" min-width="150" :formatter="dateFormatter2"/>=
      <el-table-column label="供应商名称" prop="supplierName" align="center" min-width="120" />
      <el-table-column label="物料编码" prop="materialCode" align="center" min-width="100" />
      <el-table-column label="物料名称" prop="materialName" align="center" min-width="100" />
      <el-table-column label="采购数量" prop="purQty" align="center" min-width="100" />
      <el-table-column label="收货数量" prop="deliveryQty" align="center" min-width="100" />
      <el-table-column label="规格型号" prop="spec" align="center" min-width="100" />
      <el-table-column label="要求到货日期" prop="reqDeliveryDate" align="center" min-width="150" :formatter="dateFormatter2"/>
      <el-table-column label="采购单价" prop="priceTax" align="center" min-width="100" />
      <el-table-column label="采购总价" prop="totalPrice" align="center" min-width="100" />
    </el-table>

    <template #footer>
      <div style="display: flex; justify-content: flex-end; align-items: center; padding: 10px 0;">
        <Pagination
          :total="total"
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
          style="margin-right: auto;"
        />
        <div style="display: flex; gap: 10px; flex-shrink: 0;">
          <el-button @click="handleConfirm" :disabled="!selectedRow" type="primary">确 定</el-button>
          <el-button @click="dialogVisible = false">取 消</el-button>
        </div>
      </div>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import * as PurorderItemApi from '@/api/biz/purorderitem'
import { DICT_TYPE } from '@/utils/dict'
import {dateFormatter2} from "@/utils/formatTime";

const dialogVisible = ref(false)
const loading = ref(false)
const list = ref<PurorderItemApi.PurOrderItemVO[]>([])
const total = ref(0)
const selectedRow = ref<PurorderItemApi.PurOrderItemVO | null>(null)
const tableRef = ref()
const queryFormRef = ref()

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  purStatus : 2,
  deliveryStatusList  : ['1','2'],
  supplierName:undefined,
  purOrdNo:undefined,
})

const emit = defineEmits(['confirm'])

// 打开弹窗，支持传入已选中的物料进行回显
const open = (supplierSimName) => {
  dialogVisible.value = true
  queryParams.pageNo = 1
  queryParams.supplierName = supplierSimName
  queryParams.purOrdNo = undefined

  // resetQuery()
}

defineExpose({ open })

// 查询列表
const getList = async () => {
  loading.value = true
  try {
    const data = await PurorderItemApi.getPurOrderItemPage(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.pageNo = 1
  getList()
}

// 单选框选中变化 - 仅记录选中行，点击确定按钮再传给父组件
const handleRadioChange = (row: PurorderItemApi.PurOrderItemVO) => {
  selectedRow.value = row
}

// 双击行 - 选中并关闭弹窗
const handleRowDblclick = (row: PurorderItemApi.PurOrderItemVO) => {
  selectedRow.value = row
  emit('confirm', row)
  dialogVisible.value = false
}

// 确认选择
const handleConfirm = () => {
  if (selectedRow.value) {
    emit('confirm', selectedRow.value)
    dialogVisible.value = false
  }
}
</script>
