<template>
  <el-dialog :visible.sync="visible" width="55%">
    <el-table :data="rpData" style="width: 100%">
      <el-table-column
        :show-overflow-tooltip="true"
        align="center"
        label="名称"
        prop="name"
      >
      </el-table-column>
      <el-table-column align="center" label="客户支付" prop="payPrice"> </el-table-column>
      <el-table-column align="center" label="分销支付" prop="buyPrice"> </el-table-column>
      <el-table-column align="center">
        <template slot-scope="scope">
          <el-button size="small" @click="handleRecive(scope.$index, scope.row)"
            >下单</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <div class="button">
      <el-col :span="4">
        <el-input
          size="small"
          v-model="reciveOtherData.price"
          placeholder="采购总价/分"
          clearable
        ></el-input>
      </el-col>

      <el-select v-model="state" size="small" placeholder="线下人工采购渠道">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
      <el-button size="small" type="success" @click="handleReciveOther()">发货</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      options: [
        {
          value: "3",
          label: "线下"
        }
      ],
      state: "",
      multipleSelection: [],
      rpData: [],
      visible: false,
      dataForm: {
        id: 0
      },
      reciveOtherData: {
        price: null
      },
      rules: {
        price: [{ required: true, message: "价格不能为空", trigger: "blur" }],
        orderCode: [{ required: true, message: "订单号不能为空", trigger: "blur" }]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        if (this.dataForm.id) {
          console.info(123)
          this.$http({
            url: this.$http.adornUrl("/ord/order/rateslist"),
            method: "get",
            params: this.$http.adornParams({
              id: this.dataForm.id
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.rpData = data.list
            }
          })
        }
      })
    },
    // 日期拼接
    date(data) {
      return data.inDate + "~" + data.outDate
    },
    // 预定总价
    inAllPrice(data) {
      let allPrice = 0
      let rate = data.rates
      for (var value of rate) {
        allPrice += value.sellPrice * value.num
      }
      return allPrice + "(分)"
    },
    // 采购总价计算
    buyAllPrice(data) {
      let allPrice = 0
      let rate = data.rates
      for (var value of rate) {
        allPrice += value.buyPrice * value.num
      }
      return allPrice
    },
    // 利润
    getPrice(data) {
      let buyPrice = data.payPrice
      let sellPrice = 0
      let rate = data.rates
      for (var value of rate) {
        sellPrice += value.sellPrice * value.num
      }
      return buyPrice - sellPrice
    },
    // 下单
    handleRecive(index, row) {
      this.$confirm(`确定下单操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/ord/order/buy"),
            method: "get",
            params: this.$http.adornParams({
              id: this.dataForm.id,
              rpCode: row.rpCode
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000
              })
            } else {
              this.$message.error(data.msg)
            }
            this.visible = false
          })
        })
        .catch(() => {
          console.log("取消操作")
        })
    },
    // 自采下单
    handleReciveOther(index, row) {
      this.$confirm(`确定下单操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl("/ord/order/buyother"),
          method: "get",
          params: this.$http.adornParams({
            id: this.dataForm.id,
            otherOrderCode: this.reciveOtherData.orderCode,
            price: this.reciveOtherData.price
          })
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1000
            })
          } else {
            this.$message.error(data.msg)
          }
          this.reciveOtherData.price = ""
          this.visible = false
        })
      })
    }
  }
}
</script>
<style>
.button {
  margin-top: 25px;
}
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
