<template>
  <div class="page">
    <topBar
      v-if="topList.length > 0"
      :topList="topList"
      :moren="moren"
    ></topBar>
    <div class="main">
      <div class="maintop">
        <div class="mainbtn">
          <el-button
            class="thebtn1"
            @click="adddate()"
            v-hasButtons="['PoorRepurchaseRetailAdd']"
            >{{ $t("w_0328") }}</el-button
          >
          <el-button
            class="thebtn2"
            @click="exported"
            v-hasButtons="['PoorRepurchaseRetailExport']"
          >
            {{ $t("MN_T_8") }}</el-button
          >
        </div>
      </div>
      <div class="maintable">
        <el-table
          height="750px"
          :data="tableData"
          v-loading="loading"
          style="width: 100%"
          :header-cell-style="{ background: '#EEEEEE' }"
          :row-class-name="tableRowClassName"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="111"> </el-table-column>
          <el-table-column
            prop="rangeName"
            :label="$t('MN_F_T_1237')"
            width="300"
          >
          </el-table-column>
          <el-table-column prop="moneyRatio" :label="$t('MN_F_T_1238')">
          </el-table-column>
          <el-table-column prop="achieveRatio" :label="$t('MN_F_T_452')">
          </el-table-column>
          <el-table-column
            align="center"
            prop="rangeValueVal"
            :label="$t('MN_F_T_1239')"
            width="120px"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="anames"
            :label="$t('PER_DA_5')"
            width="300"
          >
          </el-table-column>

          <el-table-column
            align="center"
            prop="dnames"
            :label="$t('CK_KS_16')"
            width="auto"
          >
          </el-table-column>

          <el-table-column
            align="center"
            prop="time"
            :label="$t('MY_ORD_14')"
            width="200"
          >
            <template slot-scope="scope">
              <el-button
                @click.native.prevent="update(scope.row, tableData)"
                type="text"
                size="small"
                class="button-s"
                style="color: #4391fd"
                v-has-buttons="['PoorRepurchaseRetailEdit']"
              >
                {{ $t("ENU_CONTROL_TYPE_2") }}
              </el-button>
              <el-button
                @click.native.prevent="deleteRow(scope.row, tableData)"
                type="text"
                class="button-s"
                size="small"
                style="color: #c73030"
              >
                {{ $t("ENU_CONTROL_TYPE_3") }}
              </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="getRangeList"
    />
    <el-dialog
      :close-on-click-modal="false"
      :title="dialogTitle"
      :visible.sync="updateShow"
      width="37%"
      center
      :before-close="handleClose"
    >
      <el-form ref="form" :rules="rules" :model="form" label-width="90px">
        <el-row>
          <el-form-item :label="$t('MN_F_T_1237')" prop="rangeName">
            <el-input
              clearable
              v-model="form.rangeName"
              placeholder=""
            ></el-input>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item :label="$t('MN_F_T_1239')" prop="rangeValue">
            <el-select clearable v-model="form.rangeValue" placeholder="">
              <el-option
                v-for="item in awardsValueList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('MN_F_T_1238')" prop="moneyRatio">
              <el-input
                clearable
                v-model="form.moneyRatio"
                :placeholder="$t('S_C_70')"
              >
                <i slot="suffix" class="neibu">%</i>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('MN_F_T_452')" prop="achieveRatio">
              <el-input
                clearable
                v-model="form.achieveRatio"
                :placeholder="$t('S_C_70')"
              >
                <i slot="suffix" class="neibu">%</i>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="等级/奖衔" prop="rangeName">
            <!-- 等级 -->
            <el-checkbox-group v-model="form.gradeList">
              <el-checkbox
                v-for="item in gradeChioseList"
                :key="item.pkId"
                :label="item.pkId"
                >{{ item.gradeName }}</el-checkbox
              >
            </el-checkbox-group>
            <!-- 奖衔 -->
            <el-checkbox-group v-model="form.awardsList">
              <el-checkbox
                v-for="item in awardsChioseList"
                :key="item.pkId"
                :label="item.pkId"
                >{{ item.awardsName }}</el-checkbox
              >
            </el-checkbox-group>
          </el-form-item>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button class="cancels" @click="handleClose">{{
          $t("ENU_P_TYPE0")
        }}</el-button>
        <el-button
          class="submit"
          style="margin-left: 40px"
          @click="onSubmit('form')"
          >{{ $t("ENU_P_TYPE4") }}</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import topBar from "@/components/topBar";
import {
  getRangeList,
  deleteRange,
  getAwardsListChiose,
  getgradeRanglist,
  getOneManage,
  updateManage,
  scRangeGenerate,
  getJcsxList,
} from "@/api/retailLevel";
export default {
  name: "retailFgjc",
  components: {
    topBar,
  },
  data() {
    return {
      moren: "retailFgjc",
      topList: [
        {
          name: this.$t("MN_F_T_1166"),
          path: "retailLevel",
        },
        {
          name: this.$t("MN_F_T_109"),
          path: "retailJxpz",
        },
        {
          name: this.$t("MN_F_T_110"),
          path: "retailFgjc",
        },
      ],
      queryParams: {
        pageNum: 1,
        pageSize: 50,
      },
      total: 0,
      multipleSelection: [],
      tableData: [],
      loading: false,
      form: {
        rangeName: "",
        gradeList: [],
        awardsList: [],
        moneyRatio: "",
        achieveRatio: "",
        rangeValue: "",
      },
      rules: {
        moneyRatio: [
          { required: true, message: "请输入金额比例", trigger: "blur" },
        ],
        achieveRatio: [
          { required: true, message: "请输入业绩比例", trigger: "blur" },
        ],

        rangeName: [
          { required: true, message: "请输入奖衔名称", trigger: "blur" },
        ],
        rangeValue: [
          { required: true, message: "请选择极差属性", trigger: "change" },
        ],
      },
      awardsChioseList: [],
      gradeChioseList: [],
      updateShow: false,
      dialogTitle: "",
      addEdit: "",
      awardsValueList: [],
    };
  },
  created() {
    this.getRangeList();
    this.getOthers();
  },
  methods: {
    exported() {
      this.$confirm(this.$t("MN_F_T_407"), this.$t("MN_F_T_304"), {
        confirmButtonText: this.$t("w_0035"),
        cancelButtonText: this.$t("ENU_P_TYPE0"),
        type: "warning",
      }).then((_) => {
        this.download(
          "/system/manage/retail-range/export",
          { ...this.queryParams },
          `${this.$t("极差配置")}${new Date().getTime()}.xlsx`
        );
      });
    },
    //添加
    adddate() {
      this.dialogTitle = this.$t("w_0328");
      this.updateShow = true;
      this.addEdit = 1;
      this.$refs["form"].clearValidate();
      this.$refs["form"].resetFields();
      // scRangeGenerate({}).then(res=>{
      //   if (res.code == 200) {
      //     this.getRangeList();
      //   }
      // })
    },
    //修改
    update(row) {
      this.dialogTitle = this.$t("PER_DA_20");
      this.addEdit = 2;
      this.updateShow = true;
      getOneManage(row.pkId).then((res) => {
        this.form = res.data;
      });
    },
    // 提交
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (
            this.form.gradeList.length == 0 &&
            this.form.awardsList.length == 0
          ) {
            this.$message({
              message: "请至少选择一个等级或奖衔",
              type: "warning",
            });
          } else {
            let url = "";
            if (this.addEdit == 1) {
              //添加
              url = scRangeGenerate;
            } else {
              //修改
              url = updateManage;
            }
            //提交接口
            url(this.form).then((res) => {
              if (res.code == 200) {
                this.updateShow = false;
                this.$message({
                  message: res.msg,
                  type: "success",
                });
                this.getRangeList();
                this.addEdit = "";
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    getOthers() {
      getAwardsListChiose().then((res) => {
        this.awardsChioseList = res.rows;
      });
      getgradeRanglist().then((res) => {
        this.gradeChioseList = res.data;
      });
      getJcsxList().then((res) => {
        this.awardsValueList = res.data;
      });
    },

    //获取极差列表
    getRangeList() {
      this.loading = true;
      getRangeList(this.queryParams).then((res) => {
        res.rows.forEach((element) => {
          element.bnames = [];
          element.anames = "";
          element.rangeDetailsGrade.forEach((item) => {
            element.bnames.push(item.pkGradeVal);
            const str = element.bnames.join("/");
            element.anames = str;
          });
          element.cnames = [];
          element.dnames = "";
          element.rangeDetailsAward.forEach((item) => {
            element.cnames.push(item.pkAwardVal);
            const str2 = element.cnames.join("/");
            element.dnames = str2;
          });
        });
        this.tableData = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //删除
    deleteRow(row, item) {
      this.$confirm(this.$t("MN_F_T_431")).then((_) => {
        deleteRange(row.pkId).then((res) => {
          if (res.code == 200) {
            this.$message({
              message: res.msg,
              type: "success",
            });
            this.getRangeList();
          }
        });
      });
    },
    //关闭弹窗
    handleClose() {
      this.updateShow = false;
      this.$refs["form"].clearValidate();
      this.$refs["form"].resetFields();
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 == 1) {
        return "warning-row";
      } else if (rowIndex % 2 == 0) {
        return "success-row";
      }
      return "";
    },
  },
};
</script>

<style lang="scss" scoped>
.button-s {
  padding: 3px 0;
}
::v-deep .el-select {
  width: 100%;
}
.submit {
  width: 209px;
  height: 48px;
  background: #c8161d;
  border-radius: 5px;
  color: #fff;
  font-size: 18px;
  border-color: #c8161d;
  margin-left: 20px;
}
.cancels {
  width: 209px;
  height: 48px;
  background: #cccccc;
  border-radius: 5px;
  color: #fff;
  font-size: 18px;
  border-color: #ccc;
}
::v-deep .el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 100%;
}
::v-deep .el-table .warning-row {
  background: #f9f9f9;
}

::v-deep .el-table .success-row {
  background: #ffffff;
}
::v-deep .el-table thead {
  color: #000000;
}
.page {
  padding: 10px 20px;
  background: #f9f9f9;
  font-size: 14px;
  .main {
    //margin-top: 20px;
    //background: #ffffff;
    //border-radius: 8px;
    //box-shadow: 0px 2px 20px 0px rgba(238, 238, 238, 0.5);
    background: #f9f9f9;
    .maintop {
      display: flex;
      // padding: 10px 0;
      justify-content: space-between;
      align-items: center;
      .mainbtn {
        .thebtn1 {
          color: #ffffff;
          background: #3181e5;
        }
        .thebtn2 {
          color: #ffffff;
          background: #ffad41;
        }
      }
    }
  }
}
</style>
