<template>
  <Dialog :is-dialog-exist="showDialog" :title="dialogTitle" :dialog-style="dialogStyle" @close-dialog="handleClose">
    <el-container>
      <el-main>
        <el-form ref="formData" :model="formData" :rules="rules" label-width="auto">
          <el-form-item label="状态" prop="audit">
            <el-select v-model="formData.audit" style="width: 100%;">
              <el-option v-for="(item, index) in auditOptions" :key="index" :label="item.name" :value="item.type"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="备注" prop="memo">
            <el-input v-model="formData.memo" type="textarea" :rows="3" resize="none"></el-input>
          </el-form-item>
        </el-form>
      </el-main>
      <el-footer>
        <el-button type="primary" @click="submitForm">确定</el-button>
        <el-button @click="handleClose">取消</el-button>
      </el-footer>
    </el-container>
  </Dialog>
</template>

<script>
import Dialog from '@/components/Dialog/index'
import { auditData } from '@/api/master-info-manage/real-name-authentication'

/**
 * 站长等级的编辑dialog
 */
export default {
  components: { Dialog },
  props: {
    // 弹窗控制参数
    showDialog: {
      type: Boolean,
      default: false
    },
    // 弹窗标题
    dialogTitle: {
      type: String,
      default: ''
    },
    // 选中的数据
    selectItem: {
      type: Object,
      default: () => {}
    },
    auditOptions: {
      type: Array,
      default: () => {}
    }
  },
  data() {
    return {
      // dialog样式
      dialogStyle: {
        '--dialog-width': '550px',
        '--dialog-height': '300px',
        '--title-height': '51px'
      },
      // 提交参数
      formData: {
        uid: '',
        audit: '',
        memo: ''
      },
      // 校验规则
      rules: {
        audit: [
          { required: true, message: '请选择', trigger: 'change' }
        ]
        // memo: [
        //   { required: true, message: '请输入', trigger: 'blur' }
        // ]
      }
    }
  },
  watch: {
    showDialog(val) {
      if (val) {
        this.initDataView()
      }
    }
  },
  methods: {
    // 关闭弹窗
    handleClose() {
      this.$emit('handle-close')
    },
    // 提交弹窗
    handleSuccess() {
      this.$emit('handle-success')
    },
    // 初始化弹窗参数
    initDataView() {
      console.log('初始化弹窗参数')
      this.formData.audit = this.selectItem.idVerified
      this.formData.memo = this.selectItem.memo
    },
    // 提交表单
    submitForm() {
      this.$refs.formData.validate(vaild => {
        if (vaild) {
          console.log('提交表单')
          this._auditData()
        }
      })
    },
    // 审核方法啊
    _auditData() {
      const param = {
        uid: this.selectItem.uid,
        audit: this.formData.audit,
        memo: this.formData.memo
      }
      auditData(param).then(resp => {
        if (resp.code === 0) {
          this.$message.success('操作成功～')
          this.handleClose()
          this.handleSuccess()
        } else {
          this.$message.error(resp.msg)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-container {
  width: 100%;
  height: 100%;
  .el-main {
    height: 90%;
    width: 100%;
    padding-right: 100px;
    padding-left: 100px;
  }
  .el-footer {
    height: 10%;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>
