<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="name" label-width="120px">
              {{dataForm.name}}
      </el-form-item>
        <el-form-item label="手机号码：" prop="mobile" label-width="120px">
            {{dataForm.mobile}}
        </el-form-item>
          <el-form-item label="性别：" prop="sex" label-width="120px">
              {{dataForm.sex}}
      </el-form-item>
<!--          <el-form-item label="民族" prop="nation" label-width="120px">-->
<!--          <el-input v-model="dataForm.nation" placeholder="民族"style="width:250px"></el-input>-->
<!--      </el-form-item>-->
        <el-form-item label="身份证号码：" prop="idcard" label-width="120px">
            {{dataForm.idcard}}
        </el-form-item>
        <el-form-item label="签发机关：" prop="organ" label-width="120px">
            {{dataForm.organ}}
        </el-form-item>
<!--          <el-form-item label="出生日期" prop="birthday" label-width="120px">-->
<!--              <el-date-picker-->
<!--                      v-model="dataForm.birthday"-->
<!--                      type="date"-->
<!--                      placeholder="出生日期"-->
<!--                      value-format="yyyy-MM-dd"-->
<!--                      format="yyyy-MM-dd"-->
<!--                      style="width:250px">-->
<!--              </el-date-picker>-->
<!--      </el-form-item>-->
        <el-form-item label="核酸检测时间：" prop="testingTime" label-width="120px">
            {{dataForm.testingTime}}
        </el-form-item>
<!--          <el-form-item label="照片base64编码" prop="imgCode">-->
<!--          <el-input v-model="dataForm.imgCode" placeholder="照片base64编码"></el-input>-->
<!--      </el-form-item>-->
<!--          <el-form-item label="照片地址" prop="imgUrl">-->
<!--          <el-input v-model="dataForm.imgUrl" placeholder="照片地址"></el-input>-->
<!--      </el-form-item>-->
        <el-form-item label="照片："
                      v-loading="loading"
                      prop="imgUrl"
                      label-width="120px">
            <el-upload class="avatar-uploader"
                       :action="uploadUrl"
                       disabled
                       :show-file-list="false"
                       :on-success="handleAvatarSuccess"
                       :before-upload="beforeAvatarUpload">
                <img v-if="dataForm.imgUrl"
                     :src="dataForm.imgUrl"
                     class="avatar">
                <i v-else
                   class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
        </el-form-item>
        <el-form-item label="住址：" prop="address" label-width="120px">
            {{dataForm.address}}
        </el-form-item>
        <el-form-item label="核酸检测点" label-width="120px">
            {{dataForm.testingPointName}}
        </el-form-item>
<!--          <el-form-item label="核酸接测结果" prop="testingResult">-->
<!--          <el-input v-model="dataForm.testingResult" placeholder="核酸接测结果"></el-input>-->
<!--      </el-form-item>-->
<!--          <el-form-item label="乐观锁" prop="revision">-->
<!--          <el-input v-model="dataForm.revision" placeholder="乐观锁"></el-input>-->
<!--      </el-form-item>-->
<!--          <el-form-item label="创建人" prop="createdBy">-->
<!--          <el-input v-model="dataForm.createdBy" placeholder="创建人"></el-input>-->
<!--      </el-form-item>-->
<!--          <el-form-item label="创建时间" prop="createdTime">-->
<!--          <el-input v-model="dataForm.createdTime" placeholder="创建时间"></el-input>-->
<!--      </el-form-item>-->
<!--          <el-form-item label="更新人" prop="updatedBy">-->
<!--          <el-input v-model="dataForm.updatedBy" placeholder="更新人"></el-input>-->
<!--      </el-form-item>-->
<!--          <el-form-item label="更新时间" prop="updatedTime">-->
<!--          <el-input v-model="dataForm.updatedTime" placeholder="更新时间"></el-input>-->
<!--      </el-form-item>-->
<!--          <el-form-item label="逻辑删除标识" prop="delFlag">-->
<!--          <el-input v-model="dataForm.delFlag" placeholder="逻辑删除标识"></el-input>-->
<!--      </el-form-item>-->
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('返回') }}</el-button>
<!--      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>-->
    </template>
  </el-dialog>
</template>

<script>
import Cookies from 'js-cookie'
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        name: '',
        sex: '',
        nation: '',
        birthday: '',
        address: '',
        idcard: '',
        organ: '',
        imgCode: '',
        imgUrl: '',
        testingResult: '',
        testingTime: '',
        revision: '',
        createdBy: '',
        createdTime: '',
        updatedBy: '',
        updatedTime: '',
        delFlag: '',
        testingPointName: ''
      },
        uploadUrl: '',
        loading: false,
    }
  },
  computed: {
    dataRule () {
      return {
        name: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        sex: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        // nation: [
          //   { required: true, message: this.$t('validate.required'), trigger: 'blur' }
          // ],
          // birthday: [
          //   { required: true, message: this.$t('validate.required'), trigger: 'blur' }
          // ],
          // address: [
          //   { required: true, message: this.$t('validate.required'), trigger: 'blur' }
          // ],
        idcard: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        // organ: [
        //   { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        // ],
        // imgCode: [
        //   { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        // ],
        // imgUrl: [
        //   { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        // ],
        // testingResult: [
        //   { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        // ],
        testingTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        // revision: [
        //   { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        // ],
        // createdBy: [
        //   { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        // ],
        // createdTime: [
        //   { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        // ],
        // updatedBy: [
        //   { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        // ],
        // updatedTime: [
        //   { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        // ],
        // delFlag: [
        //   { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        // ]
      }
    }
  },
    created () {
        // this.visible = true
        this.$nextTick(() => {
            // this.dataForm.communityId = this.dataForm.streetId = this.dataForm.gridId = null
            // 富文本
            // this.hideUpload = false
            this.uploadUrl = `${window.SITE_CONFIG['apiURL']}/oss/file/upload?token=${Cookies.get('token')}`
        })
    },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
      // 上传图片
      handleAvatarSuccess (res, file) {
          this.loading = false
          this.dataForm.imgUrl = res.data.url
      },
      beforeAvatarUpload (file) {
          this.loading = true
      },
    // 获取信息
    getInfo () {
      this.$http.get(`/custom/persontesting/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/custom/persontesting/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
<style>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409eff;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>