<!--
 * @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="tempName"
            :label="'名称'"
          />
          <el-table-column
            align="center"
            prop="specialCurrencyDetailList"
            :label="'扣款顺序/比例(%)'"
          >
            <template slot-scope="scope">
              <div
                v-for="item in scope.row.specialCurrencyDetailList"
                :key="item.pkId"
              >
                {{ item.pkAccountTranVal }}/{{ item.deductRatio }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="creationTime"
            :label="'创建日期'"
          />
          <el-table-column
            align="center"
            prop="time"
            :label="'操作'"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button
                type="text"
                size="small"
                style="color: #6962f6"
                @click="toFixed(scope.row.pkId)"
              >
                {{ '修改' }}
              </el-button>
              <el-button
                type="text"
                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="70%"
      center
      :before-close="handleClose"
    >
      <el-form ref="form" :rules="rules" :model="form" label-width="auto">
        <el-row>
          <el-col :span="24">
            <el-form-item :label="'名称'" prop="tempName">
              <!-- <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-input v-model="form.tempName" clearable />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row
          v-for="(item, index) in form.specialCurrencyDetailParamList"
          :key="index"
        >
          <el-col :span="6">
            <el-form-item
              label="扣款账户"
              :prop="`specialCurrencyDetailParamList.${index}.pkAccount`"
              :rules="rules.pkAccount"
            >
              <el-select v-model="item.pkAccount" clearable>
                <el-option
                  v-for="item in pkBdAccountList"
                  :key="item.pkId"
                  :label="item.accountName"
                  :value="item.pkId"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item
              label="扣款顺序"
              :prop="`specialCurrencyDetailParamList.${index}.deductSort`"
              :rules="rules.deductSort"
            >
              <el-input v-model="item.deductSort" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item
              label="扣款比例(%)"
              :prop="`specialCurrencyDetailParamList.${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="6">
            <el-form-item
              label="扣款最大比例(%)"
              :prop="`specialCurrencyDetailParamList.${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="2">
            <div class="middle">
              <div v-if="form.specialCurrencyDetailParamList.length > 1" class="del" @click="delKou(index)">{{ '删除' }}</div>
              <div
                v-if="form.specialCurrencyDetailParamList.length == index + 1"
                @click="addKou"
              >
                <i class="el-icon-circle-plus" />
              </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 {

  getpkBdAccountList

} from '@/api/configManage'
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: 'specialMoney',
      topList: [
        {
          name: '币种配置',
          path: 'bzpz'
        },
        {
          name: '消费配置',
          path: 'xfpz'
        },
        {
          name: '转账配置',
          path: 'zzpz'
        },
        {
          name: '提现配置',
          path: 'txpz'
        },
        {
          name: '提现个税',
          path: 'txgs'
        },
        {
          name: '提现手续费',
          path: 'txsxf'
        },
        {
          name: '特殊消费',
          path: 'specialMoney'
        }
      ],
      pkBdAccountList: [],
      form: {
        tempName: '',
        specialCurrencyDetailParamList: [
          {
            pkAccount: '',
            deductSort: '',
            deductRatio: '',
            deductMaxRatio: 0
          }
        ]
      },
      rules: {
        tempName: [
          { 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: [] // 是否
    }
  },
  mounted() {
    // 获取列表
    this.getDataList()
    // 币种下拉选
    this.getpkBdAccountList()
  },
  methods: {
    getpkBdAccountList() {
      getpkBdAccountList().then((res) => {
        this.pkBdAccountList = res.rows
      })
    },
    // 点击修改
    toFixed(id) {
      this.dialogTitle = '修改'
      this.addOrEdit = false
      this.dialogVisible = true
      api
        .specialCurrencyList({
          pkId: id
        })
        .then((res) => {
          this.form = res.rows[0]
          res.rows[0].specialCurrencyDetailList.forEach(item => {
            if (item.deductMaxRatio == undefined) {
              item.deductMaxRatio = 0
            }
          })
          this.$set(
            this.form,
            'specialCurrencyDetailParamList',
            res.rows[0].specialCurrencyDetailList
          )
        })
    },
    // 删除扣款
    delKou(index) {
      this.form.specialCurrencyDetailParamList.splice(index, 1)
    },
    // 添加扣款
    addKou() {
      this.form.specialCurrencyDetailParamList.push({
        pkAccount: '',
        deductSort: '',
        deductRatio: ''
      })
    },
    // 删除
    handleDelete(id) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        api.specialDelete({ pkId: id }).then((res) => {
          // 获取列表
          this.getDataList()
        })
      })
    },
    resetData() {
      this.form = {
        tempName: '',
        specialCurrencyDetailParamList: [
          {
            pkAccount: '',
            deductSort: '',
            deductRatio: '',
            deductMaxRatio: 0
          }
        ]
      }
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const url = ''
          api.specialCurrencySave(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.specialCurrencyList().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;
  .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>
