<template>
<div>
  <div class="app-container">
    <el-form  ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="收件人" prop="receiverName">
        <el-input v-model="receiverName" placeholder="请选择收件人" clearable="true">
          <template slot="append">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleSelect"
            >选择人员</el-button>
          </template>
        </el-input>
        <el-input v-model="receiverAccount" v-show="false"/>
      </el-form-item>
      <el-form-item label="主题" prop="mailTheme">
        <el-input v-model="form.mailTheme" placeholder="请输入邮件主题" />
      </el-form-item>
      <el-form-item label="附件">
        <el-upload
          class="upload-file"
          :before-upload="handleBefore"
          :on-preview="handlePreview"
          :on-success="handleSuccess"
          :on-remove="handleRemove"
          :show-file-list="true"
          :file-list="mail.oldFileList"
          :action=actionUp
          multiple
        >
          <el-button
            class="filter-item"
            type="primary"
            icon="el-icon-upload"
            size="mini"
          >上传</el-button>
        </el-upload>
        <ul v-show="!!mail.oldFileList.length" class="old-file-list"></ul>
      </el-form-item>
      <el-form-item label="邮件内容" prop="mailContent" style="height: 250px;">
        <Editor v-model="form.mailContent" />
      </el-form-item>
      <el-input v-model="status" v-show="false"/>
      <el-form-item style="text-align:center;">
        <el-button type="primary" @click="submitForm(1)">发 送</el-button>
        <el-button @click="submitForm(0)">保存草稿</el-button>
      </el-form-item>
    </el-form>
    <!-- 选择人员对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="ryform" :model="ryform" label-width="80px">
        <el-input v-model="filterText" placeholder="请输入姓名或账号按回车进行搜索" suffix-icon="el-icon-search"/>
        <el-tree
          :data="deptOptions"
          :props="defaultProps"
          :expand-on-click-node="false"
          :filter-node-method="filterNode"
          ref="tree"
          default-expand-all
          @node-click="handleNodeClick"
          show-checkbox
        />
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitRen">选 定</el-button>
      </div>
    </el-dialog>
  </div>
</div>
</template>

<script>
  import { listInfo, getInfo, delInfo, addInfo, updateInfo, exportInfo,selectRen,treeselect as deptTreeselect } from "@/api/system/email/send";
  import Editor from '@/components/Editor';
  import Treeselect from "@riophae/vue-treeselect";
  export default {
    name: 'replyinfo',
    components: {
      Editor,
      Treeselect
    },
    data() {
      return {

        // 遮罩层
        loading: true,
        // 【请填写功能名称】表格数据
        infoList: [],
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          mailTheme: [
            { required: true, message: "主题不能为空", trigger: "blur" }
          ]
        },
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        options1: "",
        deptOptions: undefined,
        receiverName: "", // 收件人的昵称
        receiverAccount: "", // 收件人的账号
        filterText: '',  //人员选择器中的 筛选
        mail: {
          oldFileList: [],
          oldAudioList: [],
          content: '',
          target: [],
          copy: [],
          fileList: [],
          audioList: []
        },
        actionUp:process.env.VUE_APP_BASE_API +'/system/email/info/uploadfile',//接口地址
      };
    },
    created() {
      //this.getList();
      this.getDeptTreeselect();
      this.info();
    },
    methods: {

      info(){
        // console.log(this.$route.params)
        // console.log(senderName);
        // this.$route.params
        // console.log(this.$route.query.senderName[0])
        // this.username = this.$route.params;
        // this.receiverName = this.$route.params
        console.log(this.$route.query.id);
          // console.log(this.$route.params);
          this.receiverName = this.$route.query.id
      },


      // 人员选择器----取消按钮
      cancel() {
        this.open = false;
        this.filterText='';
      },
      // 表单重置
      reset() {
        this.form = {
          mailGuid: undefined,
          receiverName: undefined,
          receiverAccount: undefined,
          mailTheme: undefined,
          mailContent: undefined,
          status: "0",
        };
        this.receiverName='';
        this.receiverAccount='';
        this.resetForm("form");
      },
      /** 写信==发送status=1、保存草稿status=0按钮 */
      submitForm: function(status) {
        this.form.status = status;
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.form.receiverAccount = this.receiverAccount;
            this.form.receiverName = this.receiverName;
            if(status==1){
              if (this.form.receiverName.length < 1 || this.form.receiverAccount.length < 1) {
                this.$message({
                  showClose: true,
                  message: '收件人不能为空',
                  type: 'warning',
                  duration: 1200
                });
                return;
              }
            }
            if (this.form.mailGuid != undefined) {
              updateInfo(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  //this.getList();
                }
              });
            } else {
              addInfo(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.reset();
                }
              });
            }
          }
        });
      },
      /** 加载修改树结构json */
      findZNodes: function () {
        selectRen(this.queryParams).then(response => {
          this.options1 = response.data;
        });
      },
      /** 查询部门树结构 */
      getDeptTreeselect() {
        deptTreeselect().then(response => {
          this.deptOptions = response.data;
        });
      },
      /** 写信中的 选择人员 按钮弹窗操作 */
      handleSelect() {
        this.open = true;
        this.title = "选择收件人";
      },
      /** 人员选择器中的 选定按钮操作 */
      submitRen(){
        this.receiverName ='冯瑞';
        this.receiverAccount='fengrui';
        this.open = false;
      },
      filterNode(value, data) {
        if (!value) return true;
        return data.label.indexOf(value) !== -1;
      },
      handleBefore(file) {
        // 上传前可以对要上传文件进行控制
        if (file.type.indexOf("image/") == -1) {
          this.msgError("文件格式错误，请上传图片类型,如：JPG，PNG后缀的文件。");
        }
      },
      handleSuccess(file, fileInfo) {
        this.mail.fileList.push({
          name: fileInfo.name,
          uuid: fileInfo.uid,
          url: fileInfo.url
        });
      },
      handlePreview(file) {
        // 预览,一些文件因为格式问题无法预览。推荐使用a标签，src为文件的下载地址，点击即可下载,参照录音
        window.open(file.url);
      },
      handleRemove(file) {
        // 移除
        this.mail.fileList.forEach((item, index) => {
          if (item.uuid === file.uid) {
            this.mail.fileList.splice(index, 1);
          }
        });
      }
    },
    watch: {
      /** 人员选择器中的 筛选 */
      filterText(val) {
        this.$refs.tree.filter(val);
      }
    }
  }
</script>

<style lang="less" scoped>

  .el-input-group__append, .el-input-group__prepend{
    background: #188efd !important;
    color: #fff !important;
  }


</style>
