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

const uploadField = [
  {
    "field": "original_name",
    "name": "源名称",
    "search_input": "text",
    "edit_input": "text",
    "admin_id": null,
    "department_id": null,
    "sort": 1,
    "help": null,
    "validation": [],
    "select_item": [],
    "list_show": 1,
    "form_show": 1,
  },
  {
    "field": "name",
    "name": "名称",
    "search_input": "text",
    "edit_input": "text",
    "admin_id": null,
    "department_id": null,
    "sort": 2,
    "help": null,
    "validation": [],
    "select_item": [],
    "list_show": 1,
    "form_show": 1,
  },
  {
    "field": "extension",
    "name": "后缀名",
    "search_input": "text",
    "edit_input": "text",
    "admin_id": null,
    "department_id": null,
    "sort": 3,
    "help": null,
    "validation": [],
    "select_item": [],
    "list_show": 1,
    "form_show": 1,
  },
  {
    "field": "size",
    "name": "大小",
    "search_input": "text",
    "edit_input": "text",
    "admin_id": null,
    "department_id": null,
    "sort": 4,
    "help": null,
    "validation": [],
    "select_item": [],
    "list_show": 1,
    "form_show": 1,
  },
  {
    "field": "url",
    "name": "地址",
    "search_input": "text",
    "edit_input": "text",
    "admin_id": null,
    "department_id": null,
    "sort": 5,
    "help": null,
    "validation": [],
    "select_item": [],
    "list_show": 1,
    "form_show": 1,
  },
  {
    "field": "folder",
    "name": "类型",
    "search_input": "text",
    "edit_input": "text",
    "admin_id": null,
    "department_id": null,
    "sort": 6,
    "help": null,
    "validation": [],
    "select_item": [],
    "list_show": 1,
    "form_show": 1,
  }
]
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 = {
  submitUpload: ({ state, commit }, tableId) => {
    return new Promise((resolve, reject) => {
      show({ id: tableId }).then(res => {
        res.fields = uploadField
        save(res).then(res1 => {
          Message({
            type: 'success',
            message: res1.msg
          })
          resolve(res1)
        }).catch(err1 => {
          reject(err1)
        })
      }).catch(err => {
        reject(err)
      })
    })
  },

  submit: ({ state, commit }, tableId) => {
    return new Promise((resolve, reject) => {
      show({ id: tableId }).then(res => {
        state.formList.forEach((item,index) => {
          item.sort = index + 1
        })
        console.log("state.formList",state.formList)
        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);
        });
    });
  },
  deleteField: ({ state, commit, dispatch }) => {
    if (state.selectedForm?.id) {
      return new Promise((resolve, reject) => {
        destroy({ id: state.selectedForm.id }).then(res => {
          resolve(res)
          commit('CLEAR_SELECTED')
          commit('CLEAR_SELECTED_INDEX')
        }).catch(err => {
          reject(err)
        })
      })
    } else {
      return new Promise((resolve, reject) => {
        state.formList.splice(state.selectedIndex, 1);
        resolve(false)
      })
    }
  }
};

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