<template>
  <div class="user-management-container">
    <div class="side-layout">
      <div class="tree-side">
        <div class="head-container">
          <span style="font-size: 25px">所属区域</span>
        </div>
        <div class="tree-container">
          <el-tree
            ref="tree"
            :data="regionOptions"
            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>
      </div>
      <div class="tree-table">
        <vab-query-form>
          <vab-query-form-top-panel>
            <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-top-panel>
        </vab-query-form>

        <vab-query-form-left-panel>
          <el-button
            v-permissions="['base:distributor:add']"
            icon="el-icon-plus"
            type="primary"
            @click="handleEdit($event)">
            新建
          </el-button>
        </vab-query-form-left-panel>
        <vab-query-form-right-panel>
          <table-tool :check-list.sync="checkList" :columns="columns" />
          <el-button icon="el-icon-download" style="float: right" />
        </vab-query-form-right-panel>
        <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 }">
              <el-button v-if="item.prop === 'distName'" style="font-size: 14px" type="text" @click="handleDetail(row)">
                {{ row.distName }}
              </el-button>
              <span v-else>{{ row[item.prop] }}</span>
            </template>
          </el-table-column>

          <el-table-column align="center" label="操作" show-overflow-tooltip width="85">
            <template #default="{ row }">
              <el-button v-permissions="['base:distributor:edit']" type="text" @click="handleEdit(row)">编辑</el-button>
              <el-button v-permissions="['base:distributor:delete']" 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" />
      </div>
    </div>
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import distrApi from '@/api/base/distr'
  import regionApi from '@/api/base/region'

  import Edit from './components/DistrEdit'
  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: '120px',
            prop: 'distName',
            sortable: false,
          },
          {
            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,
          },
        ],

        regionOptions: undefined,
        defaultProps: {
          id: 'id',
          children: 'children',
          label: 'regionDesc',
        },
        treeDefaultExpandAll: true,
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight(1)
      },
      finallyColumns() {
        return this.columns.filter((item) => this.checkList.includes(item.label))
      },
    },
    created() {
      this.fetchData()
      this.getRegionTree()
    },
    methods: {
      async getRegionTree() {
        const { data: data } = await regionApi.getRegionTree({})
        this.regionOptions = 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()
      },
      //详情
      handleDetail(row) {
        this.$router.push({
          path: '/base/info',
          query: {
            id: row.id,
          },
        })
      },
    },
  }
</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>
