<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 class="ai-form" :rules="rules" ref="form" :model="form" label-width="100px" label-position="right">

              <el-form-item label="发送方式" prop="sendType" style="width: 50%">
                <el-radio v-model="form.sendType" label="0">居民群</el-radio>
                <el-radio v-model="form.sendType" label="1">居民</el-radio>
              </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="name" style="width: 100%">
                <el-input size="small" v-model="form.name" placeholder="请选择" disabled>
                  <!-- :isMultiple="true" -->
                  <ai-wechat-selecter slot="append" refs="addTags" :instance="instance" v-model="users" @change="getSelectPerson">
                    <el-button size="small" type="info">选择成员</el-button>
                  </ai-wechat-selecter>
                </el-input>
              </el-form-item>

              <el-form-item label="标签选择" style="width: 100%" v-if="form.sendType == 0">
                <div class="tags" v-for="items in subTags" :key="items.id">
                  <div class="tag_title">{{ items.name }}</div>
                  <div class="tag_item">
                    <el-checkbox-group v-model="tags">
                      <el-checkbox v-for="item in items.tagList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
                    </el-checkbox-group>
                  </div>
                </div>
              </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="群发方式" prop="sendType"  style="width: 50%">
                <el-radio v-model="form.sendType" label="0">立即发送</el-radio>
                <el-radio v-model="form.sendType" label="1">定时发送</el-radio>
              </el-form-item> -->

              <!-- <el-form-item label="群发时间" prop="sendTime"  style="width: 50%" v-if="form.sendType == 1">
                <el-date-picker size="small"
                  v-model="form.sendTime"
                  type="datetime"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  placeholder="选择日期时间">
                </el-date-picker>
              </el-form-item> -->
              <el-form-item label="其他类型" prop="explain" style="width: 100%">
                <el-radio-group v-model="form.contentType" @change="onTypeChange">
                  <el-radio label="image">图片</el-radio>
                  <el-radio label="video">视频</el-radio>
                  <el-radio label="file">附件</el-radio>
                </el-radio-group>
              </el-form-item>
              <!-- 图片 -->
              <el-form-item label="图片" style="width: 100%" v-if="form.contentType == 'image'">
                <ai-uploader :instance="instance" isWechat v-model="imgList" acceptType=".jpg,.png,.jpeg" :limit="1"
                             url="/app/wxcp/upload/uploadFile?type=image"></ai-uploader>
              </el-form-item>
              <!-- 视频 -->
              <el-form-item label="视频" style="width: 100%" v-if="form.contentType == 'video'">
                <ai-uploader :instance="instance" fileType="file" isWechat acceptType=".mp4" v-model="videoList" :limit="1"
                             url="/app/wxcp/upload/uploadFile?type=video"></ai-uploader>
              </el-form-item>
              <!-- 附件 -->
              <el-form-item label="附件" style="width: 100%" v-if="form.contentType == 'file'">
                <ai-uploader :instance="instance" fileType="file" isWechat v-model="filesList" :limit="1"
                             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.sendType" isLine>{{ data.sendType | format }}</ai-info-item>
              <ai-info-item label="发送时间" :value="data.sendTime" isLine v-if="data.sendType == 1"></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 v-for="item in data.fileList" :key="item.id">
              <ai-info-item label="图片" isLine v-if="item.contentType == 'image'">
                <ai-uploader v-model="images" :instance="instance" :limit="1" disabled/>
              </ai-info-item>
            </ai-wrapper>

            <ai-wrapper v-for="item in data.fileList" :key="item.id">
              <ai-info-item label="视频" isLine v-if="item.contentType == 'video'">
                <video :src="item.accessUrl" style="width: 100%; height:100%; object-fit: fill;" muted controls="controls"></video>
              </ai-info-item>
            </ai-wrapper>

            <ai-wrapper v-for="item in data.fileList" :key="item.id">
              <ai-info-item label="附件" isLine v-if="item.contentType == 'file'">
                <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";

export default {
  name: 'Add',
  props: {
    instance: Function,
    dict: Object
  },
  data() {
    return {
      isShow: false,
      data: {},
      form: {
        areaId: '',
        tag: '',
        sendType: '0',
        sendTime: '',
        content: '',
        contentType: 'text',
        fileList: [
          {
            accessAppid: "",
            accessDesc: "",
            accessImgurl: "",
            accessTitle: "",
            accessUrl: "",
            content: "",
            contentType: "",
            createdA: 0,
            mediaId: ""
          }
        ],
      },
      tags: [],
      subTags: {},
      areaId: [],
      areaName: [],
      imgList: [],
      videoList: [],
      filesList: [],
      fileDownLoad: [],
      areaRootId: '',
      users: [],
      images: [],
      rules: {
        content: [{required: true, message: '请输入群发内容'}],
        sendType: [{required: true, message: '请选择群发方式'}],
        sendTime: [{required: true, message: '请选择群发时间'}],
      },
    }
  },

  computed: {
    tagsChange() {
      if (this.tags.length) {
        return this.form.tag = this.tags.toString();
      }
    },
    areaid() {
      return this.form.areaId = this.areaId.toString();
    },
    ...mapState(['user'])
  },

  created() {
    if (this.$route.query.id) {
      this.getInfo()
    } else {
      this.getSubTags()
    }
    this.areaRootId = [this.user.info.areaId.substr(0, 6), '000000'].join("")
  },

  filters: {
    format(value) {
      if (value == 0) {
        return '立即发送'
      } else if (value == 1) {
        return '定时发送'
      }
    }
  },

  methods: {
    handleAreaSelect(v) {
      this.areaName = v?.[0]?.label
    },
    getSelectPerson(val) {
      this.form.name = val[0].name;
      this.form.phone = val[0].phone;
      this.form.userId = val[0].sysUserId
      this.form.wxUserId = val[0].id
    },
    // 标签
    getSubTags() {
      this.instance.post(`/app/wxcp/wxcorptag/listAll`).then(res => {
        if (res?.data) {
          this.subTags = res.data.records
        }
      })
    },
    getInfo() {
      this.instance.post(`/app/pushmessage/detail?id=${this.$route.query.id}`).then(res => {
        if (res?.data) {
          this.data = res.data
          this.images = [{url: res.data.fileList.filter(e => e.contentType == "image")[0]?.accessUrl}]
          this.fileDownLoad = [{url: res.data.fileList.filter(e => e.contentType == "file")[0]?.accessUrl}]
        }
      })
    },
    confirm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.form.fileList = []
          let contentList = {
            content: this.form.content,
            contentType: 'text'
          }
          this.form.fileList.push(contentList)

          if (this.imgList.length) {
            let item = {
              contentType: 'image',
              mediaId: this.imgList[0].media.mediaId,
              accessUrl: this.imgList[0].url
            }
            this.form.fileList.push(item)
          }

          if (this.videoList.length) {
            let item = {
              contentType: 'video',
              mediaId: this.videoList[0].media.mediaId,
              accessUrl: this.videoList[0].url
            }
            this.form.fileList.push(item)
          }

          if (this.filesList.length) {
            let item = {
              contentType: 'file',
              mediaId: this.filesList[0].media.mediaId,
              accessUrl: this.filesList[0].url
            }
            this.form.fileList.push(item)
          }

          this.instance.post(`/app/pushmessage/addOrUpdate`, {
            ...this.form
          }).then(res => {
            if (res.code == 0) {
              this.$message.success('提交成功')
              setTimeout(() => {
                this.cancel(true)
              }, 600)
            }
          })
        }
      })
    },
    cancel(isRefresh) {
      this.$emit('change', {
        type: 'List',
        isRefresh: isRefresh ? true : false
      })
    }
  }
}
</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>
