<template>
  <div>
    <el-dialog :title="exData.id ? '编辑': '新增'"
               :visible.sync="dialog"
               width="600px"
               :close-on-click-modal="false"
               top="50px">
      <el-form ref="form"
               :model="form"
               :rules="formRules"
               label-width="120px">
        <el-form-item prop="user_name"
                      :rules="formRules.required"
                      label="吧台姓名：">
          <el-input v-model="form.user_name"
                    placeholder="请输入吧台姓名"
                    clearable></el-input>
        </el-form-item>
        <el-form-item prop="phone"
                      :rules="formRules.mobile"
                      label="手机号：">
          <el-input style="width: 70%;"
                    v-model="form.phone"
                    disabled
                    placeholder="请输入手机号"
                    @input="form.phone = form.phone.replace(/[^\d]/g, '').slice(0, 11)"
                    clearable></el-input>
          <p style="display: inline-block;width: 30%;text-align: right">
            <el-button
              type="primary"
              @click="validatePhone('form')"
              :disabled="[0, 60].findIndex(item => item === numCount) < 0">
              获取验证码<span>{{ [0, 60].findIndex(item => item === numCount) > -1 ? '' : numCount + '秒' }}</span>
            </el-button>
          </p>
        </el-form-item>
        <el-form-item prop="code"
                      :rules="formRules.code"
                      label="短信验证码：">
          <el-input v-model="form.code"
                    placeholder="请输入短信验证码"
                    @input="form.code=form.code.replace(/[^\d]/g, '').slice(0, 6)"
                    clearable></el-input>
        </el-form-item>
        <el-form-item prop="user_sign_status"
                      :rules="formRules.required"
                      label="状态：">
          <el-radio v-model="form.user_sign_status"
                    label="0">关闭
          </el-radio>
          <el-radio v-model="form.user_sign_status"
                    label="1">启用
          </el-radio>
        </el-form-item>
        <el-form-item>
          <p>关闭后，登录“听邦App”不能核销订座</p>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer text-center">
        <el-button @click="dialog = false">取 消</el-button>
        <el-button type="primary" :disabled="booLock" @click="handleSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import getCode from '@/views/ums/mixin/getCode'

export default {
  mixins: [getCode],
  components: {},
  props: {
    value: {
      type: Boolean,
      default: true
    },
    exData: {
      type: Object,
      default: function () {
        return {}
      }
    }
  },
  data () {
    return {
      dialog: !!this.value,
      form: {
        user_name: '', // 用户姓名
        phone: '', // 手机号码
        code: '', // 验证码
        user_sign_status: '1', // 艺人签约状态(0未签约1已签约)
      },
      booLock: false
    }
  },
  methods: {
    handleSubmit () {
      const url = this.exData.id ? '/v1/user/member/song/modify' : ''
      this.$refs.form.validate(async valid => {
        if (valid) {
          this.booLock = true
          const data = await this.$fetch(url, {
            ...this.form
          })
          this.booLock = false
          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)
      this.$set(this.form, 'phone', this.exData.user_phone)
      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 === 'user_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>
