// const app = getApp()
const app = {};
import editwIcon from '../../static/images/edit.png';
import delwIcon from '../../static/images/delete.png';
import noDataIcon from '../../static/images/nodataicon.png';
import noData from '../../static/images/no-invoice.svg';
import { mapState } from 'vuex';
import uni from '@/utils/uniHooks';
import { invoicesDetail, invoicesDeletetitles } from '@/api/parking';
import { Dialog } from 'vant';
export default {
  data() {
    return {
      picUrl: this.$picUrl,
      editwIcon: editwIcon,
      delwIcon: delwIcon,
      noDataIcon: noData,
      globalData: app.globalData,
      current: -1,
      ids: '',
      list: [],
      preUrl: '',
      pagesize: 10,
      page: 0,
    };
  },
  created() {
    const options = this.$route.query;
    if (options.ids) this.ids = options.ids;
    // 埋点本地化
    this.preUrl = uni.getStorageSync('previousUrl');
    uni.setStorageSync(
      'previousUrl',
      '/pages/parkingFee/parkingReceipt/parkingChooseHeader.vue'
    );
  },
  mounted() {
    setTimeout(() => {
      uni.setNavigationBarTitle({
        title: '选择抬头',
      });
    }, 300);
    setTimeout(() => {
      window?.toWXSendMsg({
        type: 'uni_func',
        funcName: 'setNavigationBarColor',
        options: {
          frontColor: '#000000',
          backgroundColor: '#FBFCFF',
        },
      });
    }, 500)
    // console.log('全剧信息', this.globalData);
    this.getInvoiceTitle();
  },
  onBackPress() {
    console.log('===> 点击返回按钮');
    const pages = getCurrentPages();
    const prePage = pages[pages.length - 2];
    const options = {
      header: this.list[this.current] || {},
    };
    // TODO 开票界面选择了 A,两个列表中如果有当前是 A，删除 A之后，点击返回按钮 开票界面 A 还存在
    prePage.$vm.pageOptions = options;
  },
  computed: {
    ...mapState({
      custTypeId: (state) => state.custTypeId,
      member: (state) => state.member,
    }),
  },
  methods: {
    changeHeader(type, id, item) {
      if (type) {
        this.$router.push({
          path: 'parkingChangeHeader',
          query: {
            id: id,
            type: type,
            item: JSON.stringify(item)
          }
        });
      }
      else
        this.$router.push({
          path: 'parkingChangeHeader?type=' + type + '&id=' + id,
        });
      
        
    },
    // 删除抬头
    delHeader: async function (id) {
      console.log(1111, id)
      const self = this;
      
      Dialog.confirm({
        message: '确认要删除该发票抬头吗？',
        confirmButtonColor: '#644A79'
      })
        .then(() => {
            invoicesDeletetitles(id).then(res=>{
              console.log(11, res)
              self.getInvoiceTitle()
            }).catch(err=>{
              uni.showToast({
                title: '服务器开小差了呢，请您稍后再试',
                icon: 'none',
              });
            })
        })
        .catch(() => {
          // on cancel
        });
    },
    // 选择抬头
    chooseHeader(item, index) {
      this.current = index;
      if (!this.ids || !this.ids.length) return false;
      this.$router.replace({
        path:
          'parkingApplication',
        query: {
          header: JSON.stringify(item),
          ids: this.ids,
          invoiceDetailList: this.$store.state.tempParkingOrder,
          money: this.$store.state.tempParkingMoney,
          invoiceId: this.$route.query?.invoiceId
        }
      });
      setTimeout(() => {
        this.$router.go(-1)
      }, 300)
    },
    // 查询全部抬头列表信息
    getInvoiceTitle: async function() {
      const params = {
        page: this.page,
        size: this.pagesize
      };
      try {
        const res = await invoicesDetail(params)
        if (res.content) {
          console.log('res:::', res)
          this.list = res.content;
          let currentIndex = 0;
          this.list.map((item, index) => {
            if (item.defaultOrNot) currentIndex = index;
          });
          if (currentIndex == -1) this.current = 0;
        } else {
          Toast({
            message: '服务器开小差了呢，请您稍后再试',
          });
        }
      } catch {
        uni.showToast({
          title: '服务器开小差了呢，请您稍后再试',
          icon: 'none',
        });
      }
      // uni.request({
      //   url: self.$baseURL + 'api/1.0/invoiceTitle/page',
      //   method: 'GET',
      //   data: params,
      //   header: JSON.parse(uni.getStorageSync('handleUser')),
      //   success: (res) => {
      //     console.log('查询全部抬头列表信息', res.data);
      //     if (res.data.code === 0) {
      //       this.list = res.data.data;
      //       if (this.list.length) {
      //         this.list.map((item, index) => {
      //           if (item.setDefault === 1) this.current = index;
      //         });
      //         if (this.current == -1) this.current = 0;
      //       }
      //     } else {
      //       uni.showToast({
      //         title: res.data.msg,
      //         duration: 2000,
      //         icon: 'none',
      //       });
      //     }
      //   },
      //   fail: () => {
      //     uni.showToast({
      //       title: '服务器开小差了呢，请您稍后再试',
      //       icon: 'none',
      //     });
      //   },
      // });
    },
  },
};
