<template>
  <view class="yd-page-container">
    <!-- 顶部导航栏 -->
    <wd-navbar
      :title="getTitle"
      left-arrow placeholder safe-area-inset-top fixed
      @click-left="handleBack"
    />

    <!-- 表单区域 -->
    <view>
      <wd-form ref="formRef" :model="formData" :rules="formRules">
        <wd-cell-group title="基本信息" border>
          <wd-cell title="供应商" title-width="180rpx" prop="supplierId" center>
            <wd-picker
              v-model="formData.supplierId"
              :columns="supplierColumns"
              label=""
              placeholder="请选择供应商"
              @confirm="onSupplierConfirm"
            />
          </wd-cell>
          <wd-cell title="采购时间" title-width="180rpx" prop="orderTime" center>
            <wd-datetime-picker
              v-model="formData.orderTime"
              type="datetime"
              label=""
              placeholder="请选择采购时间"
            />
          </wd-cell>
        </wd-cell-group>

        <wd-cell-group title="合同信息" border>
          <wd-cell title="发票类别" title-width="180rpx" center>
            <wd-radio-group v-model="formData.invoiceType" shape="button">
              <wd-radio :value="1">
                增值税普通发票
              </wd-radio>
              <wd-radio :value="2">
                增值税专用发票
              </wd-radio>
            </wd-radio-group>
          </wd-cell>
          <wd-cell title="运费承担方" title-width="180rpx" center>
            <wd-radio-group v-model="formData.freightPayer" shape="button">
              <wd-radio :value="1">
                卖方承担
              </wd-radio>
              <wd-radio :value="2">
                买方承担
              </wd-radio>
            </wd-radio-group>
          </wd-cell>
          <wd-cell title="结算方式" title-width="180rpx" center>
            <wd-radio-group v-model="formData.settlementMethod" shape="button">
              <wd-radio :value="1">
                款到发货
              </wd-radio>
              <wd-radio :value="2">
                货到票到付款
              </wd-radio>
              <wd-radio :value="3">
                预付部分货到付清
              </wd-radio>
            </wd-radio-group>
          </wd-cell>
        </wd-cell-group>

        <wd-cell-group title="其他信息" border>
          <wd-input
            v-model="formData.discountPercent"
            label="优惠率(%)"
            label-width="180rpx"
            type="number"
            placeholder="请输入优惠率"
            clearable
          />
          <wd-input
            v-model="formData.depositPrice"
            label="定金金额"
            label-width="180rpx"
            type="number"
            placeholder="请输入定金金额"
            clearable
          />
          <wd-textarea
            v-model="formData.remark"
            label="备注"
            label-width="180rpx"
            placeholder="请输入备注"
            :maxlength="200"
            show-word-limit
            clearable
          />
        </wd-cell-group>
      </wd-form>
    </view>

    <!-- 订单明细 -->
    <view class="mx-24rpx mt-24rpx pb-180rpx">
      <view class="mb-16rpx flex items-center justify-between">
        <view class="text-32rpx text-[#333] font-semibold">
          订单明细（{{ formData.items?.length || 0 }}项）
        </view>
        <wd-button size="small" type="primary" @click="handleAddItem">
          + 添加产品
        </wd-button>
      </view>
      <view
        v-for="(item, index) in formData.items"
        :key="index"
        class="mb-16rpx overflow-hidden rounded-12rpx bg-white shadow-sm"
      >
        <view class="flex items-center justify-between bg-[#f8f8f8] px-24rpx py-12rpx">
          <text class="text-28rpx text-[#333] font-semibold">产品 #{{ index + 1 }} {{ item.productName || '' }}</text>
          <wd-button size="small" type="error" plain @click="handleRemoveItem(index)">
            删除
          </wd-button>
        </view>
        <view class="p-24rpx">
          <view class="mb-16rpx">
            <text class="mb-8rpx block text-24rpx text-[#999]">选择产品</text>
            <wd-picker
              v-model="item.productId"
              :columns="productColumns"
              placeholder="请选择产品"
              @confirm="(e: any) => onProductConfirm(e, index)"
            />
          </view>
          <!-- 自动填充的产品信息（只读） -->
          <view v-if="item.productId" class="mb-16rpx rounded-8rpx bg-[#f9f9f9] p-16rpx">
            <view class="mb-8rpx flex justify-between text-24rpx">
              <text class="text-[#999]">规格</text>
              <text class="text-[#333]">{{ item.productSpec || '-' }}</text>
            </view>
            <view class="mb-8rpx flex justify-between text-24rpx">
              <text class="text-[#999]">单位</text>
              <text class="text-[#333]">{{ item.productUnitName || '-' }}</text>
            </view>
            <view class="mb-8rpx flex justify-between text-24rpx">
              <text class="text-[#999]">条码</text>
              <text class="text-[#333]">{{ item.productBarCode || '-' }}</text>
            </view>
            <view class="flex justify-between text-24rpx">
              <text class="text-[#999]">供应商</text>
              <text class="text-[#333]">{{ item.supplierName || '-' }}</text>
            </view>
          </view>
          <view class="mb-16rpx flex gap-16rpx">
            <view class="flex-1">
              <text class="mb-8rpx block text-24rpx text-[#999]">数量</text>
              <wd-input
                v-model="item.count"
                type="number"
                placeholder="数量"
                clearable
              />
            </view>
            <view class="flex-1">
              <text class="mb-8rpx block text-24rpx text-[#999]">单价（元）</text>
              <wd-input
                v-model="item.productPrice"
                type="number"
                placeholder="单价"
                clearable
              />
            </view>
          </view>
          <view class="mb-16rpx flex gap-16rpx">
            <view class="flex-1">
              <text class="mb-8rpx block text-24rpx text-[#999]">税率(%)</text>
              <wd-input
                v-model="item.taxPercent"
                type="number"
                placeholder="税率"
                clearable
              />
            </view>
            <view class="flex-1">
              <text class="mb-8rpx block text-24rpx text-[#999]">金额</text>
              <text class="block pt-16rpx text-28rpx text-[#333]">{{ calcTotalPrice(item) }}</text>
            </view>
          </view>
          <view>
            <text class="mb-8rpx block text-24rpx text-[#999]">备注</text>
            <wd-input
              v-model="item.remark"
              placeholder="请输入备注"
              clearable
            />
          </view>
        </view>
      </view>
      <view v-if="!formData.items?.length" class="py-60rpx text-center text-28rpx text-[#999]">
        暂无产品，请点击“添加产品”按钮
      </view>
    </view>

    <!-- 底部保存按钮 -->
    <view class="yd-detail-footer">
      <wd-button
        type="primary"
        block
        :loading="formLoading"
        @click="handleSubmit"
      >
        保存
      </wd-button>
    </view>
  </view>
</template>

<script lang="ts" setup>
import type { FormInstance } from 'wot-design-uni/components/wd-form/types'
import type { PurchaseOrder, PurchaseOrderItem } from '@/api/erp/purchase-order'
import type { ProductSimple } from '@/api/erp/product'
import type { SupplierSimple } from '@/api/erp/supplier'
import { computed, onMounted, ref } from 'vue'
import { useToast } from 'wot-design-uni'
import { createPurchaseOrder, getPurchaseOrder, updatePurchaseOrder } from '@/api/erp/purchase-order'
import { getProductSimpleList } from '@/api/erp/product'
import { getSupplierSimpleList } from '@/api/erp/supplier'
import { navigateBackPlus } from '@/utils'

const props = defineProps<{
  id?: number | any
}>()

definePage({
  style: {
    navigationBarTitleText: '',
    navigationStyle: 'custom',
  },
})

const toast = useToast()
const getTitle = computed(() => props.id ? '编辑采购订单' : '新增采购订单')
const formLoading = ref(false)
const formData = ref<PurchaseOrder>({
  id: undefined,
  supplierId: undefined,
  orderTime: undefined,
  discountPercent: undefined,
  depositPrice: undefined,
  invoiceType: undefined,
  freightPayer: undefined,
  settlementMethod: undefined,
  remark: '',
  items: [],
})
const formRules = {
  supplierId: [{ required: true, message: '供应商不能为空' }],
  orderTime: [{ required: true, message: '采购时间不能为空' }],
}
const formRef = ref<FormInstance>()
const supplierList = ref<SupplierSimple[]>([])
const productList = ref<ProductSimple[]>([])

/** 供应商下拉列 */
const supplierColumns = computed(() => [
  supplierList.value.map(s => ({ value: s.id, label: s.name })),
])

/** 产品下拉列 */
const productColumns = computed(() => [
  productList.value.map(p => ({ value: p.id, label: p.name })),
])

/** 供应商选择回调 */
function onSupplierConfirm({ value }: any) {
  formData.value.supplierId = value?.[0]
}

/** 产品选择回调：自动填充规格/单位/条码/单价/供应商 */
function onProductConfirm({ value }: any, index: number) {
  const productId = value?.[0]
  if (formData.value.items && formData.value.items[index]) {
    const item = formData.value.items[index]
    item.productId = productId
    const product = productList.value.find(p => p.id === productId)
    if (product) {
      item.productName = product.name
      item.productSpec = product.standard || ''
      item.productUnitName = product.unitName || ''
      item.productUnitId = product.unitId as any
      item.productBarCode = product.barCode || ''
      item.productPrice = product.purchasePrice
      item.supplierId = product.supplierId
      item.supplierName = product.supplierName || ''
    }
  }
}

/** 计算行项金额 */
function calcTotalPrice(item: PurchaseOrderItem) {
  if (item.productPrice && item.count) {
    const total = Number(item.productPrice) * Number(item.count)
    return `¥${total.toFixed(2)}`
  }
  return '-'
}

/** 加载下拉列表数据 */
async function loadDropdownData() {
  try {
    const [suppliers, products] = await Promise.all([
      getSupplierSimpleList(),
      getProductSimpleList(),
    ])
    supplierList.value = suppliers || []
    productList.value = products || []
  } catch {
    // error handled by http
  }
}

/** 返回上一页 */
function handleBack() {
  navigateBackPlus('/pages-erp/purchase-order/index')
}

/** 添加订单明细行 */
function handleAddItem() {
  if (!formData.value.items) {
    formData.value.items = []
  }
  formData.value.items.push({
    productId: undefined as any,
    productName: undefined as any,
    productUnitId: undefined as any,
    productUnitName: undefined as any,
    productPrice: undefined,
    productSpec: undefined as any,
    productBarCode: undefined as any,
    count: 1 as any,
    taxPercent: undefined,
    supplierId: undefined,
    supplierName: undefined as any,
    remark: '',
  })
}

/** 删除订单明细行 */
function handleRemoveItem(index: number) {
  uni.showModal({
    title: '提示',
    content: `确定要删除第 ${index + 1} 项产品吗？`,
    success: (res) => {
      if (res.confirm) {
        formData.value.items?.splice(index, 1)
      }
    },
  })
}

/** 加载详情 */
async function getDetail() {
  if (!props.id) {
    return
  }
  formData.value = await getPurchaseOrder(props.id)
}

/** 提交表单 */
async function handleSubmit() {
  const { valid } = await formRef.value!.validate()
  if (!valid) {
    return
  }
  // 校验订单明细
  if (!formData.value.items || formData.value.items.length === 0) {
    toast.warning('请至少添加一项产品明细')
    return
  }
  for (let i = 0; i < formData.value.items.length; i++) {
    const item = formData.value.items[i]
    if (!item.productId) {
      toast.warning(`第 ${i + 1} 项产品ID不能为空`)
      return
    }
    if (!item.count) {
      toast.warning(`第 ${i + 1} 项产品数量不能为空`)
      return
    }
  }

  formLoading.value = true
  try {
    if (props.id) {
      await updatePurchaseOrder(formData.value)
      toast.success('修改成功')
    } else {
      await createPurchaseOrder(formData.value)
      toast.success('新增成功')
    }
    setTimeout(() => {
      handleBack()
    }, 500)
  } finally {
    formLoading.value = false
  }
}

/** 初始化 */
onMounted(async () => {
  await loadDropdownData()
  getDetail()
})
</script>

<style lang="scss" scoped>
</style>
