import ggwhglApi from '@/pages/index/api/ggwhglHtgl/index.js';

const MN = {
  SET_GGLIST: 'SET_GGLIST', // 公告列表数据
  SET_PAGINATION: 'SET_PAGINATION', // 公告列表分页数据
  SET_GGDETAIL: 'SET_GGDETAIL', // 公告详情数据
  SET_ISDCFBGGSQL: 'SET_ISDCFBGGSQL', // 是否显示导出脚本按钮
  SET_DCFBGGSQLDATA: 'SET_DCFBGGSQLDATA', // 导出脚本
};

const state = {
  ggList: [],
  pagination: {},
  ggDetail: {},
  isDcFbggSql: 'N',
  dcFbggSqlData: {
    MySQL: '',
    oracleSql: '',
  },
};

const mutations = {
  // 公告列表数据
  [MN.SET_GGLIST]: (state, ggList) => {
    console.log('ggList111',ggList);
    
    // eslint-disable-next-line no-param-reassign
    state.ggList = ggList;
  },
  // 公告列表分页数据
  [MN.SET_PAGINATION]: (state, pagination) => {
    // eslint-disable-next-line no-param-reassign
    state.pagination = pagination;
  },
  // 公告详情数据
  [MN.SET_GGDETAIL]: (state, ggDetail) => {
    // eslint-disable-next-line no-param-reassign
    state.ggDetail = ggDetail;
  },
  // 是否显示导出脚本按钮
  [MN.SET_ISDCFBGGSQL]: (state, isDcFbggSql) => {
    // eslint-disable-next-line no-param-reassign
    state.isDcFbggSql = isDcFbggSql;
  },
  // 导出脚本
  [MN.SET_DCFBGGSQLDATA]: (state, dcFbggSqlData) => {
    // eslint-disable-next-line no-param-reassign
    state.dcFbggSqlData = dcFbggSqlData;
  },
};
const actions = {
  // 公告列表数据
  // eslint-disable-next-line consistent-return
  async queryGgList({ commit, state }, payload) {
    const { params, isNew } = payload;
    // const { data } = await ggwhglApi.queryGgList(params);
     const { data } = await ggwhglApi.getDtPage(params);
     console.log('queryGgList',data.records);
     
    // if (data.returnCode !== '1') {
    //   return false;
    // }
    // const newData = data.records ? data.records : [];
    // let allData = [];
    // if (isNew) {
      // commit(MN.SET_GGLIST, newData);
      return data.records;
    // } else {
    //   allData = state.ggList.concat(newData);
    //   commit(MN.SET_GGLIST, allData);
    // }
    // console.log('ggList',ggList);
    
    // const pagination = {
    //   pageNum: data.pageNum,
    //   pageSize: data.pageSize,
    //   totalPage: data.totalPage,
    //   totalSize: data.totalSize,
    // };
    // commit(MN.SET_PAGINATION, pagination);
    // return isNew ? newData : [];
  },
  async queryGgDetail({ commit }, payload) {
    const { data } = await ggwhglApi.queryGgDetail(payload);
    if (data.returnCode === '1') {
      data.xxanpz &&
        data.xxanpz.sort((a, b) => {
          return b.type - a.type;
        });
      commit(MN.SET_GGDETAIL, data);
    } else {
      this.$message.warning(data.returnMsg);
    }
  },
  // eslint-disable-next-line no-unused-vars
  async saveGgxx({ commit }, payload) {
    const { params, successCb, errorCb } = payload;
    const { data } = await ggwhglApi.saveGgxx(params);
    if (data.returnCode === '1') {
      successCb();
    } else {
      errorCb();
    }
  },
  // eslint-disable-next-line no-unused-vars
  async deleteGgxx({ commit }, payload) {
    const { params, successCb, errorCb } = payload;
    const { data } = await ggwhglApi.deleteGgxx(params);
    if (data.returnCode === '1') {
      successCb();
    } else {
      errorCb();
    }
  },
  async isDcFbggSql({ commit }) {
    const res = await ggwhglApi.isDcFbggSql();
    if (res.code === 1) {
      commit(MN.SET_ISDCFBGGSQL, res.data);
    } else {
      this.$message.warning(res.msg);
    }
  },
  async dcFbggSql({ commit }, payload) {
    const res = await ggwhglApi.dcFbggSql(payload);
    if (res.code === 1) {
      const exportFormData = {
        MySQL: `${res.data.xxfbglFbggSql}\n${res.data.fbggSql}`,
        oracleSql: `${res.data.xxfbglOrcFbggSql}\n${res.data.orcFbggSql}`,
      };
      commit(MN.SET_DCFBGGSQLDATA, exportFormData);
    } else {
      this.$message.warning(res.msg);
    }
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
