<template>
  <div class="page">
    <topBar
      v-if="topList.length > 0"
      :top-list="topList"
      :moren="moren"
    />
    <div class="main">
      <div class="maintop">
        <div class="mainbtn">
          <el-button
            size="mini"
            class="thebtn1"
            @click="adddate"
          >{{ '添加' }}</el-button>
          <el-button size="mini" class="thebtn2"> {{ '导出' }}</el-button>
        </div>
      </div>
      <div class="maintable">
        <el-table
          v-loading="loading"
          height="750px"
          :data="tableData"
          style="width: 100%"
          :header-cell-style="{ background: '#EEEEEE' }"
          :row-class-name="tableRowClassName"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="111" />
          <el-table-column prop="rangeName" :label="'级差名称'" width="300" />
          <el-table-column prop="moneyRatio" :label="'金额比例'" />
          <el-table-column prop="achieveRatio" :label="'业绩比例'" />
          <el-table-column
            align="center"
            prop="rangeValueVal"
            :label="'级差属性'"
            width="120px"
          />
          <el-table-column
            align="center"
            prop="anames"
            :label="'注册等级'"
            width="300"
          />

          <el-table-column
            align="center"
            prop="dnames"
            :label="'真实奖衔'"
            width="auto"
          />

          <el-table-column align="center" prop="time" :label="'操作'" width="200">
            <template slot-scope="scope">
              <el-button
                type="text"
                size="small"
                class="button-s"
                style="color: #4391fd"
                @click.native.prevent="update(scope.row, tableData)"
              >
                {{ '修改' }}
              </el-button>
              <el-button
                type="text"
                class="button-s"
                size="small"
                style="color: #c73030"
                @click.native.prevent="deleteRow(scope.row, tableData)"
              >
                {{ '删除' }}
              </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="getRangeList"
    />
    <el-dialog
      :close-on-click-modal="false"
      :title="dialogTitle"
      :visible.sync="updateShow"
      width="37%"
      center
      :before-close="handleClose"
    >
      <el-form ref="form" :rules="rules" :model="form" label-width="90px">
        <el-row>
          <el-form-item :label="'级差名称'" prop="rangeName">
            <el-input
              v-model="form.rangeName"
              clearable
              placeholder=""
            />
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item :label="'级差属性'" prop="rangeValue">
            <el-select
              v-model="form.rangeValue"
              clearable
              placeholder=""
            >
              <el-option
                v-for="item in awardsValueList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item :label="'金额比例'" prop="moneyRatio">
              <el-input
                v-model="form.moneyRatio"
                clearable
                :placeholder="'请输入'"
              >
                <i slot="suffix" class="neibu">%</i>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="'业绩比例'" prop="achieveRatio">
              <el-input
                v-model="form.achieveRatio"
                clearable
                :placeholder="'请输入'"
              >
                <i slot="suffix" class="neibu">%</i>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="等级/奖衔" prop="rangeName">
            <!-- 等级 -->
            <el-checkbox-group v-model="form.gradeList">
              <el-checkbox
                v-for="item in gradeChioseList"
                :key="item.pkId"
                :label="item.pkId"
              >{{ item.gradeName }}</el-checkbox>
            </el-checkbox-group>
            <!-- 奖衔 -->
            <el-checkbox-group v-model="form.awardsList">
              <el-checkbox
                v-for="item in awardsChioseList"
                :key="item.pkId"
                :label="item.pkId"
              >{{ item.awardsName }}</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button class="cancels" @click="handleClose">{{ '取消' }}</el-button>
        <el-button
          class="submit"
          style="margin-left: 40px"
          @click="onSubmit('form')"
        >{{ '确认' }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import topBar from '@/components/topBar'
import {
  getRangeList,
  deleteRange,
  getAwardsListChiose,
  getgradeRanglist,
  getOneManage,
  updateManage,
  scRangeGenerate,
  getJcsxList
} from '@/api/level'
export default {
  name: 'Fgjc',
  components: {
    topBar
  },
  data() {
    return {
      moren: 'fgjc',
      topList: [
        {
          name: '等级配置',
          path: 'level'
        },
        {
          name: '奖衔配置',
          path: 'jxpz'
        },
        {
          name: '复购级差',
          path: 'fgjc'
        }

      ],
      queryParams: {
        pageNum: 1,
        pageSize: 50
      },
      total: 0,
      multipleSelection: [],
      tableData: [],
      loading: false,
      form: {
        rangeName: '',
        gradeList: [],
        awardsList: [],
        moneyRatio: '',
        achieveRatio: '',
        rangeValue: ''
      },
      rules: {
        moneyRatio: [
          { required: true, message: '请输入金额比例', trigger: 'blur' }
        ],
        achieveRatio: [
          { required: true, message: '请输入业绩比例', trigger: 'blur' }
        ],

        rangeName: [
          { required: true, message: '请输入奖衔名称', trigger: 'blur' }
        ],
        rangeValue: [
          { required: true, message: '请选择级差属性', trigger: 'change' }
        ]
      },
      awardsChioseList: [],
      gradeChioseList: [],
      updateShow: false,
      dialogTitle: '',
      addEdit: '',
      awardsValueList: []
    }
  },
  created() {
    this.getRangeList()
    this.getOthers()
  },
  methods: {
    // 添加
    adddate() {
      this.dialogTitle = '添加'
      this.updateShow = true
      this.addEdit = 1
      this.$refs['form'].clearValidate()
      this.$refs['form'].resetFields()
      // scRangeGenerate({}).then(res=>{
      //   if (res.code == 200) {
      //     this.getRangeList();
      //   }
      // })
    },
    // 修改
    update(row) {
      this.dialogTitle = '修改'
      this.addEdit = 2
      this.updateShow = true
      getOneManage(row.pkId).then((res) => {
        this.form = res.data
      })
    },
    // 提交
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (
            this.form.gradeList.length == 0 &&
            this.form.awardsList.length == 0
          ) {
            this.$message({
              message: '请至少选择一个等级或奖衔',
              type: 'warning'
            })
          } else {
            let url = ''
            if (this.addEdit == 1) {
              // 添加
              url = scRangeGenerate
            } else {
              // 修改
              url = updateManage
            }
            // 提交接口
            url(this.form).then((res) => {
              if (res.code == 200) {
                this.updateShow = false
                this.$message({
                  message: res.msg,
                  type: 'success'
                })
                this.getRangeList()
                this.addEdit = ''
              }
            })
          }
        } else {
          return false
        }
      })
    },
    getOthers() {
      getAwardsListChiose().then((res) => {
        this.awardsChioseList = res.rows
      })
      getgradeRanglist().then((res) => {
        this.gradeChioseList = res.data
      })
      getJcsxList().then(res => {
        this.awardsValueList = res.data
      })
    },

    // 获取级差列表
    getRangeList() {
      this.loading = true
      getRangeList(this.queryParams).then((res) => {
        res.rows.forEach((element) => {
          element.bnames = []
          element.anames = ''
          element.rangeDetailsGrade.forEach((item) => {
            element.bnames.push(item.pkGradeVal)
            const str = element.bnames.join('/')
            element.anames = str
          })
          element.cnames = []
          element.dnames = ''
          element.rangeDetailsAward.forEach((item) => {
            element.cnames.push(item.pkAwardVal)
            const str2 = element.cnames.join('/')
            element.dnames = str2
          })
        })
        this.tableData = res.rows
        this.total = res.total
        this.loading = false
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 删除
    deleteRow(row, item) {
      this.$confirm('确定删除?').then((_) => {
        deleteRange(row.pkId).then((res) => {
          if (res.code == 200) {
            this.$message({
              message: res.msg,
              type: 'success'
            })
            this.getRangeList()
          }
        })
      })
    },
    // 关闭弹窗
    handleClose() {
      this.updateShow = false
      this.$refs['form'].clearValidate()
      this.$refs['form'].resetFields()
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 == 1) {
        return 'warning-row'
      } else if (rowIndex % 2 == 0) {
        return 'success-row'
      }
      return ''
    }
  }
}
</script>

<style lang="scss" scoped>
.button-s {
  padding: 3px 0;
}
::v-deep .el-select {
  width: 100%;
}
.submit {
  width: 209px;
  height: 48px;
  background: #c8161d;
  border-radius: 5px;
  color: #fff;
  font-size: 18px;
  border-color: #c8161d;
  margin-left: 20px;
}
.cancels {
  width: 209px;
  height: 48px;
  background: #cccccc;
  border-radius: 5px;
  color: #fff;
  font-size: 18px;
  border-color: #ccc;
}
::v-deep .el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 100%;
}
::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: 10px 20px;
  background: #f9f9f9;
  font-size: 14px;
  .main {
    //margin-top: 20px;
    //background: #ffffff;
    //border-radius: 8px;
    //box-shadow: 0px 2px 20px 0px rgba(238, 238, 238, 0.5);
    background: #f9f9f9;
    .maintop {
      display: flex;
      // padding: 10px 0;
      justify-content: space-between;
      align-items: center;
      .mainbtn {
        .thebtn1 {
        color: #ffffff;
        background: #3181e5;
      }
      .thebtn2 {
        color: #ffffff;
        background: #ffad41;
      }
      }
    }
  }
}
</style>
