<template>
  <div class="intro-add">
    <nav-bar></nav-bar>
    <div class="page-container">
      <!--   基本信息   -->
      <div class="card">
        <van-panel title="来访信息" >
          <template #header>
            <span></span>
          </template>
<!--          <template #header>-->
<!--            <van-cell title="状态">-->
<!--              <template #extra>-->
<!--                  <span :style="{color:getState(getDictLabel(visitInfo.status,'out_in_approve_status'))}">-->
<!--                    {{getDictLabel(visitInfo.status,'out_in_approve_status') }}-->
<!--                  </span>-->
<!--              </template>-->
<!--            </van-cell>-->
<!--          </template>-->
          <div class="panel-box">
            <select-cell
              title="出入人员"
              :prop="prop"
              is-row
              @itemInfo="getItemInfo"
              v-model="selectedUser.id"
              :data-list="userList"
              required>
            </select-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="单位" :value="selectedUser.companyName"></van-cell>
            <van-cell title="出入事由" :value="selectedUser.reasons"></van-cell>
            <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>
            <van-cell title="出入类型" :value="getDictLabel(selectedUser.type,'out_in_type')"></van-cell>
            <van-field
              v-model="formData.accompanyingPerson"
              required
              rows="1"
              autosize
              :maxlength="200"
              placeholder="请输入"
              label="陪同人员"></van-field>
            <div class="upload-box" >
              <span class="required">核验结果</span>
              <van-cell>
                <uploader :maxCount="2" v-model="formData.checkImage"/>
              </van-cell>
            </div>
            <date-cell required title="到达时间" :max-date="maxData" v-model="formData.arrivalTime" date-type="datetime" />
<!--            <date-cell title="离开时间" :max-date="maxData" v-model="formData.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 {userList, userRegister} from './api'
export default {
  components: {Uploader, DateCell},
  data(){
    return {
      maxData:new Date(),
      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.getUserList();
  },
  methods:{
    imgUrl,formatDate,
    onSubmit(){
      if(!this.selectedUser.id){
        this.$toast('请选择出入人员');
        return
      }
      if(!this.formData.accompanyingPerson){
        this.$toast('请输入陪同人员');
        return
      }
      if(!this.formData.checkImage){
        this.$toast('请上传审核结果');
        return
      }
      if(!this.formData.arrivalTime){
        this.$toast('请选择到达时间');
        return
      }
      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))
      userRegister(data).then(res=>{
        this.$toast.success('提交成功');
        this.$router.replace({
          name:'visitRecord',
          path:'/visitRecord',
          params:{event:'refresh'},
        });
      })
    },
    getItemInfo(item){
      this.selectedUser = item;
    },
    previewFile(file){
      this.openFilePreview(file);
    },
    getUserList(){
      let data = {
        orgId:this.orgId,
        arrivalTime:formatDate(new Date(),'YYYY-MM-DD')
      }
      userList(data).then(res=>{
        this.userList = res.data.map(v=>{
          v.imgFile = v.imgFile.split(',');
          if(v.letterFile && v.letterFile.length >0){
            v.letterFile = v.letterFile.map(str=>{
              return JSON.parse(str)
            })
          }
          return v
        })
      })
    },
    preView(val) {
      if(Array.isArray(val)){
        let arr = val.map(v=>{
          return imgUrl(v);
        })
        ImagePreview(arr);
      }else {
        ImagePreview([imgUrl(val)]);
      }
    },
  }
}
</script>
<style lang="scss">
.intro-add{
  .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">
.intro-add{
  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: 0 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;
    }
  }
}
.file-box{
  width: 70%;
  display: flex;
  justify-content: flex-end;
  color:#008cd6;
}
.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>
