<template>
  <div class="page">
    <topBar
      v-if="topList.length > 0"
      :top-list="topList"
      :moren="moren"
    />
    <div class="thetopbox">
      <el-form label-width="auto">
        <el-row :gutter="10">
          <el-col :span="4">
            <el-form-item label="分组名称" prop="name">
              <el-input v-model="queryParams.name" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <div class="searchbox">
              <el-button class="delBtn" @click="handleSearch()">
                {{ '搜索' }}</el-button>
              <el-button @click="reset"> {{ '重置' }}</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="main">
      <div class="maintop">
        <div class="mainbtn">
          <el-button
            v-hasButtons="['GroupConfigurationListAdd']"
            size="small"
            class="thebtn1"
            @click="handleAddOrEdit(true)"
          >添加</el-button>
        </div>
      </div>
      <div class="maintable">
        <el-table
          v-loading="loading"
          height="740px"
          style="width: 100%"
          :header-cell-style="{ background: '#EEEEEE' }"
          :row-class-name="tableRowClassName"
          :data="tableData"
        >
          >
          <el-table-column type="selection" width="55" />

          <el-table-column
            align="center"
            prop="name"
            label="分组名称"
          />

          <el-table-column
            align="center"
            prop="creationTime"
            :label="'创建日期'"
          />
          <el-table-column
            align="center"
            prop="modifyTime"
            label="修改时间"
          />
          <el-table-column
            align="center"
            prop="time"
            :label="'操作'"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button
                v-hasButtons="['GroupConfigurationListEdit']"
                class="button-s"
                type="text"
                size="small"
                style="color: #6962f6"
                @click.native.prevent="handleAddOrEdit(false, scope.row)"
              >
                {{ '修改' }}
              </el-button>
              <el-button
                v-hasButtons="['GroupConfigurationListDel']"
                class="button-s"
                type="text"
                size="small"
                style="color: #c73030"
                @click.native.prevent="deleteRow(scope.row)"
              >
                {{ '删除' }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getGroupConfigurationList"
    />
    <el-dialog
      :close-on-click-modal="false"
      :visible.sync="updateDialogVisibe"
      :title="updateTitle"
      width="500px"
      @close="handleClose"
    >
      <el-form :model="form" label-width="100px">
        <el-form-item label="分组名称">
          <el-input v-model="form.name" />
        </el-form-item>
      </el-form>
      <div class="footer" style="text-align: center; margin-top: 20px">
        <el-button @click="updateDialogVisibe = false">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import topBar from '@/components/topBar/checked.vue'
import { topList } from './mixins'
import { getRouters } from '@/api/settle'
import { getGroupConfigurationList, addGroupConfiguration, updateGroupConfiguration, deleteGroupConfiguration } from '@/api/systemconfiguration'
export default {
  name: 'Txpz',
  components: {
    topBar
  },
  data() {
    return {
      form: {
        name: undefined // 分组名称
      },
      moren: 'groupConfiguration',
      topList: topList,
      tableData: [],
      loading: false,
      multipleSelection: [],
      total: 0,
      cjsj: '',
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        name: ''
      },
      updateShow: false,
      updateDialogVisibe: false, // 添加还是修改 true添加 false修改
      updateTitle: '',
      isAdd: false,
      vertexList: []
    }
  },
  created() {
    this.getGroupConfigurationList()
    this.getUserRoute()
  },
  methods: {
    getUserRoute() {
      getRouters().then((res) => {
        console.log(res.data, 'res.data', this.topList)
        res.data.forEach((item) => {
          this.topList.forEach((items) => {
            if (item.routeName == items.url) {
              this.$set(items, 'changed', true)
            }
          })
        })
      })
    },
    reset() {
      this.queryParams.pageNum = 1
      this.queryParams.name = ''
      this.getGroupConfigurationList()
    },
    handleSearch() {
      this.queryParams.pageNum = 1
      this.getGroupConfigurationList()
    },
    getGroupConfigurationList() {
      this.loading = true
      if (this.cjsj) {
        this.queryParams.createStartDate = this.cjsj[0]
        this.queryParams.createEndDate = this.cjsj[1]
      }
      getGroupConfigurationList({
        ...this.queryParams,
        name: this.queryParams.name || undefined
      }).then((res) => {
        this.tableData = res.rows
        this.total = res.total
        this.loading = false
      })
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 == 1) {
        return 'warning-row'
      } else if (rowIndex % 2 == 0) {
        return 'success-row'
      }
      return ''
    },
    handleAddOrEdit(isAdd, row) {
      this.isAdd = isAdd
      if (isAdd) {
        this.updateDialogVisibe = true
        this.updateTitle = '添加'
      } else {
        this.updateDialogVisibe = true
        this.updateTitle = '修改'
        this.form = JSON.parse(JSON.stringify(row))
      }
    },
    handleConfirm() {
      const api = this.isAdd ? addGroupConfiguration : updateGroupConfiguration
      api(this.form).then((res) => {
        if (res.code !== 200) return
        this.updateDialogVisibe = false
        this.getGroupConfigurationList()
      })
    },
    deleteRow(row) {
      this.$confirm('确定删除该分组吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteGroupConfiguration(row).then((res) => {
          if (res.code !== 200) return
          this.getGroupConfigurationList()
        })
      })
    },
    handleClose() {
      this.form.name = ''
    }
  }
}
</script>

<style lang="scss" scoped>

::v-deep .el-table .warning-row {
  background: #f9f9f9;
}

::v-deep .el-table .success-row {
  background: #ffffff;
}
::v-deep .el-table thead {
  color: #000000;
}
::v-deep .el-row {
  // margin-top: 20px;
}
.page {
  padding: 20px;
  background: #f9f9f9;
  font-size: 14px;
  .thetopbox {
    padding: 0 20px 0px 20px !important;
    background: #ffffff;
    border-radius: 8px;

    .searchbox {
      display: flex;
      align-items: center;
      .searchtitle {
        margin-right: 10px;
      }
      .searchbtn {
        background: #08143f;
        color: #ffffff;
      }
    }
  }
  .main {
    //margin-top: 20px;
    background: #f9f9f9;
    //border-radius: 8px;
    //box-shadow: 0px 2px 20px 0px rgba(238, 238, 238, 0.5);

    .maintop {
      display: flex;
      padding: 0px 0;
      justify-content: space-between;
      align-items: center;

      .mainbtn {
        .thebtn1 {
          background: #3181e5;
          color: #ffffff;
          width: 68px;
          height: 32px;
        }
        .thebtn2 {
          background: #ffad41;
          color: #ffffff;
          width: 68px;
          height: 32px;
        }
      }
    }
    .maintable {
      // max-height: 550px;
      overflow: auto;
    }
  }
}
::v-deep .el-range-editor {
  width: auto;
}
::v-deep .el-select {
  width: 100%;
}
</style>
