<template>
  <el-row v-loading="dataListLoading"
          :element-loading-text="isDisabled ? '订单创建中' : '拼命加载中'">
    <el-col :span="20" :offset="1">
      <h3>请您根据步骤提示下单</h3>
    </el-col>
    <el-col :span="20" :offset="2">
      <el-form :model="formData"
               :rules="rules"
               label-position="right"
               ref="ruleForm">
        <el-form-item>
          <h4>1. 当前已经选择的礼品<span>(特别说明：下单后，每个人将收到的包裹将包含以上商品)</span></h4>
          <el-table
            :data="tableData"
            border
            style="width: 100%">
            <el-table-column
              prop="info"
              label="商品信息">
              <template slot-scope="scope">
                <div class="TT-goods_info">
                  <div class="photo">
                    <img :src="scope.row.productImgUrl" alt="">
                  </div>
                  <p>{{ scope.row.productName }}</p>
                </div>
              </template>
            </el-table-column>
            <el-table-column
              prop="productPrice"
              label="商品价格"
              width="180">
            </el-table-column>
            <el-table-column
              prop="warehouseName"
              label="仓库"
              width="200">
            </el-table-column>
          </el-table>
        </el-form-item>
        <el-form-item prop="miandan">
          <h4>2. 选择收件人信息数据来源</h4>
          <el-radio-group v-model="formData.miandan">
            <el-radio :label="item.value"
                      v-for="(item, index) in miandan"
                      :key="index">{{ item.name }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <h4>3. 填写收件人地址</h4>
        </el-form-item>
        <el-form-item class="TT-el-form-item" label="收件人姓名：" prop="receiverName">
          <el-input placeholder="请输入收件人姓名" v-model="formData.receiverName"></el-input>
        </el-form-item>
        <el-form-item class="TT-el-form-item" label="收件人手机号：" prop="receiverPhone">
          <el-input type="tel" placeholder="请输入收件人手机号" v-model="formData.receiverPhone"></el-input>
        </el-form-item>
        <el-form-item class="TT-el-form-item" label="收件人地址：" prop="receiverAddress">
          <el-input type="textarea" placeholder="请输入收件人地址" :rows="10" v-model="formData.receiverAddress"></el-input>
        </el-form-item>
        <el-form-item>
          <h4>4. 提交</h4>
          <el-button type="primary"
                     :disabled="isDisabled"
                     class="TT-submit-button"
                     @click="submitForm('ruleForm')">确认提交
          </el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<script>
import Crypto from '@/utils/crypto'
import { isMobile } from '@/utils/validate'

export default {
  name: 'goods-create-order',
  props: {
    id: {
      type: String,
      default: ''
    }
  },
  data () {
    let checkTel = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入手机号码'))
      } else if (!isMobile(value)) {
        callback(new Error('手机号码不合法'))
      } else {
        callback()
      }
    }
    return {
      dataListLoading: false,
      isDisabled: false,
      tableData: [],
      miandanVal: [],
      miandan: [],
      formData: {
        miandan: '',
        receiverName: '',
        receiverPhone: '',
        receiverAddress: ''
      },
      rules: {
        miandan: [{ required: true, message: '请选择收件人信息数据来源', trigger: 'blur' }],
        receiverName: [{ required: true, message: '请输入收件人姓名', trigger: 'blur' }],
        receiverPhone: [{ validator: checkTel, required: true, message: '请输入收件人手机号', trigger: ['blur', 'change'] }],
        receiverAddress: [{ required: true, message: '请输入收件人地址', trigger: 'blur' }]
      }
    }
  },
  created () {
    this.funInit()
  },
  methods: {
    funInit () {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/gift/product/detail'),
        method: 'get',
        params: this.$http.adornParams({
          'id': Crypto.decrypt(this.id)
        })
      }).then(({ data }) => {
        if (data.status) {
          const { miandan } = data.data
          this.tableData = [
            {
              ...data.data
            }
          ]
          this.miandan = miandan
        } else {
          this.$message.error(data.msg)
        }
        this.dataListLoading = false
      })
    },
    submitForm (formName) {
      const { miandan, receiverName, receiverPhone, receiverAddress } = this.formData
      const postData = {
        'id': Crypto.decrypt(this.id),
        'miandan': miandan,
        'receiverData': [
          {
            'receiverName': receiverName,
            'receiverPhone': receiverPhone,
            'receiverAddress': receiverAddress
          }
        ]
      }
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.isDisabled) {
            return
          }
          this.dataListLoading = true
          this.isDisabled = true
          this.$http({
            url: this.$http.adornUrl('/gift/order/create'),
            method: 'post',
            data: this.$http.adornData(postData)
          }).then(({ data }) => {
            this.dataListLoading = false
            this.isDisabled = false
            if (data.status) {
              const { id } = data.data
              this.$message({
                type: 'success',
                message: data.msg,
                onClose: () => {
                  this.$router.push({ name: 'goodsOrderDetail', query: { id: Crypto.encrypt(id) } })
                }
              })
              return
            }
            this.$message.error(data.msg)
          }).catch(() => {
            this.dataListLoading = false
            this.isDisabled = false
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
h4 {
  display: flex;
  align-items: center;

  span {
    font-size: 12px;
    color: #666;
  }
}

.TT-goods_info {
  display: flex;

  .photo {
    width: 100px;
    height: 100px;
    overflow: hidden;

    img {
      display: block;
      width: 100%;
    }

    & + p {
      flex: 1;
      word-break: break-all;
      margin-left: 6px;
    }
  }
}

.TT-el-form-item {
  display: flex;
  align-items: flex-start;

  /deep/ .el-form-item__label {
    width: 9em;
  }

  /deep/ .el-form-item__content {
    width: 60%;
  }
}

.TT-submit-button {
  float: right;
  width: 200px;
  margin-top: 20px;
}
</style>
