<template>
  <div class="app-container">
    <el-card class="box-card" style="margin: 0 auto;">
      <el-form ref="payFrom" :model="payRepacts" :rules="rules" label-width="120px">
        <h1 class="app-title">结算单</h1>
        <div v-for="(shopOrder,index) in payRepacts.shopOrders" :key="index" class="refund-item">
          <el-row :gutter="24" class="box-row">
            <el-col :span="5"><b>订单编号(ID)：</b> {{ shopOrder.orderNo }}({{ shopOrder.orderId }})</el-col>
            <el-col :span="5"><b>下单时间：</b> {{ shopOrder.orderTime }}</el-col>
            <el-col :span="4"><b>订单金额：</b> <span class="el-span-danger">¥{{ shopOrder.payTotalFee }}</span></el-col>
            <el-col :span="10">
              <b>所有子订单：</b>
              <span v-for="child in shopOrder.shopOrderNos" :key="child">
                【{{ child }}】
              </span>
            </el-col>
          </el-row>
          <el-row :gutter="24" class="box-row">
            <el-col :span="5"><b>机构名称：</b> {{ shopOrder.clubName }}</el-col>
            <el-col :span="5"><b>机构运费：</b>
              <span class="el-span-danger">{{ payRepacts.postageInfo }}</span>
            </el-col>
            <el-col :span="4">
              <b>收款状态：</b>
              <el-tag v-if="shopOrder.orderReceiptStatus===1" type="danger" size="small">待收款</el-tag>
              <el-tag v-if="shopOrder.orderReceiptStatus===2" type="warning" size="small">部分收款</el-tag>
              <el-tag v-if="shopOrder.orderReceiptStatus===3" type="success" size="small">已收款</el-tag>
            </el-col>
            <el-col :span="10">
              <b>收款金额：</b>
              <span class="el-span-danger">¥{{ shopOrder.receiptTotalFee | amountfilters }}</span>
            </el-col>
          </el-row>
          <el-row :gutter="24" class="box-row">
            <el-col :span="5"><b>子订单编号(ID)：</b> {{ shopOrder.shopOrderNo }}({{ shopOrder.shopOrderId }})</el-col>
            <el-col :span="5"><b>子订单佣金：</b>
              <span class="el-span-danger"> ¥{{ shopOrder.brokerage | amountfilters }}  </span>
            </el-col>
            <el-col :span="4"><b>商品费：</b> ¥{{ shopOrder.productAmount | amountfilters }}</el-col>
            <el-col :span="10">
              <b>付款状态：</b>
              <el-tag v-if="shopOrder.payStatus===1" type="danger" size="small">待付款</el-tag>
              <el-tag v-if="shopOrder.payStatus===2" type="warning" size="small">部分付款</el-tag>
              <el-tag v-if="shopOrder.payStatus===3" type="success" size="small">已付款</el-tag>
            </el-col>
          </el-row>
          <el-row :gutter="24" class="box-row">
            <el-col :span="5"><b>供应商名称：</b> {{ shopOrder.shopName }}</el-col>
            <el-col :span="18"><b>供应商运费：</b>{{ shopOrder.shopPostFee | amountfilters }}</el-col>
          </el-row>
          <el-row :gutter="24" class="box-row">
            <el-col :span="24">
              <b>付供应商：</b>
              <b style="margin-left: 10px;">应结算采美：</b>
              <span class="el-span-danger"> ¥{{ shopOrder.shouldSettleCm | amountfilters }}  </span>
              <b style="margin-left: 10px;">已结算采美：</b>
              <span class="el-span-danger"> ¥{{ shopOrder.settleCmAmount | amountfilters }}  </span>
              <b style="margin-left: 10px;">待结算采美：</b>
              <span class="el-span-danger"> ¥{{ shopOrder.waitSettleCm | amountfilters }}  </span>
              <b style="margin-left: 10px;">应结算供应商：</b>
              <span class="el-span-danger"> ¥{{ shopOrder.shouldSettleShop | amountfilters }}  </span>
              <b style="margin-left: 10px;">已结算供应商：</b>
              <span class="el-span-danger"> ¥{{ shopOrder.settleShopAmount | amountfilters }}  </span>
              <b style="margin-left: 10px;">待结算供应商：</b>
              <span class="el-span-danger"> ¥{{ shopOrder.waitSettleShop | amountfilters }}  </span>
            </el-col>
          </el-row>
          <el-row :gutter="24" class="box-row">
            <el-table :data="shopOrder.newOrderProducts" border fit highlight-current-row :header-cell-style="{background:'#eef1f6',color:'#606266'}">
              <el-table-column label="商品名" align="center" prop="name" />
              <el-table-column label="规格" align="center" prop="unit" />
              <el-table-column label="数量" align="center" prop="num" />
              <el-table-column label="退货" align="center" prop="returnedPurchaseProductNum">
                <template slot-scope="{row}">
                  <span>{{ row.returnedPurchaseProductNum ? row.returnedPurchaseProductNum : 0 }}</span>
                </template>
              </el-table-column>
              <el-table-column label="单价" align="center" prop="price">
                <template slot-scope="{row}">
                  {{ row.price | amountfilters }}
                </template>
              </el-table-column>
              <el-table-column label="总价" align="center" prop="totalFee">
                <template slot-scope="{row}">
                  {{ row.totalFee | amountfilters }}
                </template>
              </el-table-column>
            </el-table>
          </el-row>
        </div>
        <div class="refund-item">
          <el-row :gutter="24" class="box-row">
            <el-col :span="5"><b>结算供应商总金额：</b>
              <span class="el-span-danger"> ¥{{ totalShopTotalAmount | amountfilters }} </span>
            </el-col>
            <el-col :span="6"><b>结算采美总金额：</b>
              <span class="el-span-danger"> ¥{{ totalCmTotalAmount | amountfilters }}  </span>
            </el-col>
          </el-row>
          <el-row :gutter="24" class="box-row">
            <el-col :span="5"><b>申请人：</b> {{ payRepacts.applicantName }}  </el-col>
          </el-row>
        </div>
      </el-form>
    </el-card>
    <div class="filter-container" style="text-align: center;margin-top: 20px;">
      <el-button type="primary" @click="hanldeSubmit">确定结算</el-button>
      <el-button plain @click="backToList">返回</el-button>
    </div>
  </div>
</template>

<script>
import { getApplyPays, getPayDetails, subPaySettle } from '@/api/finance'
export default {
  name: 'PayapplyEdit',
  filters: {
    statusFilter(status) {
      const map = {
        0: '待审核',
        1: '审核通过',
        2: '审核不通过'
      }
      return map[status]
    },
    receiptStatusFilter(status) {
      const map = {
        1: '待收款',
        2: '部分收款',
        3: '已收款'
      }
      return map[status]
    },
    payStatusFilter(status) {
      const map = {
        1: '待付款',
        2: '部分付款',
        3: '已付款'
      }
      return map[status]
    }
  },
  data() {
    return {
      payRepacts: {},
      rules: {
        name: [{ required: true, message: '请填写付款单名称', trigger: 'blur' }],
        cmBankAccountName: [{ required: true, message: '付采美户名不能为空', trigger: 'blur' }],
        cmBankAccount: [{ required: true, message: '付采美账号不能为空', trigger: 'blur' }],
        cmBankName: [{ required: true, message: '付采美开户行不能为空', trigger: 'blur' }],
        bankAccountName: [{ required: true, message: '付供应商开户行不能为空', trigger: 'blur' }],
        bankAccount: [{ required: true, message: '付供应商开户行不能为空', trigger: 'blur' }],
        bankName: [{ required: true, message: '付供应商开户行不能为空', trigger: 'blur' }],
        cmType: [{ required: true, message: '请选择付采美账户类型', trigger: 'blur' }],
        type: [{ required: true, message: '请选择付供应商账户类型', trigger: 'blur' }]
      },
      applyType: ''
    }
  },
  computed: {
    shopOrderIds() {
      return JSON.parse(this.$route.query.shopOrderIds)
    },
    totalCmTotalAmount() {
      let shouldSettleCm = 0
      const shopOrders = this.payRepacts.shopOrders
      shopOrders.forEach(item => {
        shouldSettleCm += item.shouldSettleCm
      })
      return shouldSettleCm
    },
    totalShopTotalAmount() {
      let shouldSettleShop = 0
      const shopOrders = this.payRepacts.shopOrders
      shopOrders.forEach(item => {
        shouldSettleShop += item.shouldSettleShop
      })
      return shouldSettleShop
    }
  },
  created() {
    if (this.$route.query.type === 'edit') {
      this.applyType = this.$route.query.type
      this.getPayDetails(this.$route.query.payShopId)
    } else {
      this.getApplyPays()
    }
  },
  methods: {
    // 付款单详情
    async getApplyPays() {
      try {
        const res = await getApplyPays(this.shopOrderIds)
        this.payRepacts = res.data
        this.payRepacts.applicantName = this.$store.getters.name
      } catch (error) {
        console.log('error', error)
      }
    },
    // 结算单详情
    async getPayDetails(payShopId) {
      try {
        const res = await getPayDetails(payShopId)
        this.payRepacts = res.data
      } catch (error) {
        console.log('error', error)
      }
    },
    // 提交结算申请
    hanldeSubmit() {
      this.$confirm('确定要提交结算吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log('payRepacts', this.payRepacts)
        this.subPaySettle(this.payRepacts)
      })
    },
    async subPaySettle(params) {
      try {
        const res = await subPaySettle(params)
        this.$message.success(res.msg)
        setTimeout(() => {
          this.backToList()
        }, 1000)
      } catch (error) {
        console.log('error', error)
      }
    },
    backToList() {
      this.$store.dispatch('tagsView/delView', this.$route).then(() => {
        this.$nextTick(() => {
          this.$router.replace({
            path: '/finance/paysettle-list'
          })
        })
      })
    }
  }
}
</script>

<style scoped>
  .app-title{
    line-height: 36px;
    font-size: 26px;
    font-weight: bold;
    color: #409EFF;
    text-align: center;
    margin: 0;
  }
  .box-card{
    margin-top: 20px;
    font-size: 14px;
  }
  .box-row{
    padding: 10px 0;
  }
  .box-row .dropdown{
    margin-top: -10px;
  }
  .refund-item{
    padding: 10px 0;
  }
  .order-item{
    background:#f7f7f7;
    margin-bottom: 20px;
    padding: 10px 15px;
    border-radius: 5px;
  }
  .product-row{
    padding: 10px 0;
    background: #EBEEF5;
    border-top: 1px dashed #DCDFE6;
  }
  .op-item{
    padding: 5px 0;
  }
  .el-span-warning{
  color: #E6A23C;
}
.el-span-success{
  color: #67C23A;
}
.el-span-danger{
  color: #F56C6C;
}
</style>

