<template>
  <com-head :options="headList" class="frame-head">
  </com-head>
  <div class="farme">
    <el-tree
      node-key="id"
      default-expand-all
      class="framework"
      :data="origin"
      :expand-on-click-node="false"
    >
      <template v-slot="{node, data}">
        <div class="custom-tree-node">
          <span class="text">{{ data.name }}</span>

          <span @click="readyInsert(data)" class="btn" v-if="auth.add && data.level === 0">
            <i class="el-icon-plus"></i>
          </span>
          
          <span @click="readyUpdate(data)" class="btn">
            <i class="el-icon-edit"></i>
          </span>

          <span @click="remove(node, data)" class="btn" v-if="data.level !== 0 && auth.delete">
            <i class="el-icon-delete"></i>
          </span>


        </div>
      </template>
    </el-tree>

    <com-dialog :title="(operState.isInsert ? '添加' : '修改') + '品牌专柜'" v-model:show="operState.show" @submit="append">
      <el-form ref="form" label-width="100px" class="framework-from">
        <el-form-item label="公司名称：" class="mandatory">
          <el-input v-model="operState.name" placeholder="请输入" maxlength="15" show-word-limit></el-input>
        </el-form-item>
      </el-form>
    </com-dialog>

  </div>
</template>

<script>
import { ref } from "vue";
import comHead from "@/components/head";
import comDialog from "@/components/dialog";
import auth from "@/state/viewAuth";
import axios from 'axios'
import { 
  getCompanyThree,
  insertCompany,
  deleteCompany,
  updateCompany
} from '@/request/config'

const analysis = (data, level) => {
  return data.map(item => ({
    id: item.id,
    name: item.name,
    level: level,
    children: item.children ? analysis(item.children, level + 1) : []
  }))
}

export default {
  name: 'framework',
  setup() {
    const headList = ref([{ name: "组织架构", value: 1 }]);
    const origin = ref([]);
    const operState = ref({ name: '', data: null, show: false, isInsert: false })


    const readyInsert = (data) => {
      operState.value.data = data
      operState.value.show = true
      operState.value.isInsert = true
      operState.value.name = ''
    }
    const readyUpdate = data => {
      operState.value.data = data
      operState.value.show = true
      operState.value.isInsert = false
      operState.value.name = data.name
    }
    return { headList, origin, operState, readyInsert, readyUpdate, auth };
  },
  async mounted() {
    let res = await axios.post(getCompanyThree, {})
    // res.data[0].children = res.data[0].children.concat(res.data[0].children).concat(res.data[0].children)
    this.origin = analysis(res.data, 0)
  },
  methods: {
    async remove(node, data) {
      if (!(await this.$confirm(`确定要删除${data.name}吗？`, '提示'))) return;

      const parent = node.parent;
      const children = parent.data.children || parent.data;
      const index = children.findIndex(d => d.id === data.id);

      await axios.post(deleteCompany, {id: data.id})

      children.splice(index, 1);
      parent.data.children = [...children]

      if (parent.id === 0) {
        this.origin = [...parent.data.children]
      }
    },
    async append() {
      const data = this.operState.data

      if (!this.operState.name) {
        return this.$alert('请输入公司名称！', '提示')
      }

      if (this.operState.isInsert) {
        const newCompany = { name: this.operState.name }
        const res = await axios.post(insertCompany, { ...newCompany, parentId: data.id })
        const newChild = { id: res.data.id, ...newCompany, level: data.level + 1,  children: [] };
        data.children = data.children ? [...data.children, newChild] : [newChild]
      } else {
        await axios.post(updateCompany, { id: data.id, name: this.operState.name })
        data.name = this.operState.name
      }
      this.operState.name = ''
      this.operState.show = false
    }
  },
  components: {
    "com-dialog": comDialog,
    "com-head": comHead,
  },
};
</script>

<style lang="scss" scoped>
.farme {
  padding: 16px 124px 32px 16px;
  background-color: #fff;
  flex: 1;
  overflow-y: auto;
}

.custom-tree-node {
  margin: 10px 0;
  .text {
    font-size: 1rem;
    color: #303133;
  }

  .btn {
    color: rgb(144,144,144);
    border: 1px dashed currentColor;
    display: inline-block;
    margin-left: 10px;
    padding: 3px 5px;

    i {
      font-size: 0.8rem;
    }
  }
}

.framework-from {
  width: 320px;
  margin: 80px auto;
}

</style>

<style lang="scss">
.framework .el-tree-node__content {
  height: auto;
}
.frame-head .head-content-layer.show {
  padding: 0;
}
</style>