<template>
  <div>
    <van-nav-bar title="随访记录" @click-left="$router.back()" left-arrow fixed placeholder safe-area-inset-top />
    <div class="message">
      <van-row class="topBox">
        <van-col span="18">
          <van-row class="row">
            <van-col span="9">姓名：{{childInfo.name}}</van-col>
            <van-col span="6">性别：{{childInfo.sex}}</van-col>
            <van-col span="9">年龄：{{childInfo.age}}</van-col>
          </van-row>
          <van-row class="row">
            <van-col span="24">儿童类型：{{childInfo.childTypeName}}</van-col>
          </van-row>
          <van-row class="row">
            <van-col span="24">教育情况：{{childInfo.school}}</van-col>
          </van-row>
          <van-row class="row">
            <van-col span="24">健康情况：{{childInfo.healthStatus}}</van-col>
          </van-row>
        </van-col>
        <van-col span="6">
          <img class="tx" :src="childInfo.url?childInfo.url:require('../../assets/tx.png')" alt="">
        </van-col>
        <van-row class="row">
          <van-col span="10">监护人：{{childInfo.guardianName}}</van-col>
          <van-col span="14">联系方式：{{childInfo.guardianPhone}}</van-col>
        </van-row>
        <van-row class="row">
          <van-col span="24">居住地址：{{childInfo.address}}</van-col>
        </van-row>
      </van-row>
    </div>
    <van-form @submit="saveFollowUp">
      <div class="main">
        <van-row class="cell">
          <van-col span="6">随访时间：</van-col>
          <!-- <van-col span="18" @click="showPopup">{{form.followDate?form.followDate:'请选择'}}</van-col> -->
          <van-col span="18">
            <van-field v-model="form.followDate" @click="showPopup" :rules="[{ required: true}]" placeholder="请选择" disabled />
          </van-col>
          <van-popup v-model="show" position="bottom" :style="{ height: '50%' }">
            <van-datetime-picker @confirm="dateConfirm" @cancel="showPopup" v-model="followDate" type="date" :min-date="minDate" :max-date="maxDate" :columns-order="[ 'year','month', 'day']"
              :formatter="formatter" />
          </van-popup>
        </van-row>
        <van-row class="cell">
          <van-col span="6">选择街道：</van-col>
          <!-- <van-col span="18" @click="streetshowPopup">{{form.streetName?form.streetName:'请选择'}}</van-col> -->
          <van-col span="18">
            <van-field v-model="form.streetName" @click="streetshowPopup" :rules="[{ required: true}]" placeholder="请选择" disabled />
          </van-col>
          <van-popup v-model="streetShow" position="bottom" :style="{ height: '50%' }">
            <van-picker default-index="2" :visible-item-count="6" show-toolbar :columns="streetList" @confirm="onConfirm" @cancel="streetshowPopup" @change="onChange" value-key="name" />
          </van-popup>
        </van-row>
        <van-row class="cell">
          <van-col span="6">随访地点：</van-col>
          <van-col span="18">
            <van-field v-model="form.place" :rules="[{ required: true}]" placeholder="请输入随访地点" />
          </van-col>
        </van-row>
        <van-row class="cell">
          <van-col span="6">随访人员：</van-col>
          <van-col span="18">
            <van-field v-model="form.personnel" :rules="[{ required: true}]" placeholder="请输入随访人员" />
          </van-col>
        </van-row>
        <van-row class="cell">
          <van-col span="6">联系方式：</van-col>
          <van-col span="18">
            <van-field v-model="form.phone" type="tel" :rules="[{validator,required: true, message:'手机号格式不正确'}]" placeholder="请输入" />
          </van-col>
        </van-row>
        <van-row class="cell">
          <van-col span="6">随访记录：</van-col>
          <van-col span="18">
            <van-field class="textarea" :rules="[{ required: true}]" v-model="form.remark" rows="1" type="textarea" placeholder="请输入随访记录" />
          </van-col>
        </van-row>
        <vanUploader :text="'图片'" @getfileList="getfileList"></vanUploader>
        <vanUploader :text="'视频'" @getfileList="getfileList"></vanUploader>
      </div>
      <div class="but">
        <van-button native-type="submit" :disabled="disabled">确 定</van-button>
      </div>
    </van-form>
  </div>
</template>
<script>
import { saveFollowUp, getStreetList, getChildInfo } from '../../api/index.js'
import vanUploader from '../../components/vanUploader.vue'
export default {
  components: { vanUploader },
  data() {
    return {
      show: false,
      streetShow: false,
      disabled: false,
      showvideoplay: false,
      videourl: '',
      fileList: [],
      form: {
        fileList: [],
      },
      streetList: '',
      followDate: new Date(),
      minDate: new Date(2020, 0, 1),
      maxDate: new Date(2025, 10, 1),
      customFieldName: {
        text: 'name',
        id: 'id',
      },
      childInfo: '',
      pics: [],
      videos: [],
    }
  },
  created() {
    this.id = this.$route.query.id
    this.getStreetList()
    this.getChildInfo()
  },
  methods: {
    validator(val) {
      return /^1(3|4|5|6|7|8|9)\d{9}$/.test(val)
    },
    async getChildInfo() {
      const res = await getChildInfo({ id: this.id })
      this.childInfo = res.data
      this.form.childId = res.data.childId
    },
    formatter(type, val) {
      if (type === 'year') {
        return val + '年'
      }
      if (type === 'month') {
        return val + '月'
      }
      if (type === 'day') {
        return val + '日'
      }
      return val
    },
    timeFormat(time) {
      // 时间格式化 2019-09-08
      let year = time.getFullYear()
      let month = time.getMonth() + 1
      if (month < 10) {
        month = '0' + month
      }

      let day = time.getDate()
      if (day < 10) {
        day = '0' + day
      }
      return year + '-' + month + '-' + day
    },
    showPopup() {
      this.show = !this.show
    },
    streetshowPopup() {
      this.streetShow = !this.streetShow
    },
    onChange() {},
    onConfirm(value) {
      this.form.streetName = value.name
      this.form.areaId = value.id
      this.streetShow = false
    },
    dateConfirm(v) {
      console.log(v)
      this.form.followDate = this.timeFormat(v)
      this.show = false
    },
    async saveFollowUp() {
      if (this.pics.length == 0) {
        this.$toast.fail('请选择至少一张图片')
        return
      }
      this.form.pics = this.pics.join(',')
      this.form.videos = this.videos.join(',')
      var data = JSON.parse(JSON.stringify(this.form))
      data.followDate = data.followDate + ' 00:00:00'
      const res = await saveFollowUp(data)
      if (res.code == '0000') {
        this.disabled = true
        this.$toast.success('成功')
        this.$router.go(-1)
      }
    },
    async getStreetList() {
      const res = await getStreetList()
      this.streetList = res.data
    },
    getfileList(v, index) {
      if (index == undefined) {
        if (
          v.indexOf('.png') != -1 ||
          v.indexOf('.jpg') != -1 ||
          v.indexOf('.jpeg') != -1
        ) {
          this.pics.push(v)
        } else {
          this.videos.push(v)
        }
      } else {
        if (v == '图片') {
          this.pics.splice(index, 1)
        } else {
          this.videos.splice(index, 1)
        }
      }
    },
  },
}
</script>
<style lang="scss" scoped>
.tx {
  width: 160px;
  height: 160px;
}
.message {
  padding: 0 30px;
}
.but {
  text-align: center;
}
.van-button {
  width: 690px;
  margin: 24px auto;
  color: #fff;
  background-color: #1677ff;
  border-radius: 8px;
  font-size: 30px;
}
.topBox {
  padding: 24px 0;
  border-bottom: 1px solid #eee;
}
.van-row {
  font-size: 30px;
  text-align: left;
}
.row {
  .van-col {
    line-height: 60px;
  }
}
.main {
  padding: 0 30px;
}
.cell {
  border-bottom: 1px solid #eee;
  margin-top: 16px;
  .van-col {
    line-height: 80px;
  }
  .van-cell {
    padding: 0;
    line-height: 80px;
    font-size: 30px;
  }
}
.textarea {
  line-height: 30px !important;
  margin-top: 25px;
}
.box {
  position: relative;
  height: 240px;
  .boxlist {
    display: flex;
    position: absolute;
    top: 0;
    left: 0;
    div {
      width: 200px;
      height: 200px;
      margin: 0 20px;
    }

    img {
      width: 100%;
      height: 100%;
    }
    video {
      width: 100%;
    }
    // background-color: chocolate;
  }
  .van-uploader {
    position: absolute;
    top: 0;
    left: 0;
    /deep/.van-uploader__wrapper {
      .van-uploader__upload {
        width: 200px;
        height: 200px;
      }
    }

    /deep/.van-uploader__preview {
      margin: 0 20px;
      .van-uploader__file {
        opacity: 0;
      }
      .van-image {
        width: 200px;
        height: 200px;
        opacity: 0;
        // margin: 20px;
      }
      width: 200px;
      height: 200px;
    }
  }
}
/deep/.van-field__control {
  color: #333;
}
/deep/.van-field__control:disabled {
  color: #333;
  -webkit-text-fill-color: unset;
}
</style>