<template>
  <el-dialog
    :title="!dataForm.orderId ? '新增' : '修改'"
    :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="orderSn">
      <el-input v-model="dataForm.orderSn" placeholder="订单编号"></el-input>
    </el-form-item>
    <el-form-item label="下单人ID" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="下单人ID"></el-input>
    </el-form-item>
    <el-form-item label="收货人姓名" prop="shippingUser">
      <el-input v-model="dataForm.shippingUser" placeholder="收货人姓名"></el-input>
    </el-form-item>
    <el-form-item label="省" prop="province">
      <el-input v-model="dataForm.province" placeholder="省"></el-input>
    </el-form-item>
    <el-form-item label="市" prop="city">
      <el-input v-model="dataForm.city" placeholder="市"></el-input>
    </el-form-item>
    <el-form-item label="区" prop="district">
      <el-input v-model="dataForm.district" placeholder="区"></el-input>
    </el-form-item>
    <el-form-item label="地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="地址"></el-input>
    </el-form-item>
    <el-form-item label="支付方式">
      <el-input v-model="dataForm.paymentMethod" placeholder="支付方式"></el-input>
    </el-form-item>
    <el-form-item label="订单金额" prop="orderMoney">
      <el-input v-model="dataForm.orderMoney" placeholder="订单金额"></el-input>
    </el-form-item>
    <el-form-item label="优惠金额" prop="districtMoney">
      <el-input v-model="dataForm.districtMoney" placeholder="优惠金额"></el-input>
    </el-form-item>
    <el-form-item label="实收金额" prop="realMoney">
      <el-input v-model="dataForm.realMoney" placeholder="实收金额"></el-input>
    </el-form-item>
    <el-form-item label="运费" prop="shippingMoney">
      <el-input v-model="dataForm.shippingMoney" placeholder="运费"></el-input>
    </el-form-item>
    <el-form-item label="物流公司名称" prop="shippingCompName">
      <el-input v-model="dataForm.shippingCompName" placeholder="物流公司名称"></el-input>
    </el-form-item>
    <el-form-item label="物流单号" prop="shippingSn">
      <el-input v-model="dataForm.shippingSn" placeholder="物流单号"></el-input>
    </el-form-item>
    <el-form-item label="下单时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="下单时间"></el-input>
    </el-form-item>
    <el-form-item label="发货时间" prop="shippingTime">
      <el-input v-model="dataForm.shippingTime" placeholder="发货时间"></el-input>
    </el-form-item>
    <el-form-item label="订单状态" prop="orderStatus">
      <el-input v-model="dataForm.orderStatus" placeholder="订单状态"></el-input>
    </el-form-item>
    <el-form-item label="交易成功时间" prop="successTime">
      <el-input v-model="dataForm.successTime" 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: {
          orderId: 0,
          orderSn: '',
          userId: '',
          shippingUser: '',
          province: '',
          city: '',
          district: '',
          address: '',
          paymentMethod: '',
          orderMoney: '',
          districtMoney: '',
          realMoney: '',
          shippingMoney: '',
          shippingCompName: '',
          shippingSn: '',
          createTime: '',
          shippingTime: '',
          orderStatus: '',
          successTime: ''
        },
        dataRule: {
          orderSn: [
            { required: true, message: '订单编号 yyyymmddnnnnnnnn’不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '下单人ID不能为空', trigger: 'blur' }
          ],
          shippingUser: [
            { required: true, message: '收货人姓名不能为空', trigger: 'blur' }
          ],
          province: [
            { required: true, message: '省不能为空', trigger: 'blur' }
          ],
          city: [
            { required: true, message: '市不能为空', trigger: 'blur' }
          ],
          district: [
            { required: true, message: '区不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '地址不能为空', trigger: 'blur' }
          ],
          paymentMethod: [
            { required: true, message: '支付方式   1支付宝，2微信，3ios内购不能为空', trigger: 'blur' }
          ],
          orderMoney: [
            { required: true, message: '订单金额不能为空', trigger: 'blur' }
          ],
          districtMoney: [
            { required: true, message: '优惠金额不能为空', trigger: 'blur' }
          ],
          realMoney: [
            { required: true, message: '实收金额不能为空', trigger: 'blur' }
          ],
          shippingMoney: [
            { required: true, message: '运费不能为空', trigger: 'blur' }
          ],
          shippingCompName: [
            { required: true, message: '物流公司名称不能为空', trigger: 'blur' }
          ],
          shippingSn: [
            { required: true, message: '物流单号不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '下单时间不能为空', trigger: 'blur' }
          ],
          shippingTime: [
            { required: true, message: '发货时间不能为空', trigger: 'blur' }
          ],
          orderStatus: [
            { required: true, message: '订单状态不能为空', trigger: 'blur' }
          ],
          successTime: [
            { required: true, message: '交易成功时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {        
        this.dataForm.orderId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.orderId) {
            this.$http({
              url: this.$http.adornUrl(`/book/buyorder/info/${this.dataForm.orderId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.orderSn = data.buyOrder.orderSn
                this.dataForm.userId = data.buyOrder.userId
                this.dataForm.shippingUser = data.buyOrder.shippingUser
                this.dataForm.province = data.buyOrder.province
                this.dataForm.city = data.buyOrder.city
                this.dataForm.district = data.buyOrder.district
                this.dataForm.address = data.buyOrder.address
                this.dataForm.paymentMethod = data.buyOrder.paymentMethod
                this.dataForm.orderMoney = data.buyOrder.orderMoney
                this.dataForm.districtMoney = data.buyOrder.districtMoney
                this.dataForm.realMoney = data.buyOrder.realMoney
                this.dataForm.shippingMoney = data.buyOrder.shippingMoney
                this.dataForm.shippingCompName = data.buyOrder.shippingCompName
                this.dataForm.shippingSn = data.buyOrder.shippingSn
                this.dataForm.createTime = data.buyOrder.createTime
                this.dataForm.shippingTime = data.buyOrder.shippingTime
                this.dataForm.orderStatus = data.buyOrder.orderStatus
                this.dataForm.successTime = data.buyOrder.successTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/book/buyorder/${!this.dataForm.orderId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'orderId': this.dataForm.orderId || undefined,
                'orderSn': this.dataForm.orderSn,
                'userId': this.dataForm.userId,
                'shippingUser': this.dataForm.shippingUser,
                'province': this.dataForm.province,
                'city': this.dataForm.city,
                'district': this.dataForm.district,
                'address': this.dataForm.address,
                'paymentMethod': this.dataForm.paymentMethod,
                'orderMoney': this.dataForm.orderMoney,
                'districtMoney': this.dataForm.districtMoney,
                'realMoney': this.dataForm.realMoney,
                'shippingMoney': this.dataForm.shippingMoney,
                'shippingCompName': this.dataForm.shippingCompName,
                'shippingSn': this.dataForm.shippingSn,
                'createTime': this.dataForm.createTime,
                'shippingTime': this.dataForm.shippingTime,
                'orderStatus': this.dataForm.orderStatus,
                'successTime': this.dataForm.successTime
              })
            }).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>
