<template>
  <div class="app-container" :style="{height:height+'px'}">
    <van-nav-bar
      :title="pageTit"
      left-arrow
      fixed
      placeholder
      @click-left="onClickLeft"
    />
    <van-form @submit="submitInteraction">
      <van-field v-model="form.title" label="标题" placeholder="请输入标题" required :rules="[{ required:true }]"/>
      <van-field v-model="form.realname" label="姓名" placeholder="请输入姓名" required :rules="[{ required:true }]"/>
      <van-field v-model="form.phone" label="联系电话" placeholder="请输入联系电话" required :rules="[{ required:true }]"/>
      <van-field v-model="form.content" label="咨询内容" type="textarea" autosize placeholder="请输入咨询内容" required :rules="[{ required:true }]"/>
      <div class="submit">
        <van-button round block type="info" color="#007E72" native-type="submit">发布</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { getMember , userCommunicate , updateCommunicate , selectCommunicate } from "@/api/user/index";
import { getInfo } from "@/api/login/index";
import {Dialog} from "vant";
  export default {
    name: "interactionAdd",
    data() {
      return {
        height:0,
        pageTit:'',
        form:{
          title:'',
          realname:'',
          phone:'',
          content:'',
          logintime:'',
          deptId:100,
        }
      };
    },
    created() {
      this.height = document.body.clientHeight;
      console.log(this.$route.query.type)
      if (this.$route.query.type == 'update'){
        this.getInteraction();
        this.pageTit = '修改咨询';
      }else{
        this.getInfo();
        this.pageTit = '发布咨询';
      }
    },
    methods: {
      getInfo(){
        getInfo().then(response => {
          getMember(response.user.userId).then(response => {
            this.form.realname = response.data.realname;
            this.form.phone = response.data.phone;
            this.form.memberId = response.data.id;
          });
        });
      },
      getInteraction(){
        selectCommunicate(this.$route.query.id).then(response => {
            this.form.content = response.data.content;
            this.form.title = response.data.title;
            this.form.realname = response.data.realname;
            this.form.phone = response.data.phone;
            this.form.id = this.$route.query.id;
        });
      },
      submitInteraction(){
        this.form.logintime = this.getNowFormatDate();
        if(this.$route.query.type == 'update'){
          updateCommunicate(this.form).then(response => {
            if (response.code == 200){
              Dialog.confirm({
                title: '系统提示',
                message: '修改成功',
                confirmButtonText: '确定',
                showCancelButton:false
              }).then(() => {
                self.location=document.referrer;
              })
            }
          });
        }else{
          userCommunicate(this.form).then(response => {
            if (response.code == 200){
              Dialog.confirm({
                title: '系统提示',
                message: '发布成功',
                confirmButtonText: '确定',
                showCancelButton:false
              }).then(() => {
                self.location=document.referrer;
              })
            }
          });
        }

      },

    },
  };
</script>

<style scoped lang="scss">
  .app-container {
    background-color: #FFF;
  }
  .submit{
    position: absolute;
    bottom: 5%;
    width: 90%;
    left: 5%;
    p{
      text-align: center;
      margin-bottom: 0.2rem;
      span{
        color: #007E72;
      }
    }
  }
</style>
