<template>
  <section class="Add">
    <ai-detail v-if="!$route.query.id">
      <template slot="title">
        <ai-title title="群发通知" isShowBack isShowBottomBorder @onBackClick="cancel(false)"></ai-title>
      </template>

      <template slot="content">
        <ai-card>
          <template #title>
            <div class="ai-card__title">
              <h2>发送条件</h2>
            </div>
          </template>

          <template #content>
            <el-form size="small" class="ai-form" :rules="rules" ref="form" :model="form" label-width="100px" label-position="right">
              <el-form-item label="发送方式" prop="messageSource" style="width: 50%">
                <el-radio-group v-model="form.messageSource" @change="sourceChange" >
                  <el-radio label="2">居民群</el-radio>
                  <el-radio label="1">居民</el-radio>
                </el-radio-group>
                
              </el-form-item>
              <el-form-item label="地区选择" style="width: 100%">
                <ai-area-get v-model="areaId" :root="areaRootId" :instance="instance" @select="handleAreaSelect" multiple size="small" placeholder="请选择"/>
              </el-form-item>
              <el-form-item label="发送范围" prop="deptList" style="width: 100%">
                <select-dept-user v-model="form.deptList" v-bind="$props" :source="form.messageSource"/>
              </el-form-item>
            </el-form>
          </template>
        </ai-card>

        <ai-card title="群发消息设置">
          <template #content>
            <el-form class="ai-form" ref="form" :rules="rules" :model="form" label-width="110px" label-position="right">
              <el-form-item class="el-form-item__textarea" label="群发内容" prop="content" style="width: 100%">
                <el-input type="textarea" placeholder="请输入…" v-model="form.content" maxlength="1000" :rows="5" show-word-limit></el-input>
              </el-form-item>
              <!-- 图片 -->
              <el-form-item label="图片" style="width: 100%">
                <ai-uploader :instance="instance" isWechat v-model="imgList" multiple acceptType=".jpg,.png,.jpeg" :limit="9"
                             url="/app/wxcp/upload/uploadFile?type=image"></ai-uploader>
              </el-form-item>
              <!-- 视频 -->
              <el-form-item label="视频" style="width: 100%">
                <ai-uploader :instance="instance" fileType="file" isWechat multiple acceptType=".mp4" v-model="videoList" :limit="9"
                             url="/app/wxcp/upload/uploadFile?type=video"></ai-uploader>
              </el-form-item>
              <!-- 附件 -->
              <el-form-item label="附件" style="width: 100%">
                <ai-uploader :instance="instance" fileType="file" isWechat multiple v-model="filesList" :limit="9"
                             url="/app/wxcp/upload/uploadFile?type=file"></ai-uploader>
              </el-form-item>
            </el-form>
          </template>
        </ai-card>
      </template>

      <template #footer>
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="confirm">确认发送</el-button>
      </template>

    </ai-detail>

    <ai-detail class="add" v-if="$route.query.id">
      <template slot="title">
        <ai-title title="群发通知详情" isShowBack isShowBottomBorder @onBackClick="cancel(false)"></ai-title>
      </template>
      <template slot="content">
        <ai-card title="详情">
          <template #content>
            <ai-wrapper>
              <ai-info-item label="创建者" :value="data.userName" isLine></ai-info-item>
              <ai-info-item label="发送方式" :value="data.messageSource" isLine>{{ data.messageSource | format }}</ai-info-item>
            </ai-wrapper>

            <ai-wrapper v-for="item in data.fileList" :key="item.id">
              <ai-info-item label="群发内容" :value="item.content" isLine v-if="item.contentType == 'text'"></ai-info-item>
            </ai-wrapper>

            <ai-wrapper>
              <ai-info-item label="图片" isLine v-show="images.length">
                <ai-uploader v-model="images" :instance="instance" :limit="9" disabled/>
              </ai-info-item>
            </ai-wrapper>

            <ai-wrapper>
              <ai-info-item label="视频" isLine v-show="videoArr.length" v-for="(item,index) in videoArr" :key="index">
                <video :src="item.accessUrl" style="width: 100%; height:100%; object-fit: fill;" muted controls="controls"></video>
              </ai-info-item>
            </ai-wrapper>

            <ai-wrapper>
              <ai-info-item label="附件" isLine v-show="fileDownLoad.length">
                <ai-file-list :fileList="fileDownLoad"></ai-file-list>
              </ai-info-item>
            </ai-wrapper>

          </template>
        </ai-card>
      </template>
    </ai-detail>
  </section>
</template>

<script>
import {mapState} from "vuex";
import SelectDeptUser from "./SelectDeptUser";

export default {
  name: 'Add',
  components: {SelectDeptUser},
  props: {
    instance: Function,
    dict: Object
  },
  data() {
    return {
      isShow: false,
      data: {},
      form: {
        areaId: '',
        tag: '',
        messageSource: '2',
        sendTime: '',
        content: '',
        contentType: 'text',
        fileList: [],
        organization: '',
        deptList: [],
      },
      tags: [],
      subTags: {},
      areaId: [],
      areaName: [],
      imgList: [],
      videoList: [],
      filesList: [],
      fileDownLoad: [],
      areaRootId: '',
      users: [],
      images: [],
      imgs: [],
      videos: [],
      videoArr: [],
      files: [],
      flag: false,
      rules: {
        deptList: [{required: true, message: '请选择发送范围'}],
        content: [{required: true, message: '请输入群发内容'}],
        messageSource: [{required: true, message: '请选择群发方式'}],
      },
    }
  },
  computed: {
    ...mapState(['user']),
    tagsChange() {
      if (this.tags?.length) {
        return this.form.tag = this.tags.toString();
      }
    },
    areaid() {
      return this.form.areaId = this.areaId.toString();
    },
  },
  created() {
    if (this.$route.query.id) {
      this.getInfo()
    }
    this.areaRootId = [this.user.info.areaId.substr(0, 6), '000000'].join("")
  },
  filters: {
    format(val) {
      if(val == 1) {
        return '居民'
      } else if(val == 2) {
        return '居民群'
      }
    }
  },
  methods: {
    handleAreaSelect(v) {
      this.areaName = v?.[0]?.label
    },
    sourceChange(val) {
      this.form.messageSource = val
      this.form.deptList = []
    },
    getInfo() {
      this.instance.post(`/app/pushmessage/detail?id=${this.$route.query.id}`).then(res => {
        if (res?.data) {
          this.data = res.data
          this.images = res.data.fileList.filter(e => e.contentType == "image")?.map(i => {
            return {
              url: i.accessUrl
            }
          })
          this.fileDownLoad = res.data.fileList.filter(e => e.contentType == "file")?.map(v => {
            return {
              url: v.accessUrl
            }
          })
          if (res.data.fileList.filter(e => e.contentType == "video")) {
            this.videoArr = res.data.fileList.filter(e => e.contentType == "video")
          }

        }
      })
    },
    confirm() {
      if(this.flag) return

      if(!this.form.deptList.length) {
        return this.$message.error('请选择发送范围')
      }
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.flag = true

          this.form.fileList = []
          let contentList = [{
            content: this.form.content,
            contentType: 'text'
          }]

          if (this.imgList.length) {
            this.imgs = this.imgList.map(item => {
              return {
                contentType: 'image',
                mediaId: item.media.mediaId,
                accessUrl: item.url,
              }
            })
          }

          if (this.videoList.length) {
            this.videos = this.videoList.map(item => {
              return {
                contentType: 'video',
                mediaId: item.media.mediaId,
                accessUrl: item.url,
              }
            })
          }

          if (this.filesList.length) {
            this.files = this.filesList.map(item => {
              return {
                contentType: 'file',
                mediaId: item.media.mediaId,
                accessUrl: item.url,
              }
            })
          } 

          this.form.fileList = [...contentList, ...this.imgs, ...this.videos, ...this.files]

          this.instance.post(`/app/pushmessage/addOrUpdate`, {
            ...this.form
          }).then(res => {
            if (res.code == 0) {
              this.$message.success('提交成功')
              setTimeout(() => {
                this.cancel(true)
              }, 600)
            } else {
              this.flag = false
            }
          }).catch(() => {
            this.flex = false
          })
        }
      })
    },
    cancel(isRefresh) {
      this.$emit('change', {
        type: 'List',
        isRefresh: !!isRefresh
      })
    }
  }
}
</script>

<style scoped lang="scss">
.Add {
  height: 100%;
}

.ai-card__title {
  display: flex;
  align-items: center;

  h2 {
    margin-right: 20px;
    color: #222222;
    font-size: 16px;
    font-weight: 700;
  }

  span {
    color: #888888;
    font-size: 14px;
  }
}

.appletss {
  display: flex;
  flex-wrap: wrap;

  .applets-item {
    display: flex;
    align-items: center;
    width: 400px;
    height: 60px;
    margin-right: 8px;
    margin-bottom: 8px;
    padding: 0 17px;
    background: #FFFFFF;
    border-radius: 2px;
    border: 1px solid #D0D4DC;
    cursor: pointer;

    &:hover {
      border-color: #2266FF;
    }

    ::v-deep {
      .el-radio__label {
        display: none;
      }
    }

    img {
      width: 40px;
      height: 40px;
      margin: 0 8px 0 13px;
    }

    span {
      color: #222222;
      font-size: 12px;
    }

    .el-radio {
      margin: 0;
    }

    &.applets-active {
      border-color: #2266FF;
    }
  }
}

.tips {
  position: relative;
  top: 2px;
  padding-left: 8px;
  color: #222222;
  font-size: 14px;
}

.el-form-item-item__textarea {
  position: relative;
}

.el-form-item__btn {
  position: absolute;
  bottom: 12px;
  left: 12px;
  line-height: 1;
  z-index: 1;
  color: #2266FF;
  font-size: 14px;
  user-select: none;
  background: #fff;
  cursor: pointer;
}

.tags {
  // padding: 10px;
  .tag_title {
    font-weight: 600;
  }

  .tag_item {
    margin: 5px 0;

    span {
      display: inline-block;
      // padding: 5px 10px;
      width: 80px;
      text-align: center;
      height: 30px;
      line-height: 30px;
      background: #DDD;
      color: #333;
      border-radius: 4px;
      margin-right: 8px;
    }

    .active {
      background: #53bcea;
      color: #FFFFFF;
    }
  }
}


</style>
