<template>
  <div id="myInvoice">
    <my-invoice-list v-show="invoiceListShow" ref="invoiceList" @gotoDetail="gotoDetail"/>
    <!-- <high-search v-model="moreSearchShow" @onsearch="moreSearchConfirm" @onclean="moreSearchClean"/> -->
    <high-search :more-search-info="moreSearchInfo" v-model="moreSearchShow" @onsearch="moreSearchConfirm" @onclean="moreSearchClean"/>
  </div>
</template>

<script>
// import HighSearch from './components/HighSearch.vue'
import HighSearch from '@/views/myOrder/components/HighSearchNewDelete.vue'
import {getFormateDate} from '@/utils/common'
import myInvoiceList from './myInvoiceList.vue'
import invoiecDetail from './invoiecDetail.vue'
import {getEmployeeOrgQueryList, getConsignorSetList} from '@/api/common'
import { bridge } from "@/utils";
export default {
  name: 'MyInvoice',
  components: {
    HighSearch,
    myInvoiceList                          
  },
  data() {
    return {
      tabsTitle: [],
      moreSearchShow: false,
      activeTab: 'home',
      moreSearchInfo: {
        supplier: [], // 供应商
        consignor: [], // 货主
        goods: '', // 商品
        client: [], // 客户
        orderNo: '',
        time: {
          start: getFormateDate(new Date(), 'yyyy-MM-dd'),
          end: getFormateDate(new Date(), 'yyyy-MM-dd')
        }
      },
      invoiceList: [],
      invoiceListShow: false
    }
  },
  computed: {
    linkInfo () {
      return this.$store.state.linkInfo
    },
    routKey() {
      return this.$route.path
    },
    searchTime() {
      let time = this.time.start + '/' + this.time.end
      return time
    }
  },
  activated() {
    // 设置标题栏关闭返回按钮
    this.$store.commit('setNavBar', {
      show: true,
      leftArrow: true,
      rightIcon: true,
      title: '发票',
      leftText: '',
      leftClick: this.closeAppPage,
      rightClick: this.onMoreSearch
    })
    // 给window添加一个popstate事件，拦截返回键，执行this.onBrowserBack事件，addEventListener需要指向一个方法
    window.addEventListener('popstate', this.onBrowserBack, false)
  },
  mounted() {
    // 设置标题栏关闭返回按钮
    // this.$store.commit('setNavBar', {
    //   show: true,
    //   leftArrow: true,
    //   rightIcon: true,
    //   title: '发票',
    //   leftText: '',
    //   leftClick: this.closeAppPage,
    //   rightClick: this.onMoreSearch
    // })
    // // 给window添加一个popstate事件，拦截返回键，执行this.onBrowserBack事件，addEventListener需要指向一个方法
    // window.addEventListener('popstate', this.onBrowserBack, false)
  },
  destroyed() {
    // 当页面销毁必须要移除这个事件，vue不刷新页面，不移除会重复执行这个事件
    window.removeEventListener('popstate', this.onBrowserBack, false)
  },
  mounted() {
    // this.moreSearchShow = true
    this.getConsignorPostData()
    // this.tabsTitleData()
  },
  methods: {
    // 查询是否货主为单个
     getConsignorPostData() {
      let _this = this
      let data = {
        'attr': {
            'empid': this.linkInfo.userId
        },
        'filters': [],
        'logic': 'or',
        'sort': 'desc',
        'orderby': 'createon',
        'pageNum': 1,
        'pageSize': 10
      }
      getConsignorSetList(data).then(rt => {
        if (rt.code === 200) {
          // this.postDataList = rt.data.list
          if (rt.data.length !== 1) {
            this.moreSearchShow = true
          } else if (rt.data.length === 1) {
            this.moreSearchInfo.consignor.push(this.postDataListAction(rt.data[0]))
            this.getEmployeeOrgPostData(rt.data[0])
          } else if (rt.data.length === 0) {
            _this.$toast('暂无货主！')
          }
        } else {
          _this.$toast(rt.message)
        }
      }).catch(e => {
        _this.$toast('列表获取失败')
      })
    },
    postDataListAction(item) {
      const removeItem = item
      removeItem.cellId = item.id + item.erp_user
      return item
    },
    // 获取供应商
    getEmployeeOrgPostData(item) {
      let _this = this
      const companyList = []
      const removeItem = {
        'length': '100',
        'attr': {
            'empid': this.linkInfo.userId + ',1',
            'userid': item.erp_user,
            'sysname': item.sysname,
            'companyname': item.companyname
        },
        'dbName': '',
        'start': '1'
      }
      companyList.push(removeItem)
      let data = {
        list: companyList
      }
      getEmployeeOrgQueryList(data).then(rt => {
        if (rt.code === 200) {
          // this.postDataList = rt.data
          if (rt.data.length !== 0 && rt.data.length > 1) {
            this.moreSearchShow = true
          } else if (rt.data.length === 1) {
            this.moreSearchInfo.supplier = rt.data
            this.getClienPostData(item,rt.data[0])
          } else if (rt.data.length === 0) {
            this.moreSearchShow = true
          }
        } else {
          _this.$toast(rt.message)
        }
      }).catch(e => {
        _this.$toast('列表获取失败')
      })
    },
    // 判断客户是否为一个
    getClienPostData(consignorModel,clienItem) {
      let _this = this
      const companyList = []
      const removeItem = {
        'beforeP': consignorModel.db_source,
        'middleP': process.env.appRealDb,
        'afterP': [clienItem.org],
        'saler': consignorModel.erp_user,
        'companyname': consignorModel.companyname,
        'companynameId': consignorModel.id
      }
      companyList.push(removeItem)
      let data = {
        'customer': _this.keywords,
        'list': companyList,
        'start': _this.currentPage,
        'length': _this.pageSize
      }
      _this.loading = true
      getOrderCustomerSetList(data).then(rt => {
        if (rt.code === '1') {
          if (rt.data.length !== 0 && rt.data.length > 1) {
            this.moreSearchShow = true
          } else if (rt.data.length === 1) {
            this.moreSearchInfo.client = rt.data
            this.moreSearchConfirm(this.moreSearchInfo)
          } else if (rt.data.length === 0) {
            this.moreSearchShow = true
          }
        } else {
          _this.$toast(rt.msg)
        }
      }).catch(e => {
        _this.$toast('列表获取失败')
      })
    },
    // 跳转发票详情
    gotoDetail(model) {
      console.log(model)
      this.$router.push({
        path: '/invoiecDetail',
        query: model
       });
    },
    onBrowserBack() {
      if (this.moreSearchShow) {
        this.moreSearchShow = false
        return
      }
      this.$router.go(-1)
    },
    onSearch() {

    },
    onMoreSearch() {
      this.moreSearchShow = !this.moreSearchShow
      if (this.moreSearchShow) {
        window.history.pushState(null, null, document.URL)
      }
    },
    // 高级搜索查询
    moreSearchConfirm(data) {
      // console.log(data)
      this.invoiceListShow = true
      this.moreSearchInfo = JSON.parse(JSON.stringify(data))
      // console.log(this.$refs.invoiceList)
      this.$refs.invoiceList.httpAction(this.moreSearchInfo)
    },
    // 高级搜索取消
    moreSearchClean(data) {
      this.moreSearchInfo = JSON.parse(JSON.stringify(data))
      this.$refs.invoiceList.httpAction(this.moreSearchInfo)
    },
    closeAppPage() {
      console.log('关闭页面')
      bridge.closeWindow_c()
    }
  }
}
</script>

<style lang="scss" scoped>
  #myInvoice {
    height: 100%;
  }
</style>
