<template>
<div>
  <!-- <confirmOrder :istool="istool" :data="data" :total="total" ref="conf"></confirmOrder> -->
  <div class="total-panel normal-panel flex-align-center flex-between">
    <p class="text-grey" @click="deleSelectProd">删除选中商品</p>
    <div class="flex-align-center">
      <div class="prcieInfo">
        <p>共&nbsp;{{data.length}}&nbsp;件商品&emsp;总价 : <span class="price">¥&nbsp;{{tool.formatAmount(total,2)}}</span></p>
        <!-- <p class="text-grey">订单满{{freefreightamount}}元,免运费</p> -->
        <p v-if="freefreightamount">{{total > freefreightamount ?'当前订单免运费':`订单满${freefreightamount}元免运费,当前还差${+freefreightamount - +total}元`}}</p>
      </div>
      <div class="submitBtn" @click="confirmOrder">生成订单</div>
    </div>
  </div>
</div>
</template>

<script>
import confirmOrder from './confirmOrder.vue'
export default {
  props:['data','istool'],
  data () {
    return {
      total:0,
      freefreightamount:''
    }
  },
  components:{
    confirmOrder
  },
  methods:{
    totalPrice (val) {
      this.total = 0
      val.forEach(element => {
        this.total += element.oldprice * element.qty
      });

    },
    async deleSelectProd ()  {
      const res = await this.$api.requested({
        "id": 20220924095202,
        "content": {
          "sa_shoppingcartids": this.data.map(e=>{return e.sa_shoppingcartid})
        },
      })
      this.tool.showMessage(res,()=>{
        this.$emit('onSuccess')
      })
    },
    confirmOrder () {
      // console.log(this.data)
      if (this.data.length === 0) return this.$message({
        message:'请先选择要下单的产品!',
        type:'error'
      })
      this.onSubmit()
    },
    async onSubmit() {
      const res = await this.$api.requested({
        "id": 20221128183202,
        "content": {
          "istool":this.istool,
           "tradefield":this.istool ==0 ?this.data[0].tradefield_shoppingcart:'',
          "items": this.data.map(e=>{
            return {
              "sa_orderitemsid": 0, //写死0
              "itemid": e.itemid, //商品ID
              "sa_brandid": e.sa_brandid,
              "qty": e.qty //数量
            }
          })
        },
      })
      this.tool.showMessage(res,()=>{
        let usertype = JSON.parse(sessionStorage.getItem('active_account')).usertype
        switch (usertype) {
          case 1:
            this.$store.dispatch('changeDetailDrawer',true)
            this.$router.push({path:'/saler_orderDetails',query:{id:res.data.sa_orderid,rowindex:res.data.rowindex}})
            // this.$router.replace({path:'/ordersaler'})
            break;
          case 0:
            this.$store.dispatch('changeDetailDrawer',true)
            this.$router.push({path:'/orderdetail',query:{id:res.data.sa_orderid,rowindex:res.data.rowindex}})
            // this.$router.replace({path:'/ordermag'})
            break;
          default:
            // this.$router.replace({path:'/order'})
            this.$store.dispatch('changeDetailDrawer',true)
            this.$router.push({path:'/agent_orderDetails',query:{id:res.data.sa_orderid,rowindex:res.data.rowindex}})
            break;
        }
      })
    },
    async queryBasicInfo () {
      const res = await this.$api.requested({
        "id":20220920084001,"content":{
          sys_enterpriseid:this.data.sys_enterpriseid
        },
      })
      this.freefreightamount = res.data.freefreightamount
    }
  },
  mounted () {
    this.queryBasicInfo()
  }
}

</script>
<style>
</style>
<style scoped>
.total-panel{
  height: 60px;
  text-align: right;
  border-top:1px solid rgb(0 0 0 / 5%);
  padding-left:10px
}
.submitBtn{
  width: 150px;
  height: 60px;
  line-height: 60px;
  text-align: center;
  color:#fff;
  background: red;
  cursor: pointer;
}
.prcieInfo{
  font-size: 14px;
  padding: 5px 0;
  margin-right: 10px;
}
.text-grey{
  font-size: 12px;
  color: #999;
  cursor: pointer;
}
.deletBtn:hover{
  color: red;
}
.price{
  font-size: 1.25rem;
  color: red;
}
</style>