<template>
  <ai-detail>
    <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>
            <span>*一个成员如果被设置了多个欢迎语，将会使用最新设置或修改的欢迎语</span>
          </div>
        </template>

        <template #content>
          <el-form class="ai-form" :rules="rules" ref="userForm" :model="form" label-width="100px" label-position="right">
            <el-form-item label="使用成员" prop="users" style="width: 100%">
              <el-input size="small" placeholder="请选择..." disabled v-model="users">
                <ai-wechat-selecter slot="append" :instance="instance" v-model="form.users">
                  <el-button type="info">选择</el-button>
                </ai-wechat-selecter>
              </el-input>
            </el-form-item>
          </el-form>
        </template>
      </ai-card>

      <ai-card title="发送欢迎语">
        <template #content>
          <el-form class="ai-form" ref="form" :model="form" label-width="110px" label-position="right">
            <el-form-item class="el-form-item__textarea" label="文本内容" prop="explain" style="width: 100%">
              <span @click="insertNickname" class="el-form-item__btn" type="text">[插入居民昵称]</span>
              <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="explain" style="width: 100%">
              <el-radio-group v-model="form.type" @change="onTypeChange">
                <el-radio label="image">图片</el-radio>
                <el-radio label="link">链接</el-radio>
                <el-radio label="video">视频</el-radio>
                <el-radio label="miniapp">小程序</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="图片" prop="files" style="width: 100%" v-if="form.type === 'image'">
              <ai-uploader :instance="instance" isWechat v-model="form.files" :limit="1" url="/app/wxcp/upload/uploadFile?type=image"></ai-uploader>
            </el-form-item>
            <el-form-item label="链接" prop="linkUrl" style="width: 100%" v-if="form.type === 'link'">
              <el-input placeholder="链接地址请以http或https开头" :rows="2" type="textarea" v-model="form.linkUrl"></el-input>
            </el-form-item>
            <el-form-item label="链接图片地址" prop="picUrl" style="width: 100%" v-if="form.type === 'link'">
              <ai-uploader :instance="instance" v-model="form.picUrl" :limit="1"></ai-uploader>
            </el-form-item>
            <el-form-item label="链接标题" prop="title" style="width: 100%" v-if="form.type === 'link'">
              <el-input placeholder="请输入链接标题" v-model="form.title"></el-input>
            </el-form-item>
            <el-form-item label="视频" prop="files" style="width: 100%" v-if="form.type === 'video'">
              <ai-uploader :instance="instance" fileType="file" isWechat acceptType=".mp4" v-model="form.files" :limit="1" url="/app/wxcp/upload/uploadFile?type=video"></ai-uploader>
            </el-form-item>
            <!-- <el-form-item label="小程序" prop="applets" style="width: 100%" v-if="form.type === 'miniapp'">
              <el-radio-group v-model="form.applets">
                <div class="appletss">
                  <div class="applets-item" @click="form.applets = '0'" :class="[form.applets === '0' ? 'applets-active' : '']">
                    <el-radio label="0"></el-radio>
                    <img src="http://www.9665.com/uploadfile/2018/0607/20180607042142312.png">
                    <span>小程序</span>
                  </div>
                  <div class="applets-item" @click="form.applets = '1'" :class="[form.applets === '1' ? 'applets-active' : '']">
                    <el-radio label="1"></el-radio>
                    <img src="http://www.9665.com/uploadfile/2018/0607/20180607042142312.png">
                    <span>小程序</span>
                  </div>
                  <div class="applets-item" @click="form.applets = '2'" :class="[form.applets === '2' ? 'applets-active' : '']">
                    <el-radio label="2"></el-radio>
                    <img src="http://www.9665.com/uploadfile/2018/0607/20180607042142312.png">
                    <span>小程序</span>
                  </div>
                </div>
              </el-radio-group>
            </el-form-item> -->
            <el-form-item label="小程序标题" prop="title" style="width: 100%" v-if="form.type === 'miniapp'">
              <el-input placeholder="请输入小程序标题" v-model="form.title"></el-input>
            </el-form-item>
            <el-form-item label="小程序APPID" prop="appid" style="width: 100%" v-if="form.type === 'miniapp'">
              <el-input placeholder="请输入小程序APPID" v-model="form.appid"></el-input>
            </el-form-item>
            <el-form-item label="小程序跳转页面" prop="page" style="width: 100%" v-if="form.type === 'miniapp'">
              <el-input placeholder="如pages/home/Home" v-model="form.page"></el-input>
            </el-form-item>
            <el-form-item label="小程序图片" prop="files" style="width: 100%" v-if="form.type === 'miniapp'">
              <ai-uploader :instance="instance" v-model="form.files" isWechat :limit="1" url="/app/wxcp/upload/uploadFile?type=image"></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>
</template>

<script>
  const validateUser = (rule, value, callback) => {
    if (!value.length) {
      callback(new Error('请选择使用成员'))
    } else {
      callback()
    }
  }
  export default {
    name: 'Add',

    props: {
      instance: Function,
      dict: Object,
      params: Object
    },

    data () {
      return {
        isShow: false,
        info: {},
        form: {
          users: [],
          appId: '',
          page: '',
          title: '',
          miniappImg: [],
          picUrl: [],
          content: '',
          files: [],
          linkUrl: '',
          isRemind: true,
          type: 'text'
        },
        rules: {
          users: [
            { required: true, message: '请选择使用成员', trigger: 'change' },
            { validator: validateUser, trigger: 'change' }
          ]
        }
      }
    },

    computed: {
      users () {
        return this.form.users.map(v => v.name).join(',')
      }
    },

    created () {
    },

    methods: {
      getInfo (id) {
        this.instance.post(`/app/appleavemessage/queryDetailById?id=${id}`).then(res => {
          if (res.code === 0) {
            this.info = res.data
            this.info.appLeaveMessageReplyList = res.data.appLeaveMessageReplyList.map(item => {
              item.images = JSON.parse(item.images).map(item => {
                return {
                  ...item,
                  url: item.accessUrl
                }
              })

              return item
            })
          }
        })
      },

      validateUser (rule, value, callback) {
        if (!value.length) {
          callback(new Error('请选择使用成员'))
        } else {
          callback()
        }
      },

      onClose () {
        this.form.explain = ''
      },

      onTypeChange () {
        this.form.files = []
        this.form.picUrl = []
        this.form.appId = ''
        this.form.page = ''
        this.form.title = ''
        this.form.miniappImg = []
        this.form.linkUrl = ''
      },

      insertNickname () {
        this.form.content = this.form.content + '[用户昵称]'
      },

      onChange () {

      },

      confirm () {
        this.$refs.userForm.validate((valid) => {
          if (valid) {
            if (this.form.type === 'text' && !this.form.content) {
              return this.$message.error('请输入消息内容')
            }

            if (this.form.type === 'image' && !this.form.files.length) {
              return this.$message.error('请上传图片')
            }
            // if (this.form.type === 'file' && !this.form.files.length) {
            //   return this.$message.error('请上传附件')
            // }
            if (this.form.type === 'video' && !this.form.files.length) {
              return this.$message.error('请上传视频')
            }
            if (this.form.type === 'link' && !this.form.linkUrl) {
              return this.$message.error('请输入链接')
            }
            if (this.form.type === 'link' && !this.form.picUrl.length) {
              return this.$message.error('请输入链接图片')
            }
            if (this.form.type === 'link' && !this.form.title) {
              return this.$message.error('请输入链接标题')
            }
            if (this.form.type === 'miniapp' && !this.form.title) {
              return this.$message.error('请输入小程序标题')
            }
            if (this.form.type === 'miniapp' && !this.form.appid) {
              return this.$message.error('请输入小程序appid')
            }
            if (this.form.type === 'miniapp' && !this.form.page) {
              return this.$message.error('请输入小程序page')
            }
            if (this.form.type === 'miniapp' && !this.form.files.length) {
              return this.$message.error('请上传小程序图片')
            }

            this.instance.post(`/app/wxcp/wxwelcomeword/add`, {
              type: '1',
              content: this.form.content || '',
              isNotify: '0',
              media: {
                createdAt: this.form.files.length ? this.form.files[0].media.createdAt : '',
                file: this.form.files.length ? this.form.files[0] : {},
                mediaId: this.form.files.length ? this.form.files[0].media.mediaId : '',
                sysFileId: this.form.files.length ? this.form.files[0].id : '',
                type: this.form.type,
                linkUrl: this.form.type === 'link' && this.form.linkUrl ? this.form.linkUrl : '',
                title: this.form.title,
                appId: this.form.appid,
                page: this.form.page,
                picUrl: this.form.type === 'link' ? this.form.picUrl[0].url : ''
              },
              users: this.form.users.map(item => {
                return {
                  type: 0,
                  objectId: item.id,
                  objectName: item.name
                }
              })
            }).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">
  .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;
    }
</style>
