<!--
 * @Description: In User Settings Edit
 * @Author: your name
 * @Date: 2019-08-15 15:32:16
 * @LastEditTime: 2019-08-15 15:32:16
 * @LastEditors: your name
 -->
<template>
  <common-dialog id="add_child_dialog" @closePopup="emitClose">
    <div class="c-dialog">
      <h2 class="c-dialog-title l-title">添加下一级</h2>
      <!--<div class="dialog-input-con-con">-->
      <PcMyscroll ref="scrollObj">
        <div slot="mylist">
          <dialog-input
            ref="List"
            v-for="item in propertyList"
            class="l-input t-input"
            v-model="item.label"
            :errorShowing="isErrorShowing"
            maxlength="10"
            :key="item.id"
          >
            <div class="shanchu-con">
              <i class="iconfont iconshanchu-" @click="deleteTmpItem(item)"></i>
            </div>
          </dialog-input>
        </div>
      </PcMyscroll>
      <!--</div>-->

      <div class="btn_add" @click="addNewProperty">
        <i class="iconfont btn_add-icon iconjixutianjia"></i>
        <span class="btn_add-text">继续添加</span>
      </div>
      <div class="c-dialog-btn_wrapper l-btn_wrapper">
        <button class="g-btn_primary c-dialog-btn_cancel" @click="emitClose">
          取消
        </button>
        <button class="g-btn_primary c-dialog-btn_confirm" @click="emitConfirm">
          确定
        </button>
      </div>
    </div>
  </common-dialog>
</template>

<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
import CommonDialog from "@/components/CommonDialog.vue";
import { getUuid } from "@/utils/tool";
import DialogInput from "@/components/DialogInput.vue";
let Con33 = require("../../../node_modules/simple-components01/src/main.js");
let PcMyscroll = Con33.default;

interface Item {
  id: string;
  label: string;
}

/**
 * 添加下一级字段
 * @author yuanzeyu
 * @date 2019-06-04
 * @desc
 */
@Component({
  components: {
    CommonDialog,
    DialogInput,
    PcMyscroll,
  },
})
export default class AddChildDialog extends Vue {
  public propertyList: Item[] = [];
  public isErrorShowing: boolean = false;

  public addNewProperty() {
    this.propertyList.push({
      id: getUuid(),
      label: "",
    });
    // 聚焦最底部input
    // setTimeout(() => {
    //   let List: any = this.$refs.List;
    //   List[List.length - 1].$el.childNodes[0].focus();
    // }, 200);
    setTimeout(() => {
      let srlObj: any = this.$refs.scrollObj;
      if (srlObj && srlObj.init2) {
        srlObj.reCalculate(true);
        srlObj.init2();
        srlObj.scrollToEnd("y");
      }
    }, 300);
  }
  // 删除
  public deleteTmpItem(item: Item) {
    for (let i = this.propertyList.length - 1; i > -1; i--) {
      let _item = this.propertyList[i];

      if (_item.id == item.id) {
        this.propertyList.splice(i, 1);

        setTimeout(() => {
          let srlObj: any = this.$refs.scrollObj;
          if (srlObj && srlObj.init2) {
            srlObj.reCalculate(false, 66); //注意这个配置参数
            srlObj.init2();
          }
        }, 100);

        break;
      }
    }
    if (this.propertyList.length == 0) {
      this.emitClose();
    }
  }

  public emitConfirm() {
    const empty = this.propertyList.some((item) => item.label.length === 0);
    if (empty) {
      this.isErrorShowing = true;
      return;
    }
    this.$emit(
      "confirm",
      this.propertyList.map((item) => item.label)
    );

    this.emitClose();
  }

  public emitClose() {
    this.$emit("close");
  }

  public created() {
    this.addNewProperty();
  }
}
</script>

<style scoped lang="less">
// @import "./common.css";
/* 添加模板公用样式 */
.c-dialog {
  width: 500px;
  /* 600 - 100 */
  padding: 34px 50px 63px 50px;
}
.g-btn_primary {
    border: none;
    background: #5779F4;
    font-size: 14px;
    color: #fff;
    outline: none;
    cursor: pointer;
    border-radius: 8px;
}
.c-dialog-title {
  margin: 0;
  font-size: 22px;
  line-height: 30px;
  color: #000;
  font-weight: 500;
}

.c-dialog-input {
  box-sizing: border-box;
  padding: 0 26px;
  width: 500px;
  height: 44px;
  border: 1px solid #e2e2e2;
 
  font-weight: 400;
  font-size: 18px;
  background: transparent;
  outline: none;

  &::placeholder {
   
  }
}

.c-dialog-btn_wrapper {
  text-align: right;
}

.c-dialog-btn_confirm {
  width: 120px;
  height: 40px;
  border-radius: 4px;

  &:after {
    clear: both;
  }
}

.c-dialog-btn_cancel {
  margin-right: 22px;
  /* 28 - (40 - 28) / 2 */
  color: #5c6476;
  background: transparent;
  height: 40px;
  width: 60px;
}

.c-dialog {
  padding: 34px 0 63px 50px;
}
.c-dialog-btn_wrapper {
  padding-right: 50px;
}
.l-title {
  margin-bottom: 42px;
}

.l-input {
  margin-bottom: 16px; /* todo ui未给出多行情况 */
}
/deep/ .l-input input {
  width: 440px;
}

.btn_add {
  color: #abafb6;
  line-height: 20px;
  height: 20px;
  font-size: 0;
  cursor: pointer;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.btn_add-icon {
  margin: 1px 6px 0 0;
  display: inline-block;
  vertical-align: top;
  font-size: 20px;
  color: #b1b5bc;
}

.btn_add-text {
  font-size: 14px;
  vertical-align: top;
}

.l-btn_wrapper {
  margin-top: 11px;
}

.shanchu-con {
  display: inline-block;
  position: relative;
  width: 22px;
  position: absolute;
  line-height: 44px;
  padding-left: 14px;
}
.iconshanchu- {
  font-size: 20px;
  left: 0;
  top: 0;
  color: #eaeaea;
}
.dialog-input-con-con {
  height: 360px;
  position: relative;
}
.dialog-input-con {
  position: absolute;
  z-index: 1;
  top: 45px;
  bottom: 48px;
  left: 0;
  width: 100%;
  background: #aaa;
  overflow: auto;
}
/deep/ .c-dialog {
  max-height: 600px;
}
/deep/ .dialog_content {
  max-height: 570px;
}
/deep/ .pc-myscroll {
  height: auto;
  max-height: 360px;
}
/deep/ .mylist {
  padding: 5px 0 5px 0;
}
/deep/.mybar {
  background-color: #efefef;
  width: 16px;
}
</style>
