<template>
  <div class="jp-common-layout page">
    <div class="jp-common-layout-left">
      <div class="jp-common-title">
        <el-input
          placeholder="请输入关键字过滤"
          size="small"
          clearable
          v-model="filterText">
        </el-input>
      </div>
      <div class="jp-common-el-tree-scrollbar el-scrollbar">
        <div class="el-scrollbar__wrap">
          <div class="el-scrollbar__view">
            <el-tree
              class="filter-tree jp-common-el-tree"
              :render-content="renderContent"
              :data="helpTreeData"
              :props="{
                    value: 'id',             // ID字段名
                    label: 'name',         // 显示名称
                    children: 'childrenList'    // 子级字段名
                  }"
              node-key="id"
              default-expand-all
              :filter-node-method="filterNode"
              :expand-on-click-node="false"
              highlight-current
              @node-click="handleNodeClick"
              ref="helpTree">
            </el-tree>
          </div>
        </div>
      </div>
    </div>
    <div class="jp-common-layout-center jp-flex-main">
      <el-form size="small" :inline="true" class="query-form" ref="searchForm" :model="searchForm" @keyup.enter.native="refreshList()" @submit.native.prevent>
        <el-form-item prop="number" label="编号">
          <el-input size="small" v-model="searchForm.number" placeholder="请输入编号" clearable></el-input>
        </el-form-item>
        <el-form-item prop="features" label="功能">
          <el-input size="small" v-model="searchForm.features" placeholder="请输入功能" clearable></el-input>
        </el-form-item>
        <el-form-item prop="description" label="描述">
          <el-input size="small" v-model="searchForm.description" placeholder="请输入描述" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button  type="primary" @click="refreshList()" size="small" icon="el-icon-search">查询</el-button>
          <el-button @click="resetSearch()" size="small" icon="el-icon-refresh-right">重置</el-button>
        </el-form-item>
      </el-form>
      <div class="bg-white top">
        <vxe-toolbar  :refresh="{query: refreshList}" resizable custom>
          <template #buttons>
            <el-button v-if="hasPermission('help:add')" type="primary" size="small" icon="el-icon-plus" @click="add()">新建</el-button>
            <el-button v-if="hasPermission('help:del')" type="danger"   size="small" icon="el-icon-delete" @click="del()" :disabled="$refs.helpTable && $refs.helpTable.getCheckboxRecords().length === 0" plain>删除</el-button>
          </template>
        </vxe-toolbar>
        <div style="height: calc(100% - 80px);">
          <vxe-table
            border="inner"
            auto-resize
            resizable
            height="auto"
            :loading="loading"
            size="small"
            ref="helpTable"
            show-header-overflow
            show-overflow
            highlight-hover-row
            :data="dataList"
          >
            <vxe-column type="seq" width="60" title="序号"></vxe-column>
            <vxe-column type="checkbox"  width="40px"></vxe-column>
            <vxe-column min-width="100px" align="center" title="关联项" field="treeName"></vxe-column>
            <vxe-column min-width="100px" align="center" title="编号" field="number">
              <template slot-scope="scope">
                <el-link type="primary" :underline="false" v-if="hasPermission('help:view')" @click="view(scope.row.id)">{{scope.row.number}}</el-link>
                <span v-else>{{scope.row.number}}</span>
              </template>
            </vxe-column>
            <vxe-column min-width="100px" align="center" title="功能" field="features"></vxe-column>
            <vxe-column min-width="100px" align="center" title="描述" field="description"></vxe-column>
            <vxe-column min-width="80px" align="center" title="停用/启用" fixed="right" field="useFlag">
              <template slot-scope="scope">
<!--                <el-tag v-if="scope.row.useFlag === '1'" size="small" type="success">正常</el-tag>-->
<!--                <el-tag v-else-if="scope.row.useFlag === '0'" size="small" type="danger">禁用</el-tag>-->
                <el-switch
                  v-model="scope.row.useFlag === '1'"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                  @change="updateStatus(scope.row.id)"
                >
                </el-switch>
              </template>
            </vxe-column>
            <vxe-column title="操作" width="100px" fixed="right" align="center">
              <template slot-scope="scope">
                <el-button v-if="hasPermission('help:edit')" type="text" size="small"  @click="edit(scope.row.id)">修改</el-button>
                <el-button v-if="hasPermission('help:del')" type="text" size="small"  @click="del(scope.row.id)">删除</el-button>
              </template>
            </vxe-column>
          </vxe-table>
          <vxe-pager
            background
            size="small"
            :current-page="tablePage.currentPage"
            :page-size="tablePage.pageSize"
            :total="tablePage.total"
            :page-sizes="[10, 20, 100, 1000, {label: '全量数据', value: 1000000}]"
            :layouts="['PrevPage', 'JumpNumber', 'NextPage', 'FullJump', 'Sizes', 'Total']"
            @page-change="currentChangeHandle">
          </vxe-pager>
        </div>
        <!-- 弹窗, 新增 / 修改 -->
        <HelpForm  ref="helpForm" @refreshDataList="refreshList"></HelpForm>
      </div>
    </div>
  </div>
</template>

<script>
  import HelpForm from '@/views/modules/help/HelpForm'
  import HelpService from '@/api/help/HelpService'
  import HelpTreeService from '@/api/help/HelpTreeService'

  export default {
    data () {
      return {
        searchForm: {
          treeId: '',
          number: '',
          features: '',
          description: ''
        },
        filterText: '',
        dataList: [],
        helpTreeData: [],
        tablePage: {
          total: 0,
          currentPage: 1,
          pageSize: 10,
          orders: []
        },
        loading: false
      }
    },
    components: {
      HelpForm
    },
    helpTreeService: null,
    helpService: null,
    created () {
      this.helpTreeService = new HelpTreeService()
      this.helpService = new HelpService()
    },
    activated () {
      this.refreshTree()
      this.refreshList()
    },
    watch: {
      filterText (val) {
        this.$refs.helpTree.filter(val)
      }
    },
    methods: {
      filterNode (value, data) {
        if (!value) return true
        return data.name.indexOf(value) !== -1
      },
      renderContent (h, { node, data, store }) {
        return (
          <span class="custom-tree-node">
                {
                  data.parentId === '0' ? <i class="fa fa-sitemap"></i> : <i class="el-icon-share"></i>
                }
            <span class="text">{node.label}</span>
              </span>
        )
      },
      // 获取数据列表
      refreshList () {
        this.loading = true
        this.helpService.list({
          'current': this.tablePage.currentPage,
          'size': this.tablePage.pageSize,
          'orders': this.tablePage.orders,
          ...this.searchForm
        }).then(({data}) => {
          this.dataList = data.records
          this.tablePage.total = data.total
          this.loading = false
        })
      },
      refreshTree () {
        this.loading = true
        this.helpTreeService.getList().then(({data}) => {
          this.helpTreeData = data
          this.loading = false
        }).catch(() => {
          this.loading = false
        })
      },
      // 当前页
      currentChangeHandle ({ currentPage, pageSize }) {
        this.tablePage.currentPage = currentPage
        this.tablePage.pageSize = pageSize
        this.refreshList()
      },
      // 新增
      add () {
        this.$refs.helpForm.init('add', '')
      },
      // 修改
      edit (id) {
        id = id || this.$refs.helpTable.getCheckboxRecords().map(item => {
          return item.id
        })[0]
        this.$refs.helpForm.init('edit', id)
      },
      // 查看
      view (id) {
        this.$refs.helpForm.init('view', id)
      },
      // 删除
      del (id) {
        let ids = id || this.$refs.helpTable.getCheckboxRecords().map(item => {
          return item.id
        }).join(',')
        this.$confirm(`确定删除所选项吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          this.helpService.delete(ids).then(() => {
            this.loading = false
            this.refreshList()
          }).catch(() => {
            this.loading = false
          })
        })
      },
      handleNodeClick (data) {
        if (data.parentId === '0') {
          this.searchForm.treeId = ''
        } else {
          this.searchForm.treeId = data.id
        }
        this.refreshList()
      },
      resetSearch () {
        this.filterText = ''
        this.$refs.helpTree.setCurrentKey(null)
        this.$refs.searchForm.resetFields()
        this.searchForm = {
          treeId: '',
          number: '',
          features: '',
          description: ''
        }
        this.refreshTree()
        this.refreshList()
      },
      // 修改帮助状态 启用/停用
      updateStatus (id) {
        this.loading = true
        this.helpService.updateStatus(id).then(({data}) => {
          this.$message.success(data)
          this.loading = false
          this.refreshList()
        }).catch(() => {
          this.loading = false
        })
      }
    }
  }
</script>
<style lang="scss">
  .el-card__body {
    overflow: auto;
  }
</style>
