<template>
  <LjDrawer
    v-if="visible"
    ref="LjDrawerRef"
    :class="`${prefixCls}`"
    :id="prefixCls"
    direction="btt"
    size="70%"
    destroy-on-close
    append-to-body
    :show-close="false"
    @close="autoClose"
  >
    <template #header>
      <div class="flx-justify-between">
        <div class="">
          详细报价
          <el-segmented v-model="dannumValue" :options="dannumOptions" class="ml-32" size="default" />
        </div>
        <div class="flx-shrink">
          <el-button type="danger" :icon="Close" text circle @click="cancelClick"></el-button>
        </div>
      </div>
    </template>
    <template #default>
      <template v-if="iforigin">
        <el-tabs v-model="activeName">
          <el-tab-pane label="新公式" name="first">
            <!-- <LjHeader class="flx-1" title="单据统计" /> -->
            <FormulaGroup :data="isNormalFormulas" :fields="isFieldsReplace" />
          </el-tab-pane>
          <el-tab-pane label="旧公式" name="second">
            <!-- <LjHeader class="flx-1" title="单据统计" /> -->
            <FormulaGroup :data="isNormalFormulasOri" :fields="isFieldsReplaceOri" />
          </el-tab-pane>
        </el-tabs>
      </template>
      <template v-else>
        <!-- <LjHeader class="flx-1" title="单据统计" /> -->
        <FormulaGroup :data="isNormalFormulas" :fields="fieldsReplace" />
      </template>
    </template>
  </LjDrawer>
</template>

<script setup lang="ts" name="mattressQuoteDetail_AllFormula">
import { ref, computed, watch, nextTick } from "vue";
import LjDrawer from "@/components/LjDrawer/index.vue";
import { useI18n } from "vue-i18n";
import { useDesign } from "@/hooks/useDesign";
import { useGlobalStore } from "@/stores/modules/global";
import { Close, Search } from "@element-plus/icons-vue";
import { ElSegmented } from "element-plus";
import FormulaGroup from "./FormulaGroup.vue";
import LjHeader from "@/components/LjHeader/index.vue";
import { cloneDeep } from "lodash-es";

interface WidgetProps {
  iforigin: boolean;
}

const props = withDefaults(defineProps<WidgetProps>(), {
  iforigin: false
});

const { t } = useI18n();
const { prefixCls } = useDesign("bednet-formal-detail");
const globalStore = useGlobalStore();

const visible = ref(false);
const LjDrawerRef = ref();
const formulaList = ref<any>([]);
const fieldsReplace = ref<any>([]);
const formulaListOri = ref<any>([]);
const differ = ref<any>([]);
// const mainData = ref<any>({});
const formulaMxList = ref<any>([]);

const dannumValue = ref("标准");

const dannumOptions = ["标准", "散单", "大单", "小单"];

const isNormalFormulas = computed(() => {
  let _formula = formulaList.value.filter((item: any) => item.type == 0);
  _formula = _formula.map((item: any) => {
    let _val = fieldsReplace.value.find((_item: any) => _item.label == item.label);
    if (!_val) {
      let tgItem = formulaList.value.find(_item => _item.value.indexOf(item.label) > -1);
      if (tgItem) {
        item.parentId = tgItem.label;
      } else {
        item.parentId = null;
      }
    } else {
      item.parentId = null;
    }
    return item;
  });

  // let replaceFormula = cloneDeep(_formula.filter((item: any) => item.ifnotreplace == true));

  // _formula = cloneDeep(_formula.filter((item: any) => !item.ifnotreplace));

  // replaceFormula.map((item: any) => {
  //   let index = _formula.findIndex((_item: any) => _item.value.indexOf(item.label) > -1);
  //   if (index > -1) {
  //     if (!Object.keys(_formula[index]).includes("children")) {
  //       _formula[index].children = [];
  //     }
  //     item.ifnotreplace = null;
  //     _formula[index].children.push(item);
  //   }
  // });
  // console.log("isNormalFormulas: >>> ", buildTreeArr(_formula));

  return buildTreeArr(_formula);
});
const isNormalFormulasOri = computed(() => {
  return formulaListOri.value.filter((item: any) => item.type == 0);
});

const getDannumType = computed(() => {
  let _type = 1;
  switch (dannumValue.value) {
    case "标准":
      _type = 2;
      break;
    case "散单":
      _type = 1;
      break;
    case "大单":
      _type = 3;
      break;
    case "小单":
      _type = 4;
      break;
  }
  return _type;
});

const isFieldsReplace = computed(() => {
  let tg = differ.value.find((item: any) => item.type == getDannumType.value);
  return tg.replace;
});
const isFieldsReplaceOri = computed(() => {
  let tg = differ.value.find((item: any) => item.type == getDannumType.value);
  return tg.replace_origin;
});

const isMxFormulas = computed(() => {
  return formulaList.value.filter((item: any) => item.type == 2);
});
// const isWeigthFormulas = computed(() => {
//   return formulaList.value.filter((item: any) => item.type == 3);
// });

const buildTreeArr = (items, parentId = null) => {
  return items
    .filter(item => item.parentId === parentId)
    .map(item => ({
      ...item,
      children: buildTreeArr(items, item.label)
    }));
};

const activeName = ref("first");

const autoClose = () => {
  setTimeout(() => {
    visible.value = false;
  }, 200);
};

const cancelClick = () => {
  // searchField.value = "";
  LjDrawerRef.value.hide();
  // selectColumn.value = [];
  // ifAllSelect.value = false;
};

/**
 * @description 显示组件
 */
const open = (data: any) => {
  switch (data.dannum_type) {
    case 2:
      dannumValue.value = "标准";
      break;
    case 1:
      dannumValue.value = "散单";
      break;
    case 3:
      dannumValue.value = "大单";
      break;
    case 4:
      dannumValue.value = "小单";
      break;
  }

  visible.value = true;
  formulaList.value = data.formula;
  fieldsReplace.value = data.replace;
  formulaListOri.value = data.formula_ori;
  differ.value = data.differ;
  // mainData.value = data;

  // formulaMxList.value;

  nextTick(() => {
    //   list.value = cloneDeep(params);
    //   oriList.value = cloneDeep(params);

    //   let arr: string[] = [];
    //   params.map((item: any) => {
    //     if (item.basicinfo && item.basicinfo.hasOwnProperty("group")) {
    //       item.basicinfo.group && !arr.includes(item.basicinfo.group) && arr.push(item.basicinfo.group);
    //     }
    //   });
    //   groupList.value = arr;

    //   selectGroup.value = group;

    LjDrawerRef.value.show();
    //   console.log("open columnsDrag.value :>> ", columnsDrag.value);

    //   nextTick(() => {
    //     initDragSelect();
    //   });
  });
};

defineExpose({
  open
});
</script>

<style lang="scss" scoped>
$prefix-cls: "#{$namespace}-bednet-formal-detail";

.#{$prefix-cls} {
  .summary-wrapper {
    &__label-item {
      position: relative;
      margin-right: $space-a3;

      &::after {
        content: "=";
        position: absolute;
        bottom: 0;
        right: -$space-b3;
        font-size: 24px;
        color: $color-text-disable;
      }
    }
  }
}
</style>
