<template>
  <div class="sales-invoice">
    <div class="search">
      <div class="left" />
      <div class="right">
        <div>
          <el-input v-model="search.invoiceNo" placeholder="发票号码" />
        </div>
        <div>
          <el-input v-model="search.orderNo" placeholder="销售订单ID" />
        </div>
        <div>
          <el-select v-model="search.importerId" placeholder="请选择进口商" clearable>
            <el-option
              v-for="item in importerList"
              :key="item.entityNo"
              :label="item.entityName"
              :value="item.id"
            />
          </el-select>
        </div>
        <div v-if="enterpriseInfo.exportType === 'proxy_export'">
          <el-select v-model="search.agencyCertNo" placeholder="请选择代理公司" clearable>
            <el-option
              v-for="item in tradePlatformList"
              :key="item.platformCode"
              :label="item.platformName"
              :value="item.platformCode"
            />
          </el-select>
        </div>
        <div>
          <el-input v-model="search.contractNo" placeholder="销售合同编号" />
        </div>
        <div>
          <el-date-picker
            v-model="addDate"
            type="daterange"
            range-separator="至"
            start-placeholder="开票日期范围"
            end-placeholder="开票日期范围"
          />
        </div>
        <div class="right-btn">
          <el-button type="primary" @click="fetchData('search')">查询</el-button>
        </div>
      </div>
    </div>
    <cy-comm-table
      ref="commTable"
      v-loading="loading"
      :columns="columns"
      :data="tableData"
      :count="count"
      parent-component="SalesInvoice"
    />
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import store from '@/store'
import { handleMoney } from '@/utils'
import { getImport, getPlat, getInvoiceList } from '@/api/myTrade/salesBill'
export default {
  name: 'SalesInvoice',
  data() {
    return {
      count: 0,
      search: {
        invoiceNo: '',
        orderNo: '',
        importerId: '',
        agencyCertNo: '',
        contractNo: ''
      },
      page: 1,
      size: 10,
      addDate: [],
      tableData: [],
      columns: [
        {
          label: '序号',
          index: true
        },
        {
          label: '发票号码',
          prop: 'invoiceNo',
          showTooltip: true
        },
        {
          label: '发票金额 (USD)',
          showTooltip: true,
          prop: 'amount',
          render: (h, row) => {
            return <div> <el-tooltip class='item' effect='dark' content={handleMoney(row.amount)} placement='top'>
              <div class='one-ellipsis'>
                {handleMoney(row.amount)}
              </div>
            </el-tooltip></div>
          }
        },
        {
          label: '销售订单ID',
          prop: 'orderNo',
          showTooltip: true
        },
        {
          label: '进口商名称',
          prop: 'importerName',
          showTooltip: true
        },
        {
          label: '销售合同编号',
          prop: 'contractNo',
          showTooltip: true
        },
        {
          label: '代理公司名称',
          showTooltip: true,
          prop: 'agencyEnterpriseName'
        },
        {
          label: '开票时间',
          showTooltip: true,
          prop: 'gmtCreate'
        },
        {
          label: '操作',
          render: (h, row) => {
            const btnList = [
              {
                msg: '下载发票',
                icon: 'iconfont icon-orange icon-Union',
                // rowPower: ['1', '2', '3', '4'],
                style: 'display: inline-block; transform: scale(0.65)',
                category: 'download'
              },
              {
                msg: '详情',
                icon: 'iconfont icon-a-Group957',
                power: '',
                // rowPower: ['', null],
                category: 'see'
              }
            ]
            const options = btnList.map(item => {
              return (
                this.tablePower({ item, row }) && <el-tooltip class='item' effect='dark' content={item.msg} placement='top'>
                  <span class='table-icon-box'>
                    <i class={item.icon} style={item.style} onClick={() => { this.handlerOperate(item.category, row) }}></i>
                  </span>
                </el-tooltip>
              )
            })
            return <div>{ options }</div>
          },
          width: 120
        }
      ],
      loading: false,
      importerList: [],
      tradePlatformList: []

    }
  },
  computed: {
    ...mapGetters(['constant', 'enterpriseInfo']),
    tablePower() {
      return ({ item, row }) => {
        const { rowPower } = item
        if (rowPower) {
          if (rowPower.includes(row.status)) {
            return true
          }
        } else {
          return true
        }
      }
    }
  },
  created() {
    this.getImporterList()

    this.getPlatList()
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    fetchData(type) {
      this.loading = true
      if (type === 'search') {
        this.page = 1
        this.size = 10
      }
      const params = {
        ...this.search,
        createdTimeStart: this.addDate && this.addDate.length ? this.addDate[0] : '',
        createdTimeEnd: this.addDate && this.addDate.length ? this.addDate[1] : '',
        page: this.page || 1,
        rows: this.size || 10
      }
      getInvoiceList(params).then(({ data }) => {
        this.loading = false
        this.tableData = data.list
        this.count = Number(data.total)
      })
    },
    handlerOperate(type, row) {
      if (type === 'download') {
        this.downloadContract(row)
      } else if (type === 'see') {
        this.$router.push({
          name: 'SalesInvoiceView',
          query: {
            type: 'view',
            id: row.invoiceNo
          }
        })
      }
    },
    downloadContract(val) {
      if (val.fileId) {
        const idNum = val.fileId
        window.open(`${process.env.VUE_APP_BASE_API}/file_storage_oss/download/${idNum}`)
      }
    },

    getImporterList() {
      const userId = store.getters.userInfo.id
      getImport({ userId: userId }).then(res => {
        this.importerList = res.data || []
      })
    },
    getPlatList() {
      getPlat().then(res => {
        this.tradePlatformList = res.data || []
      })
    }
  }
}
</script>

  <style lang="scss" scoped>
      .sales-invoice {

      }
  </style>
  <style lang="scss">
      .sales-invoice {
          .icon-a-Group957, .icon-a-Group1087 {
              font-size: 28px;
          }
      }
  </style>
