<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="['VertexAwardConfigurationAdd']"
            size="small"
            class="thebtn1"
            @click="handleAdd()"
          >添加</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
            align="center"
            prop="vertexName"
            label="体系名称"
          />

          <el-table-column
            align="center"
            prop="bonusItems"
            label="奖项名称"
          >
            <template slot-scope="scope">
              <span>
                {{ getBonusItemByEnumKey(scope.row.bonusItems) }}
              </span>
            </template>
          </el-table-column>

          <el-table-column
            align="center"
            prop="time"
            :label="'操作'"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button
                v-hasButtons="['VertexAwardConfigurationDel']"
                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="getList"
    />
    <el-dialog
      :close-on-click-modal="false"
      :visible.sync="updateDialogVisibe"
      title="新增"
      width="500px"
      @close="handleClose"
    >
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="选择体系" prop="pkVertex">
          <el-select v-model="form.pkVertex" placeholder="请选择体系">
            <el-option v-for="item in vertexList" :key="item.pkId" :label="item.vertexName" :value="item.pkId" />
          </el-select>
        </el-form-item>
        <el-form-item label="奖项名称" prop="bonusItems">
          <el-select v-model="form.bonusItems" placeholder="请选择奖项名称">
            <el-option v-for="item in vertexAwardBonusOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </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 {
  getVertexAwardConfiguration,
  saveVertexAwardConfiguration,
  delVertexAwardConfiguration
} from '@/api/vertex'
import { getVertexAwardBonusOptions } from '@/api/enums'
import { getvertexValue } from '@/api/member'
export default {
  name: 'VertexAwardConfiguration',
  components: {
    topBar
  },
  data() {
    return {
      form: {
        pkVertex: null,
        bonusItems: null
      },
      rules: {
        pkVertex: [{ required: true, message: '请选择体系', trigger: 'change' }],
        bonusItems: [{ required: true, message: '请选择奖项名称', trigger: 'change' }]
      },
      total: 0,
      moren: 'vertexAwardConfiguration',
      topList: topList,
      tableData: [],
      loading: false,
      queryParams: {
        pageNum: 1,
        pageSize: 50
      },
      updateDialogVisibe: false,
      vertexAwardBonusOptions: [],
      vertexList: []
    }
  },
  created() {
    this.getList()
    this.getUserRoute()
    this.getVertexAwardBonusOptions()
    this.getvertexValue()
  },
  methods: {
    getVertexAwardBonusOptions() {
      getVertexAwardBonusOptions().then((res) => {
        this.vertexAwardBonusOptions = res.data
      })
    },
    getvertexValue() {
      getvertexValue().then((res) => {
        this.vertexList = res.data
      })
    },
    getBonusItemByEnumKey(enumKey) {
      return this.vertexAwardBonusOptions.find((item) => item.value === enumKey)?.label || ''
    },
    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.getList()
    // },
    // handleSearch() {
    //   this.queryParams.pageNum = 1
    //   this.getList()
    // },
    getList() {
      this.loading = true
      getVertexAwardConfiguration({
        ...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 ''
    },
    handleAdd() {
      this.updateDialogVisibe = true
    },
    handleConfirm() {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          saveVertexAwardConfiguration(this.form).then((res) => {
            if (res.code !== 200) return
            this.updateDialogVisibe = false
            this.getList()
          })
        }
      })
    },
    deleteRow(row) {
      this.$confirm('确定删除该条配置吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delVertexAwardConfiguration(row.pkId).then((res) => {
          if (res.code !== 200) return
          this.getList()
        })
      })
    },
    handleClose() {
      this.form.pkVertex = null
      this.form.bonusItems = null
    }
  }
}
</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;
}

.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;
      margin-top: 10px;
    }
  }
}
::v-deep .el-range-editor {
  width: auto;
}
::v-deep .el-select {
  width: 100%;
}
</style>
