<template>
  <div class="page">
    <topBar
      v-if="topList.length > 0"
      :topList="topList"
      :moren="moren"
    ></topBar>
    <div class="thetopbox">
      <el-form label-width="100px">
        <el-row>
          <el-col :span="4">
            <el-form-item :label="$t('会员编号')">
              <el-input clearable v-model="queryParams.memberCode"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="$t('生效状态')">
              <el-select clearable v-model="queryParams.status">
                <el-option value="1" label="无效"></el-option>
                <el-option value="2" label="有效"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('生效时间')">
              <el-date-picker
                v-model="queryParams.thedate"
                @change="checkTime"
                type="daterange"
                range-separator="——"
                :start-placeholder="'开始日期'"
                :end-placeholder="'结束日期'"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('失效时间')">
              <el-date-picker
                v-model="queryParams.thedates"
                @change="checkTime1"
                type="daterange"
                range-separator="——"
                :start-placeholder="'开始日期'"
                :end-placeholder="'结束日期'"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <div class="searchbox">
              <el-button class="my_search" @click="getSearch">
                {{ '搜索' }}</el-button
              >
              <el-button class="my_reset" @click="reChongzhi">
                {{ '重置' }}</el-button
              >
            </div>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="main">
      <div class="maintop">
        <div class="mainbtn">
          <el-button
            size="small"
            class="thebtn1"
            @click="addData"
            v-hasButtons="['LenjoyStoreAdd']"
            >{{ '添加' }}</el-button
          >
          <el-button
            size="small"
            class="thebtn2"
            @click="handleExport"
            v-hasButtons="['LenjoyStoreExport']"
          >
            {{ '导出' }}</el-button
          >
        </div>
      </div>
      <div class="maintable">
        <el-table
          :data="tableData"
          v-loading="loading"
          height="720px"
          style="width: 100%"
          :header-cell-style="{ background: '#EEEEEE' }"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column
            align="center"
            prop="memberCode"
            min-width="100px"
            :label="$t('会员编号')"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="memberName"
            :label="$t('会员姓名')"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="awardsName"
            :label="$t('真实奖衔')"
          >
          </el-table-column>
          <el-table-column
            align="center"
            min-width="100px"
            prop="status"
            :label="$t('生效状态')"
          >
            <template slot-scope="scope">
              <el-text type="danger" v-if="scope.row.status == 1">无效</el-text>
              <el-text type="success" v-if="scope.row.status == 2"
                >有效</el-text
              >
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            min-width="130px"
            prop="effectiveDate"
            :label="$t('生效时间')"
          >
          </el-table-column>
          <el-table-column
            align="center"
            min-width="130px"
            prop="failureDate"
            :label="$t('失效时间')"
          >
          </el-table-column>
          <el-table-column
            align="center"
            min-width="130px"
            prop="creationTime"
            :label="$t('创建时间')"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="time"
            :label="'操作'"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button
                @click="deleteRow(scope.row)"
                type="text"
                size="small"
                style="color: #c8161d"
                v-has-buttons="['LenjoyStoreDel']"
              >
                {{ $t("删除") }}
              </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="getSearch"
    />
    <el-dialog
      :title="dialogTitle"
      :close-on-click-modal="false"
      :visible.sync="dialogVisible"
      width="40%"
      center
      :before-close="handleClose"
    >
      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('会员编号')" prop="memberCode">
              <el-input v-model="form.memberCode" @change="getMemberInfo">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('会员姓名')" prop="memberName">
              <el-input disabled v-model="form.memberName"> </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">{{ '取消' }}</el-button>
        <el-button type="primary" @click="onSubmit('form')">{{
          '确认'
        }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import topBar from "@/components/topBar";
import * as con from "@/api/configManage";
import ImageUpload from "@/components/ImageUpload";
export default {
  name: "LenjoyStore",
  components: {
    topBar,
    ImageUpload,
  },
  data() {
    return {
      moren: "lenjoyStore",
      topList: [
        {
          name: this.$t("乐享店铺"),
          path: "lenjoyStore",
        },
      ],
      tableData: [],
      loading: false,
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        thedate: [],
        thedates: [],
      },
      total: 0,
      multipleSelection: [],

      dialogVisible: false,
      rules: {
        storeLevelName: [
          { required: true, message: "请输入店铺等级", trigger: "blur" },
        ],
        levelValue: [
          { required: true, message: "请选择店属性", trigger: "change" },
        ],

        picture: [
          {
            required: true,
            message: '请上传图片',
            trigger: "change",
          },
        ],
      },
      form: {
        memberCode: "",
        memberName: "",
      },

      levelValueList: [], //店铺属性下拉选
      dialogTitle: "",
      ifadd: "", //1添加2修改
      open_form: false,
    };
  },
  created() {
    this.getSearch();
    // this.getData();
  },
  methods: {
    getMemberInfo() {
      con.getLenjoyMember({ memberCode: this.form.memberCode }).then((res) => {
        if (res.code == 200) {
          this.$message({
            message: res.msg,
            type: "success",
          });
          this.form.memberName = res.data.memberName;
          this.open_form = true;
        } else {
          this.open_form = false;
        }
      });
    },
    deleteRow(row) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning",
      }).then(() => {
        con.delRangeStore(row.pkId).then((res) => {
          if (res.code == 200) {
            this.$message({
              message: res.msg,
              type: "success",
            });
            this.getSearch();
          } else {
            this.$message({
              message: res.msg,
              type: "warning",
            });
          }
        });
      });
    },
    addList() {
      this.form.detailList.push({
        endAchievement: "",
        ratio: "",
      });
    },
    subList() {
      this.form.detailList.pop();
    },
    //修改
    update(pkId) {
      this.dialogTitle = '修改';
      this.ifadd = 2;
      con.getStoreLevelOne({ pkId: pkId }).then((res) => {
        this.form = res.data;
        if (res.code == 200) {
          this.dialogVisible = true;
        }
      });
    },
    //导出
    handleExport() {
      this.$confirm('是否确认导出所有数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning",
      }).then((_) => {
        this.download(
          "/lenjoy-member/manage/range-store/export",
          { ...this.queryParams },
          `${this.$t("乐享店铺")}${new Date().getTime()}.xlsx`
        );
      });
    },
    onSubmit(formName) {
      if (!this.open_form) {
        this.$message({
          message: "请输入正确的会员编码",
          type: "warning",
        });
        return;
      }
      this.$refs[formName].validate((valid) => {
        if (valid) {
          con.rangeStoreAdd(this.form).then((res) => {
            if (res.code == 200) {
              this.$message({
                message: '添加成功',
                type: "success",
              });
              this.dialogVisible = false;
              this.getSearch();
            }
          });
        }
      });
    },
    handleClose() {
      this.dialogVisible = false;
      this.$refs["form"].clearValidate();
      this.$refs["form"].resetFields();
    },
    checkTime(val) {
      this.queryParams.startDate = val ? val[0] : "";
      this.queryParams.endDate = val ? val[1] : "";
    },
    checkTime1(val) {
      this.queryParams.failureStartDate = val ? val[0] : "";
      this.queryParams.failureEndDate = val ? val[1] : "";
    },
    getData() {
      con.rangeStoreStatus().then((res) => {
        this.noPageList = res.data;
      });
      con.getEnumsLevelValue().then((res) => {
        this.levelValueList = res.data;
      });
    },
    addData() {
      this.dialogTitle = '添加';
      this.ifadd = 1;
      this.form = {
        memberCode: "",
        memberName: "",
      };
      this.dialogVisible = true;
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    reChongzhi() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 50,
        thedate: [],
        thedates: [],
      };
      this.getSearch();
    },
    getSearch() {
      this.loading = true;
      con.rangeStoreList(this.queryParams).then((res) => {
        this.tableData = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-range-editor.el-input__inner {
  width: 100%;
}
::v-deep .el-table thead {
  color: #000000;
}
::v-deep .el-table .warning-row {
  background: #f9f9f9;
}
.page {
  padding: 10px 20px;
  background: #f9f9f9;
  font-size: 14px;
  .thetopbox {
    padding: 15px 20px 15px 0;
    background: #ffffff;
    border-radius: 8px;
    .searchbox {
      display: flex;
      align-items: center;
      margin-left: 40px;
    }
    .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: 10px 0;
      justify-content: space-between;
      align-items: center;
      .mainbtn {
        .thebtn1 {
          color: #ffffff;
          background: #48b2fd;
        }
        .thebtn2 {
          color: #ffffff;
          background: #ffad41;
        }
      }
    }
  }
}
</style>
