<template>
  <Dialog title="选择销售出库单" v-model="dialogVisible" width="1200px">
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="出库单号" prop="no">
          <el-input
            v-model="queryParams.no"
            placeholder="请输入出库单号"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="客户" prop="customerId">
          <el-select
            v-model="queryParams.customerId"
            clearable
            filterable
            placeholder="请选择客户"
            class="!w-240px"
          >
            <el-option
              v-for="item in customerList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="出库时间" prop="outTime">
          <el-date-picker
            v-model="queryParams.outTime"
            value-format="YYYY-MM-DD HH:mm:ss"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item>
          <el-button @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>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap>
      <el-table
        v-loading="loading"
        :data="list"
        :stripe="true"
        :show-overflow-tooltip="true"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column label="出库单号" align="center" prop="no" width="180"/>
        <el-table-column label="出库时间" align="center" prop="outTime" :formatter="dateFormatter2" width="180"/>

        <el-table-column label="客户" align="center" prop="customerName" />
<!--        <el-table-column label="销售人员" align="center" prop="saleUserName" />-->
        <el-table-column label="商品数量" align="center" prop="totalCount" />
        <el-table-column label="总金额" align="center" prop="totalPrice">
          <template #default="scope">
            <span>{{ erpPriceInputFormatter(scope.row.totalPrice) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.ERP_AUDIT_STATUS" :value="scope.row.status" />
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>

    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="!selectedOut">
        确 定
      </el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import { DICT_TYPE } from '@/utils/dict'
import { dateFormatter2 } from '@/utils/formatTime'
import { SaleOutApi,SaleOutVO} from '@/api/erp/sale/out'
import {CustomerApi,CustomerVO} from '@/api/erp/sale/customer'
import { erpPriceInputFormatter } from '@/utils'

defineOptions({ name: 'SaleOutEnableList' })

const dialogVisible = ref(false)
const loading = ref(true)
const total = ref(0)
const list = ref([])
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  no: undefined,
  customerId: undefined,
  outTime: [],
  status: 20 // 只显示已审核的出库单
})
const queryFormRef = ref()
const selectedOut = ref<SaleOutVO | null>(null)

const customerList = ref<CustomerVO[]>([])

const getList = async () => {
  loading.value = true
  try {
    const data = await SaleOutApi.getSaleOutPage(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 handleSelectionChange = (selection: SaleOutVO[]) => {
  selectedOut.value = selection.length > 0 ? selection[0] : null
}

const open = async () => {
  dialogVisible.value = true
  selectedOut.value = null
  await getList()
}

const submitForm = async () => {
  if (!selectedOut.value) return

  // 获取出库单详情，包含商品明细
  const outDetail = await SaleOutApi.getSaleOut(selectedOut.value.id)
  emit('success', outDetail)
  dialogVisible.value = false
}



const emit = defineEmits(['success'])

if (onMounted) {
  onMounted(async () => {
    customerList.value = await CustomerApi.getCustomerSimpleList()
  })
}

defineExpose({ open })
</script>
