// pages/invoiceList/components/info/index.js
import { getInvoiceInfo, updateInvoiceInfo } from "../../../../service/invoice"
import { getUserInfo } from "../../../../service/user";
import { alertInfo, alertSuccess, isValidPhoneNumber } from "../../../../utils/index";
import request from '../../../../utils/request'
const { OK } = request

Component({

  /**
   * 组件的属性列表
   */
  properties: {

  },
  lifetimes: {
    attached() {
      this.getDetail();
      this.getUser();
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    detail: {   },
    formData: {headerType: 0, name: wx.getStorageSync('tenement')?.name},
    editType: "detail"
  },

  /**
   * 组件的方法列表
   */
  methods: {
    async getDetail() {
      const { code, message, data } = await getInvoiceInfo()
      if (code !== OK) {
        alertInfo(message)
        return;
      }
      this.setData({ detail: {...data,name: data?.name || wx.getStorageSync('tenement')?.name, },editType: 'detail', })
    },
    async getUser() {
      const tenement = wx.getStorageSync('tenement')
      const { code, message, data } = await getUserInfo(tenement?.id)
      if (code !== OK) {
        alertInfo(message)
        return;
      }
      this.setData({ user: data });
    },
    changeEditType() {
      this.setData({ editType: 'edit', formData: {...this.data.detail, tenement: wx.getStorageSync('tenement')?.id} })
    },
    cancelEdit() {
      this.setData({ editType: 'detail', formData: {} })
    },
    onChangeType(e) {
      const { formData } = this.data;
      this.setData({ formData: {...formData, type: e.detail} })
    },
    onChangeHeaderType(e) {
      const { formData } = this.data;
      this.setData({ formData: {...formData, headerType: e.detail} })
    },
    onChangeText(e) {
      const { name } = e.currentTarget.dataset;
      const { formData } = this.data;
      this.setData({ formData: {...formData, [name]: e.detail} })
    },
    async submit() {
      const { formData } = this.data;
      const { tin, address, phone, bank, account, email, type, headerType, name } = formData;
      if (type !== 0 && type !== 1) {
        alertInfo("请选择发票类型");
        return;
      }
      if (headerType !== 0 && headerType !== 1) {
        alertInfo("请选择抬头类型");
        return;
      }
      if (headerType === 1) {
        if (!name) {
          alertInfo("请输入发票抬头")
          return
        }
      }
      if (!email || !/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(email)) {
        alertInfo("邮箱格式不正确")
        return;
      }
      if (headerType === 0) {
        if (!tin) {
          alertInfo("请输入发票税号")
          return;
        }
        if (phone && !isValidPhoneNumber(phone)) {
          alertInfo("手机号格式不正确")
          return
        }
        if (!bank) {
          alertInfo("请输入开户行")
          return;
        }
        if (!account) {
          alertInfo("请输入银行账号")
          return;
        }
      }
      const { code, message } = await updateInvoiceInfo(formData)
      if (code !== OK) {
        alertInfo(message)
        return;
      }
      alertSuccess("编辑成功")
      this.getDetail()
    }
  }
})