import {index} from "@/api/system/customFormField";
import {show,save} from "@/api/system/customForm";
import {Message} from "element-ui";

const state = {
  formList: [], //更个表单配置信息
  copyFormListId: [], //备份原始的字段id数组,以做删除
  selectedForm: null, //当前编辑的表单字段配置
  selectedIndex: null,
};

const mutations = {
  SET_COPY_FORM_LIST_ID: (state, arr) => {
    state.copyFormListId = arr;
  },
  SET_SELECTED_INDEX: (state, index) => {
    state.selectedIndex = index;
  },
  CLEAR_SELECTED_INDEX: (state) => {
    state.selectedIndex = null;
  },
  SET_SELECTED: (state, value) => {
    state.selectedForm = value;
  },
  CLEAR_SELECTED: (state, value) => {
    state.selectedForm = null;
  },
  SET_FORM_LIST: (state, list) => {
    state.formList = list;
  },
  SPLICE_FORM_LIST: (state, info) => {
    const { index, length, config } = info;
    if (config) {
      state.formList.splice(index, length || 0, config || state.selectedForm);
    } else {
      state.formList.splice(index, length || 0);
    }
  },
};

const actions = {
  submit: ({ state, commit }, tableId) => {
    return new Promise((resolve, reject) => {
      show({ id: tableId }).then(res => {
        state.formList.forEach((item,index) => {
          item.sort = index + 1
        })
        res.fields = state.formList
        save(res).then(res1 => {
          Message({
            type: 'success',
            message: res1.msg
          })
          resolve(res1)
        }).catch(err1 => {
          reject(err1)
        })
      }).catch(err => {
        reject(err)
      })
    })
    // return new Promise((resolve, reject) => {
    //   state.formList.forEach((item,index) => {
    //     item.sort = index + 1
    //   })
    //   let formListId = state.formList.filter((i) => !!i.id).map((i) => i.id);
    //   let deleteIds = state.copyFormListId.filter(
    //     (i) => !formListId.includes(i)
    //   );
    //   let promiseAll = [
    //     ...state.formList.map((i) => save(i)),
    //     ...deleteIds.map((i) => destroy({ id: i })),
    //   ];
    //   Promise.all(promiseAll)
    //     .then((res) => {
    //       Message({
    //         type: "success",
    //         message: "保存成功",
    //       });
    //       resolve(res);
    //     })
    //     .catch((err) => {
    //       reject(err);
    //     });
    // });
  },
  getFormList: ({ state, commit }, custom_form_id) => {
    return new Promise((resolve, reject) => {
      index(
        {
          page: 1,
          page_size: 999,
          sort_name: "sort",
          sort_type: "asc",
          custom_form_id,
        },
        true
      )
        .then((res) => {
          resolve(res);
          if (res?.data instanceof Array) {
            commit("SET_FORM_LIST", res.data);
            commit(
              "SET_COPY_FORM_LIST_ID",
              res.data.map((i) => i.id)
            );
          } else {
            console.warn("res.data not Array");
          }
        })
        .catch((err) => {
          reject(err);
        });
    });
  },
};

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