<!--
 * @Descripttion:
 * @version:
 * @Author: kBank
 * @Date: 2022-10-24 15:45:01
-->
<template>
  <div class="page">
    <topBar
      v-if="topList.length > 0"
      :topList="topList"
      :moren="moren"
    ></topBar>
    <div class="thetopbox">
      <el-form ref="form" :model="queryParams" label-width="100px">
        <el-row>
          <el-col :span="4">
            <el-form-item :label="'会员编号'" prop="memberCode">
              <el-input
                clearable
                v-model="queryParams.memberCode"
                :placeholder="'请输入'"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <div class="searchbox">
              <el-button class="my_search" @click="getDataList()">
                {{ '搜索' }}</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="mini"
            v-hasButtons="['consumptionAdd']"
            @click="openDig"
            class="thebtn1"
            >{{ '添加' }}</el-button
          >
          <!-- <el-button size="mini" class="thebtn2"> {{ '导出' }}</el-button> -->
        </div>
      </div>
      <div class="maintable">
        <el-table
          :data="tableData"
          v-loading="loading"
          height="760px"
          style="width: 100%"
          :header-cell-style="{ background: '#EEEEEE' }"
          :row-class-name="tableRowClassName"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column
            align="center"
            prop="memberCode"
            :label="$t('会员编号')"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="memberName"
            :label="$t('会员姓名')"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="specialAreaVal"
            :label="$t('所属专区')"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="detailList"
            :label="'扣款顺序/比例(%)'"
          >
            <template slot-scope="scope">
              <div v-for="item in scope.row.detailList" :key="item.pkId">
                {{ item.accountName }}/{{ item.deductRatio }}
              </div>
            </template>
          </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
                @click="toFixed(scope.row.pkId)"
                type="text"
                size="small"
                style="color: #6962f6"
                v-has-buttons="['consumptionUpdate']"
              >
                {{ '修改' }}
              </el-button>
              <el-button
                @click="handleDelete(scope.row.pkId)"
                type="text"
                size="small"
                v-has-buttons="['consumptionDel']"
                style="color: #c73030"
              >
                {{ '删除' }}
              </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="getDataList"
    />
    <el-dialog
      :title="dialogTitle"
      :close-on-click-modal="false"
      :visible.sync="dialogVisible"
      width="70%"
      center
      :before-close="handleClose"
    >
      <el-form ref="form" :rules="rules" :model="form" label-width="auto">
        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('会员编号')" prop="memberCode">
              <el-input clearable v-model="form.memberCode"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('所属专区')" prop="specialAreaVal">
              <el-select clearable v-model="form.specialArea">
                <el-option
                  v-for="item in areaList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-for="(item, index) in form.detailList" :key="index">
          <el-col :span="6">
            <el-form-item
              label="扣款账户"
              :prop="`detailList.${index}.pkAccount`"
              :rules="rules.pkAccount"
            >
              <el-select clearable v-model="item.pkAccount">
                <el-option
                  v-for="item in pkBdAccountList"
                  :key="item.pkId"
                  :label="item.accountName"
                  :value="item.pkId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item
              label="扣款顺序"
              :prop="`detailList.${index}.deductSort`"
              :rules="rules.deductSort"
            >
              <el-input clearable v-model="item.deductSort"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item
              label="扣款比例(%)"
              :prop="`detailList.${index}.deductRatio`"
              :rules="rules.deductRatio"
            >
              <!-- <el-input clearable  v-model="item.deductRatio"
                        placeholder="请输入扣款比例"></el-input> -->
              <el-input-number
                v-model="item.deductRatio"
                :disabled="item.deductMaxRatio != 0"
                :max="100"
                :min="0"
                :controls="false"
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item
              label="扣款最大比例(%)"
              :prop="`detailList.${index}.deductMaxRatio`"
              :rules="rules.deductMaxRatio"
            >
              <el-input-number
                :disabled="item.deductRatio != 0"
                v-model="item.deductMaxRatio"
                :max="100"
                :min="0"
                :controls="false"
              />
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <div class="middle">
              <div
                @click="delKou(index)"
                class="del"
                v-if="form.detailList.length > 1"
              >
                {{ '删除' }}
              </div>
              <div v-if="form.detailList.length == index + 1" @click="addKou">
                <i class="el-icon-circle-plus"></i>
              </div>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button class="cancels" @click="handleClose">{{
          '取消'
        }}</el-button>
        <el-button
          class="submit"
          style="margin-left: 40px"
          @click="onSubmit('form')"
          >{{ '确认' }}</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import topBar from "@/components/topBar";
import * as api from "@/api/configManage.js";
import {
  accountSave,
  accountUpdate,
  getpkBdAccountList,
  getPubEnumsAccount,
  specialArea,
} from "@/api/configManage";
import { userCountryList } from "@/api/user";
import * as del from "@/api/delivery.js";
import { getToken } from "@/utils/auth";
import ImageUpload from "@/components/ImageUpload";
export default {
  name: "Bzpz",
  components: {
    topBar,
    ImageUpload,
  },
  filters: {
    isAgree(val) {
      if (!val) {
        return '允许';
      } else {
        return '禁止';
      }
    },
  },
  data() {
    return {
      // 折叠搜索框
      isActive: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
      },
      addOrEdit: "",
      total: 0,
      showImg: true, //默认展示
      noneUploadImg: false, //
      openImg: false,
      openImgUrl: "",
      uploadImgUrl: process.env.VUE_APP_BASE_API + "/system/upload",
      headers: {
        Authorization: getToken(),
      },
      dialogVisible: false,
      tableData: [],
      loading: false,
      agreeList: [
        {
          text: '允许',
          val: 0,
        },
        {
          text: '禁止',
          val: 1,
        },
      ],
      moren: "consumption",
      topList: [
        {
          name: '币种配置',
          path: "bzpz",
        },
        {
          name: '消费配置',
          path: "xfpz",
        },
        {
          name: '转账配置',
          path: "zzpz",
        },
        {
          name: '提现配置',
          path: "txpz",
        },
        {
          name: '提现个税',
          path: "txgs",
        },
        {
          name: '提现手续费',
          path: "txsxf",
        },
        {
          name: '特殊消费',
          path: "specialMoney",
        },
        {
          name: this.$t("会员消费配置"),
          path: "consumption",
        },
      ],
      pkBdAccountList: [],
      form: {
        memberCode: "",
        specialArea: "",
        detailList: [
          {
            pkAccount: "",
            deductSort: "",
            deductRatio: "",
            deductMaxRatio: 0,
          },
        ],
      },
      rules: {
        memberCode: [
          { required: true, message: '请输入', trigger: "blur" },
        ],
        deductMaxRatio: [
          { required: true, message: "请输入扣款最大比例", trigger: "blur" },
        ],
        deductSort: [
          { required: true, message: '请输入', trigger: "blur" },
        ],
        deductRatio: [
          { required: true, message: '请输入', trigger: "blur" },
        ],
        pkAccount: [
          { required: true, message: "请选择扣款账户", trigger: "change" },
        ],
      },
      pubEnumsAccountList: [],
      dialogTitle: "",
      yesornoList: [], //是否
      areaList: [],
    };
  },
  mounted() {
    this.getArea();
    // 获取列表
    this.getDataList();
    //币种下拉选
    this.getpkBdAccountList();
  },
  methods: {
    getArea() {
      specialArea().then((res) => {
        this.areaList = res.data;
      });
    },
    reChongzhi() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 50,
      };
      this.getDataList();
    },
    getpkBdAccountList() {
      getpkBdAccountList().then((res) => {
        this.pkBdAccountList = res.rows;
      });
    },
    // 点击修改
    toFixed(id) {
      this.dialogTitle = '修改';
      this.addOrEdit = false;
      this.dialogVisible = true;
      api
        .memberSpecialCurrencyConfigOne({
          pkId: id,
        })
        .then((res) => {
          this.form = res.data;
          res.data.detailList.forEach((item) => {
            if (item.deductMaxRatio == undefined) {
              item.deductMaxRatio = 0;
            }
          });
          this.$set(this.form, "detailList", res.data.detailList);
        });
    },
    // 删除扣款
    delKou(index) {
      this.form.detailList.splice(index, 1);
    },
    // 添加扣款
    addKou() {
      this.form.detailList.push({
        pkAccount: "",
        deductSort: "",
        deductRatio: "",
      });
    },
    // 删除
    handleDelete(id) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning",
      }).then(() => {
        api.specialCurrencyDelete(id).then((res) => {
          // 获取列表
          this.getDataList();
        });
      });
    },
    resetData() {
      this.form = {
        tempName: "",
        detailList: [
          {
            pkAccount: "",
            deductSort: "",
            deductRatio: "",
            deductMaxRatio: 0,
          },
        ],
      };
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = "";
          api.memberSpecialCurrencySave(this.form).then((res) => {
            if (res.code == 200) {
              this.$message({
                message: res.msg,
                type: "success",
              });
              this.dialogVisible = false;
              this.$refs[formName].resetFields();
              this.resetData();
              // 获取列表
              this.getDataList();
              this.dialogTitle = "";
            }
          });
        } else {
          return false;
        }
      });
    },
    handleClose() {
      this.dialogVisible = false;
      this.resetData();
      this.$refs["form"].clearValidate();
      this.$refs["form"].resetFields();
    },
    openDig() {
      this.dialogVisible = true;
      this.addOrEdit = true;
      this.dialogTitle = '添加';
    },
    getDataList() {
      this.loading = true;
      api.specialCurrencyConfigList(this.queryParams).then((res) => {
        this.tableData = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 == 1) {
        return "warning-row";
      } else if (rowIndex % 2 == 0) {
        return "success-row";
      }
      return "";
    },
  },
};
</script>
<style scoped lang="scss">
::v-deep .el-date-editor.el-input,
.el-date-editor.el-input__inner {
  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-table .warning-row {
  background: #f9f9f9;
}

::v-deep .el-table .success-row {
  background: #ffffff;
}
::v-deep .el-table thead {
  color: #000000;
}
::v-deep .el-select {
  width: 100%;
}
.uploadIcon ::v-deep .el-upload--picture-card {
  display: none !important; /* 上传按钮隐藏 */
}
.page {
  padding: 20px;
  background: #f9f9f9;
  font-size: 14px;
  .thetopbox {
    background: #ffffff;
    border-radius: 8px;
    padding-bottom: 10px;
    .searchbox {
      display: flex;
      align-items: center;
      .my_search {
        margin-left: 20px;
      }
      .searchtitle {
        margin-right: 10px;
      }
      .searchbtn {
        background: #08143f;
        color: #ffffff;
      }
    }
  }
  .main {
    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: #3181e5;
        }
        .thebtn2 {
          color: #ffffff;
          background: #ffad41;
        }
      }
      .maintitle {
        font-size: 10px;
        font-family: MicrosoftYaHei;
        color: #999999;
      }
    }
  }
  .tem {
    display: flex;
    align-items: center;
    justify-content: center;
    img {
      width: 28px;
      height: 20px;
      margin-right: 3px;
    }
  }
  .isRed {
    color: #ed1d25;
  }
  .isGreen {
    color: #1ab62b;
  }
  .bgImg {
    width: 48px;
    height: 48px;
  }
}
.openClose {
  text-align: right;
  margin-right: 10px;
  color: #3181e5;
}
.middle {
  line-height: 40px;
  margin-left: 20px;
  white-space: nowrap;
  display: flex;
}
.del {
  margin-right: 10px;
  cursor: pointer;
}
::v-deep .el-date-editor.el-input {
  width: 100%;
}
::v-deep .el-input-number {
  width: auto !important;
}
</style>
