"use strict";
const common_vendor = require("../../../common/vendor.js");
const api_request = require("../../../api/request.js");
if (!Array) {
  const _easycom_uni_popup2 = common_vendor.resolveComponent("uni-popup");
  _easycom_uni_popup2();
}
const _easycom_uni_popup = () => "../../../uni_modules/uni-popup/components/uni-popup/uni-popup.js";
if (!Math) {
  _easycom_uni_popup();
}
const _sfc_main = {
  __name: "Contact",
  setup(__props) {
    common_vendor.onMounted(() => {
      getContacts();
    });
    const newContact = common_vendor.ref({
      name: "",
      phone: "",
      isDefault: 0,
      id: 0
    });
    const newContacts = common_vendor.ref({
      name: "",
      phone: "",
      isDefault: 0,
      id: 0
    });
    const editContact = async (index) => {
      popup1.value.open("center");
      const contact = filteredContacts.value[index];
      newContact.value = {
        name: contact.name,
        id: contact.id,
        phone: contact.phone,
        isDefault: contact.isDefault
      };
    };
    const handleIsDefaultChanges = (e) => {
      let currentValue = newContact.value.isDefault;
      let updatedValue = currentValue === 0 ? 1 : 0;
      newContact.value.isDefault = updatedValue;
      console.log(updatedValue);
    };
    const getContacts = async () => {
      const res = await common_vendor.index.request({
        url: api_request.baseUrl + "/contacts/list",
        method: "POST",
        header: {
          cookie: common_vendor.wx$1.getStorageSync("cookie")
        }
      });
      if (res.data.code === 1) {
        const correctContacts = [];
        let defaultContact = null;
        res.data.data.forEach((contact) => {
          if (contact.isDefault === 1) {
            if (!defaultContact) {
              defaultContact = contact;
            } else {
              contact.isDefault = 0;
            }
          }
          correctContacts.push(contact);
        });
        filteredContacts.value = correctContacts;
        if (defaultContact) {
          const index = filteredContacts.value.findIndex(
            (c) => c.id === defaultContact.id
          );
          if (index !== -1) {
            filteredContacts.value[index].isDefault = 1;
          }
        }
      } else {
        common_vendor.index.showToast({
          icon: "error",
          title: "获取失败"
        });
      }
    };
    const searchText = common_vendor.ref("");
    const filteredContacts = common_vendor.ref([]);
    common_vendor.watch(searchText, () => {
      if (searchText.value === "") {
        filteredContacts.value = contacts.value;
      } else {
        filteredContacts.value = contacts.value.filter((contact) => {
          return contact.name.includes(searchText.value) || contact.phone.includes(
            searchText.value
          );
        });
      }
    });
    const addContact = async () => {
      popup.value.close();
      if (newContacts.value.isDefault) {
        for (let i = 0; i < filteredContacts.value.length; i++) {
          if (filteredContacts.value[i].isDefault === 1) {
            filteredContacts.value[i].isDefault = 0;
          }
        }
      }
      const res = await common_vendor.index.request({
        url: api_request.baseUrl + "/contacts/add",
        method: "POST",
        header: {
          cookie: common_vendor.wx$1.getStorageSync("cookie")
        },
        data: {
          name: newContacts.value.name,
          phone: newContacts.value.phone,
          isDefault: newContacts.value.isDefault
        }
      });
      if (res.data.code === 1) {
        console.log("添加成功");
        newContacts.value = {
          name: "",
          phone: "",
          isDefault: 0,
          id: 0
        };
      } else {
        common_vendor.index.showToast({
          icon: "error",
          title: "添加失败"
        });
      }
      getContacts();
    };
    const deleteContact = async (index) => {
      const res = await common_vendor.index.request({
        url: api_request.baseUrl + "/contacts/delete",
        method: "POST",
        header: {
          cookie: common_vendor.wx$1.getStorageSync("cookie")
        },
        data: {
          id: filteredContacts.value[index].id
        }
      });
      console.log(res, "1111111111111");
      if (res.data.code === 1) {
        console.log("删除成功");
      } else {
        common_vendor.index.showToast({
          icon: "error",
          title: "删除失败"
        });
      }
      getContacts();
    };
    common_vendor.ref({});
    common_vendor.ref(null);
    const saveModifiedContact = async () => {
      popup1.value.close();
      if (!newContact.value.name || !newContact.value.phone) {
        common_vendor.index.showToast({
          icon: "error",
          title: "不能为空"
        });
        return;
      }
      if (newContact.value.isDefault) {
        for (let i = 0; i < filteredContacts.value.length; i++) {
          if (filteredContacts.value[i].isDefault === 1) {
            filteredContacts.value[i].isDefault = 0;
          }
        }
      }
      const res = await common_vendor.index.request({
        url: api_request.baseUrl + "/contacts/update",
        method: "POST",
        header: {
          cookie: common_vendor.wx$1.getStorageSync("cookie")
        },
        data: {
          name: newContact.value.name,
          phone: newContact.value.phone,
          id: newContact.value.id,
          isDefault: newContact.value.isDefault
        }
      });
      if (res.data.code === 1) {
        console.log("更新成功");
        console.log(newContact.value.isDefault, "000000000000000000000000 ");
      } else {
        common_vendor.index.showToast({
          icon: "error",
          title: "更新失败"
        });
      }
      getContacts();
    };
    const popup = common_vendor.ref(null);
    const popup1 = common_vendor.ref(null);
    const open = () => {
      popup.value.open("center");
    };
    const close = () => {
      popup.value.close();
    };
    const close1 = () => {
      popup1.value.close();
    };
    return (_ctx, _cache) => {
      return {
        a: newContacts.value.name,
        b: common_vendor.o(($event) => newContacts.value.name = $event.detail.value),
        c: newContacts.value.phone,
        d: common_vendor.o(($event) => newContacts.value.phone = $event.detail.value),
        e: common_vendor.o(($event) => handleIsDefaultChanges()),
        f: newContacts.value.id,
        g: common_vendor.o(addContact),
        h: common_vendor.o(close),
        i: common_vendor.sr(popup, "0aa6b83b-0", {
          "k": "popup"
        }),
        j: common_vendor.p({
          ["mask-click"]: false,
          ["background-color"]: "white",
          borderRadius: "10px"
        }),
        k: newContact.value.name,
        l: common_vendor.o(($event) => newContact.value.name = $event.detail.value),
        m: newContact.value.phone,
        n: common_vendor.o(($event) => newContact.value.phone = $event.detail.value),
        o: common_vendor.o(($event) => handleIsDefaultChanges()),
        p: newContacts.value.id,
        q: newContact.value.isDefault === 1,
        r: common_vendor.o(saveModifiedContact),
        s: common_vendor.o(close1),
        t: common_vendor.sr(popup1, "0aa6b83b-1", {
          "k": "popup1"
        }),
        v: common_vendor.p({
          ["mask-click"]: false,
          ["background-color"]: "white",
          borderRadius: "10px"
        }),
        w: common_vendor.f(filteredContacts.value, (item, index, i0) => {
          return common_vendor.e({
            a: common_vendor.t(item.name),
            b: common_vendor.t(item.phone),
            c: item.isDefault === 1
          }, item.isDefault === 1 ? {} : {}, {
            d: common_vendor.o(($event) => editContact(index), index),
            e: common_vendor.o(($event) => deleteContact(index), index),
            f: index
          });
        }),
        x: common_vendor.o(open)
      };
    };
  }
};
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-0aa6b83b"], ["__file", "D:/微信小程序/课设整合/匠承非遗小程序端/jiangchengfeiyi-xiaochengxu/pages/mine/Contact/Contact.vue"]]);
wx.createPage(MiniProgramPage);
