<template>
  <common-dialog
    :visible="visible"
    showMask
    @closePopup="emitClose"
    type="element"
    :elementDialogProps="{
      title: '添加下一级',
    }"
    :btnConfig="{
      type: 'noBorder',
      btns: btns,
      btnFunctions: [
        {
          name: '取消',
          click: async () => {
            emitClose();
            return true;
          },
        },
        {
          name: '确定',
          click: async () => {
            return emitConfirm();
          },
        },
      ],
    }"
  >
    <div class="c-dialog" v-if="visible">
      <Scroll ref="scroll" class="max-h-80" type="normal">
        <div
          v-for="item in propertyList"
          :key="item.id"
          class="flex items-center mb-4"
        >
          <dialog-input
            ref="List"
            class="l-input t-input flex-grow"
            v-model:value="item.label"
            :errorShowing="isErrorShowing"
            maxlength="10"
          >
          </dialog-input>
          <div class="shanchu-con ml-4">
            <i class="iconfont iconshanchu-" @click="deleteTmpItem(item)"></i>
          </div>
        </div>
      </Scroll>
      <div class="btn_add mt-3" @click="addNewProperty">
        <Button
          iconName="iconjixutianjia"
          mode="iconBtnWithoutOutline2"
          :style="'color: #b1b5bc;font-size:20px;'"
          >继续添加</Button
        >
      </div>
    </div>
  </common-dialog>
</template>

<script lang="ts" setup>
import { btns } from "@/config/btns";
import {
  syDialogInput as DialogInput,
  getUuid,
  syCommonDialog as CommonDialog,
  syScroll as Scroll,
  syButton as Button,
} from "cqk-sy-ui";
import { onMounted, ref } from "vue";
import { watch } from "vue";

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

/**
 * 添加下一级字段
 * @author yuanzeyu
 * @date 2019-06-04
 * @desc
 */

const propertyList = ref([] as Item[]);
const isErrorShowing = ref(false);
const scroll = ref<{ scrollToEnd: any } | null>(null);

const $emit = defineEmits(["confirm", "close"]);
const props = defineProps<{
  visible: boolean;
}>();

const addNewProperty = () => {
  propertyList.value.push({
    id: getUuid(),
    label: "",
  });

  if (props.visible) {
    setTimeout(() => {
      scrollToEnd();
    }, 300);
  }
};
// 删除
const deleteTmpItem = (item: Item) => {
  for (let i = propertyList.value.length - 1; i > -1; i--) {
    let _item = propertyList.value[i];

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

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

const scrollToEnd = () => {
  scroll.value?.scrollToEnd();
};

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

const emitClose = () => {
  $emit("close");
};

onMounted(() => {
  addNewProperty();
});

watch(
  () => props.visible,
  (newV) => {
    if (newV === true) {
      propertyList.value = [];
      addNewProperty();
      isErrorShowing.value = false;
    }
  }
);
</script>

<style scoped lang="scss">
/* 添加模板公用样式 */
.c-dialog {
  box-sizing: content-box;
}
.g-btn_primary {
  border: none;
  background: var(--sy-blue);
  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;
}

.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-btn_wrapper {
  padding-right: 50px;
}
.l-title {
  margin-bottom: 42px;
}

.l-input {
  display: flex;
  align-items: center;
}

.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;
  line-height: 44px;
}
.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>
