<template>
  <div class="user-management-container">
    <el-row :gutter="20" type="flex">
      <el-col :span="6">
        <div class="head-container">
          <span style="font-size: 25px">所属区域</span>
        </div>
        <div class="tree-container">
          <el-tree
            ref="tree"
            :data="deptOptions"
            default-expand
            :default-expand-all="false"
            :expand-on-click-node="true"
            :filter-node-method="filterNode"
            highlight-current
            :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>
        </div>
      </el-col>
      <el-col :span="18">
        <vab-query-form>
          <vab-query-form-left-panel :span="20">
            <el-form :inline="true" :model="queryForm" @submit.native.prevent>
              <el-form-item>
                <el-input v-model.trim="queryForm.distCode" clearable placeholder="经销商编码" />
              </el-form-item>
              <el-form-item>
                <el-input v-model.trim="queryForm.distName" clearable placeholder="经销商名称" />
              </el-form-item>
              <el-form-item>
                <el-input v-model.trim="queryForm.belongSale" clearable placeholder="所属销售" />
              </el-form-item>
              <el-form-item>
                <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
                <el-button icon="el-icon-refresh-right" @click="reset">重置</el-button>
              </el-form-item>
            </el-form>
            <vab-query-form-left-panel :span="5">
              <el-button icon="el-icon-plus" type="primary" @click="handleEdit($event)">新建</el-button>
            </vab-query-form-left-panel>
          </vab-query-form-left-panel>
          <vab-query-form-right-panel :span="29">
            <el-button icon="el-icon-download" />
            <table-tool :check-list.sync="checkList" :columns="columns" />
          </vab-query-form-right-panel>
        </vab-query-form>

        <el-table
          v-loading="listLoading"
          border
          :data="list"
          style="width: 100%; height: 500px"
          @selection-change="setSelectRows">
          <el-table-column
            v-for="(item, index) in finallyColumns"
            :key="index"
            align="center"
            :label="item.label"
            :prop="item.prop"
            show-overflow-tooltip
            :sortable="item.sortable"
            :width="item.width">
            <template #default="{ row }">
              <span>{{ row[item.prop] }}</span>
            </template>
          </el-table-column>

          <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>

          <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" />
  </div>
</template>

<script>
  import Edit from './components/DistrEdit'
  import distrApi from '@/api/distr'
  import TableTool from '@/components/table/TableTool'

  export default {
    name: 'Distr',
    components: { Edit, TableTool },
    data() {
      return {
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 10,
          userName: '',
        },

        checkList: [],
        columns: [
          {
            label: '所在省份',
            width: '100px',
            prop: 'provinceDesc',
            sortable: false,
          },
          {
            label: '归属销售',
            width: 'auto',
            prop: 'belongSale',
            sortable: false,
            disableCheck: true,
          },
          {
            label: '业务范围',
            width: 'auto',
            prop: 'businessScope',
            sortable: false,
            disableCheck: true,
          },
          {
            label: '负责人',
            width: 'auto',
            prop: 'distBoss',
            sortable: false,
            disableCheck: true,
          },
          {
            label: '归属销售',
            width: 'auto',
            prop: 'belongSale',
            sortable: false,
            disableCheck: true,
          },
        ],

        deptOptions: undefined,
        defaultProps: {
          id: 'id',
          children: 'children',
          label: 'regionDesc',
        },
        treeDefaultExpandAll: true,
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight(1)
      },
      finallyColumns() {
        console.log('finallyColums')
        return this.columns.filter((item) => this.checkList.includes(item.label))
      },
    },
    created() {
      this.fetchData()
      this.getTree()
    },
    methods: {
      async getTree() {
        const { data: data } = await distrApi.getTree({})
        console.log('区域', data.list)
        this.deptOptions = data.list
      },
      // 筛选节点
      filterNode(value, data) {
        if (!value) return true
        return data[this.defaultProps.label].indexOf(value) !== -1
      },
      // 节点单击事件
      handleNodeClick(data) {
        console.log('ID', data.id)
        this.queryForm.provinceId = data.id
        this.fetchData()
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      handleEdit(row) {
        if (row.id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        if (row.id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await distrApi.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 distrApi.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 distrApi.getList(this.queryForm)
        this.list = list
        this.total = total
        this.listLoading = false
      },
      reset() {
        ;(this.queryForm = {
          pageNum: 1,
          pageSize: 10,
          distCode: '',
          distName: '',
          belongSale: '',
        }),
          this.fetchData()
      },
    },
  }
</script>

<style>
  .el-tree-node:focus > .el-tree-node__content {
    /*设置选中的样式 */
    background-color: #dde9ff !important;
  }

  .el-tree-node__content:hover {
    /*设置鼠标飘过的颜色 */
    background: #eaf9ff !important;
    color: #007bff;
  }

  .el-tree--highlight-current .el-tree-node.is-current > .el-tree-node__content {
    /*current选中的样式 */
    color: #4d95fd;
    font-weight: bold;
    background-color: #dde9ff !important;
  }
</style>
