<template>
  <el-dialog
    title="设置当前工作月"
    v-model="formOpen"
    width="500px"
    append-to-body
    draggable
    @close="cancel"
    :close-on-click-modal = "false"
  >
    <el-form ref="dictRef" :model="form" label-width="100">
      <el-form-item label="当前工作月：">
        <el-date-picker
          v-model="currentMonth"
          size="small"
          placeholder="起始月"
          :clearable="true"
          value-format="YYYY-MM-01"
          format="YYYY年MM月"
          type="month"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button
          type="primary"
          icon="Finished"
          size="small"
          @click="submitForm"
          >确 定</el-button
        >
        <el-button icon="close" size="small" @click="cancel">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup>
import { initCurrentMonth } from "@/api/business/entrust/currentWorkOrder";
import { ref } from "vue";

const { proxy } = getCurrentInstance();

const props = defineProps({
  getList: {
    type: Function,
    default: () => {},
  },
  setCurrentMonth: {
    type: Function,
    default: () => {},
  },
});

const { getList, setCurrentMonth } = toRefs(props);
const currentMonth = ref(proxy.moment().format("YYYY-MM-01"));

const formOpen = ref(false);
const form = ref({});

function submitForm() {
  if (currentMonth.value == null || currentMonth.value == "") {
    proxy.$modal.msgError("请选择月份!");
    return;
  }
  initCurrentMonth({
    year: currentMonth.value.substring(0, 4),
    month: currentMonth.value.substring(5, 7),
  }).then((response) => {
    proxy.$modal.msgSuccess("设置完成!");
    formOpen.value = false;
    setCurrentMonth.value(proxy.moment().format("YYYY-MM-01"));
    cancel();
    getList.value();
  });
}

function cancel() {
  formOpen.value = false;
  reset();
}

function open(options) {
  formOpen.value = true;
}

function reset() {
  currentMonth.value = proxy.moment().format("YYYY-MM-01");
}

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