<template>
  <div class="purchase-order">
    <div class="search">
      <div class="left" />
      <div class="right">
        <div style="width: 280px;">
          <el-input v-model="search.orderNo" placeholder="报关单号 / 销售订单ID / 提运单号 / 出口发票号码" />
        </div>
        <div>
          <el-select v-model="search.importerId" placeholder="请选择进口商" clearable>
            <el-option
              v-for="item in entryComList"
              :key="item.id"
              :label="item.entityName"
              :value="item.id"
            />
          </el-select>
        </div>
        <div>
          <el-select v-model="search.tradeTerms" placeholder="请选择贸易术语" clearable>
            <el-option
              v-for="item in constant.trade_terms"
              :key="item.dictCode"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </el-select>
        </div>
        <div>
          <el-select v-model="search.status" placeholder="请选择报关结关状态" clearable>
            <el-option
              v-for="item in constant.declaration_status"
              :key="item.dictCode"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </el-select>
        </div>
        <div style="max-width: 350px;">
          <el-date-picker
            v-model="applyDate"
            type="daterange"
            range-separator="至"
            start-placeholder="申报日期范围"
            end-placeholder="申报日期范围"
            value-format="yyyy-MM-dd"
          />
        </div>
        <div>
          <el-date-picker
            v-model="outDate"
            type="daterange"
            range-separator="至"
            start-placeholder="出口日期范围"
            end-placeholder="出口日期范围"
            value-format="yyyy-MM-dd"
          />
        </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>
    <cy-comm-table
      ref="commTable"
      v-loading="loading"
      :columns="columns"
      :data="tableData"
      :count="count"
      parent-component="DeclarationForm"
    />
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { declarationFormList, entryEntityList } from '@/api/myTrade/salesBill'
import { codeChangeName } from '@/utils'
import store from '@/store'
export default {
  name: 'DeclarationForm',
  data() {
    return {
      count: 0,
      search: {
        status: '',
        tradeTerms: '',
        importerId: '',
        orderNo: ''
      },
      page: 1,
      size: 10,
      applyDate: [],
      outDate: [],
      tableData: [],
      columns: [
        {
          label: '序号',
          index: true
        },
        {
          label: '报关单号',
          prop: 'bgdhgbh',
          showTooltip: true
        },
        {
          label: '销售订单ID',
          prop: 'orderNo',
          showTooltip: true
        },
        {
          label: '进口商名称',
          prop: 'importerName',
          showTooltip: true
        },
        {
          label: '贸易术语',
          prop: 'invoiceCode',
          showTooltip: true,
          render: (h, row) => {
            return <div> <el-tooltip class='item' effect='dark' content={codeChangeName(this.constant.trade_terms, row.tradeTerms, 'dictValue', 'dictLabel')} placement='top'>
              <div class='one-ellipsis'>
                {codeChangeName(this.constant.trade_terms, row.tradeTerms, 'dictValue', 'dictLabel')}
              </div>
            </el-tooltip></div>
          }
        },
        {
          label: '出口发票号码',
          prop: 'invoiceNo',
          showTooltip: true
        },
        {
          label: '提运单号',
          prop: 'ladingBillNo',
          showTooltip: true
        },
        {
          label: '报关结关状态',
          prop: 'invoiceCode',
          showTooltip: true,
          render: (h, row) => {
            return <div> <el-tooltip class='item' effect='dark' content={codeChangeName(this.constant.declaration_status, row.status, 'dictValue', 'dictLabel')} placement='top'>
              <div class='one-ellipsis'>
                {codeChangeName(this.constant.declaration_status, row.status, 'dictValue', 'dictLabel')}
              </div>
            </el-tooltip></div>
          }
        },
        {
          label: '申报日期',
          prop: 'hgckhwbgdsbrq',
          showTooltip: true
        },
        {
          label: '出口日期',
          prop: 'ckrq1',
          showTooltip: true
        },
        {
          label: '操作',
          render: (h, row) => {
            const btnList = [
              {
                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,
      entryComList: []
    }
  },
  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', ['trade_terms', 'declaration_status'])
    const userId = store.getters.userInfo.id
    entryEntityList({ userId: userId }).then(res => {
      this.entryComList = 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.applyDate = []
          this.outDate = []
        }
      }
      const params = {
        ...this.search,
        declareTimeStart: this.applyDate && this.applyDate.length ? this.applyDate[0] : '',
        declareTimeEnd: this.applyDate && this.applyDate.length ? this.applyDate[1] : '',
        exportTimeStart: this.outDate && this.outDate.length ? this.outDate[0] : '',
        exportTimeEnd: this.outDate && this.outDate.length ? this.outDate[1] : '',
        page: this.page || 1,
        rows: this.size || 10
      }
      declarationFormList(params).then(({ data }) => {
        this.loading = false
        this.tableData = data.list || []
        this.count = data.records
      })
    },
    handlerOperate(type, row) {
      if (type === 'see') {
        this.$router.push({
          name: 'DeclarationFormView',
          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>
