<template>
  <div class="record-detail">
    <nav-bar></nav-bar>
    <div class="page-container">
      <!--   基本信息   -->
      <div class="card">
        <van-panel >
            <template #header>
              <span></span>
            </template>
          <div class="panel-box">
            <van-cell title="出入人员" :value="selectedUser.userName"></van-cell>
            <van-cell title="证件号码" :value="selectedUser.idCard"></van-cell>
            <div class="upload-box" v-if="selectedUser.imgFile && selectedUser.imgFile.length > 0">
              <span>证件照</span>
              <van-cell >
                <div
                  class="nfc-img van-hairline--surround"
                  v-for="(v, i) in selectedUser.imgFile"
                  :key="v"
                  @click="preView(v)">
                  <img :src="imgUrl(v)" alt="" />
                </div>
              </van-cell>
            </div>
            <van-cell title="单位" :value="selectedUser.companyName"></van-cell>
            <van-cell title="出入事由" :value="selectedUser.reasons"></van-cell>
            <div class="upload-box" v-if="selectedUser.letterFile && selectedUser.letterFile.length > 0">
              <span>介绍信</span>
              <van-cell >
                <div
                  class="nfc-img van-hairline--surround"
                  v-for="(v, i) in selectedUser.letterFile"
                  :key="v.url"
                  @click="preView(v.url)">
                  <img :src="imgUrl(v.url)" alt="" />
                </div>
              </van-cell>
            </div>
            <van-cell title="出入类型" :value="getDictLabel(selectedUser.type,'out_in_type')"></van-cell>
            <van-cell title="陪同人员" :value="selectedUser.accompanyingPerson"></van-cell>
            <div class="upload-box" >
              <span>核验结果</span>
              <div
                class="nfc-img van-hairline--surround"
                v-for="(v, i) in selectedUser.checkImage"
                :key="v"
                @click="preView(v)">
                <img :src="imgUrl(v)" alt="" />
              </div>
            </div>
            <date-cell required title="到达时间" :is-row="true"  v-model="selectedUser.arrivalTime" date-type="datetime" />
            <date-cell required title="离开时间" :is-row="true"  v-model="selectedUser.departureTime" date-type="datetime" />
          </div>
          <div  class="big-btn-box" >
            <van-button  type="info" size="large" @click="onSubmit">确认登记</van-button>
          </div>
        </van-panel>
      </div>

    </div>
  </div>
</template>

<script>

import DateCell from "@/components/dateCell/index.vue";
import Uploader from "@/components/upload/gxuploader.vue";
import {formatDate} from "@/filters/filter";
import {mapGetters} from "vuex";
import {imgUrl} from "@/utils";
import { ImagePreview } from 'vant'
import {userDetails, userDepart} from './api'
export default {
  components: {Uploader, DateCell},
  data(){
    return {
      visitId:null,
      userList:[],
      selectedUser:{},
      prop:{
        label:'userName',
        value:'id'
      },
      formData:{
        letterUserId:null,
        arrivalTime:null,
        departureTime:null,
        accompanyingPerson:null,
        checkImage:null,
      },
      dicts:['out_in_approve_status','out_in_type']
    }
  },
  computed:{
    ...mapGetters(['orgId','id','dictionary'])
  },
  mounted(){
    this.visitId = this.$route.query.id;
    this.getUserInfo();
  },
  methods:{
    imgUrl,formatDate,
    onSubmit(){
      let data = {
        ...this.formData,
        letterId:this.selectedUser.letterId,
        checkImage:this.formData.checkImage.map(v=>{return v.imgPath}).toString(','),
        letterUserId:this.selectedUser.id,
        orgId:this.orgId,
      }
      alert(JSON.stringify(data))
      userDepart(data).then(res=>{
        this.$toast.success('提交成功');
        this.$router.replace({
          path:'/visitRecord',
        });
      })
    },
    getUserInfo(){

      userDetails(this.visitId).then(res=>{
        let checkImage = res.data.checkImage.split(',');
        let imgFile = res.data.imgFile.split(',');
        let letterFile = res.data.letterFile.map(v=>{
          return JSON.parse(v)
        })
        this.selectedUser = res.data;
        this.selectedUser.checkImage = checkImage;
        this.selectedUser.imgFile = imgFile;
        this.selectedUser.letterFile = letterFile;
      })
    },
    preView(val) {
      ImagePreview(val);
    },
  }
}
</script>
<style lang="scss">
.record-detail{
  .van-card{
    padding: 20px;
  }
  .card-cell-box{
    width: 70%;
    .van-cell{
      padding: 10px;
      &::after{
        left:10px;
        right:10px;
      }
    }
    .van-cell__title{
      flex:.25;
    }
    .van-cell__value{
      flex:.75;
    }
  }
}
</style>
<style scoped lang="scss">
.record-detail{
  height: 100%;
  overflow: hidden;
}
.page-container{
  height: calc(100vh - 94px);
  overflow: auto;
  padding: 20px;

}
.flex-box{
  display: flex;
  justify-content: space-between;
  align-items: center;
  >span{
    margin: 0 20px;
  }
}
.card{
  margin-bottom: 20px;
  box-shadow: 0 10px 10px #eaeaea;
}
.card:last-child{
  margin-bottom: 0;
}
.panel-box{
  -padding:0 20px;
}
.panel-box-item{
  height: 36px;
  line-height: 36px;
}
.item-label{
  width: 100%;
  display: flex;
  justify-content: right;
  align-items: center;
}
.item-value{
  width: 100%;
  display: flex;
  justify-content: left;
  align-items: center;
}
.upload-box{
  padding: 20px 30px;
  display: flex;
  >span{
    display: inline-block;
    height: 160px;
    width: 200px;
    line-height: 160px;
    font-size: 28px;
    color:#999;
    >i{
      font-style: normal;
      color: #ee0a24;
    }
  }
  .required::before{
    content: '*';
    color: #ee0a24;
    position: absolute;
    left: 14px;
  }
}
.goods-card{
  width: 100%;
  display: flex;
  align-items: center;
  padding: 10px;
  background-color: #fff;
  .card-img-box{
    width: 200px;
    height: 200px;
    margin-right: 10px;
    >img{
      width: 100%;
      height: 100%;
      object-fit: cover;
      border-radius: 10px;
    }
  }
}
.big-btn-box{
  padding: 20px;
}
.nfc-img {
  display: inline-block;
  width: 140px;
  height: 140px;
  margin: 0 10px;
  position: relative;
  > img {
    width: 100%;
    height: 100%;
    border: none;
  }
  > span {
    position: absolute;
    padding: 0 10px;
    bottom: 0;
    left: 0;
    display: block;
    width: 100%;
    background-color: rgba(0, 0, 0, 0.2);
    color: #eaeaea;
    font-size: 20px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    line-height: 30px;
    height: 30px;
  }
}
</style>
