<template>
  <el-dialog :visible.sync="visible"
             title="审核"
             :modal-append-to-body='false'
             :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"
                    v-if="false">
        <span>{{dataForm.identityNo}}</span>
      </el-form-item>
      <el-form-item label="认证历史"
                    prop="realName"
                    v-if="true">
        <span>累计提交认证{{dataForm.totalSubmitNum}}次&nbsp;&nbsp;&nbsp;通过{{dataForm.totalPassSubmitNum}}次,&nbsp;不通过{{dataForm.totalFailNum}}次</span>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <el-button  type="text" size="small" @click="userAuthenticateHistoryListHandle(dataForm.id)">查看详情</el-button>
      </el-form-item>
      <el-form-item label="居民住址"
                    prop="address">
        <span>{{dataForm.address}}</span>
      </el-form-item>
      <!-- <el-form-item label="是否党员"
                    prop="partyFlag">
        <template>
          <el-radio v-model="dataForm.partyFlag"
                    label="1">是</el-radio>
          <el-radio v-model="dataForm.partyFlag"
                    label="0">否</el-radio>
        </template>
      </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="通过审核"
                    prop="state">
        <template>
          <el-radio v-model="dataForm.state"
                    label="3">是</el-radio>
          <el-radio v-model="dataForm.state"
                    label="2">否</el-radio>
        </template>
      </el-form-item>
      <el-form-item v-if="dataForm.state === '2'"
                    label="审核备注"
                    prop="remark">
        <el-input v-model="dataForm.remark"
                  maxlength="50"
                  show-word-limit
                  placeholder="审核备注"></el-input>
      </el-form-item>
      <el-form-item label="扫码网格">
        <el-table :data="userGridList"
                  style="width: 100%">
          <!-- <el-table-column prop="street"
                           label="街道"
                           width="180"
                           v-if="false">
          </el-table-column>
          <el-table-column prop="community"
                           label="社区"
                           width="180"
                           v-if="false">
          </el-table-column>
          <el-table-column prop="grid"
                           label="网格"
                           v-if="false">
          </el-table-column> -->
          <el-table-column prop="allDeptNames"
                           label="街道-社区-网格">
            </el-table-column>
          <el-table-column prop="grid"
                           label="网格名称" v-if="false">
          </el-table-column>
          <el-table-column prop="leaderFlag"
                           label="是否为网格长"
                           :formatter="showLeaderFlagFormatter">
          </el-table-column>
        </el-table>
      </el-form-item>
      <!-- <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>
    <authenticate-history v-if="authenticateHistoryVisible" ref="authenticateHistory"></authenticate-history>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
import AuthenticateHistory from './authenticate-history'

export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        partyFlag: '',
        cadreFlag: '',
        state: '',
        tagIds: [],
        remark: '',
        totalSubmitNum: '',
        totalPassSubmitNum: '',
        totalFailNum: ''
      },
      authenticateHistoryVisible: false,
      cadreOptions: [{ cadreFlag: '1', cadreTitle: '是' }, { cadreFlag: '0', cadreTitle: '否' }],
      tagOptions: [],
      userGridList: []
    }
  },
  computed: {
    dataRule () {
      return {
        partyFlag: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        state: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        remark: [
          { 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
        }
        this.dataForm.tagIds = []
      }).catch(() => { })
      this.$http.get(`/app-user/usergrid/listUserGrid/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.userGridList = res.data
      }).catch(() => { })
    },
    // 审核
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        let postData = {
          tagIds: this.dataForm.tagIds,
          id: this.dataForm.id,
          state: this.dataForm.state,
          partyFlag: this.dataForm.partyFlag,
          cadreFlag: this.dataForm.cadreFlag,
          remark: this.dataForm.remark
        }
        this.$http[!this.dataForm.id ? 'post' : 'post']('/app-user/user/audit/', postData).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          // 审核通过之后触发发短信
          // this.registerResult(this.dataForm.id)
          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) {
      this.$http.get(`api/message/sms/registerResult?userId=` + userId).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
      }).catch(() => { })
    },
    // 查看详情
    userAuthenticateHistoryListHandle (userId) {
      if (!this.dataForm.totalSubmitNum > 0) {
        return this.$message.error('累计提交' + this.dataForm.totalSubmitNum + '次,没有认证历史记录')
      }
      this.authenticateHistoryVisible = true
      this.$nextTick(() => {
        this.$refs.authenticateHistory.dataForm.userId = userId
        this.$refs.authenticateHistory.init()
      })
    },
    showLeaderFlagFormatter (row, column, cellValue, index) {
      console.log(cellValue)
      if (cellValue === '1') {
        return '是'
      } else {
        return '否'
      }
    }
  },
  components: {
    AuthenticateHistory
  }
}
</script>
