<template>
  <div class="user-management-container">
    <el-row :gutter="20" type="flex">
      <el-col :span="6">
        <div class="head-container" style="margin-bottom: 5px">
          <el-input v-model="filterText" placeholder="输入关键字进行过滤" />
          <el-button icon="el-icon-plus" style="margin-top: 10px" type="primary" @click="getCheckedNodes($event)">
            添加区域
          </el-button>
          <el-tree
            ref="tree"
            class="filter-tree"
            :data="deptOptions"
            default-expand-all
            :default-expanded-keys="[1]"
            :filter-node-method="filterNode"
            highlight-current
            node-key="id"
            :props="defaultProps"
            @node-click="handleNodeClick">
            <span slot-scope="{ node }" class="custom-tree-node">
              <span>{{ node.label }}</span>
              <span>
                <i class="el-icon-more"></i>
              </span>
            </span>
          </el-tree>
          <!-- <el-tree ref="tree" :data="deptOptions" node-key="id" 
          :filter-node-method="filterNode" :props="defaultProps"
          @node-click="handleNodeClick" /> -->
        </div>
        <!-- <div class="buttons">
    <el-button  @click="getCheckedNodes($event)">添加区域</el-button>
  </div> -->
      </el-col>
      <el-col :span="18">
        <vab-query-form>
          <vab-query-form-left-panel :span="18">
            <el-form :inline="true" :model="queryForm" @submit.native.prevent>
              <el-form-item style="letter-spacing: 12px">
                <el-input v-model.trim="queryForm.custCode" clearable placeholder="客户编码" />
              </el-form-item>
              <el-form-item>
                <el-input v-model.trim="queryForm.custName" clearable placeholder="客户名称" />
              </el-form-item>
              <el-form-item>
                <el-input v-model.trim="queryForm.custIndustry" clearable placeholder="客户行业" />
              </el-form-item>
            </el-form>
          </vab-query-form-left-panel>
          <vab-query-form-right-panel :span="6">
            <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
            <el-button icon="el-icon-plus" type="primary" @click="handleEdit($event)">添加</el-button>
          </vab-query-form-right-panel>
        </vab-query-form>

        <el-table
          v-loading="listLoading"
          border
          :data="list"
          height="calc(100% - 52px - 43px)"
          style="width: 100%"
          @selection-change="setSelectRows">
          <el-table-column align="center" show-overflow-tooltip type="selection" />
          <el-table-column align="center" label="省份" prop="distName" show-overflow-tooltip />
          <el-table-column align="center" label="客户数量" prop="count" show-overflow-tooltip />
          <el-table-column align="center" label="创建人" prop="createdName" show-overflow-tooltip />
          <el-table-column align="center" label="创建时间" prop="createdTime" show-overflow-tooltip />
          <el-table-column align="center" label="操作" show-overflow-tooltip width="85">
            <template #default="{ row }">
              <el-button type="text" @click="handleEdit(row)">编辑</el-button>
              <el-button type="text" @click="handleDelete(row)">删除</el-button>
            </template>
          </el-table-column>
          <template #empty>
            <el-image class="vab-data-empty" :src="require('@/assets/empty_images/data_empty.png')" />
          </template>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNum"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange" />
      </el-col>
    </el-row>
    <edit ref="edit" @fetch-data="fetchData" />

    <reg-edit ref="reg-edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import Edit from './components/RegionEdit'
  import RegEdit from './components/RegEdit'
  import regionApi from '@/api/base/region/region'

  export default {
    name: 'Distr',
    components: { Edit, RegEdit },
    data() {
      return {
        current: '',
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        filterText: '',
        queryForm: {
          pageNum: 1,
          pageSize: 10,
          userName: '',
        },
        deptOptions: undefined,
        defaultProps: {
          id: 'id',
          regionCode: 'regionCode',
          label: 'regionDesc',
        },
        treeDefaultExpandAll: true,
        regionId: 0,
      }
    },

    computed: {
      height() {
        return this.$baseTableHeight(10)
      },
    },
    watch: {
      filterText(val) {
        this.$refs.tree.filter(val)
      },
    },
    created() {
      this.fetchData()
      this.getRegion()
    },
    methods: {
      async getRegion() {
        const { data: data } = await regionApi.getRegion({})
        var first_id = data.list[0].id

        console.log('区域', first_id)
        this.deptOptions = data.list
        this.queryForm.regionId = first_id //data.list[0].id
        this.$refs['edit'].setRegion(first_id)
        //默认第一选中
        this.$nextTick(() => {
          this.$refs.tree.setCurrentKey(first_id)
        })
        this.fetchData()
      },
      // 筛选节点
      filterNode(value, data) {
        if (!value) return true
        return data[this.defaultProps.label].indexOf(value) !== -1
      },
      // 节点单击事件
      handleNodeClick(data) {
        console.log('ID', data.id)
        this.$refs['edit'].setRegion(data.id)
        this.queryForm.regionId = data.id
        this.fetchData()
      },

      setSelectRows(val) {
        this.selectRows = val
      },
      getCheckedNodes() {
        this.$refs['reg-edit'].showEdit()
      },
      handleEdit(row) {
        if (row.id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        if (row.id) {
          console.log('deletedId', row.id)
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await regionApi.doDelete({ ids: row.id })
            this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            await this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => parseInt(item.id))
            console.log(ids)
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await regionApi.doDelete({ ids })
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
              await this.fetchData()
            })
          } else {
            this.$baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const {
          data: { list, total },
        } = await regionApi.getList(this.queryForm)
        this.list = list
        this.total = total
        this.listLoading = false
      },
    },
  }
</script>
<style lang="scss" scoped>
  $base: '.user-management';
  #{$base}-container {
    > .el-row {
      height: calc(100vh - 60px - 50px - 12px * 2 - 40px - 24px);
    }
    .head-container {
      height: calc(100% - 5px);
      .el-tree {
        height: calc(100% - 74px);
        overflow-y: auto;
      }
    }
  }
</style>
