<template>
  <AFormPanel
    ref="formPanel"
    v-bind="formPanelProps">
    <template #default="formData">
      <div class="form-items">
        <ElFormItem label="图片" prop="picture">
          <Uploader
            ref="uploader"
            v-model:file="formData.picture"/>
        </ElFormItem>
        <ElFormItem v-if="status !== 'add'" label="产品编码" prop="sn">
          <ElInput v-model="formData.sn" placeholder="产品编码" readonly/>
        </ElFormItem>
        <ElFormItem label="产品分类" prop="goodsCategoryId">
          <GoodsCategoryDropTable v-model="formData.goodsCategoryId"/>
        </ElFormItem>
        <ElFormItem label="产品名称" prop="goodsName">
          <ElInput v-model="formData.goodsName" 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="specParams">
          <ElInput v-model="formData.specParams" placeholder="规格"/>
        </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="sort">
          <ElInputNumber v-model="formData.sort" controls-position="right" placeholder="请输入排序"/>
        </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 GoodsApi from '@/pages/gds/goods/goods-api.ts'
import Strings from '@/common/utils/strings.ts'
import Uploader from '@/components/uploader/Uploader.vue'
import AFormPanel, {
  type AFormPanelInstance,
  buildFormPanelProps,
} from '@/components/a-form-panel/AFormPanel.tsx'
import GoodsCategoryDropTable from '@/pages/gds/goods-category/GoodsCategoryDropTable.vue'
import {
  expenseStrategy,
  unit,
} from '@/pages/gds/goods/constants.ts'

const props = withDefaults(defineProps<{
  research?: () => void
}>(), {
  research: () => {
  },
})

const formPanelIns = useTemplateRef<AFormPanelInstance>('formPanel')
const uploaderIns = useTemplateRef<InstanceType<typeof Uploader>>('uploader')
const status = ref<'add' | 'modify'>('add')
const formPanelProps = buildFormPanelProps<GoodsTypes.SearchGoodsResult>({
  title: status.value === 'add' ? '新建产品' : '修改产品',
  detailsLoader(id?: string) {
    if (Strings.isBlank(id)) {
      status.value = 'add'
      return Promise.resolve({
        expenseStrategy: expenseStrategy.MianFei,
        unit: unit.Dun,
        canuse: true,
      })
    } else {

      status.value = 'modify'
      return GoodsApi
        .detail(id!)
        .then((res) => {
          if (res.data.picture != null) uploaderIns.value?.setDefaultFiles([ res.data.picture ])
          return res.data
        })
    }
  },
  doSubmit(data) {
    if (status.value === 'add') {
      return GoodsApi.add(data)
        .then(props.research)
    } else {
      return GoodsApi.modify(data)
        .then(props.research)
    }
  },
  rules: {
    goodsCategoryId: [ {required: true, message: '请填写产品类型', trigger: 'blur'} ],
    sn: [ {required: true, message: '请填写产品编码', trigger: 'blur'} ],
    goodsName: [ {required: true, message: '请填写产品名称', trigger: 'blur'} ],
    specParams: [ {required: true, message: '请填写规格', trigger: 'blur'} ],
    picture: [ {required: true, message: '请填写图片', trigger: 'blur'} ],
    unit: [ {required: true, message: '请填写计量单位', trigger: 'blur'} ],
    sort: [ {required: true, message: '请填写排序', trigger: 'blur'} ],
    initialPrice: [ {required: true, message: '请填写排序', trigger: 'blur'} ],
  },
})

defineExpose({
  open(data: GoodsTypes.SearchGoodsResult = {}) {
    formPanelIns.value?.open(data.id)
  },
})
</script>

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

  :deep(.el-form-item) {
    &:first-child {
      align-items: start;
      grid-area: picture
      max-width 300px

      .el-form-item__content {
        justify-content center
      }
    }


    &:last-child:nth-child(2n) {
      grid-column: span 2;
      width: 100%;
    }
  }
}
</style>
