<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true"  label-width="68px">
      <el-form-item :label="$t('名称')" prop="name">
        <el-input v-model="queryParams.name" :placeholder="$t('请输入名称')" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item :label="$t('等级')" prop="level">
        <dict-selector filterable multiple clearable
          v-model="queryParams.level"
          :type="DICT_TYPE.MEMBERSHIP_LEVELS"
        ></dict-selector>
      </el-form-item>
      <el-form-item :label="$t('状态')" prop="status">
        <dict-selector filterable multiple clearable
          v-model="queryParams.status"
          :type="DICT_TYPE.ENABLE_STATUS"
        ></dict-selector>
      </el-form-item>
      <el-form-item :label="$t('启用时间')">
        <el-date-picker
          v-model="dateRangeCreateTime"
          type="datetimerange"
          value-format="yyyy-MM-dd HH:mm:ss"
          :start-placeholder="$t('开始日期')"
          :end-placeholder="$t('结束日期')"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">{{$t('搜索')}}</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">{{$t("重置")}}</el-button>
      </el-form-item>
    </el-form>

    <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 width="90" :label="$t('编号')" align="center">
        <template #default="{ row }">
          <el-button size="mini" type="text" @click="handleViewDetails(row)"> {{ row.ruleNumber || "/" }}</el-button>
        </template>
      </el-table-column>
      <el-table-column :label="$t('名称')" align="center" prop="name"></el-table-column>
      <el-table-column :label="$t('等级')" align="center" prop="level">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.MEMBERSHIP_LEVELS" :value="scope.row.level" />
        </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" prop="validityPeriod"></el-table-column>
      <el-table-column :label="$t('积分累计周期')" align="center" prop="accumulationPeriod"></el-table-column>
      <el-table-column :label="$t('积分累计开始日期')" min-width="100px"  align="center">
        <template #default="{ row }"> {{ parseTime(row.startDate) || "/" }}</template>
      </el-table-column>
      <el-table-column :label="$t('规则有效期')" width="150px" align="center">
        <template #default="{ row }"> {{ parseTime(row.startTime) }}-{{ parseTime(row.endTime) }}</template>
      </el-table-column>
      <el-table-column width="140" :label="$t('图标')" align="center" prop="icon">
        <template slot-scope="scope">
          <el-image v-show="scope.row.icon" :src="scope.row.icon" style="width: 50px; height: 50px">{{ $t("无") }}</el-image>
        </template>
      </el-table-column>
      <el-table-column :label="$t('启用时间')" align="center">
        <template #default="{ row }"> {{ parseTime(row.enableDate) || "/" }}</template>
      </el-table-column>
      <el-table-column :label="$t('状态')" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.ENABLE_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="text" v-if="scope.row.status==0" @click="handleUpdate(scope.row)">{{ $t("修改") }}</el-button>
          <el-button size="mini" type="text" v-if="scope.row.status==0" @click="handleDelete(scope.row)">{{ $t("删除") }}</el-button>
          <el-button size="mini" v-if="scope.row.status==0" type="text"  @click="handleEnable(scope.row)">{{ $t("启用") }}</el-button>
          <el-button size="mini" v-if="scope.row.status==1&&scope.row.status!=3" type="text"  @click="handleCloseButton(scope.row)">{{ $t("停用") }}</el-button>
          <el-button size="mini" type="text"  @click="handleCopyButton(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(titleLabel)" :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.ruleNumber" :disabled="isDisable"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item :label="$t('状态')">-->
<!--          <dict-selector clearable :disabled="isDisable" :type="DICT_TYPE.ENABLE_STATUS" v-model="ruleForm.status"> </dict-selector>-->
<!--        </el-form-item>-->
<!--        <el-form-item :label="$t('名称')">-->
<!--          <el-input v-model="ruleForm.name"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item :label="$t('等级')">-->
<!--          <dict-selector clearable :disabled="isDisable" :type="DICT_TYPE.MEMBERSHIP_LEVELS" v-model="ruleForm.level"> </dict-selector>-->
<!--        </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('等级有效期')">-->
<!--          <el-input v-model="ruleForm.validityPeriod"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item :label="$t('积分累计周期')">-->
<!--          <el-input v-model="ruleForm.accumulationPeriod"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item :label="$t('积分开始累计日期')">-->
<!--          <el-input v-model="ruleForm.startDate"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item :label="$t('等级规则有效期')">-->
<!--          <el-col :span="11">-->
<!--            <el-form-item>-->
<!--              <el-input v-model="ruleForm.startTime"></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.endTime"></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-item :label="$t('等级说明')">-->
<!--          <el-input v-model="ruleForm.description"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item :label="$t('匹配顺序')">-->
<!--          <el-input v-model="ruleForm.sort"></el-input>-->
<!--        </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,
  memberLevelStatus,
} from "@/api/ecw/memberManagement"
import ImageUpload from "@/components/ImageUpload"
export default {
  name: "integralRecord",
  components: {
    ImageUpload
  },
  data() {
    return {
      dialogVisible: false,
      selectList: [],
      dateRangeCreateTime: [],
      ruleForm: {
        ruleNumber: null,
        status: null,
        level: null,
        icon: null,
        lowerCount: null,
        name: null,
        upperCount: null,
        validityPeriod: null,
        accumulationPeriod: null,
        startDate: null,
        startTime: null,
        endTime: null,
        description: null,
        sort: null
      },
      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: {
    resetQuery(){
      this.dateRangeCreateTime=[]
      this.queryParams= {
        pageNo: 1,
        pageSize: 10
      }
    },
    handleUpdate(row) {
      this.$router.push({
        path: "/member/operatingLevel",
        query: { id: row.id, pageStatus: "edit" }
      })
    },
    handleViewDetails(row) {
      this.$router.push({
        path: "/member/operatingLevel",
        query: { id: row.id, pageStatus: "view" }
      })
    },
    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.$router.push({
        path: "/member/operatingLevel",
        query: { pageStatus: "new" }
      })
    },
    //启用登记规则
    handleEnable(row) {
      this.$modal
        .confirm(this.$t("是否确认启用此等级规则"))
        .then(function () {
          return memberLevelStatus({ id: row.id, status: "1" })
        })
        .then(() => {
          this.handleQuery()
          this.$modal.msgSuccess(this.$t("启用成功"))
        })
        .catch(() => {
        })
    },
    // 停用按钮操作
    handleCloseButton(row) {
      this.$modal
        .confirm(this.$t("是否确认停用此等级规则"))
        .then(function () {
          return memberLevelStatus({ id: row.id, status: "2" })
        })
        .then(() => {
          this.handleQuery()
          this.$modal.msgSuccess(this.$t("关闭成功"))
        })
        .catch(() => {})
    },
    // 复制按钮操作
    handleCopyButton(row) {
      this.$router.push({
        path: "/member/operatingLevel",
        query: { pageStatus: "new",params: row }
      })
    },
    handleQuery() {
      this.queryParams.pageNo = 1
      let params = { ...this.queryParams }
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'Time', false);

      getMemberLevelList(params).then((res) => {
        this.loading = false
        this.memberLevelList = res.data.list
        this.total = res.data.total
      })
    },
    handleQueryPagination() {
      let params = { ...this.queryParams }
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, "Time", false)
      getMemberLevelList(params).then((res) => {
        this.loading = false
        this.memberLevelList = res.data.list
        this.total = res.data.total
      })
    }
  }
}
</script>
