<template>
  <div>
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      v-dialogDrag
      width="410px"
      height="500px"
      append-to-body
      @close="close"
      @keyup.enter.native="getData"
      :visible.sync="visible">
      <div style="height: calc(100% - 50px);">
        <el-form size="small" :inline="true" class="query-form" ref="searchForm" :model="searchForm"  @submit.native.prevent>
          <el-form-item prop="name">
            <el-input size="small" v-model="searchForm.name" placeholder="请输入名称" clearable></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="list()" size="small" >查询</el-button>
            <el-button @click="resetSearch()" size="small" >重置</el-button>
          </el-form-item>
        </el-form>
        <vxe-toolbar size="medium">
          <template #tools>
            <vxe-button content="新建关联项" @click="addMenu"></vxe-button>
          </template>
        </vxe-toolbar>
        <vxe-table
          border="inner"
          auto-resize
          resizable
          height="360"
          :loading="loading"
          size="small"
          ref="helpTreeTable"
          show-header-overflow
          show-overflow
          :show-header="false"
          :row-config="{isCurrent: true}"
          :radio-config="{trigger: 'row'}"
          :data="dataList"
          :tree-config="{transform: true, rowField: 'id', parentField: 'parentId'}"
          :checkbox-config="{}">
          <vxe-column type="radio" width="40" ></vxe-column>
          <vxe-column min-width="300" title="名称" field="name" align="left" tree-node></vxe-column>
        </vxe-table>
      </div>
      <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="close()" icon="el-icon-circle-close">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="getData()" icon="el-icon-circle-check" v-noMoreClick>确定</el-button>
    </span>
      <HelpTreeAddForm ref="helpTreeAddForm" @refreshDataList="resetSearch"></HelpTreeAddForm>
    </el-dialog>
  </div>
</template>

<script>
  import InputNumber from '@/views/modules/sys/workContract/InputNumber.vue'
  import HelpTreeService from '@/api/help/HelpTreeService'
  import SelectUserTree from '@/views/modules/utils/treeUserSelect'
  import HelpTreeAddForm from './HelpTreeAddForm'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        tablePage: {
          total: 0,
          currentPage: 1,
          pageSize: 10,
          orders: []
        },
        dataList: [],
        searchForm: {
          name: ''
        },
        expandAll: true
      }
    },
    helpTreeService: null,
    created () {
      this.helpTreeService = new HelpTreeService()
    },
    components: {
      SelectUserTree,
      InputNumber,
      HelpTreeAddForm
    },
    methods: {
      init () {
        this.title = '关联项选择'
        this.visible = true
        this.list()
      },
      // 表单提交
      getData () {
        let row = this.$refs.helpTreeTable.getRadioRecord()
        if (this.commonJS.isEmpty(row)) {
          this.$message.error('请至少选择一条数据')
        } else {
          this.close()
          this.$emit('getData', row)
        }
      },
      list () {
        this.dataList = []
        this.loading = true
        this.helpTreeService.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
          this.$nextTick(() => {
            this.$refs.helpTreeTable.setAllTreeExpand(true)
          })
        })
      },
      // 当前页
      currentChangeHandle ({currentPage, pageSize}) {
        this.tablePage.currentPage = currentPage
        this.tablePage.pageSize = pageSize
        this.list()
      },
      resetSearch () {
        this.$refs.searchForm.resetFields()
        this.list()
      },
      close () {
        this.$refs.searchForm.resetFields()
        this.visible = false
      },
      addMenu () {
        this.$refs.helpTreeAddForm.init()
      }
    }
  }
</script>
<style scoped>
  /deep/ .el-dialog__body {
    padding-top: 0;
  }
  /deep/ .el-form-item--mini.el-form-item, .el-form-item--small.el-form-item {
    margin-bottom: 0px;
  }
</style>
