<template>
  <el-dialog
    class="addFavorDialog"
    :title="title"
    :visible.sync="isShow"
    :close-on-click-modal="false"
    width="470px"
    @close="handleClose"
  >
    <el-form
      label-position="left"
      ref="formInfo"
      :rules="rules"
      :model="formdata"
    >
      <el-form-item label="一级收藏夹名称：" prop="name">
        <el-select v-model="formdata.favorName" placeholder="请选择一级收藏夹">
          <el-option
            v-for="items in favorOptions"
            :key="items.id"
            :label="items.name"
            :value="items.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级收藏夹名称：" prop="name">
        <el-input v-model="formdata.name" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="isShow = false">取 消</el-button>
      <el-button type="primary" @click="submitForm()">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import {
  addFavorApi,
  updateFavorApi,
  listMainFavorApi,
} from "@/utils/api/index";

export default {
  props: {
    title: String,
  },
  data() {
    return {
      isShow: false,
      favorOptions: [],
      formdata: {},
      rules: {
        name: [{ required: true, message: "请输入杆塔名称", trigger: "blur" }],
      },
    };
  },
  mounted() {},
  methods: {
    //判断
    getdataform(val) {
      if (val == null) {
        return (this.formdata = {});
      }
      //this.formdata = val;
      this.formdata = JSON.parse(JSON.stringify(val));
      console.log(this.formdata);
      this.formdata.favorName = val.mainId;
    },
    // 保存确定操作
    submitForm() {
      this.$refs.formInfo.validate((valid) => {
        if (valid) {
          if (this.title == "新增") {
            addFavorApi({
              mainId: this.formdata.favorName,
              name: this.formdata.name,
            })
              .then((res) => {
                if (res.code == 200) {
                  this.isShow = false;
                  this.$message({
                    duration: 1500,
                    showClose: true,
                    message: "添加成功",
                    type: "success",
                  });
                  this.$parent.favorList();
                } else {
                  this.$message({
                    duration: 1500,
                    showClose: true,
                    message: "添加失败",
                    type: "error",
                  });
                }
              })
              .catch((err) => {});
          } else {
            console.log(this.formdata);
            updateFavorApi({
              mainId: this.formdata.favorName,
              id: this.formdata.id,
              name: this.formdata.name,
            })
              .then((res) => {
                if (res.code == 200) {
                  this.isShow = false;
                  //this.$message.success("修改成功");
                  this.$message({
                    duration: 1500,
                    showClose: true,
                    message: "修改成功",
                    type: "success",
                  });
                  this.$parent.favorList();
                } else {
                  //this.$message.error("修改失败");
                  this.$message({
                    duration: 1500,
                    showClose: true,
                    message: "修改失败",
                    type: "error",
                  });
                }
              })
              .catch((err) => {});
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //获取一级收藏加
    getmainFavor() {
      listMainFavorApi()
        .then((res) => {
          this.favorOptions = res.data;
        })
        .catch((err) => {});
    },
    display() {
      this.isShow = true;
      this.getmainFavor();
    },
    hide() {
      this.isShow = false;
    },
    handleClose() {
      this.$parent.favorList();
    },
  },
};
</script>
<style lang="less">
.addFavorDialog {
  .el-select {
    width: 100%;
  }
  .el-input-number {
    width: 100%;
  }
  .el-input-number .el-input__inner {
    text-align: left;
  }
}
</style>
