<template>
  <div class="page">
    <el-form size="small" :inline="true" class="query-form" ref="searchForm" :model="searchForm" @keyup.enter.native="refreshList()" @submit.native.prevent>
      <!-- 搜索框-->
      <el-form-item prop="rankName">
        <el-input size="small" v-model="searchForm.rankName" placeholder="职级名称" clearable></el-input>
      </el-form-item>
      <el-form-item prop="levelId" :rules="[]">
        <el-select v-model="searchForm.levelId" @change="$forceUpdate()" style="width:100%" placeholder="请选择职层名称">
          <el-option
            v-for="rankLevel in rankLevelList"
            :key="rankLevel.id"
            :label="rankLevel.name"
            :value="rankLevel.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="rsId" :rules="[]">
        <el-select v-model="searchForm.rsId" @change="$forceUpdate()" style="width:100%" placeholder="请选择序列名称">
          <el-option
            v-for="sequence in sequenceList"
            :key="sequence.id"
            :label="sequence.name"
            :value="sequence.id">
          </el-option>
        </el-select>
      </el-form-item>
      <!--<el-form-item prop="name">
        <el-input size="small" v-model="searchForm.sequenceChildName" placeholder="子序列名称" clearable></el-input>
      </el-form-item>
      <el-form-item prop="rankRstLevelDTO.id" :rules="[]">
        <el-select v-model="searchForm.rankRstLevelDTO.id" style="width:100%" placeholder="请选择职层名称">
          <el-option
            v-for="rankLevel in rankLevelList"
            :key="rankLevel.id"
            :label="rankLevel.name"
            :value="rankLevel.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="name">
        <el-input size="small" v-model="searchForm.rankName" 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" style="">
<!--      <vxe-toolbar :refresh="{query: refreshList}" custom>-->
<!--        <template #buttons>-->
<!--          <el-button type="default" size="small"  @click="goLog()"  disabled 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="rankSystemTable"
          show-header-overflow
          show-overflow
          highlight-hover-row
          :menu-config="{}"
          @sort-change="sortChangeHandle"
          :sort-config="{remote:true}"
          :data="dataList"
          :checkbox-config="{}">
          <vxe-column type="seq" width="60" title="序号"></vxe-column>
<!--          <vxe-column type="checkbox" width="40" ></vxe-column>-->
          <vxe-column field="sequenceName" title="序列" :edit-render="{}"></vxe-column>
          <vxe-column v-if="isShowC === true" field="sequenceChildName" title="子序列" :edit-render="{}"></vxe-column>
          <vxe-column field="rankRstLevelDTO.name" title="职层" :edit-render="{}"></vxe-column>
          <vxe-column field="rankName" title="职级名称" :edit-render="{}"></vxe-column>
          <vxe-column field="rankRstPositionDTOList" title="适用职务" :edit-render="{}">
            <template scope="scope" >
              <el-tag v-for="(item, index) in scope.row.rankRstPositionDTOList">{{item.name}}</el-tag>
            </template>
          </vxe-column>
          <vxe-column field="rankRstPostDTOList" title="适用岗位" :edit-render="{}">
            <template scope="scope" >
              <el-tag v-for="(item, index) in scope.row.rankRstPostDTOList">{{item.name}}</el-tag>
            </template>
          </vxe-column>
          <vxe-column field="useFlag" title="启用状态" :edit-render="{}">
            <template  slot-scope="scope">
              <el-tag type="success" v-if="scope.row.useFlag === '1' ">已启用</el-tag>
              <el-tag type="danger" v-if="scope.row.useFlag === '2' ">禁用</el-tag>
            </template>
          </vxe-column>
          <vxe-column title="操作" width="200px" fixed="right" align="center">
            <template  slot-scope="scope">

              <el-button v-if="hasPermission('rank:rank_system_table:edit')" type="text" icon="el-icon-edit" size="small" @click="edit(scope.row)">修改</el-button>
              <el-button v-if="hasPermission('rank:rank_system_table:del')" type="text"  icon="el-icon-delete" 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>
    </div>
    <RankSystemTableForm  ref="rankSystemTableForm" @refreshDataList="refreshList"></RankSystemTableForm>
  </div>
</template>

<script>
  import RankSystemTableService from '@/api/rank/RankSystemTableService'
  import RankSystemTableForm from './RankSystemTableForm'
  import RankService from '@/api/rank/RankService'
  export default {
    props: {
      index: {
        type: String,
        default: ''
      },
      i: {
        type: String,
        default: ''
      }
    },
    data () {
      return {
        searchForm: {
          rankName: ''
        },
        dataList: [],
        tablePage: {
          total: 0,
          currentPage: 1,
          pageSize: 10,
          orders: []
        },
        loading: false,
        isShowC: false,
        rankLevelList: [],
        sequenceList: []
      }
    },
    rankSystemTableService: null,
    rankService: null,
    created () {
      this.rankSystemTableService = new RankSystemTableService()
      this.rankService = new RankService()
    },
    watch: {
      index (index) {
        if (index === this.i) {
          this.isShowC = false
          this.refreshList()
        }
      }
    },
    components: {
      RankSystemTableForm
    },
    mounted () {
      this.refreshList()
    },

    methods: {
      goLog () {
        this.$router.push({path: `/position/PositionLog`, query: {type: '1'}})
      },
      // 新增
      add () {
        this.$refs.rankSystemTableForm.init('add', '')
      },
      // 修改
      edit (row) {
        this.$refs.rankSystemTableForm.queryAllRankLevel()
        this.$refs.rankSystemTableForm.queryAllPosition()
        this.$refs.rankSystemTableForm.queryAllPost()
        this.$refs.rankSystemTableForm.init('edit', row.rankId, row.rcId, row.rsId, this.isShowC)
      },
      // 查看
      view (id) {
        this.$refs.rankSystemTableForm.init('view', id)
      },
      // 获取数据列表
      refreshList () {
        this.loading = true
        this.rankSystemTableService.list({
          'current': this.tablePage.currentPage,
          'size': this.tablePage.pageSize,
          'orders': this.tablePage.orders,
          ...this.searchForm
        }).then(({data}) => {
          this.dataList = data.records
          this.dataList.forEach(item => {
            if (item.sequenceChildName !== null && item.sequenceChildName !== undefined && item.sequenceChildName !== '') {
              this.isShowC = true
            }
          })
          this.tablePage.total = data.total
          this.queryAllRankLevel()
          this.getAllSequence()
          this.loading = false
        }).catch(() => {
          this.loading = false
        }).finally(() => {
          this.loading = false
        })
      },
      // 当前页
      currentChangeHandle ({currentPage, pageSize}) {
        this.tablePage.currentPage = currentPage
        this.tablePage.pageSize = pageSize
        this.refreshList()
      },
      // 排序
      sortChangeHandle (column) {
        this.tablePage.orders = []
        if (column.order != null) {
          this.tablePage.orders.push({column: this.$utils.toLine(column.property), asc: column.order === 'asc'})
        }
        this.refreshList()
      },
      resetSearch () {
        this.$refs.searchForm.resetFields()
        this.refreshList()
      },
      queryAllRankLevel () {
        this.rankSystemTableService.queryAllRankLevel().then(({data}) => {
          this.rankLevelList = JSON.parse(JSON.stringify(data))
        })
      },
      getAllSequence () {
        this.rankService.queryAllSequence().then(({data}) => {
          this.sequenceList = JSON.parse(JSON.stringify(data))
        })
      }
    }
  }
</script>
