<template>
  <div class="system-busWeeklySecurityReport-add">
    <!-- 添加或修改安全周期对话框 -->
    <el-dialog v-model="isShowDialog" width="500px" :close-on-click-modal="false" :destroy-on-close="true">
      <template #header>
        <div v-drag="['.system-busWeeklySecurityReport-add .el-dialog', '.system-busWeeklySecurityReport-add .el-dialog__header']">添加安全周期</div>
      </template>
      <el-form ref="formRef" :model="formData" :rules="rules" label-width="90px">
        <el-form-item label="周期范围" prop="timeRange">
          <el-date-picker
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
            v-model="formData.timeRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            size="large"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="onSubmit">确 定</el-button>
          <el-button @click="onCancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts">
import { reactive, toRefs, defineComponent, ref, unref, getCurrentInstance } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { addSafetyWeeklyReport } from '@/api/safety/safetyWeeklyReport';
// 获取用户 store
import { useUserStoreHook } from '@/store/modules/user';
const userStore = useUserStoreHook();
// 从 store 中获取项目列表和当前选中的项目
const currentProject = computed(() => userStore.selectedProject);
export default defineComponent({
  name: 'index',
  setup(props, { emit }) {
    const { proxy } = <any>getCurrentInstance();
    const formRef = ref<HTMLElement | null>(null);
    const menuRef = ref();
    const state = reactive({
      loading: false,
      isShowDialog: false,
      formData: {
        timeRange: [],
        projectId: currentProject.value?.goId
      },
      // 表单校验
      rules: {
        timeRange: [{ required: true, message: '时间范围不能为空', trigger: 'blur' }]
      }
    });
    // 打开弹窗
    const openDialog = () => {
      resetForm();
      state.isShowDialog = true;
    };
    // 关闭弹窗
    const closeDialog = () => {
      state.isShowDialog = false;
    };
    // 取消
    const onCancel = () => {
      closeDialog();
    };
    // 提交
    const onSubmit = () => {
      const formWrap = unref(formRef) as any;
      if (!formWrap) return;
      formWrap.validate((valid: boolean) => {
        if (valid) {
          state.loading = true;
          let timeRange = state.formData.timeRange.join(',');
          addSafetyWeeklyReport({ projectId: state.formData.projectId, timeRange })
            .then(() => {
              ElMessage.success('添加成功');
              closeDialog(); // 关闭弹窗
              emit('busWeeklySecurityReportList');
            })
            .finally(() => {
              state.loading = false;
            });
        }
      });
    };
    const resetForm = () => {
      state.formData = {
        timeRange: [],
        projectId: currentProject.value?.goId
      };
    };
    return {
      proxy,
      openDialog,
      closeDialog,
      onCancel,
      onSubmit,
      menuRef,
      formRef,
      ...toRefs(state)
    };
  }
});
</script>
<style scoped></style>
