<template>
  <div class="createPost-container">
    <el-form ref="postForm" :model="postForm" :rules="rules" class="form-container">
      <sticky :z-index="10" class-name="sub-navbar draft">
        <CommentDropdown v-show="dialogEmailTypeVisible" v-model="postForm.mailtype" />
        <el-button style="margin-left: 10px;" type="success" @click="dialogStatus==='create'?submitForm():updateFrom()">
          发布
        </el-button>
      </sticky>
      <div class="createPost-main-container">
        <el-row>
          <el-col :span="10">
            <el-form-item label-width="120px" label="发送时间:" class="postInfo-container-item" prop="sendtime">
              <el-date-picker v-model.number="postForm.sendtime" type="datetime" format="yyyy-MM-dd HH:mm:ss" value-format="timestamp" placeholder="Select date and time" required />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label-width="120px" label="过期时间:" class="postInfo-container-item" prop="expiretime">
              <el-date-picker v-model.number="postForm.expiretime" type="datetime" format="yyyy-MM-dd HH:mm:ss" value-format="timestamp" placeholder="Select date and time" required />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item style="margin-bottom: 40px;" prop="subject">
              <MDinput v-model="postForm.subject" :maxlength="100" name="subject" required>
                邮件标题
              </MDinput>
            </el-form-item>
            <el-form-item prop="content" style="margin-bottom: 30px;" label="邮件正文:">
              <el-input v-model="postForm.content" type="textarea" :rows="6" />
            </el-form-item>
            <el-form-item style="margin-bottom: 30px;" label="附件:">
              <el-input v-model="postForm.attachments" type="textarea" :rows="6" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item style="margin-bottom: 40px;" prop="to">
              <MDinput v-show="personalMailVisible" v-model="postForm.to" :maxlength="100" name="to" required>
                收件人(account_id)
              </MDinput>
            </el-form-item>
            <el-form-item v-show="groupMailVisible" style="margin-bottom: 40px;" prop="to" label="收件人列表:">
              <el-input v-model="postForm.to" type="textarea" :rows="6" required />
              </el-input>
            </el-form-item>
            <div class="postInfo-container">
              <el-row>
                <el-col :span="30">
                  <el-form-item v-show="dialogUserTypeVisible" label="群发邮件用户类型(个人时忽略）:" class="postInfo-container-item">
                    <el-radio-group v-model="postForm.usertype">
                      <el-radio :label="0">全体用户</el-radio>
                      <el-radio :label="1">only邮件发送时间之前注册的用户</el-radio>
                      <el-radio :label="2">only邮件发送之后注册的用户</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </el-col>

              </el-row>
            </div>
          </el-col>
        </el-row>

      </div>
    </el-form>
  </div>
</template>

<script>
import { CommentDropdown } from '@/views/example/components/Dropdown'
import Sticky from '@/components/Sticky/index.vue'
import MDinput from '@/components/MDinput/index.vue'
import { addMail, updateEmail } from '@/api/mail'
import { parseTime } from '@/utils'
import { MAIL_TYPE_PERSONAL, MAIL_TYPE_GROUP, MAIL_TYPE_ALL } from '@/constant/constant'

export default {
  name: 'Email',
  components: { MDinput, CommentDropdown, Sticky },
  data() {
    return {
      postForm: {
        mailtype: MAIL_TYPE_PERSONAL,
        sendtime: '',
        expiretime: '',
        subject: '',
        to: '',
        from: '',
        usertype: 0,
        content: '',
        attachments: ''
      },
      rules: {
        sendtime: [{ required: true, message: '请选择发送时间', trigger: 'blur' }],
        expiretime: [{ required: true, message: '请选择过期时间', trigger: 'blur' }],
        subject: [{ required: true, message: 'subject is required', trigger: 'blur' }],
        content: [{ required: true, message: 'content is required', trigger: 'blur' }]
      },
      dialogUserTypeVisible: false,
      personalMailVisible: true,
      dialogEmailTypeVisible: true,
      dialogStatus: 'create'
    }
  },
  watch: {
    'postForm.mailtype'(val) {
      switch (val) {
        case MAIL_TYPE_PERSONAL:
        {
            this.personalMailVisible = true
            this.groupMailVisible = false

            this.dialogUserTypeVisible = false
          }
          break
        case MAIL_TYPE_GROUP:
        {
            this.personalMailVisible = false
            this.groupMailVisible = true

            this.dialogUserTypeVisible = true
          }
          break
        case MAIL_TYPE_ALL:
        {
            this.personalMailVisible = false
            this.groupMailVisible = false

            this.dialogUserTypeVisible = true
          }
          break
      }
      /*
      if (this.dialogStatus === 'create' && val === 2) {
      }*/
    }
  },
  created() {
    const mailid = this.$route.params.mailid
    if (mailid) {
      this.dialogUserTypeVisible = false
      this.personalMailVisible = false
      this.dialogEmailTypeVisible = false
      this.dialogStatus = 'update'
      const editEmail = JSON.parse(sessionStorage.getItem('editEmail' + mailid))
      this.postForm = editEmail
      let str = ''
      editEmail.attachments.forEach((item) => {
        str += item['itemid'] + ':' + item['itemnum'] + '|'
      })
      this.postForm.attachments = str.slice(0, -1)
      this.postForm.sendtime = editEmail.sendtime * 1000
      this.postForm.expiretime = editEmail.expiretime * 1000
    } else {
      this.dialogStatus = 'create'
      sessionStorage.removeItem('editEmail' + mailid)
    }
  },
  methods: {
    parseTime,
    submitForm() {
      const data = JSON.parse(JSON.stringify(this.postForm))
      data.sendtime = data.sendtime / 1000
      data.expiretime = data.expiretime / 1000
      this.$refs['postForm'].validate((valid) => {
        if (valid) {
          addMail(data).then(response => {
            if (response.code === 0) {
              this.$message({
                message: '发布成功',
                type: 'success',
                duration: 1200,
                onClose: () => {
                  this.$router.push('index')
                }
              })
            }
          })
        }
      })
    },
    updateFrom() {
      const data = JSON.parse(JSON.stringify(this.postForm))
      data.sendtime = data.sendtime / 1000
      data.expiretime = data.expiretime / 1000
      this.$refs['postForm'].validate((valid) => {
        if (valid) {
          updateEmail(data).then(response => {
            if (response.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1200,
                onClose: () => {
                  sessionStorage.removeItem('editEmail' + data.mailid)
                  this.$router.replace('/mail/index')
                }
              })
            }
          })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
@import "~@/styles/mixin.scss";

.createPost-container {
  position: relative;

  .createPost-main-container {
    padding: 40px 45px 20px 50px;

    .postInfo-container {
      position: relative;
      @include clearfix;
      margin-bottom: 10px;

      .postInfo-container-item {
        float: left;
      }
    }
  }

  .word-counter {
    width: 40px;
    position: absolute;
    right: 10px;
    top: 0px;
  }
}

.article-textarea ::v-deep {
  textarea {
    padding-right: 40px;
    resize: none;
    border: none;
    border-radius: 0px;
    border-bottom: 1px solid #bfcbd9;
  }
}

</style>
