<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}.productName`" :rules="formRules.productName" class="mb-0px!">
            {{row.productName}}
          </el-form-item>
        </template>
      </el-table-column>
       <el-table-column label="产品类型" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.category`" :rules="formRules.category" class="mb-0px!">
          <dict-tag :type="DICT_TYPE.CRM_PRODUCT_CATEGORY" :value="row.category" />
          </el-form-item>
        </template>
      </el-table-column>
      
      <el-table-column label="产品明细类型" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.detailType`" :rules="formRules.detailType" class="mb-0px!">
            <dict-tag :type="DICT_TYPE.CRM_PRODUCT_DETAIL_TYPE" :value="row.detailType" />
          </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!">
            <dict-tag :type="DICT_TYPE.CRM_PRODUCT_UNIT" :value="row.productUnit" />
          </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!">
            {{row.onlinePrice}}
          </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!">
            {{row.offlinePrice}}
          </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!">
            {{row.totalPrice}}
          </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!">
            <dict-tag :type="DICT_TYPE.CRM_PRODUCT_INVOICE" :value="row.productInvoice" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="产品开具项目" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.productInvoiceItems`" :rules="formRules.productInvoiceItems" class="mb-0px!">
            <dict-tag :type="DICT_TYPE.CRM_PRODUCT_INVOICE_ITEMS" :value="row.productInvoiceItems" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="服务费票据" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.serviceFeeInvoice`" :rules="formRules.serviceFeeInvoice" class="mb-0px!">
            <dict-tag :type="DICT_TYPE.CRM_SERVICE_FEE_INVOICE" :value="row.serviceFeeInvoice" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="服务开具项目" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.serviceFeeInvoiceItems`" :rules="formRules.serviceFeeInvoiceItems" class="mb-0px!">
            <dict-tag :type="DICT_TYPE.CRM_SERVICE_FEE_INVOICE_ITEMS" :value="row.serviceFeeInvoiceItems" />
          </el-form-item>
        </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 { BillTemplateApi, BillTemplateVO } from '@/api/crm/billtemplate'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import * as BusinessApi from '@/api/crm/business'

const props = defineProps<{
  quotationId: undefined, // 报价编号（主表的关联字段）
  businessId: undefined,
  billTemplateId: undefined,
  products: 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' }],
  detailType: [{ 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
    }
  },
)

watch(
  () => props.billTemplateId,
  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 BillTemplateApi.getTemplateProduct({
          businessId: props.businessId,
          billTemplateId: val,
        });
        console.log('%csrc/views/crm/quotation/components/QuotationProductForm.vue:167 business.data', 'color: #007acc;', business.data);
        formData.value = business
      // }
    } finally {
      formLoading.value = false
    }
  },
)
watch(
  () => props.products,
  (val) => {
    if (!val || val.length === 0) {
      return;
    }
    nextTick(() => {
      formData.value = val
    })
  },
  { immediate: true }

);

/** 新增按钮操作 */
const handleAdd = () => {
  const row = {
    quotationId: undefined,
    productId: undefined,
    productName: undefined,
    detailType: undefined,
    productUnit: undefined,
    onlinePrice: undefined,
    offlinePrice: undefined,
    totalPrice: undefined,
    productInvoice: undefined,
    productInvoiceItems: undefined,
    serviceFeeInvoice: undefined,
    serviceFeeInvoiceItems: 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>