<template>
  <Dialog
    :dialog-style="dialogStyle"
    :is-dialog-exist="dialogShow"
    :title="dialogTitle"
    @close-dialog="closeDialog">
    <el-container>
      <el-form ref="formData" :model="formData" label-position="right" label-width="100px" :rules="rule">
      <el-main>
        <div class="container-main-titile">
          <h2>安全校验</h2>
          <span>为了保护您的账户安全，请通过安全验证</span>
          <br>
        </div>
        <el-form-item label="绑定手机号" prop="phone">
          <el-input v-model="formData.phone" class="input-phone-width" size="mini" :readonly="isPhoneReadOnly" disabled></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="smsCode">
          <div class="input-smscode-width">
          <el-input v-model="formData.smsCode" type="number" size="mini" placeholder="请输入验证码" style="width: 150px;"></el-input>
          <CountDownBtn class="input-sms-code-margin" @on-click="sendSms"></CountDownBtn>
          </div>
        </el-form-item>
      </el-main>
      <el-footer>
         <el-button type="primary" style=" margin-right: 27px;" @click="submitForm('formData')">确认</el-button>
          <el-button type="info" plain @click="closeDialog">取消</el-button>
      </el-footer>
      </el-form>
    </el-container>
  </Dialog>
</template>

<script>
import Dialog from '@/components/Dialog/index'
import CountDownBtn from '@/components/Common/CountDownBtn'
import request from '@/utils/request'

/**
 * 短信校验Dialog
 *
 * 参数：
 * dialogShow：dialog 展示的控制参数
 * getSmsUrl：获取短信的链接
 *
 * 方法：
 * closeDialog：dialog手动关闭的回调
 * smsCodeCallBack： 返回短信验证码的方法
 *
 */
export default {
  components: {
    Dialog,
    CountDownBtn
  },
  props: {
    // 下发验证码的URL
    getSmsUrl: {
      type: String,
      default: ''
    },
    // 因业务改变，不进行短信验证码校验
    // verifySmsUrl: {
    //   type: String,
    //   default: ''
    // },
    // verifyParams: {
    //   type: Object,
    //   default: () => {}
    // },
    dialogShow: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      isPhoneReadOnly: true,
      dialogTitle: '安全验证',
      dialogStyle: {
        '--dialog-width': '560px',
        '--dialog-height': '410px',
        '--title-height': '55px'
      },
      formData: {
        phone: '',
        smsCode: ''
      },
      rule: {
        smsCode: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    dialogShow(val) {
      if (val) {
        this.fetchPhone()
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // console.log('提交')
          this.successCallBack()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 关闭dialog
    closeDialog() {
      this.$emit('closeDialog')
    },
    // 成功的回调
    successCallBack() {
      this.$emit('smsCodeCallBack', this.formData.smsCode)
    },
    // 失败的回调
    // failCallBack() {
    //   this.$emit('failCallBack')
    // },
    // 下发验证码
    sendSms() {
      console.log('下发验证码')
      request({
        url: this.getSmsUrl,
        method: 'post'
      }).then(res => {
        if (parseInt(res.code) === 0) {
          this.$message.success('发送验证码成功')
        } else {
          this.$message.error('发送验证码失败')
        }
      })
    },
    // 校验验证码
    // verifySms() {
    //   this.verifyParams['code'] = this.smsCode
    //   request({
    //     url: this.verifySmsUrl,
    //     method: 'post',
    //     data: this.verifyParams
    //   }).then(res => {
    //     if (parseInt(res.code) === 0) {
    //       this.successCallBack()
    //     } else {
    //       this.failCallBack()
    //     }
    //   })
    // },
    // 获取手机号码
    fetchPhone() {
      request({
        url: '/admin/User/getBase',
        method: 'get'
      }).then(res => {
        if (parseInt(res.code) === 0) {
          this.formData.phone = res.data.smsPhone
          console.log('this phone = ' + this.formData.phone)
        } else {
          this.isPhoneReadOnly = false
          console.log('没有获取到手机号')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  .el-form {
    width: 100%;
    .el-main {
      width: 100%;
      padding-right: 100px;
      padding-left: 100px;
      .container-main-titile {
        width: 100%;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        align-items: center;
      }

      // height: 100%;
      .input-phone-width {
        /*width: 203px;*/
      }
      .input-sms-code-margin{
        margin-left: 15px;
      }
      .input-smscode-width {
        display: flex;
        justify-content: center;
        align-items: center;
      }
    }

    .el-footer {
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }

</style>
