<template>
  <div class="case">
    <van-form @submit="consultationSave">
      <div class="main">
        <van-row class="cell">
          <van-col span="6">会商议题：</van-col>
          <van-col span="18">
            <van-field v-model="form.name" :rules="[{ required: true}]" placeholder="请输入会商议题" />
          </van-col>
        </van-row>
        <van-row class="cell">
          <van-col span="6">会商时间：</van-col>
          <van-col span="18" @click="clickdateshow">
            <!-- {{form.workDate?form.workDate:'请选择'}} -->
            <van-field v-model="form.workDate" :rules="[{ required: true}]" placeholder="请选择会商时间" disabled />
          </van-col>
          <van-popup v-model="dateshow" position="bottom" :style="{ height: '50%' }">
            <van-datetime-picker @confirm="dateConfirm" @cancel="dateshow=false" v-model="workDate" 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">
            <van-field v-model="form.streetName" @click="clickstreetShow" :rules="[{ required: true}]" placeholder="请选择街道" disabled />
          </van-col>
          <!-- <van-col span="18" @click="streetshowPopup">{{form.streetName?form.streetName:'请选择'}}</van-col> -->
          <van-popup v-model="streetShow" position="bottom" :style="{ height: '50%' }">
            <van-picker default-index="2" show-toolbar :columns="streetList" @confirm="onConfirm" @cancel="streetShow=false" 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.address" :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="orglist.name" placeholder="请输入发起单位" disabled />
          </van-col>
        </van-row>
        <van-row class="cell">
          <van-col span="6">参与单位：</van-col>
          <van-col span="18">
            <van-field v-model="form.participateOrgName" :rules="[{ required: true}]" @click="ownerShow=true" placeholder="请选择参与单位" disabled />
            <van-popup v-model="ownerShow" position="bottom" :style="{ height: '50%' }">
              <div class="popup">
                <div class="btn">
                  <div @click="ownerShow=false" class="cancle">取消</div>
                  <div @click="ownerConfirm" class="confirm">确定</div>
                </div>
                <van-checkbox-group class="group" v-model="result">
                  <van-cell-group>
                    <van-cell v-for="(item,index) in orgform" clickable :key="index" :title="item.name" @click="toggle(item,index)">
                      <template #right-icon>
                        <van-checkbox :name="item.name" ref="checkboxes" />
                      </template>
                    </van-cell>
                  </van-cell-group>
                </van-checkbox-group>
              </div>
            </van-popup>
          </van-col>
        </van-row>
        <van-row class="cell">
          <van-col span="6">会商内容：</van-col>
          <van-col span="18">
            <van-field class="textarea" v-model="form.content" :rules="[{ required: true}]" rows="1" type="textarea" placeholder="请输入活动详情" />
          </van-col>
        </van-row>
        <vanUploader :text="'图片'" ref="vanimg" @getfileList="getfileList"></vanUploader>
        <vanUploader :text="'视频'" ref="vanvideo" @getfileList="getfileList"></vanUploader>
      </div>
      <div style="text-align:center;">
        <van-button native-type="submit">确 定</van-button>
      </div>
    </van-form>
  </div>
</template>
<script>
import {
  organizationDetail,
  consultationSave,
  uploadImage,
  getStreetList,
  organizationList,
} from '../../api/index.js'
import vanUploader from '../../components/vanUploader.vue'
export default {
  components: { vanUploader },
  data() {
    return {
      streetshowPopup: false,
      streetShow: false,
      show: false,
      searchshow: false,
      ownerShow: false,
      fileList: [],
      streetList: [],
      form: {
        fileList: [],
      },
      dateshow: false,
      appendFile: '',
      workDate: new Date(),
      minDate: new Date(2020, 0, 1),
      maxDate: new Date(2025, 10, 1),
      orglist: '',
      positionSearch: '',
      serviceTarget: [],
      childList: [],
      id: '',
      orgform: {},
      result: [],
      resultId: [],
      pics: [],
      videos: [],
    }
  },
  created() {
    this.getStreetList()
    this.organizationDetail()
    this.organizationList()
  },
  methods: {
    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)
        }
      }
    },
    clickdateshow() {
      if (this.id) return
      this.dateshow = !this.dateshow
    },
    clickstreetShow() {
      if (this.id) return
      this.streetShow = !this.streetShow
    },
    async getStreetList() {
      const res = await getStreetList()
      this.streetList = res.data
    },
    async organizationList() {
      const res = await organizationList()
      this.orgform = res.data
    },
    toggle(item) {
      if (this.result.indexOf(String(item.name)) == -1) {
        this.result.push(String(item.name))
        this.resultId.push(String(item.id))
      } else {
        var index = this.result.indexOf(String(item.name))
        var id = this.resultId.indexOf(String(item.name))
        this.result.splice(index, 1)
        this.resultId.splice(id, 1)
      }
    },
    async organizationDetail() {
      const res = await organizationDetail({ id: '' })
      this.orglist = res.data
    },
    onConfirm(value) {
      this.form.streetName = value.name
      this.form.areaId = value.id
      this.streetShow = false
    },
    ownerConfirm() {
      this.form.participateOrgName = this.result.join(',')
      this.form.participateOrgId = this.resultId.join(',')
      this.ownerShow = false
    },
    dateConfirm(v) {
      this.form.workDate = this.timeFormat(v)
      this.dateshow = false
    },
    onConfirmPicker(value) {
      this.serviceTarget.push(value.name)
      this.$set(this.form, 'helpChildren', this.serviceTarget.join(','))
      this.childList = []
      this.positionSearch = ''
    },
    async consultationSave() {
      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.workDate = data.workDate + ' 00:00:00'
      const res = await consultationSave(data)
      if (res.code == '0000') {
        this.$toast.success('成功')
        this.$emit('changactive', 1)
        this.form = { pics: [], videos: [] }
        this.pics = []
        this.videos = []
        this.$refs.vanimg.childrenClear()
        this.$refs.vanvideo.childrenClear()
      }
    },
    showPopup() {
      this.show = true
    },
    getPositionList() {},
    async afterRead(file) {
      file.status = 'uploading'
      file.message = '上传中...'
      let formDate = new FormData()
      //添加入参
      formDate.append('file', file.file)
      const res = await uploadImage(formDate)
      if (res.code == '0000') {
        file.status = 'success'
        file.message = '成功'
        this.form.fileList.push({
          url: res.data,
          type: file.file.type,
        })
        // 上传图片 设置col高度
        if (this.form.fileList.length % 2 == 0) {
          var index = 3.2 * (this.form.fileList.length / 2 + 1) + 'rem'
          this.$refs.boxlist.style.height = index
        }
      } else {
        file.status = 'failed'
        file.message = '上传失败'
      }
    },
    deleteUploader(v, index) {
      console.log(index)
      this.form.fileList.splice(index.index, 1)
    },
    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
    },
  },
}
</script>
<style lang="scss" scoped>
.case {
  height: calc(100vh - 220px);
  overflow: auto;
}
.group {
  margin-top: 30px;
  height: calc(100% - 180px);
  overflow: auto;
}
.tx {
  width: 160px;
  height: 160px;
}
.message {
  padding: 0 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;
  }
  .col {
    min-height: 80px !important;
  }
  .van-cell {
    padding: 0;
    line-height: 80px;
  }
}
.textarea {
  line-height: 30px !important;
  margin-top: 25px;
}
.van-button {
  width: 690px;
  margin: 24px auto;
  color: #fff;
  background-color: #1677ff;
  border-radius: 8px;
  font-size: 30px;
}
.box {
  position: relative;
  min-height: 240px;
  .boxlist {
    display: flex;
    position: absolute;
    top: 0;
    left: 0;
    flex-wrap: wrap;
    div {
      width: 200px;
      height: 200px;
      margin: 20px;
    }

    img {
      width: 100%;
      height: 100%;
    }
    video {
      width: 100%;
      height: 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: 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;
}
.popup {
  height: 100%;
  padding: 0 30px;
  .btn {
    padding: 16px 0;
    display: flex;
    justify-content: space-between;
  }
  .cancle {
    font-size: 26px;
    font-weight: bold;
  }
  .confirm {
    font-size: 26px;
    font-weight: bold;
  }
}
</style>