// packageD/mycomponent/invoice-pop/invoice-pop.js
var app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    popShow: false,
    explainShow: false,
    invoice_type_index: 1, //发票类型
    invoice_type_arr: [],
    invoice_header_index: 1, //发票抬头
    invoice_header_arr: [{
      name: "个人",
      value: 1
    }, {
      name: "单位",
      value: 0
    }],
    invoice_content_index: 1, //发票内容
    invoice_content_arr: [],

    invoice_header_list: [], //发票抬头列表
    invoice_header_list_show: false,

    invoice_explain: "", //发票须知

    address: "", //地址，只有纸质发票跟专用发票才需要

    collect_name: "", //发票抬头 || 单位名称
    gmf_taxpayer: "", //纳税号
    gmf_bank: "", //开户银行
    gmf_bank_admin: "", //购买方银行账户
    gmf_address: "", //注册地址
    gmf_mobile: "", //注册电话
    col_mobile: "", //收票人手机号
    email: "", //电子邮箱

    inputFocusName: "", //判断当前那个输入框获得光标 用来显示输入框右边是否显示 清除 图标
  },

  /**
   * 组件的方法列表
   */
  methods: {
    initData(data) {
      let {address, sinvoice} = data;
      if (sinvoice.is_open == 1 && sinvoice.invoice_type.length >= 1) {
        let changeData = {};
        changeData.popShow = true;
        changeData.invoice_type_arr = sinvoice.invoice_type;
        changeData.invoice_type_index = sinvoice.invoice_type[0].value;
        changeData.invoice_content_arr = sinvoice.content;
        changeData.invoice_content_arr.push({
          name: "不开发票",
          value: 2
        });
        changeData.invoice_content_index = changeData.invoice_content_arr[0].value;
        changeData.invoice_explain = sinvoice.notice;
        changeData.invoice_header_list = sinvoice.rises;
        this.setDefalutRises(sinvoice.rises);
        if (!this.data.address) {
          changeData.address = {
            username: address.realname,
            mobile: address.mobile,
            col_address: address.address
          }
        }
        this.setData(
          changeData,
        );
      }
    },

    setDefalutRises (rises) {
      if (rises) {
        let bearerPer = rises.personal?rises.personal.find((item) => item.is_default == '1'):'';
        let bearerCom = rises.company?rises.company.find((item) => item.is_default == '1'):'';
        this.setData({
          defaultPersonal: bearerPer,
          defaultCompany: bearerCom
        })
      }
      // 默认设置个人发票抬头
      this.setData({
        collect_name: this.data.defaultPersonal.collect_name,
        defaultPersonal: null,
      })
    },
    send() {
      let {
        invoice_type_index,
        invoice_header_index,
        invoice_content_index,
        collect_name,
        gmf_taxpayer,
        gmf_bank,
        gmf_bank_admin,
        gmf_address,
        gmf_mobile,
        col_mobile,
        email,
        address
      } = this.data;

      // 不开发票
      if (this.data.invoice_content_index == "2") {
        this.closePop();
        return false;
      }

      // status和apply 后端要求写死
      let json = {
        status: 2,
        apply: 1,
        invoice_type: invoice_type_index,
        rise_type: invoice_header_index,
        content: invoice_content_index
      };

      if (app._isTextEmpty(collect_name)) {
        if (invoice_header_index == 0 || invoice_type_index == 2) {
          return app.tips("请输入单位名称");
        } else {
          return app.tips("请输入发票抬头");
        }
      }
      json.collect_name = collect_name;


      //单位 或者 专用发票
      if (invoice_type_index == 2 || invoice_header_index == 0) {
        if (app._isTextEmpty(gmf_taxpayer)) return app.tips("请输入纳税人识别号");

        //专用发票要求必须输入，其他选选填
        if (invoice_type_index == 2) {
          if (app._isTextEmpty(gmf_bank)) return app.tips("请输入单位开户银行");
          if (app._isTextEmpty(gmf_bank_admin)) return app.tips("请输入单位银行账号");
          if (app._isTextEmpty(gmf_address)) return app.tips("请输入注册地址");
          if (app._isTextEmpty(gmf_mobile)) return app.tips("请输入注册电话");
        }

        json.gmf_taxpayer = gmf_taxpayer;

        json.gmf_bank = gmf_bank;
        json.gmf_bank_admin = gmf_bank_admin;
        json.gmf_address = gmf_address;
        json.gmf_mobile = gmf_mobile;
      }

      //非 电子发票 需要选择地址
      if (invoice_type_index != 0) {
        if (!address) return app.tips("请选择地址");
        json.col_address = address.col_address;
        json.col_name = address.username;
        json.col_mobile = address.mobile;
      }

      //电子发票
      if (invoice_type_index == 0) {
        json.email = email;
        json.col_mobile = col_mobile;
      }
      this.triggerEvent("confirm", json);
    },

    openAddList() {
      this.triggerEvent('openAddList');
    },
    setAddress(item) {
      let address = item;
      address.col_address = `${address.province} ${address.city} ${address.district} ${address.street ? address.street : ''}`;
      this.setData({
        address: address
      });
    },
    setPopShow() {
      this.setData({
        popShow: !this.data.popShow
      })
    },
    closePop () {
      this.setData({
        invoice_type_index: 1,
        invoice_header_index: 1,
        invoice_content_index: 1,
        address: "",
        collect_name: "",
        gmf_taxpayer: "",
        gmf_bank: "",
        gmf_bank_admin: "",
        gmf_address: "",
        gmf_mobile: "",
        col_mobile: "",
        email: "",
        inputFocusName: "", 
      });
      this.setData({ popShow: false });
    },
    setExplainShow() {
      this.setData({
        explainShow: !this.data.explainShow
      })
    },
    setTabsIndex(evt) {
      let {value, name} = evt.currentTarget.dataset;
      if (name === "invoice_header_index" && this.data.invoice_header_index != value) {
        // 重置
        this.setData({
          collect_name: "",
        })
        // 设置公司 默认发票抬头
        if (value == "0" && this.data.defaultCompany) {
          let {collect_name, tax_num, bank, bank_admin, address, mobile} = this.data.defaultCompany;
          this.setData({
            collect_name: collect_name,
            gmf_taxpayer: tax_num,
            gmf_bank: bank,
            gmf_bank_admin: bank_admin,
            gmf_address: address,
            gmf_mobile: mobile,
            defaultCompany: null
          })
        }
      }
      this.setData({
        [name]: value
      })
    },
    setInvoiceHeaderListShow() {
      this.setData({
        invoice_header_list_show: !this.data.invoice_header_list_show
      })
    },
    setInvoiceHeader(evt) {
      let content = evt.currentTarget.dataset.content;
      let {collect_name, tax_num, bank, bank_admin, address, mobile} = content;
      let setJson = {};
      if (this.data.invoice_header_index == 1) {
        setJson.collect_name = collect_name;
      } else {
        setJson = {
          collect_name: collect_name,
          gmf_taxpayer: tax_num,
          gmf_bank: bank,
          gmf_bank_admin: bank_admin,
          gmf_address: address,
          gmf_mobile: mobile,
        }
      }
      this.setData({
        invoice_header_list_show: false,
        ...setJson
      })
    },
    clearInputText(evt) {
      let name = evt.currentTarget.dataset.name;
      this.setData({
        [name]: ""
      })
    },
    inputFocus(evt) {
      let name = evt.currentTarget.dataset.name;
      this.setData({
        inputFocusName: name
      })
    },
    inputBlur() {
      let name = this.data.inputFocusName;
      this.setData({
        [name]: this.data[name].replace(/\s+/g, ''),
        inputFocusName: ""
      })
    }
  }
})