<template>
  <div class="export-tax">
    <div class="search">
      <div class="left" />
      <div class="right">
        <el-date-picker
          v-model="applyDate"
          type="daterange"
          range-separator="至"
          value-format="yyyy-MM-dd HH:mm:ss"
          start-placeholder="申报年月日期范围"
          end-placeholder="申报年月日期范围"
        />
        <div>
          <el-input v-model="search.declarationBatch" placeholder="申报批次" />
        </div>
        <div>
          <el-input v-model="search.customsDeclarationNo" placeholder="报关单号" />
        </div>
        <div>
          <el-input v-model="search.salesOrderNo" placeholder="销售订单ID" />
        </div>
        <div>
          <el-input v-model="search.proformaInvoiceNo" placeholder="出口发票号" />
        </div>
        <el-date-picker
          v-model="exportDate"
          type="daterange"
          range-separator="至"
          start-placeholder="出口日期范围"
          end-placeholder="出口日期范围"
          value-format="yyyy-MM-dd HH:mm:ss"
        />
        <div>
          <el-select v-model="search.status" placeholder="请选择退税状态" clearable>
            <el-option
              v-for="item in constant.rebate_process_status"
              :key="item.dictCode"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </el-select>
        </div>
        <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>
    <div style="text-align: right; margin-top: 10px;">
      <el-button v-if="$store.state.user.showApplyTaxRebateBtn" type="primary" @click="taxRebate(1)">申请出口退税</el-button>
      <el-button v-if="$store.state.user.showTaxRebateBtn" type="primary" @click="taxRebate(2)">出口退税</el-button>
    </div>
    <cy-comm-table
      ref="commTable"
      v-loading="loading"
      :columns="columns"
      :data="tableData"
      :count="count"
      parent-component="ExportTax"
    />

  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import {
  getList, applyTaxRebate, taxRebate
} from '@/api/myApplication/exportTax'
import { formatMoney, codeChangeName } from '@/utils'

export default {
  name: 'ExportTax',
  components: {
  },
  data() {
    return {
      count: 0,
      search: {
        declarationBatch: '',
        customsDeclarationNo: '',
        salesOrderNo: '',
        proformaInvoiceNo: '',
        status: ''
      },
      applyDate: [],
      exportDate: [],
      page: 1,
      size: 10,
      tableData: [],
      columns: [
        {
          label: '序号',
          index: true
        },
        {
          label: '申报年月',
          prop: 'declarationMonthYear',
          showTooltip: true
        },
        {
          label: '申报批次',
          showTooltip: true,
          prop: 'declarationBatch'
        },
        {
          label: '报关单号',
          showTooltip: true,
          prop: 'customsDeclarationNo'
        },
        {
          label: '销售订单ID',
          showTooltip: true,
          prop: 'salesOrderNo'
        },
        {
          label: '退税总额',
          showTooltip: true,
          render: (h, row) => {
            return <div>{ formatMoney(row.taxRefundAmount) }</div>
          }
        },
        {
          label: '出口发票号',
          showTooltip: true,
          prop: 'proformaInvoiceNo'
        },
        {
          label: '出口数量',
          showTooltip: true,
          prop: 'exportQuantity'
        },
        {
          label: '出口日期',
          showTooltip: true,
          prop: 'exportDate'
        },
        {
          label: '退税状态',
          prop: 'status',
          showTooltip: true,
          render: (h, row) => {
            return <div> <el-tooltip class='item' effect='dark' content={codeChangeName(this.constant.rebate_process_status, row.status, 'dictValue', 'dictLabel')} placement='top'>
              <div class='one-ellipsis'>
                {codeChangeName(this.constant.rebate_process_status, row.status, 'dictValue', 'dictLabel')}
              </div>
            </el-tooltip></div>
          }
        }

      ],
      loading: false
    }
  },
  computed: {
    ...mapGetters(['constant', '']),
    tablePower() {
      return ({ item, row }) => {
        const { rowPower } = item
        if (rowPower) {
          if (rowPower.includes(row.status)) {
            return true
          }
        } else {
          return true
        }
      }
    }
  },
  created() {
    this.$store.dispatch('getConstant', ['rebate_process_status'])
  },
  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.applyDate = []
          this.exportDate = []
        }
      }
      const params = {
        ...this.search,
        declarationMonthYearStart: this.applyDate && this.applyDate.length ? this.applyDate[0] : '',
        declarationMonthYearEnd: this.applyDate && this.applyDate.length ? this.applyDate[1] : '',
        exportDateStart: this.exportDate && this.exportDate.length ? this.exportDate[0] : '',
        exportDateEnd: this.exportDate && this.exportDate.length ? this.exportDate[1] : '',
        page: this.page || 1,
        rows: this.size || 10
      }
      getList(params).then(({ data }) => {
        this.loading = false
        this.tableData = data.rows
        this.count = Number(data.records)
      })
    },
    addContract() {
      this.$router.push({
        name: 'SalesContractAdd',
        query: {
          type: 'add'
        }
      })
    },
    taxRebate(type) {
      if (type === 1) {
        applyTaxRebate()
      } else {
        taxRebate()
      }
    }
  }
}
</script>

  <style lang="scss" scoped>
      .export-tax {

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