<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
  >
    <el-table :data="formData" class="-mt-10px">
      <el-table-column label="序号" type="index" width="100" />
       <el-table-column label="产品编号" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.productId`" :rules="formRules.productId" class="mb-0px!">
            <el-input v-model="row.productId" placeholder="请输入产品编号" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="产品名称" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.productName`" :rules="formRules.productName" class="mb-0px!">
            <el-input v-model="row.productName" placeholder="请输入产品名称" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="产品分类编号" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.productCategoryId`" :rules="formRules.productCategoryId" class="mb-0px!">
            <el-input v-model="row.productCategoryId" placeholder="请输入产品分类编号" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="产品单位" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.productUnit`" :rules="formRules.productUnit" class="mb-0px!">
            <el-input v-model="row.productUnit" placeholder="请输入产品单位" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="线上价格" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.onlinePrice`" :rules="formRules.onlinePrice" class="mb-0px!">
            <el-input v-model="row.onlinePrice" placeholder="请输入线上价格" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="线下价格" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.offlinePrice`" :rules="formRules.offlinePrice" class="mb-0px!">
            <el-input v-model="row.offlinePrice" placeholder="请输入线下价格" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="总计价格" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.totalPrice`" :rules="formRules.totalPrice" class="mb-0px!">
            <el-input v-model="row.totalPrice" placeholder="请输入总计价格" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="产品票据" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.productInvoice`" :rules="formRules.productInvoice" class="mb-0px!">
            <el-input v-model="row.productInvoice" placeholder="请输入产品票据" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="产品开具项目" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.productInvoiceItem`" :rules="formRules.productInvoiceItem" class="mb-0px!">
            <el-input v-model="row.productInvoiceItem" placeholder="请输入产品开具项目" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="服务费票据" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.serviceInvoice`" :rules="formRules.serviceInvoice" class="mb-0px!">
            <el-input v-model="row.serviceInvoice" placeholder="请输入服务费票据" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="服务开具项目" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.serviceInvoiceItem`" :rules="formRules.serviceInvoiceItem" class="mb-0px!">
            <el-input v-model="row.serviceInvoiceItem" placeholder="请输入服务开具项目" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="60">
        <template #default="{ $index }">
          <el-button @click="handleDelete($index)" link>—</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
  <el-row justify="center" class="mt-3">
    <!-- <el-button @click="handleAdd" round>+ 添加CRM 报价产品关联</el-button> -->
  </el-row>
</template>
<script setup lang="ts">
import { QuotationApi } from '@/api/crm/quotation'
import * as BusinessApi from '@/api/crm/business'

const props = defineProps<{
  quotationId: undefined, // 报价编号（主表的关联字段）
  businessId: undefined
}>()
const formLoading = ref(false) // 表单的加载中
const formData = ref([])
const formRules = reactive({
  quotationId: [{ required: true, message: '报价编号不能为空', trigger: 'blur' }],
  productId: [{ required: true, message: '产品编号不能为空', trigger: 'blur' }],
  productName: [{ required: true, message: '产品名称不能为空', trigger: 'blur' }],
  productCategoryId: [{ required: true, message: '产品分类编号不能为空', trigger: 'blur' }],
  productUnit: [{ required: true, message: '产品单位不能为空', trigger: 'blur' }],
  onlinePrice: [{ required: true, message: '线上价格不能为空', trigger: 'blur' }],
  offlinePrice: [{ required: true, message: '线下价格不能为空', trigger: 'blur' }],
  totalPrice: [{ required: true, message: '总计价格不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.businessId,
  async (val) => {
    // 1. 重置表单
    formData.value = []
    // 2. val 非空，则加载数据
    if (!val) {
      return;
    }
    try {
      formLoading.value = true
      // if(val.quotationId) {
        // formData.value = await QuotationApi.getQuotationProductListByQuotationId(val.quotationId)
      // } else if(val.businessId) {
        const business = await BusinessApi.getBusiness(val);
        formData.value = business.products
      // }
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

/** 新增按钮操作 */
const handleAdd = () => {
  const row = {
    quotationId: undefined,
    productId: undefined,
    productName: undefined,
    productCategoryId: undefined,
    productUnit: undefined,
    onlinePrice: undefined,
    offlinePrice: undefined,
    totalPrice: undefined,
    productInvoice: undefined,
    productInvoiceItem: undefined,
    serviceInvoice: undefined,
    serviceInvoiceItem: undefined
  }
  row.quotationId = props.quotationId
  formData.value.push(row)
}

/** 删除按钮操作 */
const handleDelete = (index) => {
  formData.value.splice(index, 1)
}

/** 表单校验 */
const validate = () => {
  return formRef.value.validate()
}

/** 表单值 */
const getData = () => {
  return formData.value
}

defineExpose({ validate, getData })
</script>