<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="分销商编码" prop="ourRpId">
      <el-input v-model="dataForm.ourRpId" placeholder="分销商编码"></el-input>
    </el-form-item>
    <el-form-item label="价格区间始" prop="changePriceStart">
      <el-input v-model="dataForm.changePriceStart" placeholder="价格区间始"></el-input>
    </el-form-item>
    <el-form-item label="价格区间止" prop="changePriceEnd">
      <el-input v-model="dataForm.changePriceEnd" placeholder="价格区间止"></el-input>
    </el-form-item>
    <el-form-item label="加价百分比" prop="chargePricePercent">
      <el-input v-model="dataForm.chargePricePercent" placeholder="加价百分比"></el-input>
    </el-form-item>
    <el-form-item label="加价额" prop="chargePriceAmount">
      <el-input v-model="dataForm.chargePriceAmount" placeholder="加价额"></el-input>
    </el-form-item>
    <el-form-item label="最高价" prop="highestPrice">
      <el-input v-model="dataForm.highestPrice" placeholder="最高价"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          ourRpId: '',
          changePriceStart: '',
          changePriceEnd: '',
          chargePricePercent: '',
          chargePriceAmount: '',
          highestPrice: ''
        },
        dataRule: {
          ourRpId: [
            { required: true, message: '分销商编码不能为空', trigger: 'blur' }
          ],
          changePriceStart: [
            { required: true, message: '价格区间始不能为空', trigger: 'blur' }
          ],
          changePriceEnd: [
            { required: true, message: '价格区间止不能为空', trigger: 'blur' }
          ],
          chargePricePercent: [
            { required: true, message: '加价百分比不能为空', trigger: 'blur' }
          ],
          chargePriceAmount: [
            { required: true, message: '加价额不能为空', trigger: 'blur' }
          ],
          highestPrice: [
            { required: true, message: '最高价不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/op/ourrateplanprice/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.ourRpId = data.ourRateplanPrice.ourRpId
                this.dataForm.changePriceStart = data.ourRateplanPrice.changePriceStart
                this.dataForm.changePriceEnd = data.ourRateplanPrice.changePriceEnd
                this.dataForm.chargePricePercent = data.ourRateplanPrice.chargePricePercent
                this.dataForm.chargePriceAmount = data.ourRateplanPrice.chargePriceAmount
                this.dataForm.highestPrice = data.ourRateplanPrice.highestPrice
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/op/ourrateplanprice/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'ourRpId': this.dataForm.ourRpId,
                'changePriceStart': this.dataForm.changePriceStart,
                'changePriceEnd': this.dataForm.changePriceEnd,
                'chargePricePercent': this.dataForm.chargePricePercent,
                'chargePriceAmount': this.dataForm.chargePriceAmount,
                'highestPrice': this.dataForm.highestPrice
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
