<!--
 * @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="main">
      <div class="maintop">
        <div class="mainbtn">
          <el-button size="mini" @click="openDig" class="thebtn1">{{
            '添加'
          }}</el-button>
          <el-button size="mini" class="thebtn2" @click="handleExport">
            {{ '导出' }}</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="sort" label="显示顺序">
          </el-table-column>
          <el-table-column
            align="center"
            prop="backgroundImg"
            :label="'背景图'"
          >
            <template slot-scope="scope">
              <img class="bgImg" :src="scope.row.backgroundImg" alt="" />
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="pkCountry"
            :label="'国家'"
          >
            <template slot-scope="scope">
              <div class="tem">
                <img :src="scope.row.nationalFlag2" alt="" />
                <!-- <div>{{ scope.row.pkCountryName }}</div> -->
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="accountName" label="账户名称">
          </el-table-column>
          <el-table-column align="center" prop="fieldValueVal" label="账户属性">
          </el-table-column>
          <el-table-column align="center" prop="accountFame" label="转化币种">
          </el-table-column>
          <el-table-column align="center" prop="showStatusVal" label="显示隐藏">
          </el-table-column>
          <el-table-column align="center" prop="isRegular" label="固定">
            <template slot-scope="scope">
              <div :class="scope.row.isRegular ? 'isRed' : 'isGreen'">
                {{ scope.row.isRegular | isAgree(that) }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="isPostage"
            :label="'邮费'"
          >
            <template slot-scope="scope">
              <div :class="scope.row.isPostage ? 'isRed' : 'isGreen'">
                {{ scope.row.isPostage | isAgree(that) }}
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="isTransfer" label="转账">
            <template slot-scope="scope">
              <div :class="scope.row.isTransfer ? 'isRed' : 'isGreen'">
                {{ scope.row.isTransfer | isAgree(that) }}
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="isWithdrawal" label="提现">
            <template slot-scope="scope">
              <div :class="scope.row.isWithdrawal ? 'isRed' : 'isGreen'">
                {{ scope.row.isWithdrawal | isAgree(that) }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="isRecharge"
            :label="'充值'"
          >
            <template slot-scope="scope">
              <div :class="scope.row.isRecharge ? 'isRed' : 'isGreen'">
                {{ scope.row.isRecharge | isAgree(that) }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="onLineRechargeVal"
            label="是否在线充值"
            width="150"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="effectiveDate"
            :label="'生效日期'"
            width="150"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="creationTime"
            :label="'创建日期'"
            width="150"
          >
          </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"
              >
                {{ '修改' }}
              </el-button>
              <!-- <el-button
                @click="handleDelete(scope.row.pkId)"
                type="text"
                size="small"
                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"
      :visible.sync="dialogVisible"
      width="37%"
      center
      :before-close="handleClose"
    >
      <el-form ref="form" :rules="rules" :model="form" label-width="100px">
        <el-row>
          <el-col :span="10">
            <el-form-item label="账户名称" prop="accountName">
              <el-input
                clearable
                v-model="form.accountName"
                placeholder="请输入账户名称"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10" style="margin-left: 70px">
            <el-form-item label="账户属性" prop="fieldValue">
              <el-select clearable v-model="form.fieldValue">
                <el-option
                  v-for="item in pubEnumsAccountList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="显示顺序" prop="sort">
              <el-input
                clearable
                v-model="form.sort"
                placeholder="请输入显示顺序"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10" style="margin-left: 70px">
            <el-form-item label="币种转化" prop="pkBdAccount">
              <el-select clearable v-model="form.pkBdAccount">
                <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="10">
            <el-form-item label="固定" prop="isRegular">
              <el-select clearable v-model="form.isRegular">
                <el-option
                  v-for="item in agreeList"
                  :key="item.val"
                  :label="item.text"
                  :value="item.val"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="10" style="margin-left: 70px">
            <el-form-item :label="'邮费'" prop="isPostage">
              <el-select clearable v-model="form.isPostage">
                <el-option
                  v-for="item in agreeList"
                  :key="item.val"
                  :label="item.text"
                  :value="item.val"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="转账" prop="isTransfer">
              <el-select clearable v-model="form.isTransfer">
                <el-option
                  v-for="item in agreeList"
                  :key="item.val"
                  :label="item.text"
                  :value="item.val"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="10" style="margin-left: 70px">
            <el-form-item label="提现" prop="isWithdrawal">
              <el-select clearable v-model="form.isWithdrawal">
                <el-option
                  v-for="item in agreeList"
                  :key="item.val"
                  :label="item.text"
                  :value="item.val"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="10">
            <el-form-item :label="'充值'" prop="isRecharge">
              <el-select clearable v-model="form.isRecharge">
                <el-option
                  v-for="item in agreeList"
                  :key="item.val"
                  :label="item.text"
                  :value="item.val"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="10" style="margin-left: 70px">
            <el-form-item label="是否在线充值" prop="onLineRecharge">
              <el-select clearable v-model="form.onLineRecharge">
                <el-option
                  v-for="item in yesornoList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item :label="'上传封面'" prop="backgroundImg">
              <imageUpload v-model="form.backgroundImg" />
            </el-form-item>
          </el-col>
          <el-col :span="10" style="margin-left: 70px">
            <el-form-item :label="'生效日期'" prop="effectiveDate">
              <el-date-picker
                v-model="form.effectiveDate"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                :placeholder="'请选择'"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="10" style="margin-left: 70px">
            <el-form-item label="是否显示" prop="showStatus">
              <el-select clearable v-model="form.showStatus">
                <el-option
                  v-for="item in yesornoList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button class="cancels" @click="handleClose">{{
          '取消'
        }}</el-button>
        <el-button
          style="margin-left: 40px"
          class="submit"
          @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,
} 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, that) {
      if (!val) {
        return that.$t("ENU_POPUP_TYPE_1");
      } else {
        return that.$t("ENU_POPUP_TYPE_2");
      }
    },
  },
  data() {
    return {
      that: this,
      // 折叠搜索框
      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,
      countryList: [],
      agreeList: [
        {
          text: '允许',
          val: 0,
        },
        {
          text: '禁止',
          val: 1,
        },
      ],
      moren: "bzpz",
      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: {
        pkCountry: "",
        accountName: "",
        sort: "",
        pkBdAccount: 0,
        isRegular: 0,
        isPostage: 0,
        isTransfer: 0,
        isWithdrawal: 0,
        isRecharge: 0,
        backgroundImg: "",
        effectiveDate: "",
        fieldValue: "",
        onLineRecharge: 1,
        showStatus: 0,
      },
      rules: {
        pkCountry: [
          { required: true, message: "请选择国家名称", trigger: "change" },
        ],
        accountName: [
          { required: true, message: "请输入账户名称", trigger: "blur" },
        ],
        sort: [{ required: true, message: "请输入显示顺序", trigger: "blur" }],
        // pkBdAccount: [
        //   { required: true, message: "请选择转换币种", trigger: "change" },
        // ],
        fieldValue: [
          { required: true, message: "请选择账户属性", trigger: "change" },
        ],
        isRegular: [
          { required: true, message: "请选择固定", trigger: "change" },
        ],
        isPostage: [
          { required: true, message: "请选择邮费", trigger: "change" },
        ],
        isTransfer: [
          { required: true, message: "请选择转账", trigger: "change" },
        ],
        isWithdrawal: [
          { required: true, message: "请选择提现", trigger: "change" },
        ],
        isRecharge: [
          { required: true, message: "请选择充值", trigger: "change" },
        ],
        onLineRecharge: [
          { required: true, message: "请选择是否在线充值", trigger: "change" },
        ],
        backgroundImg: [
          { required: true, message: "请上传封面", trigger: "change" },
        ],
        effectiveDate: [
          { required: true, message: '请选择生效时间', trigger: "change" },
        ],
        showStatus: [
          { required: true, message: '请选择', trigger: "change" },
        ],
      },
      pubEnumsAccountList: [],
      dialogTitle: "",
      yesornoList: [], //是否
    };
  },
  mounted() {
    // 获取国家userCountryList
    this.getCountry();
    // 获取列表
    this.getDataList();
    //币种下拉选
    this.getpkBdAccountList();
    //账户属性下拉选-币种值
    this.getPubEnumsAccount();
  },
  methods: {
    /** 导出按钮操作 */
    handleExport() {
      this.$confirm('是否确认导出所有数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning",
      }).then((_) => {
        this.download(
          "/system/manage/account/export",
          { ...this.queryParams },
          `消费明细${new Date().getTime()}.xlsx`
        );
      });
    },
    getPubEnumsAccount() {
      getPubEnumsAccount().then((res) => {
        this.pubEnumsAccountList = res.data;
      });
      // 是否
      del.getDealerYesNo().then((res) => {
        this.yesornoList = res.data;
      });
    },
    getpkBdAccountList() {
      getpkBdAccountList().then((res) => {
        this.pkBdAccountList = res.rows;
      });
    },
    // 展开折叠
    changeActive() {
      this.isActive = !this.isActive;
    },
    // 点击修改
    toFixed(id) {
      this.dialogTitle = '修改';
      this.addOrEdit = false;
      this.dialogVisible = true;
      api.getAccount(id).then((res) => {
        this.form = res.data;
      });
    },
    // 删除
    handleDelete(id) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning",
      }).then(() => {
        api.delAccount(id).then((res) => {
          // 获取列表
          this.getDataList();
        });
      });
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = "";
          if (this.addOrEdit == true) {
            url = accountSave;
          } else {
            url = accountUpdate;
          }
          url(this.form).then((res) => {
            if (res.code == 200) {
              this.$message({
                message: res.msg,
                type: "success",
              });
              this.dialogVisible = false;
              // 充值数据
              this.$refs[formName].resetFields();
              // 获取列表
              this.getDataList();
              this.dialogTitle = "";
            }
          });
        } else {
          return false;
        }
      });
    },
    handleClose() {
      this.dialogVisible = false;
      this.$refs["form"].clearValidate();
      this.$refs["form"].resetFields();
    },
    openDig() {
      this.dialogVisible = true;
      this.addOrEdit = true;
      this.dialogTitle = '添加';
    },
    getCountry() {
      userCountryList().then((res) => {
        this.countryList = res.data;
      });
    },
    getDataList() {
      this.loading = true;
      api.accountList().then((res) => {
        res.rows.forEach((item) => {
          this.countryList.forEach((child) => {
            if (item.pkCountry == child.pkCountry) {
              item.pkCountryName = child.name;
              item.pkCountryIcon = child.nationalFlag2;
            }
          });
        });
        this.tableData = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    uploadImgChange(file, fileList) {
      this.noneUploadImg = fileList.length > 0;
    },
    //图片上传成功后的回调
    handlePictureSuccess(res, file) {
      this.form.backgroundImg = res.data.url;
      console.log(
        "%c [ res ]-345",
        "font-size:13px; background:#490cce; color:#8d50ff;",
        res
      );
    },
    //图片上传删除
    handleRemove(file, fileList) {
      this.form.backgroundImg = "";
      this.noneUploadImg = fileList.length > 0;
    },
    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;
  .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 {
          background: #3181e5;
          color: #ffffff;
        }
        .thebtn2 {
          background: #ffad41;
          color: #ffffff;
        }
      }
      .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;
}
</style>
