<template>
  <div class="page">
    <topBar
      v-if="topList.length > 0"
      :topList="topList"
      :moren="moren"
    ></topBar>
    <div class="thetopbox">
      <el-form label-width="70px">
        <el-row>
          <el-col :span="4">
            <el-form-item :label="'名称'">
              <el-input
                v-model="queryParams.name"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="'创建日期'">
              <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="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-has-buttons="['addHonor']"
          >{{'添加'}}</el-button
          >
        </div>
      </div>
      <div class="maintable">
        <el-table
          :data="tableData"
          v-loading="loading"
          height="730px"
          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="picture"
            :label="'图片'"
            width="180"
          >
            <template slot-scope="scope">
              <img
                style="width: 48px; height: 48px"
                :src="scope.row.img"
                alt=""
              />
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="valueStr"
            :label="'名称'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="typeStr"
            :label="'达标标准'"
          >
          </el-table-column>

          <el-table-column
            align="center"
            prop="creationTime"
            :label="'创建日期'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="time"
            :label="'操作'"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button
                class="button"
                @click="update(scope.row,scope.row.pkId)"
                type="text"
                size="small"
                style="color: #48B2FD"
                v-has-buttons="['updateHonor']"
              >
                {{'修改'}}
              </el-button>
              <el-button
                class="button"
                @click="deleteRow(scope.row,scope.row.pkId)"
                type="text"
                size="small"
                style="color: #C8161D"
                v-has-buttons="['delHonor']"
              >
                {{'删除'}}
              </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="30%"
      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="'名称'" prop="name">
              <el-input v-model="form.name" :placeholder="'请输入'"> </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="'标题'" prop="title">
              <el-input v-model="form.title" :placeholder="'请输入标题'"> </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="'达标标准'" prop="type">
              <el-select
                @change="changes"
                class="select-s"
                clearable
                :placeholder="'请选择'"
                v-model="form.type"
              >
                <el-option
                  v-for="item in levelValueList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="form.type==1">
            <el-form-item :label="'等级'" prop="value">
              <el-select
                class="select-s"
                clearable
                :placeholder="'请选择'"
                v-model="form.value"
              >
                <el-option
                  v-for="item in gradeChioseList"
                  :key="item.pkId"
                  :label="item.gradeName"
                  :value="item.pkId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24"  v-if="form.type==2">
            <el-form-item label="奖衔" prop="value">
              <el-select
                class="select-s"
                clearable
                :placeholder="'请选择'"
                v-model="form.value"
              >
                <el-option
                  v-for="item in awardsChioseList"
                  :key="item.pkId"
                  :label="item.awardsName"
                  :value="item.pkId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24"  v-if="form.type!=1 && form.type!=2 && form.type!='' " style="position: relative">
            <div class="unit" v-if="form.type==3">年</div>
            <el-form-item :label="levelValueList[selectIndex-1].label" prop="type">
              <el-input v-model="form.value" type="number">
                </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="'上传图片'" prop="img">
              <imageUpload v-model="form.img" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button class="close" type="primary" @click="handleClose"> {{ '取消' }}</el-button>
        <el-button class="submit" 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";
import {delMember, getHonorType, getMemberList, getStoreGradeAdd, getStoreGradeUpdate} from "@/api/configManage";
import {getAwardsListChiose, getgradeRanglist} from "@/api/level";
export default {
  name: "StoreGradeConfiguration",
  components: {
    topBar,
    ImageUpload,
  },
  data() {
    return {
      moren: "honorAdmin",
      topList: [
        {
          name: '荣誉馆配置',
          path: "honorAdmin",
        },
      ],
      tableData: [],
      loading:false,
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        thedate: [],
      },
      total: 0,
      multipleSelection: [],

      dialogVisible: false,
      rules: {
        name: [
          { required: true, message: '请输入', trigger: "blur" },
        ],
        title: [
          { required: true, message: '请输入', trigger: "blur" },
        ],
        value: [
          { required: true, message: '请输入', trigger: "blur" },
        ],
        type: [
          { required: true, message: "请选择达标标准", trigger: "change" },
        ],

        img: [
          {
            required: true,
            message: '请上传图片',
            trigger: "change",
          },
        ],
      },
      gradeChioseList: [],
      awardsChioseList: [],
      form: {
        title:'',
        name:'',
        value: "",
        type: "",
        img: "",
      },
      selectIndex:0,
      levelValueList: [], //荣誉馆下拉选择
      dialogTitle: "",
      ifadd: "", //1添加2修改
    };
  },
  created() {
    this.getSearch();
    this.getData();
  },
  methods: {
    //删除
    deleteRow(row) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning",
      }).then(() => {
        delMember(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",
            });
          }
        });
      });
    },
    changes(index) {
       this.selectIndex = index
       this.form.value = ''
    },
    addList() {
      this.form.detailList.push({
        endAchievement: "",
        ratio: "",
      });
    },
    subList() {
      this.form.detailList.pop();
    },
    //修改
    update(data) {
      this.selectIndex = data.type
      this.dialogTitle = '修改';
      this.ifadd = 2;
      this.form.name = data.name
      this.form.value = data.value
      this.form.type = data.type
      this.form.img = data.img
      this.form.pkId = data.pkId
      this.form.title = data.title
      this.dialogVisible = true;
    },
    //导出
    handleExport() {
      this.$confirm('是否确认导出所有数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning",
      }).then((_) => {
        this.download(
          "/system/manage/store-level/export",
          { ...this.queryParams },
          `${'店铺等级'}${new Date().getTime()}.xlsx`
        );
      });
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = '';
          if (this.ifadd == 1) {
            url = con.getStoreGradeAdd;
          } else if (this.ifadd == 2) {
            url = con.getStoreGradeUpdate;
          }
          url(this.form).then((res) => {
            if (res.code == 200) {
              this.$message({
                message: res.msg,
                type: 'success'
              });
              this.dialogVisible = false
              this.getSearch()
              this.form = {
                value: "",
                type: "",
                title: '',
                img: "",
                name:'',
              }
            }
          });
        }
      });
    },
    handleClose() {
      this.dialogVisible = false;
      this.$refs["form"].clearValidate();
      this.$refs["form"].resetFields();
      this.form = {
        value: "",
        type: "",
        img: "",
        name:'',
        title: '',
      }
    },
    checkTime(val) {
      this.queryParams.startDate = val[0];
      this.queryParams.endDate = val[1];
    },

    getData() {
      con.getHonorType().then((res) => {
        this.levelValueList = res.data;
      });
      getgradeRanglist().then((res) => {
        this.gradeChioseList = res.data;
      });
      getAwardsListChiose().then((res) => {
        this.awardsChioseList = res.rows;
      });
    },
    addData() {
      this.dialogTitle = '添加';
      this.ifadd = 1;
      this.form = {
        value: "",
        type: "",
        img: "",
        name:'',
        title: '',
      };
      this.dialogVisible = true;
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    reChongzhi() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 50,
        thedate: [],
      };
      this.getSearch();
    },
    getSearch() {
      this.loading = true
      con.getMemberList(this.queryParams).then((res) => {
        this.tableData = res.rows;
        this.total = res.total;
        this.loading = false
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.unit{
  font-size: 16px;
  color: #999;
  position: absolute;
  right:40px;
  z-index: 1;
  top: 12px;
}
.button{
  padding: 0 5px;
}
::v-deep .image-slot{
  display: flex;
  justify-content: center;
  align-items: center;
  height: 44px;
}
::v-deep .el-image{
  width: 44px !important;
  height: 44px !important;
}
::v-deep .el-upload--picture-card{
  min-width: 48px !important;
  min-height: 48px!important;
}
.select-s{
  width: 100%;
}
.submit{
  width: 209px;
  height: 48px;
  background: #48B2FD;
  border-radius: 5px 5px 5px 5px;
  opacity: 1;
  border: none;
  font-size: 18px;
  margin-left: 40px;
}
.close{
  font-size: 18px;
  border: none;
  width: 209px;
  height: 48px;
  background: #CCCCCC;
  border-radius: 5px 5px 5px 5px;
  opacity: 1;
}
::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 5px 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: 0px 0;
      justify-content: space-between;
      align-items: center;
      .mainbtn {
        .thebtn1 {
          color: #ffffff;
          background: #48b2fd;
          width: 68px;
          height: 32px;
        }
        .thebtn2 {
          color: #ffffff;
          background: #ffad41;
          width: 68px;
          height: 32px;
        }
      }
    }
  }
}
</style>

