import { reactive, watch, ref } from 'vue';
import { cloneDeep } from 'lodash';

export default function useClearDialog(data, props, formRef, getDetail) {
  let formData = reactive(cloneDeep(data));
  let isEdit = ref(false);
  watch(
    () => props.curRow,
    (val) => {
      if (!val) {
        let cloneData = cloneDeep(data);
        for (let key in cloneData) {
          formData[key] = cloneData[key];
        }
        isEdit.value = false;
      } else {
        isEdit.value = true;
      }
    }
  );
  watch(
    () => props.visible,
    (visible) => {
      if (!visible) {
        // formRef?.value?.clearValidate();
        let cloneData = cloneDeep(data);
        for (let key in cloneData) {
          formData[key] = cloneData[key];
        }
      } else {
        !!props.curRow && getDetail && getDetail();
        setTimeout(() => {
          formRef?.value?.clearValidate();
        }, 0);
      }
    }
  );
  return { formData, isEdit };
}
