<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="5">
        <div class="grid-content" style="background: #fff">
          <div class="subtitle">成员列表</div>

          <el-tree
            :data="data"
            :props="defaultProps"
            node-key="id"
            :default-expanded-keys="[0, 0]"
            :default-checked-keys="[3]"
            @node-click="handleNodeClick"
          >
            <span
              class="custom-tree-node"
              :class="data.class"
              slot-scope="{ node, data }"
            >
              <span>
                <svg-icon icon-class="tree-user" />
                {{ node.label }}
              </span>
            </span>
          </el-tree>
        </div>
      </el-col>
      <el-col :span="19">
        <div class="grid-content" style="background: #fff">
          <div class="subtitle">部门列表</div>
          <div class="filter-container">
            <div class="filter-left">
              <div class="filter-item">
                姓名:
                <el-input
                  v-model="input"
                  placeholder=""
                  style="width: 120px"
                ></el-input>
              </div>

              <el-button
                type="primary"
                icon="el-icon-search"
                class="search-button"
                >搜索</el-button
              >
            </div>

            <div class="handle-button-right">
              <el-button
                icon="el-icon-plus"
                type="success"
                @click="dialogFormVisible = true"
                >新增</el-button
              >
              <el-button
                icon="el-icon-delete"
                type="danger"
                @click="batchDelete"
                >删除</el-button
              >
              <el-button type="warning" @click="handleDownload">
                <svg-icon icon-class="export" />
                导出
              </el-button>
            </div>
          </div>

          <el-divider></el-divider>
          <el-table
            :data="tableData"
            stripe
            border
            id="out-table"
            :header-cell-style="{ background: '#EBF1FF', color: '#606266' }"
            style="width: 100%"
            @selection-change="handleSelectionChange"
          >
          <el-table-column type="selection" width="40"> </el-table-column>
          <el-table-column :key="col.prop"
            :label="col.label"
            :prop="col.prop"
            v-for="col in cols"> </el-table-column>
          <!--
            <el-table-column type="selection" width="40"> </el-table-column>

            <el-table-column prop="name" label="姓名"> </el-table-column>
            <el-table-column prop="sex" label="性别"> </el-table-column>
            <el-table-column prop="company" label="所在单位" width="120">
            </el-table-column>
            <el-table-column prop="department" label="所在部门" width="120">
            </el-table-column>
            <el-table-column prop="workNumber" label="工号"> </el-table-column>
            <el-table-column prop="job" label="职位"> </el-table-column>
            <el-table-column prop="phone" label="手机号" width="120">
            </el-table-column>
            <el-table-column prop="email" label="邮箱" width="150">
            </el-table-column>

            <el-table-column prop="createMan" label="创建人"> </el-table-column>
            <el-table-column
              prop="createTime"
              label="创建时间"
              width="150"
            ></el-table-column>
            <el-table-column prop="editMan" label="修改人"> </el-table-column>

            <el-table-column prop="editTime" label="修改时间" width="150">
            </el-table-column> -->

            <el-table-column label="操作" width="150" fixed="right">
              <template slot-scope="scope">
                <el-button
                  icon="el-icon-edit"
                  style="color: #406ce5"
                  @click="handleEdit(scope.$index, scope.row)"
                  >编辑</el-button
                >
                <el-button
                  icon="el-icon-delete"
                  style="color: #f27979"
                  @click="handleDelete(scope.$index, scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-col>
    </el-row>

    <el-dialog title="新增" :visible.sync="dialogFormVisible" >
      <el-form :model="form">
        <el-form-item label="姓名:" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别:" :label-width="formLabelWidth">
          <el-select v-model="form.region" style="width: 100%" placeholder="">
            <el-option label="男" value="1"></el-option>
            <el-option label="女" value="2"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="所在单位:" :label-width="formLabelWidth">
          <el-select v-model="form.region2" style="width: 100%" placeholder="">
            <el-option label="单位一" value="3"></el-option>
            <el-option label="单位二" value="4"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="所在部门:" :label-width="formLabelWidth">
          <el-select v-model="form.region3" style="width: 100%" placeholder="">
            <el-option label="部门一" value="5"></el-option>
            <el-option label="部门二" value="6"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="工号:" :label-width="formLabelWidth">
          <el-input v-model="form.name2" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="职位:" :label-width="formLabelWidth">
          <el-select v-model="form.region4" style="width: 100%" placeholder="">
            <el-option label="职位一" value="7"></el-option>
            <el-option label="职位二" value="8"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="手机号:" :label-width="formLabelWidth">
          <el-input v-model="form.name3" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="邮箱:" :label-width="formLabelWidth">
          <el-input v-model="form.name4" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          @click="dialogFormVisible = false"
          style="background: #f6f6f6"
          >取 消</el-button
        >
        <el-button type="primary" @click="dialogFormVisible = false"
          >提 交</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>


<script>
// 引入导出Excel表格依赖
// import FileSaver from "file-saver";
// import XLSX from "xlsx";

import { parseTime } from "@/utils";

export default {
  data() {
    return {
      //树形控件
      data: [
        {
          id: 1,
          label: "上海永天集团",
          bool:false,
          children: [
            {
              id: 3,
              label: "物联网事业部",
              children: [
                {
                  label: "项目部",
                },
                {
                  label: "设计部",
                },
                {
                  label: "技术部",
                },
              ],
            },
          ],
        },
        {
          id: 2,
          label: "一级 2",
          bool:false,
          children: [
            {
              label: "二级 2-1",
              children: [
                {
                  label: "三级 2-1-1",
                },
              ],
            },
            {
              label: "二级 2-2",
              children: [
                {
                  label: "三级 2-2-1",
                },
              ],
            },
          ],
        },
      ],
      defaultProps: {
        children: "children",
        label: "label",
      },
      cols: [
          {prop: 'name',label: '名字'},
          {prop: 'sex',label:'性别'},
          {prop: 'company',label:'所在单位'},
          {prop: 'department',label:'所在部门'},
          {prop: 'workNumber',label:'工号'},
          {prop: 'job',label:'职位'},
          {prop: 'phone',label:'手机号'},
          {prop: 'email',label:'邮箱'},
          {prop: 'createMan',label:'创建人'},
          {prop: 'createTime',label:'创建时间'},
          {prop: 'editMan',label:'修改人'},
          {prop: 'editTime',label:'修改时间'},
      ],
      //表格数据
      tableData: [
        {
          name: "张三1",
          sex: "男",
          company: "上海永天集团",
          department: "物联网事业部",
          workNumber: "006",
          job: "程序开发",
          phone: "13262998445",
          email: "123456801@qq.com",
          createMan: "李四",
          createTime: "2020-08-15 12:22:20",
          editMan: "李四",
          editTime: "2020-09-15 12:22:20",
        },
        {
          name: "张三2",
          sex: "男",
          company: "上海永天集团",
          department: "物联网事业部",
          workNumber: "006",
          job: "程序开发",
          phone: "13262998445",
          email: "123456801@qq.com",
          createMan: "李四",
          createTime: "2020-08-15 12:22:20",
          editMan: "李四",
          editTime: "2020-09-15 12:22:20",
        },
        {
          name: "张三3",
          sex: "男",
          company: "上海永天集团",
          department: "物联网事业部",
          workNumber: "006",
          job: "程序开发",
          phone: "13262998445",
          email: "123456801@qq.com",
          createMan: "李四",
          createTime: "2020-08-15 12:22:20",
          editMan: "李四",
          editTime: "2020-09-15 12:22:20",
        },
        {
          name: "张三4",
          sex: "男",
          company: "上海永天集团1",
          department: "物联网事业部1",
          workNumber: "006",
          job: "程序开发",
          phone: "13262998445",
          email: "123456801@qq.com",
          createMan: "李四",
          createTime: "2020-08-15 12:22:20",
          editMan: "李四",
          editTime: "2020-09-15 12:22:20",
        },
      ],
      multipleSelection: [],

      options: [
        {
          value: "1",
          label: "单位1",
        },
        {
          value: "2",
          label: "单位2",
        },
        {
          value: "3",
          label: "单位3",
        },
      ],
      value: "",

      options2: [
        {
          value: "1",
          label: "隶属单位1",
        },
        {
          value: "2",
          label: "隶属单位2",
        },
        {
          value: "3",
          label: "隶属单位3",
        },
      ],
      value2: "",

      options3: [
        {
          value: "1",
          label: "类型1",
        },
        {
          value: "2",
          label: "类型2",
        },
        {
          value: "3",
          label: "类型3",
        },
      ],
      value3: "",

      input: "",

      dialogFormVisible: false,
      form: {
        name: "",
        region: "",
        region2: "",
        date1: "",
        date2: "",
        delivery: false,
        type: [],
        resource: "",
        desc: "",
      },
      formLabelWidth: "100px",
    };
  },
  methods: {
    handleDownload() {
      this.downloadLoading = true;
      import("@/vendor/Export2Excel").then((excel) => {
        const tHeader = [
          "姓名",
          "性别",
          "所在单位",
          "部门",
          "工号",
          "职位",
          "手机号",
          "邮箱",
          "创建人",
          "创建时间",
          "修改人",
          "修改时间",
        ];
        const filterVal = [
          "name",
          "sex",
          "company",
          "department",
          "workNumber",
          "job",
          "phone",
          "email",
          "createMan",
          "createTime",
          "editMan",
          "editTime",
        ];
        const data = this.formatJson(filterVal);
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: "table-list",
        });
        this.downloadLoading = false;
      });
    },
    formatJson(filterVal) {
      return this.tableData.map((v) =>
        filterVal.map((j) => {
          if (j === "timestamp") {
            return parseTime(v[j]);
          } else {
            return v[j];
          }
        })
      );
    },

    //树形控件
    handleNodeClick(data) {
      if(data.bool == false){
        data.bool = true
      }else{
        data.bool = false
      }
    },

    handleEdit(index, row) {
      alert(index, row);
    },
    handleDelete(index, row) {
      this.$confirm("确定删除该部门吗?", "删除", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.tableData.splice(index, 1);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    batchDelete() {
      let multData = this.multipleSelection;
      let tableData = this.tableData;
      let multDataLen = multData.length;
      let tableDataLen = tableData.length;

      let confirmMessage = "";

      // alert(multDataLen)
      if (multDataLen == 1) {
        confirmMessage = "确定删除该用户吗";
      } else {
        confirmMessage = "确定删除这些用户吗";
      }




      this.$confirm(confirmMessage, "删除", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {

          for (let i = 0; i < multDataLen; i++) {
            for (let y = 0; y < tableDataLen; y++) {
              if (JSON.stringify(tableData[y]) == JSON.stringify(multData[i])) {
                //判断是否相等，相等就删除
                this.tableData.splice(y, 1);

              }
            }
          }


          this.$message({
            type: "success",
            message: "删除成功!",
          });

        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
  },
};
</script>
<style scoped>
.app-container {
  background: #f4f4f4;
  margin: 0;
}
.grid-content {
  min-height: calc(100vh - 80px);
  padding: 8px 15px;
}
</style>
<style >


.el-tree-node__content {
  background: #eaeef7;
}
.el-tree-node__content:hover {
  background: #cbd9fe;
}
.el-tree-node.is-current > .el-tree-node__content {
  background: #cbd9fe !important;
}
.el-tree-node__content {
  height: 30px;
}
.el-tree {
  margin-top: 10px;
}
.tree-user {
  width: 14px;
}

.el-tree-node__content {
  font-size: 14px;
}
</style>
