<template>
  <el-dialog title="存储码新增" v-model="visible" width="400px" append-to-body draggable>
    <div class="form-container">
      <el-form ref="codeRef" class="master-container" :model="form" :rules="rules"
        label-width="80px">
        <el-form-item label="年份" prop="year">
          <el-input-number  v-model="form.year" style="width: 220px;" :min="10" :max="99" :step="1" step-strictly
          :controls="false" placeholder="请输入年份" />
        </el-form-item>
        <el-form-item label="生成数量" prop="num">
          <el-input-number  v-model="form.num" style="width: 220px;" min="1" max="1000" :step="1" step-strictly
          :controls="false" placeholder="请输入1-1000的数字" />
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <el-button type="primary" icon="Check" @click="handleSave">确 定</el-button>
      <el-button icon="Close" @click="handleCancel">取 消</el-button>
    </template>
  </el-dialog>
</template>
<script setup>
import { addStorageCode } from "@/api/business/storageCode";
const { proxy } = getCurrentInstance();
const emit = defineEmits(["handleSaveSuccess"]);
const visible = ref(false);
const data = reactive({
  form: {},
  rules: {
    year: [{ required: true, message: "年份不能为空", trigger: "blur" }],
    num: [{ required: true, message: "生成数量不能为空", trigger: "blur" }]
  },
});
const { form, rules } = toRefs(data);

/***********************  方法区  ****************************/
/** 打开抽屉 */
function open() {
  reset();
  visible.value = true;
}

/** 取消按钮 */
function handleCancel() {
  visible.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    year: null,
    num: null
  };
  proxy.resetForm("codeRef");
}

/** 提交按钮 */
function handleSave() {
  proxy.$refs["codeRef"].validate((valid) => {
    if (valid) {
      addStorageCode(form.value).then((res) => {
        if (res.code === 200) {
          proxy.$modal.msgSuccess("操作成功");
          emit("handleSaveSuccess");
        }
        visible.value = false;
      });
    }
  });
}

/** 暴露给父组件的方法 */
defineExpose({
  open,
});
</script>
