<template>
  <el-dialog v-model="dialogVisible" title="添加心愿单" width="900px" @closed="closeDialog" @open="openDialog">
    <el-form ref="formRef" :model="formData" :rules="rules">
      <el-row>


        <el-col :span="12">
          <el-form-item :label-width="labelWidth" label="心愿名称" prop="wish_list_name">
            <el-input v-model='formData.wish_list_name' type="text" placeholder='请输入心愿名称'></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label-width="labelWidth" label="作者" prop="wish_list_author">
            <el-input v-model='formData.wish_list_author' type="text" placeholder='请输入作者'></el-input>
          </el-form-item>
        </el-col>
        <el-col :span='12'>
          <el-form-item :label-width='labelWidth' label='完成状态' prop='wish_list_status'>
            <el-switch v-model='formData.wish_list_status' class='mt-2' inline-prompt :inactive-value=1 :active-value=2
              style='--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949' />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label-width="labelWidth" label="完成日期" prop="wish_list_complete_date">
            <el-date-picker :disabled="formData.wish_list_status === 1" v-model="formData.wish_list_complete_date" type="date" value-format="YYYY-MM-DD"
              placeholder="完成日期" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label-width="labelWidth" label="排序" prop="wish_list_sort">
            <el-input-number v-model='formData.wish_list_sort' controls-position='right' :min='1'></el-input-number>
          </el-form-item> 
        </el-col>

      </el-row>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="handleAddClick(formRef)">添加</el-button>
        <el-button @click="handleResetClick(formRef)">重置</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { reactive, ref, watch } from "vue";
import { addWishList, getDictionary } from "~/service/wish_list";
import { useLoginStore } from "~/store";

// --业务参数



// --业务方法

// 字典获取
const complete_status = ref([]);
async function get_complete_status() {
  await getDictionary({ dictionary_value: 'complete_status' }).then((res) => {
    complete_status.value = res
  })
}




// --基础参数
const store = useLoginStore();
const headers = {
  Accept: "application/json",
  ...store.headers,
};

const isBtnLod = ref(false);
const formRef = ref();
const labelWidth = 90;
const props = defineProps({
  modelValue: Boolean,
  done: Function,
});
const emits = defineEmits(["update:modelValue"]);
const dialogVisible = ref(props.modelValue);
const formData = reactive({});

const uoloadData = ref({
  dirName: "WishList"
})

watch(props, (v) => {
  dialogVisible.value = v.modelValue;
});

const rules = reactive({
  wish_list_name: [
    {
      required: true,
      message: '心愿名称不能为空'
    }
  ],
  wish_list_status: [
    {
      required: true,
      message: '完成状态不能为空'
    }
  ],

});


// --基础方法

// 打开弹窗时执行
const openDialog = () => {
  get_complete_status()

};

const closeDialog = () => {
  handleResetClick(formRef.value);
  dialogVisible.value = false;
  emits("update:modelValue", false);
};

const handleAddClick = async (formEl) => {
  if (!formEl) return;
  formEl.validate(async (valid) => {
    if (!valid) {
      return;
    }
    isBtnLod.value = true;


    const { code } = await addWishList(formData);
    if (code == 0) {
      closeDialog();
      props.done();
    }
    isBtnLod.value = flase;
  });
};

const handleResetClick = async (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
};
</script>

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