<template>
  <div class="salesOrder">
    <div class="search">
      <div class="left" />
      <div class="right">
        <div>
          <el-input v-model="search.orderNo" placeholder="销售订单ID" />
        </div>
        <div>
          <el-select v-model="search.paymentWay" placeholder="请选择付款方式" clearable>
            <el-option
              v-for="item in constant.payment_way"
              :key="item.dictCode"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </el-select>
        </div>
        <div>
          <el-select v-model="search.importerId" placeholder="请选择进口商" clearable>
            <el-option
              v-for="item in importerList"
              :key="item.entityNo"
              :label="item.entityName"
              :value="item.id"
            />
          </el-select>
        </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-input v-model="search.contractNo" placeholder="销售合同编号" />
        </div>
        <div>
          <el-select v-model="search.status" placeholder="订单状态" clearable @clear="search.status = null">
            <el-option
              v-for="item in constant.sales_order_status"
              :key="item.dictCode"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </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>
          <el-button type="primary" @click="addOrder">创建订单</el-button>
        </div>
      </div>
    </div>
    <cy-comm-table
      ref="commTable"
      v-loading="loading"
      :columns="columns"
      :data="tableData"
      :count="count"
      parent-component="SalesOrder"
    />
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import store from '@/store'
import { handleMoney, codeChangeName } from '@/utils'
import { getImport, getPlat, getOrderList } from '@/api/myTrade/salesBill'
export default {
  name: 'SalesOrder',
  data() {
    return {
      count: 0,
      search: {
        orderNo: '',
        paymentWay: '',
        importerId: '',
        agencyCertNo: '',
        contractNo: '',
        status: null
      },
      page: 1,
      size: 10,
      addDate: [],
      tableData: [],
      columns: [
        {
          label: '序号',
          index: true
        },
        {
          label: '销售订单ID',
          prop: 'orderNo',
          showTooltip: true
        },
        {
          label: '订单金额',
          showTooltip: true,
          prop: 'amount',
          render: (h, row) => {
            return <div> <el-tooltip class='item' effect='dark' content={'$ ' + handleMoney(row.amount)} placement='top'>
              <div class='one-ellipsis'>
                {'$ ' + handleMoney(row.amount)}
              </div>
            </el-tooltip></div>
          }
        },
        {
          label: '付款方式',
          showTooltip: true,
          prop: 'paymentWay',
          render: (h, row) => {
            return <div> <el-tooltip class='item' effect='dark' content={codeChangeName(this.constant.payment_way, row.paymentWay, 'dictValue', 'dictLabel')} placement='top'>
              <div class='one-ellipsis'>
                {codeChangeName(this.constant.payment_way, row.paymentWay, 'dictValue', 'dictLabel')}
              </div>
            </el-tooltip></div>
          }
        },
        {
          label: '结算账期',
          prop: 'settlementCycle',
          showTooltip: true,
          render: (h, row) => {
            return <div> <el-tooltip class='item' effect='dark' content={(row.settlementCycle ? row.settlementCycle + '天' : '')} placement='top'>
              <div class='one-ellipsis'>
                {(row.settlementCycle ? row.settlementCycle + '天' : '')}
              </div>
            </el-tooltip></div>
          }
        },
        {
          label: '进口商名称',
          prop: 'importerName',
          showTooltip: true
        },
        {
          label: '代理公司名称',
          showTooltip: true,
          prop: 'agencyEnterpriseName'
        },
        {
          label: '销售合同编号',
          showTooltip: true,
          prop: 'salesContractNo'
        },
        {
          label: '订单状态',
          showTooltip: true,
          prop: 'status',
          render: (h, row) => {
            return <div> <el-tooltip class='item' effect='dark' content={codeChangeName(this.constant.sales_order_status, String(row.status), 'dictValue', 'dictLabel')} placement='top'>
              <div class='one-ellipsis'>
                {codeChangeName(this.constant.sales_order_status, String(row.status), 'dictValue', 'dictLabel')}
              </div>
            </el-tooltip></div>
          }
        },
        {
          label: '创建时间',
          showTooltip: true,
          prop: 'gmtCreate'
        },
        {
          label: '操作',
          render: (h, row) => {
            const btnList = [
              {
                msg: '编辑',
                icon: 'iconfont icon-a-Group8151',
                rowPower: [0],
                power: 'projectNumber:delete',
                category: 'edit'
              },
              {
                msg: '发货',
                icon: 'iconfont icon-a-Group990',
                rowPower: [1],
                power: 'projectNumber:delete',
                category: 'trans'
              },
              {
                msg: '详情',
                icon: 'iconfont icon-a-Group957',
                power: '',
                // rowPower: [],
                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: [],
      tradePlatformList: []

    }
  },
  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()
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    fetchData(type) {
      this.loading = true
      if (type === 'search') {
        this.page = 1
        this.size = 10
      }
      const params = {
        ...this.search,
        createdTimeStart: this.addDate && this.addDate.length ? this.addDate[0] : '',
        createdTimeEnd: this.addDate && this.addDate.length ? this.addDate[1] : '',
        page: this.page || 1,
        rows: this.size || 10
      }
      getOrderList(params).then(({ data }) => {
        this.loading = false
        this.tableData = data.list
        this.count = Number(data.total)
      })
    },
    handlerOperate(type, row) {
      if (type === 'edit') {
        this.$router.push({
          name: 'SalesOrderEdit',
          query: {
            type: 'edit',
            id: row.id
          }
        })
      } else if (type === 'trans') {
        this.$router.push({
          name: 'SalesOrderTrans',
          query: {
            type: 'trans',
            id: row.id,
            orderNo: row.orderNo,
            fromPort: row.fromPort,
            destPort: row.destPort
          }
        })
      } else if (type === 'see') {
        this.$router.push({
          name: 'SalesOrderView',
          query: {
            type: 'view',
            id: row.id
          }
        })
      }
    },

    // 创建订单
    addOrder() {
      this.$router.push({ name: 'SalesOrderAdd', params: { id: 'child' }, query: { type: 'add' }})
    },

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

  }
}
</script>

<style lang="scss" scoped>
    .salesOrder {

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