/* 
  添加信息
  author:zsy
  date:2023-2-17
 */
<template>
  <div class="add-message">
    <el-form
      :model="ruleForm"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
      :rules="rules"
    >
      <el-form-item label="用户名称" prop="uname">
        <el-input v-model="ruleForm.uname"></el-input>
      </el-form-item>

      <el-form-item label="地址" prop="addressid">
        <el-select v-model="ruleForm.addressid" placeholder="请选择">
          <el-option
            v-for="item in addressData"
            :key="item.addressid"
            :label="item.addressname"
            :value="item.addressid"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="等级" prop="levelid">
        <el-select v-model="ruleForm.levelid" placeholder="请选择">
          <el-option
            v-for="item in levelData"
            :key="item.jrid"
            :label="item.levelname"
            :value="item.jrid"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="电话号码" prop="tel">
        <el-input v-model="ruleForm.tel"></el-input>
      </el-form-item>

      <!-- <el-form-item label="添加头像" prop="tcoin">
        <el-upload
          class="avatar-uploader"
          action="https://39.105.160.25:4000/getMessageA"
          :on-success="handleAvatarSuccess"
          name="sfile"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item> -->

      <el-form-item>
        <el-button type="primary" @click="updateForm('ruleForm')" v-if="isUpdate"
          >修改</el-button
        >
        <el-button type="primary" @click="submitForm('ruleForm')" v-else
          >提交</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getLocation } from "@/api/location";
import { getLevel } from "@/api/level";
import {addMessage,showMessage,upMessage} from '@/api/message'
export default {
  data() {
    return {
      isUpdate:false,
      ruleForm: {
        uname: "",
        addressid: "",
        leveid: "",
        tel: "",
      },
      rules: {},
      addressData: [],
      levelData: [],
    };
  },
  methods: {
    //获取地址列表
    getLocationList() {
      getLocation().then((res) => {
        this.addressData = res.data;
        console.log(this.addressData);
      });
    },
    //获取等级列表
    getLevelList() {
      getLevel().then((res) => {
        this.levelData = res.data;
        console.log(this.levelData);
      });
    },
    // //图片上传成功的事件
    // handleAvatarSuccess(response) {
    //   console.log(this.response)
    //   // this.imageUrl = URL.createObjectURL(file.raw);
    // },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addMessage({
            uname: this.ruleForm.uname,
            addressid: this.ruleForm.addressid,
            levelid: this.ruleForm.levelid,
            tel: this.ruleForm.tel,
          })
            .then((res) => {
              this.$message({
                type: "success",
                message: res.message,
              });
              this.$refs[formName].resetFields();
              this.$router.push("/message/messageList");
            })
            .catch((err) => {
              console.log(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    //根据id获取信息
    showMessageList(id){
      console.log(id)
      showMessage({
        id: id
      }).then((res)=>{
        console.log(res.data[0])
        // this.ruleForm = res.data[0]
        let _data = res.data[0]
        this.ruleForm = {
          uname: _data.jrname,
          addressid: _data.jraddressesid,
          levelid: _data.jrlevel,
          tel: _data.jrtel
        }
      }).catch((err)=>{
        console.log(err)
      })
    },
    //修改信息
    updateForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          upMessage({
            id: this.$route.query.id,
            uname: this.ruleForm.uname,
            addressid: this.ruleForm.addressid,
            levelid: this.ruleForm.levelid,
            tel: this.ruleForm.tel,
          })
            .then((res) => {
              this.$message({
                type: "success",
                message: res.message,
              });
              this.$refs[formName].resetFields();
              this.$router.push("/message/messageList");
            })
            .catch((err) => {
              console.log(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  },
  created() {
    this.getLocationList();
    this.getLevelList();
    this.showMessageList()
    // console.log(this.$route.query.id)
    if(this.$route.query.id){
      let _id = this.$route.query.id
      this.showMessageList(_id),
      this.isUpdate = true
    }
  },
};
</script>

<style>
</style>