<!--
 * @Descripttion:
 * @version:
 * @Author: kBank
 * @Date: 2022-10-24 15:45:01
-->
<template>
  <div class="page">
    <topBar
      v-if="topList.length > 0"
      :top-list="topList"
      :moren="moren"
    />
    <div class="main">
      <div class="maintop">
        <div class="mainbtn">
          <el-button size="mini" class="thebtn1" @click="openDig">{{
            '添加'
          }}</el-button>
          <el-button size="mini" class="thebtn2"> {{ '导出' }}</el-button>
        </div>
      </div>
      <div class="maintable">
        <el-table
          v-loading="loading"
          :data="tableData"
          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
            align="center"
            prop="specialArea"
            label="所属专区"
            width="140px"
          >
            <template slot-scope="scope">
              <span v-if="scope.row.specialArea">
                {{ renderArea(scope.row.specialArea) }}
              </span>
              <span v-else>--</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="sort"
            label="扣款顺序/比例(%)"
            width="auto"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <span
                v-for="(item, index) in scope.row.detailsList"
                :key="index"
                style="margin-right: 10px"
              >
                {{ item.accountName }}/{{ item.deductRatio }}
              </span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="effectiveDate"
            label="生效日期"
            width="160px"
          />
          <el-table-column
            align="center"
            prop="creationTime"
            label="创建日期"
            width="160px"
          />
          <el-table-column
            align="center"
            prop="time"
            label="操作"
            fixed="right"
            width="100px"
          >
            <template slot-scope="scope">
              <el-button
                class="button-s"
                type="text"
                size="small"
                style="color: rgb(105, 98, 246)"
                @click="toFixed(scope.row.pkId)"
              >
                修改
              </el-button>
              <el-button
                type="text"
                class="button-s"
                size="small"
                style="color: #c73030"
                @click="handleDelete(scope.row.pkId)"
              >
                删除
              </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="55%"
      center
      :before-close="handleClose"
    >
      <el-form ref="form" :rules="rules" :model="form" label-width="auto">
        <el-row>
          <el-col :span="14">
            <el-form-item label="所属专区" prop="specialArea">
              <el-select v-model="form.specialArea" clearable>
                <el-option
                  v-for="item in areaList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-for="(item, index) in form.detailsList" :key="index">
          <el-col :span="7">
            <el-form-item
              label="扣款顺序"
              :prop="`detailsList.${index}.pkAccount`"
              :rules="rules.pkAccount"
            >
              <el-select v-model="item.pkAccount" clearable>
                <el-option
                  v-for="item in bdAccountList"
                  :key="item.pkId"
                  :label="item.accountName"
                  :value="item.pkId"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item
              label="扣款比例(%)"
              :prop="`detailsList.${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="7">
            <el-form-item
              label="扣款最大比例(%)"
              :prop="`detailsList.${index}.deductMaxRatio`"
              :rules="rules.deductMaxRatio"
            >
              <el-input-number
                v-model="item.deductMaxRatio"
                :disabled="item.deductRatio != 0"
                :max="100"
                :min="0"
                :controls="false"
              />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <div class="middle">
              <div
                v-if="form.detailsList.length > 1"
                class="del"
                @click="delKou(index)"
              >
                删除
              </div>
              <div v-if="form.detailsList.length == index + 1" @click="addKou">
                <i class="el-icon-circle-plus" />
              </div>
            </div>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="14">
            <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-form-item>
          </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 { areaCurrencySave, areaCurrencyUpdate } from '@/api/configManage.js'
import { userCountryList } from '@/api/user'
import { getToken } from '@/utils/auth'
export default {
  name: 'Bzpz',
  components: {
    topBar
  },
  filters: {
    isAgree(val) {
      if (!val) {
        return '允许'
      } else {
        return '禁止'
      }
    }
  },
  data() {
    return {
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50
      },
      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: 'xfpz',
      topList: [
        {
          name: '币种配置',
          path: 'bzpz'
        },
        {
          name: '消费配置',
          path: 'xfpz'
        },
        {
          name: '转账配置',
          path: 'zzpz'
        },
        {
          name: '提现配置',
          path: 'txpz'
        },
        {
          name: '提现个税',
          path: 'txgs'
        },
        {
          name: '提现手续费',
          path: 'txsxf'
        },
        {
          name: '特殊消费',
          path: 'specialMoney'
        }
      ],
      form: {
        specialArea: '',
        detailsList: [
          {
            pkAccount: '',
            deductRatio: '',
            deductMaxRatio: 0
          }
        ]
      },
      rules: {
        deductRatio: [
          { required: true, message: '请输入扣款比例', trigger: 'blur' }
        ],
        deductMaxRatio: [
          { required: true, message: '请输入扣款最大比例', trigger: 'blur' }
        ],
        specialArea: [
          { required: true, message: '请选择所属专区', trigger: 'change' }
        ],
        pkAccount: [
          { required: true, message: '请选择扣款顺序', trigger: 'change' }
        ],
        effectiveDate: [
          { required: true, message: '请选择生效时间', trigger: 'change' }
        ]
      },
      areaList: [],
      bdAccountList: [],
      addOrEdit: true,
      dialogTitle: ''
    }
  },
  mounted() {
    // 获取国家userCountryList
    this.getCountry()
    // 获取列表
    this.getDataList()
    // 获取专区
    this.getArea()
    // 账户列表
    this.getpkBdAccountList()
  },
  methods: {
    // 删除扣款
    delKou(index) {
      this.form.detailsList.splice(index, 1)
    },
    // 添加扣款
    addKou() {
      this.form.detailsList.push({
        pkAccount: '',
        deductRatio: '',
        deductMaxRatio: 0
      })
    },
    // 点击修改
    toFixed(id) {
      this.dialogTitle = '修改'
      this.addOrEdit = false
      this.dialogVisible = true
      api.getAreaCurrency(id).then((res) => {
        res.data.detailsList.forEach(item => {
          if (item.deductMaxRatio == undefined) {
            item.deductMaxRatio = 0
          }
        })
        this.form = res.data
      })
    },
    // 账户列表
    getpkBdAccountList() {
      api.pkBdAccountList().then((res) => {
        this.bdAccountList = res.rows
      })
    },
    // 获取专区
    getArea() {
      api.specialAreaConsume().then((res) => {
        this.areaList = res.data
      })
    },
    renderArea(areaId) {
      return this.areaList.find(item => item.value === areaId)?.label || '--'
    },
    resetData() {
      this.form = {
        specialArea: '',
        detailsList: [
          {
            pkAccount: '',
            deductRatio: '',
            deductMaxRatio: 0
          }
        ]
      }
    },
    onSubmit(formName) {
      this.form.detailsList.forEach((item, index) => {
        item.deductSort = index + 1
      })
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = ''
          if (this.addOrEdit == true) {
            url = areaCurrencySave
          } else {
            url = areaCurrencyUpdate
          }
          url(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()
            }
          })
        } else {
          return false
        }
      })
    },
    handleClose() {
      this.dialogVisible = false
      this.resetData()
    },
    openDig() {
      this.dialogTitle = '添加'
      this.dialogVisible = true
      this.addOrEdit = true
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    getCountry() {
      userCountryList().then((res) => {
        this.countryList = res.data
      })
    },
    getDataList() {
      this.loading = true
      api.areaCurrencyList().then((res) => {
        this.tableData = res.rows
        this.total = res.total
        this.loading = false
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 删除
    handleDelete(id) {
      api.delAreaCurrency(id).then((res) => {
        // 获取列表
        this.getDataList()
      })
    },
    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-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-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;
  /* 上传按钮隐藏 */
}
.button-s {
  padding: 0 3px;
}
.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 {
          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;
  }
  .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>
