<template>
  <div class="purchase-order">
    <div class="search">
      <div class="left" />
      <div class="right">
        <div>
          <el-select v-model="search.invoiceType" placeholder="请选择发票类型" clearable>
            <el-option
              v-for="item in constant.invoice_type"
              :key="item.dictCode"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </el-select>
        </div>
        <div>
          <el-input v-model="search.invoiceInfo" placeholder="发票代码/发票号码" />
        </div>
        <div>
          <el-select v-model="search.entityNo" placeholder="请选择供应商" clearable>
            <el-option
              v-for="item in supplierList"
              :key="item.entityNo"
              :label="item.entityName"
              :value="item.entityNo"
            />
          </el-select>
        </div>
        <div>
          <el-input v-model="search.orderNo" placeholder="采购订单ID" />
        </div>
        <el-date-picker
          v-model="invoiceDate"
          type="daterange"
          range-separator="至"
          start-placeholder="开票日期范围"
          end-placeholder="开票日期范围"
          value-format="yyyy-MM-dd"
        />
        <div class="right-btn">
          <el-button type="primary" @click="fetchData('reset')">重置</el-button>
          <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="PurchaseInvoice"
    />
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { purchaseInvoiceList } from '@/api/myTrade/purchaseBill'
import { getSupplierList } from '@/api/myTrade/salesContract'
import { codeChangeName, handleMoney } from '@/utils'
import store from '@/store'
export default {
  name: 'PurchaseInvoice',
  data() {
    return {
      count: 0,
      search: {
        invoiceType: '',
        invoiceInfo: '',
        entityNo: '',
        orderNo: ''
      },
      page: 1,
      size: 10,
      invoiceDate: [],
      tableData: [],
      columns: [
        {
          label: '序号',
          index: true
        },
        {
          label: '发票类型',
          prop: 'invoiceType',
          showTooltip: true,
          render: (h, row) => {
            return <div> <el-tooltip class='item' effect='dark' content={codeChangeName(this.constant.invoice_type, row.invoiceType, 'dictValue', 'dictLabel')} placement='top'>
              <div class='one-ellipsis'>
                {codeChangeName(this.constant.invoice_type, row.invoiceType, 'dictValue', 'dictLabel')}
              </div>
            </el-tooltip></div>
          }
        },
        {
          label: '发票代码',
          prop: 'invoiceCode',
          showTooltip: true
        },
        {
          label: '发票号码',
          prop: 'invoiceNumber',
          showTooltip: true
        },
        {
          label: '供应商名称',
          prop: 'vendorEntityName',
          showTooltip: true
        },
        {
          label: '采购订单ID',
          prop: 'orderNo',
          showTooltip: true
        },
        {
          label: '发票金额',
          showTooltip: true,
          prop: 'amount',
          render: (h, row) => {
            return <div> <el-tooltip class='item' effect='dark' content={row.currency + ' ' + handleMoney(row.amount)} placement='top'>
              <div class='one-ellipsis'>
                {row.currency + ' ' + handleMoney(row.amount)}
              </div>
            </el-tooltip></div>
          }
        },
        {
          label: '发票税额',
          showTooltip: true,
          prop: 'taxAmount',
          render: (h, row) => {
            return <div> <el-tooltip class='item' effect='dark' content={row.currency + ' ' + handleMoney(row.taxAmount)} placement='top'>
              <div class='one-ellipsis'>
                {row.currency + ' ' + handleMoney(row.taxAmount)}
              </div>
            </el-tooltip></div>
          }
        },
        {
          label: '商品数量',
          showTooltip: true,
          prop: 'number'
        },
        {
          label: '开票日期',
          showTooltip: true,
          prop: 'invoicingDate'
        },
        {
          label: '操作',
          render: (h, row) => {
            const btnList = [
              {
                msg: '预览',
                icon: 'iconfont icon-a-Group8151',
                rowPower: 'purchaseOrderInvoice',
                category: 'edit'
              },
              {
                msg: '详情',
                icon: 'iconfont icon-a-Group957',
                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} onClick={() => { this.handlerOperate(item.category, row) }}></i>
                  </span>
                </el-tooltip>
              )
            })
            return <div>{ options }</div>
          },
          width: 120
        }
      ],
      loading: false,
      supplierList: []
    }
  },
  computed: {
    ...mapGetters(['constant']),
    tablePower() {
      return ({ item, row }) => {
        const { rowPower } = item
        if (rowPower) {
          if (row[rowPower] !== null) {
            return true
          }
          return false
        } else {
          return true
        }
      }
    }
  },
  created() {
    this.$store.dispatch('getConstant', ['invoice_type'])

    const userId = store.getters.userInfo.id
    getSupplierList({ userId: userId }).then(res => {
      this.supplierList = res.data || []
    })
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    fetchData(type) {
      this.loading = true
      if (type) {
        this.page = 1
        this.size = 10
        if (type === 'reset') {
          for (const key in this.search) {
            this.search[key] = ''
          }
          this.invoiceDate = []
        }
      }
      const params = {
        ...this.search,
        timeStart: this.invoiceDate && this.invoiceDate.length ? this.invoiceDate[0] : '',
        timeEnd: this.invoiceDate && this.invoiceDate.length ? this.invoiceDate[1] : '',
        page: this.page || 1,
        rows: this.size || 10
      }
      purchaseInvoiceList(params).then(({ data }) => {
        this.loading = false
        this.tableData = data.rows
        this.count = data.records
      })
    },
    handlerOperate(type, row) {
      if (type === 'edit') {
        window.open(`${process.env.VUE_APP_BASE_API}/file_storage_oss/preview/${row.id}#toolbar=0&navpanes=0&scrollbar=0`)
      } else if (type === 'see') {
        this.$router.push({
          name: 'PurchaseInvoiceView',
          query: {
            type: 'view',
            id: row.id
          }
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
    .purchase-order {

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