<template>
  <div class="sales-transport">
    <div class="search">
      <div class="left" />
      <div class="right">
        <div>
          <el-input v-model="search.logisticsCompany" placeholder="物流公司" />
        </div>
        <div>
          <el-input v-model="search.orderNo" placeholder="销售订单ID" />
        </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-select v-model="search.destPort" placeholder="请选择目的港" clearable>
            <el-option
              v-for="item in destPortList"
              :key="item"
              :label="item"
              :value="item"
            />
          </el-select>
        </div>
        <div>
          <el-input v-model="search.ladingBillNo" 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="SalesTransport"
    />
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import store from '@/store'
import { getImport, getPlat, getDestPort, getTransportList } from '@/api/myTrade/salesBill'
export default {
  name: 'SalesTransport',
  data() {
    return {
      count: 0,
      search: {
        logisticsCompany: '',
        orderNo: '',
        agencyCertNo: '',
        destPort: '',
        ladingBillNo: ''

      },
      page: 1,
      size: 10,
      addDate: [],
      tableData: [],
      columns: [
        {
          label: '序号',
          index: true
        },
        {
          label: '物流公司',
          prop: 'logisticsCompany',
          showTooltip: true
        },
        {
          label: '销售订单ID',
          prop: 'orderNo',
          showTooltip: true
        },
        {
          label: '代理公司名称',
          showTooltip: true,
          prop: 'agencyEnterpriseName'
        },
        {
          label: '起运港',
          showTooltip: true,
          prop: 'fromPort'
        },
        {
          label: '目的港',
          showTooltip: true,
          prop: 'destPort'
        },
        {
          label: '提单号',
          showTooltip: true,
          prop: 'ladingBillNo'
        },
        {
          label: '发货时间',
          showTooltip: true,
          prop: 'deliverTime'
        },
        {
          label: '预计送达日期',
          showTooltip: true,
          prop: 'estimatedArrivalTime'
        },
        {
          label: '操作',
          render: (h, row) => {
            const btnList = [
              {
                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} onClick={() => { this.handlerOperate(item.category, row) }}></i>
                  </span>
                </el-tooltip>
              )
            })
            return <div>{ options }</div>
          },
          width: 120
        }
      ],
      disabled: false,
      loading: false,
      importerList: [],
      tradePlatformList: [],
      destPortList: []

    }
  },
  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.$store.dispatch('getConstant', ['payment_way', 'sales_order_status'])

    this.getImporterList()
    this.getPlatList()
    this.getDestPortList()
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    fetchData(type) {
      this.loading = true
      if (type === 'search') {
        this.page = 1
        this.size = 10
      }
      const params = {
        ...this.search,
        deliverTimeStart: this.addDate && this.addDate.length ? this.addDate[0] : '',
        deliverTimeEnd: this.addDate && this.addDate.length ? this.addDate[1] : '',
        page: this.page || 1,
        rows: this.size || 10
      }
      getTransportList(params).then(({ data }) => {
        this.loading = false
        this.tableData = data.list
        this.count = Number(data.total)
      })
    },
    handlerOperate(type, row) {
      if (type === 'see') {
        this.$router.push({
          name: 'SalesTransportView',
          query: {
            type: 'view',
            id: row.orderNo
          }
        })
      }
    },

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

    <style lang="scss" scoped>
        .ssales-transport {

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