<script setup>
import { ref, reactive, computed } from "vue";

import { addBonus } from "@/api/bonus";

const props = defineProps({
  show: {
    type: Boolean,
    default: false,
  },
});

const emit = defineEmits(['update:show','reload']);

const showDrawer = computed({
  get: () => props.show,
  set: (value) => {
    emit("update:show", value);
  },
});

const formRef = ref();

const rules = reactive({
  type_name: [{ required: true, message: "請輸入優惠劵名稱", trigger: "blur" }],
  type_money: [{ required: true, message: "請輸入優惠劵金額", trigger: "blur" }],
  send_type: [{ required: true, message: "請選擇發放方式", trigger: "change" }],
});

const form = reactive({
  type_name: '',
  type_money: 0,
  send_type: 4,
  min_amount: 0,
  send_type: null,
  user_bonus_max: 0,
});

const loading = ref(false);

const handleClose = () => {
  showDrawer.value = false;
};

const handleSubmit = async (formEl) => {
  if (!formEl) return;

  await formEl.validate(async (valid, fields) => {
    if (!valid) return;

    let res = await addBonus(form)
    if(res.code === 200){
      // 關閉抽屜
      showDrawer.value = false;
      // 重置表單
      formRef.value.resetFields();

      // emit reload event
      emit('reload');

      // 顥示提示訊息
      return ElMessage.success('新增成功');
    }
    // 顯示錯誤提示訊息
    return ElMessage.error(res.msg);
  });
};
</script>

<template>
 <el-form :model="form" label-width="120px">
    <el-form-item label="商品名稱">
      <el-input v-model="form.goods_name" />
    </el-form-item>
    <el-form-item label="商品貨號">
      <el-input v-model="form.goods_sn" />
    </el-form-item>
    <el-form-item label="商品分類">
      <el-select v-model="form.region" placeholder="please select your zone">
        <el-option label="Zone one" value="shanghai" />
        <el-option label="Zone two" value="beijing" />
      </el-select>
    </el-form-item>
    <el-form-item label="商品品牌">
      <el-select v-model="form.region" placeholder="please select your zone">
        <el-option label="Zone one" value="shanghai" />
        <el-option label="Zone two" value="beijing" />
      </el-select>
    </el-form-item>
    <el-form-item label="本店售價">
      <el-input v-model="form.shop_price" />
    </el-form-item>
    <el-form-item label="市場售價">
      <el-input v-model="form.market_price" />
    </el-form-item>
    <el-form-item label="促銷價">
      <el-input v-model="form.market_price" />
    </el-form-item>
    <el-form-item label="促銷日期">
      <el-col :span="11">
        <el-date-picker
          v-model="form.date1"
          type="date"
          placeholder="Pick a date"
          style="width: 100%"
        />
      </el-col>
      <el-col :span="2" class="text-center">
        <span class="text-gray-500">-</span>
      </el-col>
      <el-col :span="11">
        <el-time-picker
          v-model="form.date2"
          placeholder="Pick a time"
          style="width: 100%"
        />
      </el-col>
    </el-form-item>
    <el-form-item label="分成金額">
      <el-input v-model="form.market_price" />
    </el-form-item>
  </el-form>  
</template>

<style lang="less"></style>
