<template>
  <Dialog title="发起采购" v-model="dialogVisible" width="800px">
    <el-form :model="form" ref="formRef" :rules="rules" label-width="100px">
      <el-row :gutter="20">
        <el-col :span="12" :offset="0">
          <el-form-item label="产品" prop="productId">
            <el-select v-model="form.productId" placeholder="请选择" filterable style="width: 100%">
              <el-option
                v-for="item in props.opts.product"
                :key="item.productId"
                :label="item.productName"
                :value="item.productId"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12" :offset="0">
          <el-form-item label="规格" prop="specsId">
            <el-select v-model="form.specsId" placeholder="请选择" filterable style="width: 100%">
              <el-option
                v-for="item in specOptions"
                :key="item.specsId"
                :label="item.specsName"
                :value="item.specsId"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12" :offset="0">
          <el-form-item label="供应商" prop="supplier">
            <el-select v-model="form.supplier" placeholder="选择供应商" filterable>
              <el-option
                v-for="item in opts.supplier"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12" :offset="0">
          <el-form-item label="采购数量" prop="num">
            <el-input-number
              :min="1"
              v-model="form.num"
              :controls="false"
              placeholder="请输入"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12" :offset="0">
          <el-form-item label="采购单价" prop="unitPrice">
            <el-input-number
              :min="0"
              :controls="false"
              v-model="form.unitPrice"
              placeholder="请输入"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12" :offset="0">
          <el-form-item label="存放仓库" prop="warehouseId">
            <el-select
              v-model="form.warehouseId"
              placeholder="请选择"
              filterable
              style="width: 100%"
            >
              <el-option
                v-for="item in warehouseOptions"
                :key="item.warehouseId"
                :label="item.warehouseName"
                :value="item.warehouseId"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24" :offset="0">
          <el-form-item label="备注" prop="remark">
            <Editor v-model:modelValue="form.remark" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <span>
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSave">保 存</el-button>
      </span>
    </template>
  </Dialog>
</template>

<script setup>
import * as PurchaseApi from '@/api/mall/purchase'
import * as WarehouseApi from '@/api/mall/warehouse'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const emit = defineEmits(['success'])

const props = defineProps({
  opts: {
    type: Object,
    default: () => {
      return {
        product: [],
        spec: [],
        supplier: [],
        warehouse: []
      }
    }
  }
})

const specOptions = computed(() => {
  if (form.value.productId) {
    return (
      props.opts.product.find((it) => it.productId == form.value.productId)?.productSpecList || []
    )
  } else {
    return []
  }
})

const dialogVisible = ref(false) // 弹窗的是否展示

const form = ref({})
const rules = {
  productId: { required: true, message: '产品不可为空', trigger: 'change' },
  specsId: { required: true, message: '规格不可为空', trigger: 'change' },
  supplier: { required: true, message: '供应商不可为空', trigger: 'change' },
  warehouseId: { required: true, message: '仓库不可为空', trigger: 'change' },
  num: { required: true, message: '采购数量不可为空', trigger: 'change,blur' },
  unitPrice: { required: true, message: '采购单价不可为空', trigger: 'change,blur' }
}

const warehouseOptions = ref([])
function getOptions() {
  WarehouseApi.getSimpleWarehouseList().then((data) => {
    warehouseOptions.value = data
  })
}

const open = (val) => {
  dialogVisible.value = true
  getOptions()
  form.value = { ...val } || {
    productId: undefined,
    specsId: undefined,
    supplier: undefined,
    num: undefined,
    unitPrice: undefined,
    warehouseId: undefined,
    remark: undefined
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

const formRef = ref()
const formLoading = ref(false)

async function handleSave() {
  // 校验表单
  if (!formRef.value) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    await PurchaseApi.createPurchase(form.value)
    message.success(t('common.createSuccess'))
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}
</script>

<style scoped></style>
