<template>
  <AFormPanel
    ref="formPanel"
    v-bind="formPanelProps">
    <template #default="formData">
      <div class="form-items">
        <ElFormItem label="项目类型" prop="expenseItemCategory">
          <ElSelect v-model="formData.expenseItemCategory">
            <ElOption
              v-for="item in expenseItemCategory"
              :key="'expenseItemCategory'+item.val"
              :label="item.txt"
              :value="item.val"/>
          </ElSelect>
        </ElFormItem>

        <ElFormItem v-if="formData.expenseItemCategory === 'ChanPin'" label="产品" prop="goodsId">
          <GoodsDropTable v-model="formData.goodsId"/>
        </ElFormItem>
        <ElFormItem v-else label="项目名称" prop="expenseItemName">
          <ElInput v-model="formData.expenseItemName" placeholder="项目名称"/>
        </ElFormItem>
        <ElFormItem label="计费策略" prop="expenseStrategy">
          <ElRadioGroup v-model="formData.expenseStrategy">
            <ElRadio v-for="it in expenseStrategy" :key="'expenseStrategy'+it.val" :value="it.val" border>{{ it.txt }}</ElRadio>
          </ElRadioGroup>
        </ElFormItem>
        <ElFormItem label="计量单位" prop="unit">
          <ElSelect v-model="formData.unit">
            <ElOption
              v-for="item in unit"
              :key="'unit'+item.val"
              :label="item.txt"
              :value="item.val"/>
          </ElSelect>
        </ElFormItem>
        <ElFormItem v-if="formData.expenseStrategy !== expenseStrategy.MianFei" label="税率" prop="taxRate">
          <ElInputNumber v-model="formData.taxRate" :max="100" :min="0" :precision="2" :step="0.01" controls-position="right" placeholder="请输入税率">
            <template #suffix>%</template>
          </ElInputNumber>
        </ElFormItem>
        <ElFormItem v-if="formData.expenseStrategy === expenseStrategy.TanXing" label="起步价" prop="initialPrice">
          <ElInputNumber v-model="formData.initialPrice" :min="0" :precision="2" :step="1" controls-position="right" placeholder="请输入起步价">
            <template #suffix>元/{{ unit.txt(formData.unit) }}</template>
          </ElInputNumber>
        </ElFormItem>
        <ElFormItem v-if="formData.expenseStrategy === expenseStrategy.TanXing" label="起步量" prop="initialQuantity">
          <ElInputNumber v-model="formData.initialQuantity" :min="0" :step="1" controls-position="right" placeholder="请输入起步量">
            <template #suffix>{{ unit.txt(formData.unit) }}</template>
          </ElInputNumber>
        </ElFormItem>
        <ElFormItem
          v-if="formData.expenseStrategy !== expenseStrategy.MianFei"
          :label="formData.expenseStrategy === expenseStrategy.TanXing?'每档单价':(formData.expenseStrategy === expenseStrategy.DanJia?'单价':'价格')"
          prop="unitPrice">
          <ElInputNumber v-model="formData.unitPrice" :min="0" :placeholder="`请输入${formData.expenseStrategy === expenseStrategy.TanXing?'每档单价':(formData.expenseStrategy === expenseStrategy.DanJia?'单价':'价格')}`" :precision="2" :step="1" controls-position="right">
            <template #suffix>{{ (formData.expenseStrategy === expenseStrategy.DanJia || formData.expenseStrategy === expenseStrategy.TanXing ? `元/${unit.txt(formData.unit)}` : `元`) }}</template>
          </ElInputNumber>
        </ElFormItem>
        <ElFormItem v-if="formData.expenseStrategy === expenseStrategy.TanXing" label="每档的量" prop="everyQuantity">
          <ElInputNumber v-model="formData.everyQuantity" :min="0" :step="1" controls-position="right" placeholder="请输入每档的量">
            <template #suffix>{{ unit.txt(formData.unit) }}</template>
          </ElInputNumber>
        </ElFormItem>
        <ElFormItem label="是否可用" prop="canuse">
          <el-switch v-model="formData.canuse"/>
        </ElFormItem>
        <ElFormItem label="备注" prop="memo">
          <ElInput v-model="formData.memo" placeholder="请输入备注"/>
        </ElFormItem>
      </div>
    </template>
  </AFormPanel>
</template>

<script lang="ts" setup>
import ExpenseItemApi from '@/pages/fin/expense-item/expense-item-api.ts'
import AFormPanel, {
  type AFormPanelInstance,
  buildFormPanelProps,
} from '@/components/a-form-panel/AFormPanel.tsx'
import Strings from '@/common/utils/strings.ts'
import {
  expenseStrategy,
  unit,
} from '@/pages/gds/goods/constants.ts'
import { expenseItemCategory } from '@/pages/fin/expense-item/constants.ts'
import GoodsDropTable from '@/pages/gds/goods/GoodsDropTable.vue'

const props = withDefaults(defineProps<{
  research?: () => void
}>(), {
  research: () => {
  },
})
const formPanelIns = useTemplateRef<AFormPanelInstance>('formPanel')
const status = ref<'add' | 'modify'>('add')
const formPanelProps = buildFormPanelProps<ExpenseItemTypes.SearchExpenseItemResult>({
  detailsLoader(id?: string) {
    if (Strings.isBlank(id)) {
      status.value = 'add'
      return Promise.resolve({
        expenseItemCategory: 'QiTa',
        unit: 'Dun',
        expenseStrategy: 'MianFei',
        canuse: true,
      })
    } else {
      status.value = 'modify'
      return ExpenseItemApi
        .detail(id!)
        .then(res => res.data)
    }
  },
  doSubmit(data) {
    if (status.value === 'add') {
      return ExpenseItemApi
        .add(data)
        .then(props.research)
    } else {
      return ExpenseItemApi
        .modify(data)
        .then(props.research)
    }
  },
  watchForm: {
    formItems: [ 'expenseStrategy' ],
  },
  rules: formData => {
    return {
      expenseItemCategory: [ {required: true, message: '请选择项目类型', trigger: 'blur'} ],
      goodsId: [ {required: true, message: '请选择产品', trigger: 'blur'} ],
      expenseItemName: [ {required: true, message: '请填写项目名称', trigger: 'blur'} ],
      unit: [ {required: true, message: '请选择计量单位', trigger: 'blur'} ],
      expenseStrategy: [ {required: true, message: '请填选择计费策略', trigger: 'blur'} ],
      taxRate: [ {required: true, message: '请填填写税率', trigger: 'blur'} ],
      unitPrice: [ {required: formData.expenseStrategy !== expenseStrategy.MianFei, message: `请填填写${formData.expenseStrategy === expenseStrategy.TanXing ? '每档单价' : (formData.expenseStrategy === expenseStrategy.DanJia ? '单价' : '价格')}`, trigger: 'blur'} ],
      initialPrice: [ {required: formData.expenseStrategy === expenseStrategy.TanXing, message: '请填写起步价', trigger: 'blur'} ],
      initialQuantity: [ {required: formData.expenseStrategy === expenseStrategy.TanXing, message: '请填写起步量', trigger: 'blur'} ],
      everyQuantity: [ {required: formData.expenseStrategy === expenseStrategy.TanXing, message: '请填写每档的量', trigger: 'blur'} ],
    }
  },
})
watchEffect(() => {
  formPanelProps.title = status.value === 'add' ? '新建收费项目' : '修改收费项目信息'
})
defineExpose({
  open(data?: ExpenseItemTypes.SearchExpenseItemResult) {
    formPanelIns.value?.open(data?.id)
  },
})

</script>

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