import { useMessage } from '@/hooks/web/useMessage';
import { useUserStore } from '@/store/modules/user';
import { defHttp } from '@/utils/http/axios';

// utils.ts
let c_formModalVisible: Boolean = false;
let c_isUpdate = true;
let c_isDetail = true;
let c_getTitle = '';
let c_primaryQuery = '';
let c_addQuery: any = [];
let c_formColumns: any = [];
let c_tabsColumns: any = [];
let c_flowCode = '';
let c_formData = {};
let c_subTableId = null;
let c_subTableColumns: any = [];
let c_subTableData: any = [];
let c_subTableList: any = [];
let c_scrollValue = '';
let c_cardLayout: any = [];
let c_createOrModifyList: any = [];
let c_buttonLayout: any = [];
let c_gridButtonLayout: any = [];
let c_record: any = {};
let c_cardValues: any = {};

const { createMessage } = useMessage();
const userStore = useUserStore();
const userInfo: any = userStore.getUserInfo;

// 通过field获取组件
function getComponentByFiled(data: string): any {
  return c_formColumns.find(item => item.field === data);
}
function getChildComponentByFiled(data: string): any {
  return c_subTableData.find(item => item.field === data);
}
function setChildComponentByFiled(childComponet: any, field: string): any {
  c_formColumns.forEach((item, index1) => {
    if (item.columns) {
      item.columns[0].children.forEach((i, index2) => {
        if (i.field == field) {
          c_formColumns[index1].columns[0].children[index2] = childComponet;
        }
      });
    }
  });
}
export const utils = {
  setCallModalData: function setCallModalData(data: any) {
    c_formModalVisible = data.formModalVisible.value;
    c_isUpdate = data.isUpdate.value;
    c_isDetail = data.isDetail.value;
    c_getTitle = data.getTitle.value;
    c_primaryQuery = data.primaryQuery.value;
    c_addQuery = data.addQuery.value;
    c_formColumns = data.formColumns.value;
    c_tabsColumns = data.tabsColumns.value;
    c_flowCode = data.flowCode.value;
    c_formData = data.formData;
    c_subTableId = data.subTableId.value;
    c_subTableColumns = data.subTableColumns.value;
    c_subTableData = data.subTableData.value;
    c_subTableList = data.subTableList.value;
    c_scrollValue = data.scrollValue.value;
    c_cardLayout = data.cardLayout.value;
    c_createOrModifyList = data.createOrModifyList.value;
    c_buttonLayout = data.buttonLayout.value;
    c_gridButtonLayout = data.gridButtonLayout.value;
    c_record = data.record;
    c_cardValues = data.cardValues.value;
  },
  formModalVisible: (): Boolean => {
    return c_formModalVisible;
  },
  isUpdate: (): Boolean => {
    return c_isUpdate;
  },
  isDetail: (): Boolean => {
    return c_isDetail;
  },
  getTitle: (): string => {
    return c_getTitle;
  },
  primaryQuery: (): string => {
    return c_primaryQuery;
  },
  addQuery: (): any => {
    return c_addQuery;
  },
  formColumns: (): any => {
    return c_formColumns;
  },
  tabsColumns: (): any => {
    return c_tabsColumns;
  },
  flowCode: (): string => {
    return c_flowCode;
  },
  formData: (): {} => {
    return c_formData;
  },
  subTableId: (): any => {
    return c_subTableId;
  },
  subTableColumns: (): any => {
    return c_subTableColumns;
  },
  subTableData: (): any => {
    return c_subTableData;
  },
  subTableList: (): any => {
    return c_subTableList;
  },
  scrollValue: (): string => {
    return c_scrollValue;
  },
  cardLayout: (): any => {
    return c_cardLayout;
  },
  createOrModifyList: (): any => {
    return c_createOrModifyList;
  },
  buttonLayout: (): any => {
    return c_buttonLayout;
  },
  gridButtonLayout: (): any => {
    return c_gridButtonLayout;
  },
  record: (): any => {
    return c_record;
  },
  cardValues: (): any => {
    return c_cardValues;
  },
  // 数据-----------------------------
  // 获取主表数据
  getValue: (field: any): any => {
    return c_record[field];
  },
  // 设置主表数据
  setValue: (field: any, value: any): any => {
    c_record[field] = value;
    return c_record;
  },
  // 获取子表数据
  getChildValue: (field: any): any => {
    if (field == '') {
      return c_subTableList;
    } else {
      const res: any = [];
      c_subTableList.forEach((v) => {
        res.push(v[field]);
      });
      return res;
    }
  },
  // 添加子表数据
  addChildValue: (data: any): any => {
    if (typeof data == 'object') {
      if (Array.isArray(data)) {
        data.forEach((d) => {
          c_subTableList.push(d);
        });
      } else {
        c_subTableList.push(data);
      }
    }
    return c_subTableList;
  },
  // 修改子表数据
  updateChildValue: (field: string, oldValue: string, newValue: string): any => {
    const c_subTableList_temp: any = [];
    c_subTableList.forEach((sub) => {
      if (sub[field] == oldValue) {
        sub[field] = newValue;
      }
      c_subTableList_temp.push(sub);
    });
    c_subTableList = c_subTableList_temp;
    return c_subTableList;
  },
  // 删除子表数据
  deleteChildValue: (field: string, value: string): any => {
    const c_subTableList_temp: any = [];
    c_subTableList.forEach((sub) => {
      if (sub[field] != value) {
        c_subTableList_temp.push(sub);
      }
    });
    c_subTableList = c_subTableList_temp;
    return c_subTableList;
  },
  // 组件-----------------------------
  // 获取组件
  getComponent: (field: any): any => {
    return getComponentByFiled(field) || getChildComponentByFiled(field);
  },
  // 隐藏与否
  setHide: (field: any, hideStatus: Boolean): any => {
    const componet: any = getComponentByFiled(field);
    if (componet) {
      componet.ifShow = hideStatus;
      c_formColumns.forEach((item, index) => {
        if (item.filed == field) {
          c_formColumns[index] = componet;
        }
      });
    } else {
      const childComponet: any = getChildComponentByFiled(field);
      if (childComponet) {
        childComponet.ifShow = hideStatus;
        setChildComponentByFiled(childComponet, field);
      }
    }
    return c_formColumns;
  },
  // 禁用与否
  setDisabled: (field: any, disabledStatus: Boolean): any => {
    const componet: any = getComponentByFiled(field);
    if (componet) {
      componet.dynamicDisabled = disabledStatus;
      c_formColumns.forEach((item, index) => {
        if (item.filed == field) {
          c_formColumns[index] = componet;
        }
      });
    } else {
      const childComponet: any = getChildComponentByFiled(field);
      if (childComponet) {
        childComponet.dynamicDisabled = disabledStatus;
        setChildComponentByFiled(childComponet, field);
      }
    }
    return c_formColumns;
  },
  // 必填与否
  setRequired: (field: any, requiredStatus: Boolean): any => {
    const componet: any = getComponentByFiled(field);
    if (componet) {
      componet.required = requiredStatus;
      c_formColumns.forEach((item, index) => {
        if (item.filed == field) {
          c_formColumns[index] = componet;
        }
      });
    } else {
      const childComponet: any = getChildComponentByFiled(field);
      if (childComponet) {
        childComponet.required = requiredStatus;
        setChildComponentByFiled(childComponet, field);
      }
    }
    return c_formColumns;
  },
  // 功能-----------------------------
  // 提示消息
  message: (message: string, type: string = 'info') => {
    if (type == 'success') {
      createMessage.success(message);
    } else if (type == 'error') {
      createMessage.error(message);
    } else if (type == 'warn') {
      createMessage.warn(message);
    } else {
      createMessage.info(message);
    }
  },
  // 获取登录者信息
  loginUser: () => {
    return { account: userInfo.account, name: userInfo.name };
  },
  // 回调方法-----------------------------
  // get请求
  httpGet: (api: string, params: any) => {
    return defHttp.get<any>({
      url: api,
      params,
    });
  },
  // post请求
  httpPost: (api: string, params: any) => {
    return defHttp.post<any>({
      url: api,
      params,
    });
  },
  // put请求
  httpPut: (api: string, params: any) => {
    return defHttp.put<any>({
      url: api,
      params,
    });
  },
};
