<template>
  <div class="app-container">
    <el-row class="mb8">
      <el-button type="success" size="mini" icon="el-icon-plus" @click="handleNewScoreRule">{{ $t("添加规则") }}</el-button>
      <el-button type="success" size="mini" icon="el-icon-delete" @click="handleDelScoreRule">{{ $t("删除规则") }}</el-button>
    </el-row>
    <el-table ref="multipleTable" v-loading="loading" @selection-change="handleSelectionChange" :data="memberLevelList">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column width="140" :label="$t('序号')" align="center" prop="id"></el-table-column>
      <el-table-column :label="$t('等级名称')" align="center" prop="name"></el-table-column>

      <el-table-column width="140" :label="$t('图标')" align="center" prop="mobile">
        <template slot-scope="scope">
          <el-image :src="scope.row.icon" style="width: 50px; height: 50px">{{ $t("无") }}</el-image>
        </template>
      </el-table-column>

      <el-table-column :label="$t('积分范围')" align="center">
        <template slot-scope="scope"> {{ scope.row.lowerCount }}-{{ scope.row.upperCount }} </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">{{ $t("修改") }}</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">{{ $t("删除") }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- //分页列表 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize" @pagination="handleQueryPagination" />

    <el-dialog :title="$t('添加规则')" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
      <el-form :model="ruleForm" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item :label="$t('等级名称')">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item :label="$t('积分范围')">
          <el-col :span="11">
            <el-form-item>
              <el-input v-model="ruleForm.lowerCount"></el-input>
            </el-form-item>
          </el-col>
          <el-col class="line" :span="2">{{ $t("至") }}</el-col>
          <el-col :span="11">
            <el-form-item>
              <el-input v-model="ruleForm.upperCount"></el-input>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item :label="$t('上传图标')">
          <image-upload v-model="ruleForm.icon" :fileType="['png', 'jpg', 'jpeg']" :limit="1" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">{{ $t("取 消") }}</el-button>
        <el-button type="primary" @click="handelAddNewMemberLevel">{{ $t("确定") }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getDictDatas, DICT_TYPE } from "@/utils/dict"
import { getMemberLevelList, addNewMemberLevel, delMemberLevel, editMemberLevel } from "@/api/ecw/memberManagement"
import ImageUpload from "@/components/ImageUpload"
export default {
  name: "integralRecord",
  components: {
    ImageUpload
  },
  data() {
    return {
      dialogVisible: false,
      selectList: [],
      ruleForm: {
        icon: "",
        lowerCount: "",
        name: "",
        upperCount: ""
      },
      total: 0,
      loading: true,
      memberLevelList: [],
      queryParams: {
        pageNo: 1,
        pageSize: 10
      }
    }
  },
  computed: {
    isChinese() {
      return this.$i18n.locale === "zh_CN"
    }
  },
  created() {
    this.handleQuery()
  },
  activated() {
    this.handleQuery()
  },
  methods: {
    handleUpdate(row) {
      this.ruleForm = { ...row }
      this.dialogVisible = true
    },
    handleDelete(row) {
      let ids = [row.id]
      delMemberLevel(ids).then((res) => {
        this.$message.success(this.$t("删除成功"))
        this.handleClose()
        this.handleQuery()
      })
    },
    handelAddNewMemberLevel() {
      if (this.ruleForm.id) {
        editMemberLevel(this.ruleForm).then((res) => {
          this.$message.success(this.$t("更新成功"))
          this.handleClose()
          this.handleQuery()
        })
      } else {
        let params = { ...this.ruleForm }
        addNewMemberLevel(params).then((res) => {
          this.$message.success(this.$t("保存成功"))
          this.handleClose()
          this.handleQuery()
        })
      }
    },
    handleSelectionChange(ids) {
      this.selectList = ids
    },
    handleClose() {
      this.dialogVisible = false
      this.selectList = []
      this.ruleForm = {
        icon: "",
        lowerCount: "",
        name: "",
        upperCount: ""
      }
    },
    handleDelScoreRule() {
      if (this.selectList.length > 0) {
        let ids = this.selectList.map((item) => {
          return item.id
        })
        delMemberLevel(ids).then((res) => {
          this.$message.success(this.$t("删除成功"))
          this.handleClose()
          this.handleQuery()
        })
      } else {
        this.$message.warning(this.$t("请先选择要删除的会员等级"))
      }
    },
    handleNewScoreRule() {
      this.dialogVisible = true
    },
    handleQuery() {
      this.queryParams.pageNo = 1
      let params = { ...this.queryParams }
      getMemberLevelList(params).then((res) => {
        this.loading = false
        this.memberLevelList = res.data.list
        this.total = res.data.total
      })
    },
    handleQueryPagination() {
      let params = { ...this.queryParams }
      getMemberLevelList(params).then((res) => {
        this.loading = false
        this.memberLevelList = res.data.list
        this.total = res.data.total
      })
    }
  }
}
</script>
