<template>
  <AFormPanel
    ref="formPanel"
    v-bind="formPanelProps">
    <template #default="formData">
      <div class="form-items">
        <ElFormItem label="Id" prop="id">
          <ElInput v-model="formData.id" placeholder="Id"/>
        </ElFormItem>
        <ElFormItem label="用户 Id" prop="userId">
          <ElInput v-model="formData.userId" placeholder="用户 Id"/>
        </ElFormItem>
        <ElFormItem label="客户 Id；cst_customer.id" prop="customerId">
          <ElInput v-model="formData.customerId" placeholder="客户 Id；cst_customer.id"/>
        </ElFormItem>
        <ElFormItem label="组织 Id" prop="orgId">
          <ElInput v-model="formData.orgId" placeholder="组织 Id"/>
        </ElFormItem>
        <ElFormItem label="账户类型；account_type，GeRen-->个人、QiYe-->企业" prop="accountType">
          <ElInput v-model="formData.accountType" placeholder="账户类型；account_type，GeRen-->个人、QiYe-->企业"/>
        </ElFormItem>
        <ElFormItem label="账期" prop="billPeriod">
          <ElInput v-model="formData.billPeriod" placeholder="账期"/>
        </ElFormItem>
        <ElFormItem label="账单开始时间" prop="startTime">
          <ElInput v-model="formData.startTime" placeholder="账单开始时间"/>
        </ElFormItem>
        <ElFormItem label="账单结束时间" prop="endTime">
          <ElInput v-model="formData.endTime" placeholder="账单结束时间"/>
        </ElFormItem>
        <ElFormItem label="订单数" prop="orderCount">
          <ElInput v-model="formData.orderCount" placeholder="订单数"/>
        </ElFormItem>
        <ElFormItem label="总车数" prop="carCount">
          <ElInput v-model="formData.carCount" placeholder="总车数"/>
        </ElFormItem>
        <ElFormItem label="总质量" prop="totalWeight">
          <ElInput v-model="formData.totalWeight" placeholder="总质量"/>
        </ElFormItem>
        <ElFormItem label="优惠金额" prop="discountMoney">
          <ElInput v-model="formData.discountMoney" placeholder="优惠金额"/>
        </ElFormItem>
        <ElFormItem label="账单金额" prop="totalMoney">
          <ElInput v-model="formData.totalMoney" placeholder="账单金额"/>
        </ElFormItem>
        <ElFormItem label="备注" prop="memo">
          <ElInput v-model="formData.memo" placeholder="备注"/>
        </ElFormItem>
        <ElFormItem label="创建人 Id；sys_user.id" prop="creatorId">
          <ElInput v-model="formData.creatorId" placeholder="创建人 Id；sys_user.id"/>
        </ElFormItem>
        <ElFormItem label="修改人 Id；sys_user.id" prop="modifierId">
          <ElInput v-model="formData.modifierId" placeholder="修改人 Id；sys_user.id"/>
        </ElFormItem>
        <ElFormItem label="创建时间" prop="createTime">
          <ElInput v-model="formData.createTime" placeholder="创建时间"/>
        </ElFormItem>
        <ElFormItem label="修改时间" prop="modifyTime">
          <ElInput v-model="formData.modifyTime" placeholder="修改时间"/>
        </ElFormItem>
        <ElFormItem label="是否删除； 0-->未删除、1-->已删除" prop="deleted">
          <ElInput v-model="formData.deleted" placeholder="是否删除； 0-->未删除、1-->已删除"/>
        </ElFormItem>
      </div>
    </template>
  </AFormPanel>
</template>

<script lang="ts" setup>
import BillApi from '@/pages/fin/bill/bill-api.ts'
import AFormPanel, {
  type AFormPanelInstance,
  buildFormPanelProps,
} from '@/components/a-form-panel/AFormPanel.tsx'
import Strings from '@/common/utils/strings.ts'

const props = withDefaults(defineProps<{
  research?: () => void
}>(), {
  research: () => {
  },
})
const formPanelIns = useTemplateRef<AFormPanelInstance>('formPanel')
const status = ref<'add' | 'modify'>('add')
const formPanelProps = buildFormPanelProps<BillTypes.SearchBillResult>({
  title: status.value === 'add' ? '新建对账单' : '修改对账单信息',
  detailsLoader(id?: string) {
    if (Strings.isBlank(id)) {
      status.value = 'add'
      return Promise.resolve()
    } else {
      status.value = 'modify'
      return BillApi
        .detail(id!)
        .then(res => res.data)
    }
  },
  doSubmit(data) {
    if (status.value === 'add') {
      return BillApi
        .add(data)
        .then(props.research)
    } else {
      return BillApi
        .modify(data)
        .then(props.research)
    }
  },
  rules: {
    id: [ {required: true, message: '请填写Id', trigger: 'blur'} ],
    userId: [ {required: true, message: '请填写用户 Id', trigger: 'blur'} ],
    customerId: [ {required: true, message: '请填写客户 Id；cst_customer.id', trigger: 'blur'} ],
    orgId: [ {required: true, message: '请填写组织 Id', trigger: 'blur'} ],
    accountType: [ {required: true, message: '请填写账户类型；account_type，GeRen-->个人、QiYe-->企业', trigger: 'blur'} ],
    billPeriod: [ {required: true, message: '请填写账期', trigger: 'blur'} ],
    startTime: [ {required: true, message: '请填写账单开始时间', trigger: 'blur'} ],
    endTime: [ {required: true, message: '请填写账单结束时间', trigger: 'blur'} ],
    orderCount: [ {required: true, message: '请填写订单数', trigger: 'blur'} ],
    carCount: [ {required: true, message: '请填写总车数', trigger: 'blur'} ],
    totalWeight: [ {required: true, message: '请填写总质量', trigger: 'blur'} ],
    discountMoney: [ {required: true, message: '请填写优惠金额', trigger: 'blur'} ],
    totalMoney: [ {required: true, message: '请填写账单金额', trigger: 'blur'} ],
    memo: [ {required: true, message: '请填写备注', trigger: 'blur'} ],
    creatorId: [ {required: true, message: '请填写创建人 Id；sys_user.id', trigger: 'blur'} ],
    modifierId: [ {required: true, message: '请填写修改人 Id；sys_user.id', trigger: 'blur'} ],
    createTime: [ {required: true, message: '请填写创建时间', trigger: 'blur'} ],
    modifyTime: [ {required: true, message: '请填写修改时间', trigger: 'blur'} ],
    deleted: [ {required: true, message: '请填写是否删除； 0-->未删除、1-->已删除', trigger: 'blur'} ],
  },
})

defineExpose({
  open(data?: BillTypes.SearchBillResult) {
    formPanelIns.value?.open(data?.id)
  },
})

</script>

<style lang="stylus" scoped>
.form-items {
  grid-template-columns: 1fr 1fr;
}
</style>
