<template>
  <div>
    <el-dialog :title="exData.id ? '编辑': '新增'"
               :visible.sync="dialog"
               width="50%"
               :close-on-click-modal="false"
               top="50px">
      <el-form ref="form"
               :model="form"
               :rules="formRules"
               label-width="160px">
        <el-form-item prop="order_user_name"
                      label="预订人：">
          <el-col :span="16">
            <el-input v-model="form.order_user_name"
                      placeholder=""
                      disabled
                      clearable></el-input>
          </el-col>
        </el-form-item>
        <el-form-item prop="order_user_phone"
                      label="手机号：">
          <el-col :span="16">
            <el-input v-model="form.order_user_phone"
                      placeholder=""
                      disabled
                      clearable></el-input>
          </el-col>
        </el-form-item>
        <el-form-item prop="place_name"
                      label="预定座位类型：">
          <el-col :span="16">
            <el-input v-model="form.place_name"
                      placeholder=""
                      disabled
                      clearable></el-input>
          </el-col>
        </el-form-item>
        <el-form-item prop="order_sign_status"
                      :rules="formRules.required"
                      label="签到状态：">
          <el-radio v-model="form.order_sign_status" label="1">已签到</el-radio>
          <el-radio v-model="form.order_sign_status" label="0">未签到</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer text-center">
        <el-button @click="dialog = false">取 消</el-button>
        <el-button type="primary"
                   @click="handleSubmit">保 存
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  components: {},
  props: {
    value: {
      type: Boolean,
      default: true
    },
    exData: {
      type: Object,
      default: function () {
        return {}
      }
    }
  },
  data () {
    return {
      dialog: !!this.value,
      form: {
        order_user_name: '',
        order_user_phone: '',
        place_name: '',
        order_sign_status: '0' // 签到状态(0未签到1已签到)
      }
    }
  },
  methods: {
    handleSubmit () {
      const url = '/v1/prepare/order/sign'
      this.$refs.form.validate(async valid => {
        if (valid) {
          const data = await this.$fetch(url, {
            ...this.form
          })
          if (data.code === 200) {
            this.$message.success('提交成功')
            this.$emit('success')
            this.dialog = false
          }
        }
      })
    }
  },
  mounted () {
    if (this.exData.id) {
      this.$set(this.form, 'id', this.exData.id)
      for (const key in this.exData) {
        if (this.form.hasOwnProperty(key)) {
          let value = this.exData[key]
          if ((Array.isArray(value) && value.length >= 1) || (Object.prototype.toString.call(value) === '[object Object]') || (typeof value === 'string' && value) || typeof value === 'number') {
            if (key === 'order_sign_status') {
              value = value.toString()
            }
            this.$set(this.form, key, value)
          }
        }
      }
    }
  },
  watch: {
    dialog (val) {
      if (!val) this.$emit('input', val)
    }
  }
}
</script>

<style lang="scss" scoped>
.top-tip {
  margin-top: -20px;
  margin-bottom: 20px;
}
</style>
