<template>
  <div class="goods-order-cont">
    <!-- 标题 -->
    <div class="goods-order-header">
      <!-- 待发货 -->
      <div class="unDelivered" v-if="type == 'orderGoods'">
        <p class="order-status">待发货</p>
        <a-button v-if="$hasPermissions('B_order_sendGoods') && (orderState != 'WAIT_PAY') && (orderState != 'CANCEL')" id="goodsOrder-send" type="primary" size="small" @click="sendGoods">立即发货</a-button>
      </div>
      <!-- 已发货 -->
      <div class="delivered" v-if="type == 'express'">
        <p class="companyInfo">快递公司：<span>{{ orderData.expressName }}</span></p>
        <div class="orderNoInfo">
          <p class="orderNo">运单编号：<span>{{ orderData.expressNum }}</span></p>
          <a-button id="goodsOrder-copy" size="small" v-clipboard:copy="orderData.expressNum" v-clipboard:success="onCopy">复制</a-button>
        </div>
      </div>
    </div>
    <!-- 表格 -->
    <a-table
      v-if="dataSource.length>0"
      ref="table"
      size="default"
      :row-selection="rowSelection"
      :columns="columns"
      :rowKey="(record) => record.orderGoodsNo"
      :data-source="dataSource"
      :pagination="false"
      bordered>
    </a-table>
  </div>
</template>

<script>
export default {
  name: 'GoodsOrder',
  props: {
    orderData: {
      type: [Object, Array],
      default: () => {
        return {}
      }
    },
    type: {
      type: String,
      default: ''
    },
    isReset: {
      type: Boolean,
      default: false
    },
    orderState: {
      type: String,
      default: ''
    }
  },
  computed: {
    dataSource: function () {
      if (this.type == 'orderGoods') {
        return this.orderData
      } else if (this.type == 'express') {
        return this.orderData.orderGoodsList
      } else {
        return []
      }
    },
    rowSelection: function () {
      //  订单状态不可为"待支付"、"已取消" 且 "待发货" 且 有立即发货 权限的即可操作表格
      if (this.orderState != 'CANCEL' && this.orderState != 'WAIT_PAY' && this.type == 'orderGoods' && this.$hasPermissions('B_order_sendGoods')) {
        return { selectedRowKeys: this.selectedRowKeys, onChange: this.onSelectChange }
      } else {
        return null
      }
    }
  },
  data () {
    return {
      selectedRowKeys: [], //  已选项
      columns: [
        { title: '商品名称', dataIndex: 'goodsName', width: 200, align: 'center' },
        { title: '商品单价', dataIndex: 'payGold', width: 200, align: 'center' },
        { title: '购买数量', dataIndex: 'buyQty', width: 200, align: 'center' }
      ]
    }
  },
  methods: {
    //  选择商品
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    //  立即发货
    sendGoods () {
      if (this.selectedRowKeys.length < 1) {
        this.$message.warning('请勾选需要发货的商品')
        return
      }
      this.$emit('sendGoods', this.selectedRowKeys)
    },
    //  复制成功
    onCopy (e) {
      this.$message.success('复制成功')
    }
  },
  watch: {
    //  是否重置表格选中状态
    isReset (newValue, oldValue) {
      if (newValue) {
        this.selectedRowKeys = []
      }
    }
  }
}
</script>

<style lang="less" scoped>
  .goods-order-cont{
    p{
      margin: 0;
    }
    .goods-order-header{
      border-left: 5px solid #ff9900;
      padding-left: 10px;
      margin: 25px 0 20px;
      .unDelivered{
        .order-status{
          display: inline-block;
          margin-right: 65px;
        }
      }
      .delivered{
        .companyInfo{
          display: inline-block;
          margin-right: 65px;
        }
        .orderNoInfo{
          display: inline-block;
          .orderNo{
            display: inline-block;
            margin-right: 20px;
          }
        }
      }
    }
  }
</style>
