<template>
  <div class="category-add">
    <h2 class="dialog_add-title">修改标题名</h2>
    <dialog-input
      class="dialog_add-form dialog_add-input_name"
      v-model="editTitle"
      placeholder="请输入标题名称"
    />
    <button class="dialog_add-btn_confirm" @click="changeEdit">确定</button>
    <button class="dialog_add-btn_cancel" @click="closeDialog">取消</button>
  </div>
</template>

<script>
import { Property, Unit } from "@/plugins/types";
import DialogInput from "@/components/DialogInput.vue";
export default {
  //   引入组件
  components: {
    DialogInput
  },
  props: ["selectedEditItem"],
  data() {
    return {
      editTitle: ""
    };
  },
  watch: {
    selectedEditItem: {
      handler(newName, oldName) {
    
        if (newName.title) this.editTitle = newName.title;
        if (newName.label) this.editTitle = newName.label;
      },
      deep: true,
      immediate: true
    }
  },
  created() {},
  computed: {},
  methods: {
    changeEdit() {
      this.$nextTick(() => {
        if (this.selectedEditItem) {
          if (this.selectedEditItem.hasOwnProperty("title"))
            this.selectedEditItem.title = this.editTitle;
          if (this.selectedEditItem.hasOwnProperty("label"))
            this.selectedEditItem.label = this.editTitle;
        }
      });
      this.$emit("close");
    },
    // 取消
    closeDialog() {
      if (
        this.selectedEditItem &&
        this.selectedEditItem.title &&
        this.selectedEditItem.title.trim() === ""
      ) {
        this.$message({
          message: "标题不可为空",
          type: "warning"
        });
        return false;
      }
      this.$emit("close");
    }
  }
};
</script>
<style scoped lang="less">
.category-add {
  padding: 34px 50px 0 50px;
  height: 221px; /* 325px - 34px */
  width: 500px; /* 600px - 100px */
}

.dialog_add-title {
  margin: 0;
  color: #000;
  font-size: 22px;
  line-height: 30px;
}

.dialog_add-select_subject {
  margin-top: 29px;
}

.dialog_add-input_name {
  margin: 20px 0 34px 0;
}

.dialog_add-btn_confirm {
  float: right;
  width: 120px;
  height: 40px;
  color: #fff;
  font-size: 14px;
  outline: none;
  background: #3f79fe;
  border-radius: 4px;
  border: none;
  cursor: pointer;
}

.dialog_add-btn_cancel {
  float: right;
  margin-right: 18px; /* 28 - 10 */
  border: none;
  width: 48px; /* 28 + 增大20 */
  height: 40px;
  outline: none;
  background: #fff;
  color: #5c6476;
  font-size: 14px;
  cursor: pointer;
}
</style>

