<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="'店铺等级'">
              <el-select clearable v-model="queryParams.levelValue">
                <el-option
                  v-for="item in noPageList"
                  :key="item.pkId"
                  :label="item.storeLevelName"
                  :value="item.pkId"
                ></el-option>
              </el-select>
            </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"
            >{{'添加'}}</el-button
          >
          <el-button size="small" class="thebtn2" @click="handleExport"
            > {{ '导出' }}</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="picture"
            :label="'图片'"
            width="180"
          >
            <template slot-scope="scope">
              <img
                style="width: 48px; height: 48px"
                :src="scope.row.picture"
                alt=""
              />
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="storeLevelName"
            :label="'店铺等级'"
          >
          </el-table-column>
          <el-table-column align="center" prop="storeName" :label="'奖金比例'">
            <template slot-scope="scope">
              <div>
                <div style="border-bottom: 1px solid rgba(204, 204, 204, 0.5)">
                  <span style="margin-right: 5px">{{ '比例' }}</span>
                  <span
                    style="margin-right: 5px"
                    v-for="(aitem, aindex) in scope.row.detailList"
                    :key="aindex"
                    >{{ aitem.ratio }}</span
                  >
                </div>
                <div>
                  <span style="margin-right: 5px">{{ '业绩' }}</span>
                  <span
                    style="margin-right: 5px"
                    v-for="(bitem, bindex) in scope.row.detailList"
                    :key="bindex"
                  >
                    {{ bitem.endAchievement }}</span
                  >
                </div>
              </div>
            </template>
          </el-table-column>

          <el-table-column
            align="center"
            min-width="100px"
            prop="creationTime"
            :label="'创建日期'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="time"
            :label="'操作'"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button
                @click="update(scope.row.pkId)"
                type="text"
                size="small"
                style="color: #6962f6"
              >
                {{ '修改' }}
              </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
          style="
            border-bottom: 1px solid rgba(0, 0, 0, 0.1);
            margin-bottom: 10px;
          "
        >
          <el-col :span="12">
            <el-form-item :label="'店铺等级'" prop="storeLevelName">
              <el-input v-model="form.storeLevelName"> </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="'店铺属性'" prop="levelValue">
              <el-select
                clearable
                :placeholder="'请选择'"
                v-model="form.levelValue"
              >
                <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="12">
            <el-form-item :label="'上传图片'" prop="picture">
              <imageUpload v-model="form.picture" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-for="(item, i) in form.detailList" :key="i">
          <el-col :span="12">
            <el-form-item
              required
              :label="'业绩'"
              :prop="form.detailList[i].endAchievement"
            >
              <el-input v-model="form.detailList[i].endAchievement"> </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              required
              :label="'比例'+'(%)'"
              :prop="form.detailList[i].ratio"
            >
              <el-input v-model="form.detailList[i].ratio"> </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col style="text-align: center">
            <el-button
              circle
              icon="el-icon-plus"
              @click="addList()"
            ></el-button>
            <el-button
              circle
              icon="el-icon-minus"
              @click="subList()"
            ></el-button>
          </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: "StoreGradeConfiguration",
  components: {
    topBar,
    ImageUpload,
  },
  data() {
    return {
      moren: "storeGradeConfiguration",
      topList: [
        {
          name: '店铺等级',
          path: "storeGradeConfiguration",
        },
      ],
      tableData: [],
      loading:false,
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        thedate: [],
      },
      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: {
        storeLevelName: "",
        levelValue: "",
        picture: "",
        detailList: [
          {
            endAchievement: "1",
            ratio: "",
          },
        ],
      },
      noPageList: [], //店铺等级下拉列表
      levelValueList: [], //店铺属性下拉选
      dialogTitle: "",
      ifadd: "", //1添加2修改
    };
  },
  created() {
    this.getSearch();
    this.getData();
  },
  methods: {
    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(
          "/system/manage/store-level/export",
          { ...this.queryParams },
          `${'店铺等级'}${new Date().getTime()}.xlsx`
        );
      });
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (!this.form.detailList.length > 0) {
            this.$message({
              message: "请至少添加一条业绩/比例",
              type: "warning",
            });
            return;
          }
          let url = "";
          if (this.ifadd == 1) {
            url = con.saveStoreLevel;
          } else if (this.ifadd == 2) {
            url = con.updateStoreLevel;
          }
          url(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[0];
      this.queryParams.endDate = val[1];
    },

    getData() {
      con.getListNoPage().then((res) => {
        this.noPageList = res.data;
      });
      con.getEnumsLevelValue().then((res) => {
        this.levelValueList = res.data;
      });
    },
    addData() {
      this.dialogTitle = '添加';
      this.ifadd = 1;
      this.form = {
        storeLevelName: "",
        levelValue: "",
        picture: "",
        detailList: [
          {
            endAchievement: "",
            ratio: "",
          },
        ],
      };
      this.dialogVisible = true;
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    reChongzhi() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 50,
        thedate: [],
      };
      this.getSearch();
    },
    getSearch() {
      this.loading = true
      con.getStoreGradeList(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>
