<template>
  <el-dialog :visible.sync="visible"
             title="审核"
             :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-form :model="dataForm"
             :rules="dataRule"
             ref="dataForm"
             @keyup.enter.native="dataFormSubmitHandle()"
             :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
      <el-form-item label="真实姓名"
                    prop="realName">
        <span>{{dataForm.realName}}</span>
      </el-form-item>
      <el-form-item label="手机号"
                    prop="mobile">
        <span>{{dataForm.mobile}}</span>
      </el-form-item>
      <el-form-item label="身份证号"
                    prop="identityNo">
        <span>{{dataForm.identityNo}}</span>
      </el-form-item>
      <el-form-item label="是否党员">
        <el-select v-model="dataForm.partyFlag"
                   clearable
                   placeholder="请选择">
          <el-option v-for="item in partyFlagOptions"
                     :key="item.id"
                     :label="item.name"
                     :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="干部下沉">
        <el-select v-model="dataForm.cadreFlag"
                   clearable
                   placeholder="请选择">
          <el-option v-for="item in cadreOptions"
                     :key="item.cadreFlag"
                     :label="item.cadreTitle"
                     :value="item.cadreFlag">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="扫码网格">
        <span>{{dataForm.street}} - </span>
        <span>{{dataForm.community}} - </span>
        <span>{{dataForm.grid}}</span>
      </el-form-item>
      <el-form-item label="居民住址"
                    prop="address">
        <span>{{dataForm.address}}</span>
      </el-form-item>
      <el-form-item label="通过审核"
                    prop="pass">
        <template>
          <el-radio v-model="dataForm.pass"
                    label="1">是</el-radio>
          <el-radio v-model="dataForm.pass"
                    label="0">否</el-radio>
        </template>
      </el-form-item>
      <el-form-item label="审核备注">
        <el-input v-model="dataForm.remark"
                  maxlength="50"
                  show-word-limit
                  placeholder="审核备注"></el-input>
      </el-form-item>
      <p></p>
      <el-form-item label="用户标签"
                    prop="tagIds">
        <el-checkbox-group v-model="dataForm.tagIds">
          <el-checkbox v-for="item in tagOptions"
                       :key="item.tagName"
                       :label="item.id">{{item.tagName}}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary"
                 @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        partyFlag: '',
        cadreFlag: '',
        pass: '',
        street: '',
        community: '',
        grid: '',
        tagIds: [],
        registTime: '',
        remark: ''
      },
      partyFlagOptions: [{ id: '0', name: '不是' }, { id: '1', name: '是' }],
      cadreOptions: [{ cadreFlag: '1', cadreTitle: '是' }, { cadreFlag: '0', cadreTitle: '否' }],
      tagOptions: []
    }
  },
  computed: {
    dataRule () {
      return {
        nickname: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        realName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        identityNo: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        address: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        partyFlag: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        gridId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        pass: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
      this.getTagOptions()
    },
    getTagOptions () {
      this.$http.get(`/app-user/usertag/list`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.tagOptions = res.data
      }).catch(() => { })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/app-user/user/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
        console.log(this.dataForm)
        this.dataForm.tagIds = []
      }).catch(() => { })
    },
    // 审核
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'post']('/app-user/user/audit/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          // 审核通过之后触发发短信
          this.registerResult(this.dataForm.id, this.dataForm.pass)
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false }),
    registerResult (userId, state) {
      this.$http.get(`api/message/sms/registerResult?userId=` + userId + '&state=' + state).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
      }).catch(() => { })
    }
  }
}
</script>
