<template>
  <div class="add-message">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名称" prop="uname">
        <el-input v-model="ruleForm.uname"></el-input>
      </el-form-item>
      <el-form-item label="地址ID" 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="等级ID" 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>
        <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 { getLevel } from "@/api/level";
import { getLocation } from "@/api/location";
import { addMessage,showMessage,updateMessage} from "@/api/message";
export default {
  data() {
    return {
      ruleForm: {
        uname: "",
        addressid: "",
        levelid: "",
        tel: "",
      },
      rules: {
        uname: [{ required: true, message: "请输入用户名称", trigger: "blur" }],
        addressid: [
          { required: true, message: "请选择地址", trigger: "change" },
        ],
        levelid: [{ required: true, message: "请选择等级", trigger: "change" }],
        tel: [{ required: true, message: "请输入电话号码", trigger: "blur" }],
      },
      addressData: [],
      levelData: [],
    };
  },
  methods: {
    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({
                message: "恭喜你，添加成功",
                type: "success",
              });
              this.$refs[formName].resetFields();
              this.$router.push('/message/messageList')
            })
            .catch((err) => {
              this.$message.error('错了哦，添加失败');
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    //获取地址列表
    getLocationList() {
      getLocation().then((res) => {
        this.addressData = res.data;
      });
    },
    //获取等级列表
    getLevelList() {
      getLevel().then((res) => {
        this.levelData = res.data;
      });
    },
    //显示数据
    showMessageList(id){
      console.log(id)
      showMessage({
        id: id
      }).then((res)=>{
        console.log(res)
        // this.ruleForm = res.data[0]
        this.ruleForm = {
          uname: res.data[0].jrname,
          addressid: res.data[0].jraddressesid,
          levelid: res.data[0].jrlevel,
          tel: res.data[0].jrtel
        }
      })
    },
    //修改
    updateForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          updateMessage({
            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({
                message: "恭喜你，修改成功",
                type: "success",
              });
              this.$refs[formName].resetFields();
              this.$router.push('/message/messageList')
            })
            .catch((err) => {
              this.$message.error('错了哦，修改失败');
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
  created() {
    this.getLocationList();
    this.getLevelList();
    
    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 scoped>
.add-message {
  width: 70%;
  margin: 120px auto;
}
</style>