<template>
  <div class="addorder">
    <person-title>当前位置 :: 我的订单 >> <em>发布订单</em></person-title>
    <div class="createNewOrder">
      <el-form :model="orderForm"
               :rules="rules" ref="addorder"
               label-position='left'
               @submit.native.prevent
               :status-icon='true'
               label-width="7rem" class="demo-orderForm">
        <el-form-item label="收货人 : " prop="name">
          <el-input
            style="width: 11rem"
            v-model="orderForm.name"
            placeholder="请输入收货人">
          </el-input>
        </el-form-item>
        <el-form-item label="电话号码 : " prop="phone">
          <el-input
            style="width: 11rem"
            v-model="orderForm.phone"
            placeholder="请输入电话号码">
          </el-input>
        </el-form-item>
        <el-form-item label="取件码 : " prop="number">
          <el-input
            style="width: 11rem"
            v-model="orderForm.number"
            placeholder="请输入取货码">
          </el-input>
        </el-form-item>
        <el-form-item label="快递公司 : " prop="company">
          <el-select v-model="orderForm.company"
                     placeholder="请选择快递公司">
            <el-option label="请选择" value="请选择" disabled></el-option>
            <el-option
              v-for="item in companyList"
              :key='item'
              :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="物品大小 : " prop="type">
          <el-select v-model="orderForm.type" placeholder="请选择物品大小">
            <el-option label="请选择" value="请选择" disabled></el-option>
            <el-option
              v-for="item in priceList"
              :key='item.id'
              :label="item.desc" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="公寓 : " prop="flatsid">
          <el-select v-model="orderForm.flatsid"
                     placeholder="请选择公寓">
            <el-option label="请选择" value="请选择" disabled></el-option>
            <el-option
              v-for="item in flatsList"
              :key='item.id'
              :label="item.flatsName" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="宿舍号 : ">
          <el-input
            style="width: 11rem"
            v-model="orderForm.hostel"
            placeholder="请输入宿舍号">
          </el-input>
        </el-form-item>
        <el-form-item style='margin-left:0.5rem' label="备注 : " prop="remarks">
          <el-input type="textarea" :rows="3"
                    placeholder="请输入内容..." resize='none'
                    v-model="orderForm.decs">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary"
                     @click="submitForm()">发布
          </el-button>
          <el-button @click="resetForm()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>

import {addOrder, getAllFlats, getPriceList} from '../../../api/order'

export default {
  data () {
    return {
      flatsList: [],
      priceList: [],
      companyList: ['顺丰', '韵达', '圆通', '中通', '申通', '邮政', '天天', '安能', '优速', '京东', '其他'],
      orderForm: {
        number: '',
        company: '',
        name: '',
        phone: '',
        flatsid: '',
        hostel: '',
        type: '',
        decs: ''
      },
      rules: {
        name: [
          {required: true, message: '请输入收货人姓名', trigger: 'change'}
        ],
        number: [
          {required: true, message: '请输入取件号', trigger: 'change'}
        ],
        phone: [
          {required: true, message: '请输入收货人手机号', trigger: 'change'},
          {pattern: /^1[3-8][0-9]{9}$/, message: '手机号格式不正确', trigger: 'blur'}
        ],
        flatsid: [
          {required: true, message: '请选择公寓', trigger: 'change'}
        ],
        type: [
          {required: true, message: '请选择物品类型', trigger: 'change'}
        ],
        company: [
          {required: true, message: '请选择快递公司', trigger: 'change'}
        ]
      }
    }
  },
  created () {
    let loading = this.$loading({lock: true, text: '玩命加载中...'})
    this.getFlats()
    this.getPrice()
    loading.close()
  },
  mounted () {
    window.onkeydown = (e) => {
      let key = window.event.keyCode
      if (key == 13) {
        this.submitForm()
      }
    }
  },
  methods: {
    getFlats () {
      getAllFlats().then((res) => {
        var data = res.data
        if (data.success) {
          this.flatsList = data.data
        }
      })
    },
    getPrice () {
      getPriceList().then((res) => {
        var data = res.data
        if (data.success) {
          this.priceList = data.data
        }
      })
    },
    submitForm () {
      this.$refs.addorder.validate((valid) => {
        if (valid) {
          this.$confirm(`确认填写正确`,
            '发布订单', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'success'
            }).then(() => {
            let loading = this.$loading({lock: true, text: '正在添加订单...'})
            setTimeout(() => {
              addOrder(this.orderForm).then((res) => {
                console.log(res)
                var data = res.data
                loading.close()
                if (data.success) {
                  this.$message({
                    message: '添加成功!',
                    type: 'success'
                  })
                  this.orderForm.decs = ''
                  this.orderForm.hostel = ''
                  this.$refs.addorder.resetFields()
                } else {
                  this.$message({
                    message: data.msg,
                    type: 'error'
                  })
                }
              })
            }, 600)
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消发布'
            })
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm () {
      this.$refs.addorder.resetFields()
    }
  }
}
</script>

<style lang='scss'>
.createNewOrder {
  padding-left: 5rem;
  padding-top: 2rem;
  width: 40%;
  min-width: 20rem;
}

@media only screen and (max-width: 1650px) {
  .addorder .el-form--label-left .el-form-item__label {
    width: 10rem !important;
  }
}

@media only screen and (max-width: 768px) {
  .createNewOrder {
    width: 75%;
  }
}

.el-form-item__error {
  margin-left: 4.5rem;
}
</style>
