<template>
  <a-drawer
    :visible="vdata.visible"
    :title=" vdata.isAdd ? '新增法定节假日/工作日' : '修改法定节假日/工作日'"
    width="40%"
    :maskClosable="false"
    @close="onClose"
  >
    <a-form ref="infoFormModel" :model="vdata.saveObject" layout="vertical" :rules="vdata.rules">
      <a-row :gutter="16">
        <a-col :span="16">
          <a-form-item label="日期类型" name="dateType">
            <a-radio-group v-model:value="vdata.saveObject.dateType">
              <a-radio value="holiday">节假日</a-radio>
              <a-radio value="workday">工作日</a-radio>
            </a-radio-group>
          </a-form-item>
        </a-col>
        <a-col :span="16">
          <a-form-item label="日期" name="dateValueArr">
            <a-range-picker v-model:value="vdata.saveObject.dateValueArr" valueFormat="YYYY-MM-DD" />
            <!-- <a-date-picker v-model:value="vdata.saveObject.dateValue" valueFormat="YYYY-MM-DD" /> -->
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>

    <div class="drawer-btn-center">
      <a-button :style="{ marginRight: '8px' }" @click="onClose"><close-outlined />取消</a-button>
      <a-button type="primary" :loading="vdata.btnLoading" @click="onSubmit"><check-outlined />保存</a-button>
    </div>
  </a-drawer>
</template>

<script setup lang="ts">
import { API_URL_SYS_LEGAL_DAY, req } from '@/api/manage'
import {ref, reactive} from 'vue'

const props = defineProps({
  callbackFunc: { type: Function, default: () => () => ({}) }
})
  
const vdata : any =  reactive({
  isAdd: true, // 新增 or 修改
  visible: false, // 抽屉开关
  recordId: '', // id
  saveObject: {}, // 数据对象
  btnLoading:false,
  rules: {
    dateType: [{ required: true, message: '请选择日期类型', trigger: 'blur' }],
    dateValueArr: [{ required: true, message: '请选择日期', trigger: 'blur' }],
  }
})

// 表单组件
const infoFormModel = ref()

// 抽屉显示
const show = (recordId) => {
  vdata.isAdd = !recordId
  // 数据清空
  vdata.saveObject = {}
  
  if (infoFormModel.value !== undefined) {
    infoFormModel.value.resetFields()
  }

  if (!vdata.isAdd) { // 修改信息 延迟展示弹层
    vdata.recordId = recordId
    // 拉取详情
    req.getById(API_URL_SYS_LEGAL_DAY, recordId).then(res => {
      vdata.saveObject = res
    })

    vdata.visible = true
  } else {
    vdata.visible = true // 展示弹层信息
  }

}
// 表单提交
const onSubmit = () => {

  infoFormModel.value.validate().then(() => {

    vdata.btnLoading = true
    let reqObject = Object.assign({}, vdata.saveObject) // 请求数据
  
    req.addOrUpdate(vdata.isAdd ? null : vdata.recordId, API_URL_SYS_LEGAL_DAY, reqObject).then(res => {
        vdata.visible = false
        props.callbackFunc() // 刷新列表
    }).finally(() => {
        vdata.btnLoading = false
    })
  })
}

const onClose = () => {
  vdata.visible = false
}

defineExpose({ show })
</script>

