<template>
  <div class="purchase-transport">
    <div class="search">
      <div class="left" />
      <div class="right">
        <div>
          <el-input v-model="search.logisticsOrderNumber" placeholder="物流单号" />
        </div>
        <div>
          <el-input v-model="search.companyName" placeholder="物流公司" />
        </div>
        <div>
          <el-input v-model="search.purchaseOrderNo" placeholder="采购订单ID" />
        </div>
        <div>
          <el-input v-model="search.purchaseContractInfo" placeholder="采购合同编号 / 采购合同名称" />
        </div>
        <div v-if="enterpriseInfo.exportType == 'self_export' && enterpriseInfo.enterpriseType !== '1' && enterpriseInfo.enterpriseType !== '2'">
          <el-select v-model="search.vendorEntityNo" placeholder="请选择供应商" clearable>
            <el-option
              v-for="item in supplierList"
              :key="item.entityNo"
              :label="item.entityName"
              :value="item.entityNo"
            />
          </el-select>
        </div>
        <div>
          <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-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="PurchaseTransport"
    />
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import store from '@/store'
import { getImport, getSupplier, getPlat } from '@/api/myTrade/salesBill'
import { getTransportList } from '@/api/myTrade/purchaseBill'
export default {
  name: 'PurchaseTransport',
  data() {
    return {
      count: 0,
      search: {
        agencyCertNo: '',
        companyName: '',
        logisticsOrderNumber: '',
        purchaseContractInfo: '',
        purchaseOrderNo: '',
        vendorEntityNo: ''

      },
      page: 1,
      size: 10,
      addDate: [],
      tableData: [],
      columns: [
        {
          label: '序号',
          index: true
        },
        {
          label: '物流单号',
          prop: 'logisticsOrderNumber',
          showTooltip: true
        },
        {
          label: '物流公司',
          prop: 'companyName',
          showTooltip: true
        },
        {
          label: '采购订单ID',
          prop: 'purchaseOrderNo',
          showTooltip: true
        },
        {
          label: '采购合同名称',
          showTooltip: true,
          prop: 'purchaseContractName'
        },
        {
          label: '采购合同编号',
          showTooltip: true,
          prop: 'purchaseContractNo'
        },
        {
          label: '供应商名称',
          showTooltip: true,
          prop: 'vendorEntityName'
        },
        {
          label: '代理公司名称',
          showTooltip: true,
          prop: 'agencyName'
        },
        {
          label: '发货时间',
          showTooltip: true,
          prop: 'deliveryTime'
        },
        {
          label: '送达日期',
          showTooltip: true,
          prop: 'arriveTime'
        },
        {
          label: '运输证明',
          showTooltip: true,
          prop: 'transportCertificateFileId',
          render: (h, row, index) => {
            if (row.purchaseOrderTransProve) {
              return <cy-upload
                limit={1}
                fileData={ [row.purchaseOrderTransProve] }
                defaultDesc='请上传'
                disabled
              />
            } else {
              return ''
            }

            // return <span style={'cursor: pointer;color: #5AC4FC;font-size: 14px;'} onClick={ () => { this.downloadContract(row) } } > { row.purchaseOrderTransProve ? row.purchaseOrderTransProve.originalName : '' } </span>
          }
        }
        // {
        //   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
        // }
      ],
      loading: false,
      importerList: [],
      supplierList: [],
      tradePlatformList: []

    }
  },
  computed: {
    ...mapGetters(['constant', 'enterpriseInfo']),
    tablePower() {
      return ({ item, row }) => {
        const { rowPower } = item
        if (rowPower) {
          if (rowPower.includes(row.stage)) {
            return true
          }
        } else {
          return true
        }
      }
    }
  },
  created() {
    // this.$store.dispatch('getConstant', ['payment_way', 'sales_order_status'])

    this.getImporterList()

    this.getSupplierList()

    this.getPlatList()

    // 代理出口 || 内资生产企业 外商投资生产企业 --无供应商列
    if (this.enterpriseInfo.exportType === 'proxy_export' || (this.enterpriseInfo.exportType === 'self_export' && ['1', '2'].includes(this.enterpriseInfo.enterpriseType))) {
      this.columns = this.columns.slice().filter(el => el.label !== '供应商名称')
    }
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    fetchData(type) {
      this.loading = true
      if (type === 'search') {
        this.page = 1
        this.size = 10
      }
      const params = {
        ...this.search,
        timeStart: this.addDate.length ? this.addDate[0] : '',
        timeEnd: this.addDate.length ? this.addDate[1] : '',
        page: this.page || 1,
        rows: this.size || 10
      }
      getTransportList(params).then(({ data }) => {
        this.loading = false
        this.tableData = data.rows
        this.count = data.records
      })
    },
    handlerOperate(type, row) {
      if (type === 'play') {
        this.$router.push({
          name: 'RepaymentPlay',
          params: {
            id: row.id
          }
        })
      } else if (type === 'edit') {
        const { currentStage } = row
        if (currentStage === '合同签署') {
          this.$router.push({
            name: 'myFinancingContractSign',
            params: {
              id: row.id
            }
          })
        } else if (currentStage === null) {
          this.$router.push({
            name: 'ApplyFinancing',
            params: {
              id: row.id
            },
            query: {
              type: 'edit'
            }
          })
        }
      } else if (type === 'see') {
        this.$router.push({
          name: 'MyFinancingSee',
          params: {
            id: row.id
          },
          query: {
            type: 'see'
          }
        })
      }
    },

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

    downloadContract(val) {
      if (val.purchaseOrderTransProve && val.purchaseOrderTransProve.id) {
        const idNum = val.purchaseOrderTransProve.id
        window.open(`${process.env.VUE_APP_BASE_API}/file_storage_oss/preview/${idNum}?resource=1#toolbar=0&navpanes=0&scrollbar=0`)
      }
    }
  }
}
</script>

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

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