<template>
  <el-dialog title="封禁" :close-on-click-modal="false" :visible.sync="open" width="700px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="封禁用户" prop="">
        <div  class="context-avatar-center">
          {{ userData.nickname }} 【{{ userData.usercode }}】 <image-avatar :src="userData.avatar"/>
        </div>
      </el-form-item>
      <el-form-item label="用户目前情况" prop="">
        <div>用户【{{userData.nickname}}】: <span v-html="userForbidMessage"/></div>
        <div v-if="userInfoData.lastLoginIp">最后登陆IP【{{userInfoData.lastLoginIp}}】: <span v-html="ipForbidMessage"/></div>
        <div v-if="userInfoData.imei">设备号【{{userInfoData.imei}}】: <span v-html="imeiForbidMessage"/></div>
      </el-form-item>
      <el-form-item label="封禁设备确认" v-if="form.type.includes(3)">
        <el-input v-model="userInfoData.imei" disabled />
      </el-form-item>
      <el-form-item label="封禁IP确认" v-if="form.type.includes(5)">
        <el-input v-model="userInfoData.lastLoginIp" disabled />
      </el-form-item>
      <el-form-item label="封禁类型" prop="type">
        <el-checkbox-group v-model="form.type" @change="typeChange">
          <el-checkbox :label="1">用户</el-checkbox>
          <el-checkbox :label="3">设备</el-checkbox>
          <el-checkbox :label="5">登录IP</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="封禁时长" prop="forbidTime">
        <el-radio-group v-model="form.forbidTime">
          <el-radio
              v-for="dict in forbidTimeList"
              :key="dict.value"
              :label="dict.value"
          ><span :class="dict.value === 0 ? 'red-color' : ''">{{dict.label}}</span></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="理由" prop="reason">
        <el-input v-model="form.reason" placeholder="请输入理由"   type="textarea" :rows="2"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="open = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import ImageUpload from '@/components/ImageUpload/index'
import { getUser, userForbid } from '@/api/cai/user'
import {forbidStatusList, forbidTimeList, forbidTypeList} from "@/constant/statusMap";
import { userForbidInfo } from '@/api/cai/userForbid'

export default {
  components: {
    ImageUpload,
  },
  data () {
    return {
      forbidTypeList,forbidStatusList,
      forbidTimeList:[],
      open: false,
      title: '',
      form:{
        userId: undefined,
        type: [],
        forbidTime: undefined,
        reason: undefined,
      },
      userData:{},
      userInfoData:{},
      userForbid:{},
      ipUserForbid:{},
      imeiUserForbid:{},
      userForbidMessage: '',
      ipForbidMessage: '',
      imeiForbidMessage: '',
      // 表单校验
      rules: {
        type: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        forbidTime: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        reason: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ]
      },
      buttonLoading: false,
      ipShow: false,
      uuidShow: false
    }
  },
  created() {
    this.forbidTimeList = [{ value: 0, label: '解封' }]
    this.forbidTimeList = this.forbidTimeList.concat(forbidTimeList)
  },
  methods: {
    init (userId) {
      this.$nextTick(() => {
        userForbidInfo(userId).then(res => {
          this.userData = res.data.user
          this.userInfoData = res.data.userInfo
          this.userForbid = res.data.userForbid
          this.ipUserForbid = res.data.ipUserForbid
          this.imeiUserForbid = res.data.imeiUserForbid
          if(this.userForbid){
            this.userForbidMessage = `<span style="color: red">`+this.userForbid.endTime+`</span><span style="color: burlywood">（`+res.data.userForbidBetween+` 后解封）</span>`
          }else{
            this.userForbidMessage = `<span style="color: #1ab394">正常</span>`
          }
          if(this.ipUserForbid){
            this.ipForbidMessage = `<span style="color: red">`+this.ipUserForbid.endTime+`</span><span style="color: burlywood">（`+res.data.ipForbidBetween+` 后解封）</span>`
          }else{
            this.ipForbidMessage = `<span style="color: #1ab394">正常</span>`
          }
          if(this.imeiUserForbid){
            this.imeiForbidMessage = `<span style="color: red">`+this.imeiUserForbid.endTime+`</span><span style="color: burlywood">（`+res.data.imeiForbidBetween+` 后解封）</span>`
          }else{
            this.imeiForbidMessage = `<span style="color: #1ab394">正常</span>`
          }

        })
      })
      this.form.userId = userId
      this.form.type = []
      this.form.forbidTime = undefined
      this.form.reason = undefined
      this.ipShow = false
      this.uuidShow = false
      this.open = true;
    },
    typeChange(value){
      // this.ipShow = value.includes(3)
      // this.uuidShow = value.includes(2)
    },
    // 表单提交
    submitForm () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          userForbid(this.form).then(data => {
            this.$modal.msgSuccess("操作成功");
            this.buttonLoading = false;
            this.open = false
            this.$emit('refreshDataList')
          }).finally(() => {
            this.buttonLoading = false;
          });
        }
      })
    },
  }
}
</script>
<style lang="scss" scoped>
.context-avatar-center {
  display: flex;
  align-items: center; /* 垂直居中 */
  //justify-content: center; /* 水平居中 */
}
.red-color{
  color: red;
}
</style>
