<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="getDictLabel(selectedUser.type,'out_in_type')"></van-cell>
            <van-cell title="来访事由" :value="selectedUser.reasons"></van-cell>
            <van-cell title="来访单位" :value="selectedUser.companyName"></van-cell>
            <van-cell title="来访人员" :value="selectedUser.userName"></van-cell>
            <van-cell title="证件类型" :value="getDictLabel(selectedUser.idType,'letter_id_type')"></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(selectedUser.imgFile)">
                  <img :src="imgUrl(v)" alt="" />
                </div>
              </van-cell>
            </div>

            <van-cell title="介绍信附件"  v-if="selectedUser.letterFile && selectedUser.letterFile.length > 0">
              <template #right-icon>
                <div class="file-box">
                  <p class="van-ellipsis" v-for="(v, i) in selectedUser.letterFile"
                     :key="v.url"
                     @click="previewFile(v)">{{v.name}}</p>
                </div>
              </template>
            </van-cell>
            <div class="upload-box" v-if="selectedUser.checkImage && selectedUser.checkImage.length > 0">
              <span>核验结果</span>
              <van-cell >
                <div
                  class="nfc-img van-hairline--surround"
                  v-for="(v, i) in selectedUser.checkImage"
                  :key="v"
                  @click="preView(selectedUser.checkImage)">
                  <img :src="imgUrl(v)" alt="" />
                </div>
              </van-cell>
            </div>
            <van-cell title="陪同人员" :value="selectedUser.accompanyingPerson"></van-cell>
            <van-cell title="到达时间" v-if="selectedUser.arrivalTime" :value="selectedUser.arrivalTime"></van-cell>
            <date-cell v-else required title="到达时间" :max-date="maxData" :is-row="true"  v-model="formData.arrivalTime" date-type="datetime" />
            <van-cell title="离开时间" v-if="selectedUser.departureTime" :value="selectedUser.departureTime"></van-cell>
            <date-cell v-else required title="离开时间" :max-date="maxData" :min-date='minDate' :is-row="true"  v-model="formData.departureTime" date-type="datetime" />
          </div>
          <div v-if="!selectedUser.departureTime" 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'
import dayjs from 'dayjs'
export default {
  components: {Uploader, DateCell},
  data(){
    return {
      maxData:new Date(),
      minDate:null,
      visitId:null,
      userList:[],
      selectedUser:{},
      prop:{
        label:'userName',
        value:'id'
      },
      formData:{
        arrivalTime:null,
        departureTime:null,
      },
      dicts:['out_in_approve_status','out_in_type','letter_id_type']
    }
  },
  computed:{
    ...mapGetters(['orgId','id','dictionary']),
  },
  created(){
    this.visitId = this.$route.query.id;
    this.getUserInfo();
  },
  methods:{
    imgUrl,formatDate,
    onSubmit(){
      let {departureTime} = this.formData;
      if(!departureTime){
        this.$toast('请选择离开时间');
        return
      }
      let data = {
        id:this.selectedUser.id,
        arrivalTime:this.selectedUser.arrivalTime,
        departureTime,
      }
      //alert(JSON.stringify(data))
      userDepart(data).then(res=>{
        this.$toast.success('提交成功');
        this.$router.replace({
          name:'visitRecord',
          path:'/visitRecord',
          params:{event:'refresh'},
        });
      })
    },
    getUserInfo(){
      userDetails(this.visitId).then(res=>{
        let checkImage = res.data.checkImage.split(',');
        let imgFile = res.data.imgFile.split(',');
        let letterFile = [];
        if(res.data.letterFile && res.data.letterFile.length > 0){
            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;
        //设置最小时间
        let minDate = this.selectedUser.arrivalTime;
        this.minDate = new Date(minDate);
      })
    },
    previewFile(file){
      this.openFilePreview(file);
    },
    preView(val) {
      if(Array.isArray(val)){
        let arr = val.map(v=>{
          return imgUrl(v);
        })
        ImagePreview(arr);
      }else {
        ImagePreview([imgUrl(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;
}
.file-box{
  width: 70%;
  display: flex;
  justify-content: flex-end;
  color:#008cd6;
}
.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>
