<!--
 * @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"
          height="750px"
          :data="tableData"
          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="pkCountry"
            :label="'国家'"
          >
            <template slot-scope="scope">
              <div class="tem">
                <img :src="scope.row.pkCountryIcon" alt="">
                <div>{{ scope.row.pkCountryName }}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="accountName"
            label="币种"
            min-width="150px"
          />

          <el-table-column
            align="center"
            prop="minAmount"
            :formatter="stateFormat"
            :label="`最小手动提现(${getUser.currency.symbol})`"
            width="150"
          />
          <el-table-column
            align="center"
            prop="maxAmount"
            :formatter="stateFormat"
            :label="`最大手动提现(${getUser.currency.symbol})`"
            width="150"
          />

          <el-table-column
            align="center"
            prop="lowerBound"
            :formatter="stateFormat"
            :label="`最小自动提现(${getUser.currency.symbol})`"
            width="150"
          />
          <el-table-column
            align="center"
            prop="upperBound"
            :formatter="stateFormat"
            :label="`最大自动提现(${getUser.currency.symbol})`"
            width="150"
          />

          <el-table-column align="center" prop="isAuto" label="自动提现">
            <template slot-scope="scope">
              <div :class="scope.row.isAuto ? 'isRed' : 'isGreen'">
                {{ scope.row.isAuto | isAgree(that) }}
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="isRounding" label="取整">
            <template slot-scope="scope">
              <div :class="scope.row.isRounding ? 'isRed' : 'isGreen'">
                {{ scope.row.isRounding | isAgree(that) }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="withdrawalNumber"
            label="提现次数"
          />
          <el-table-column align="center" prop="withdList" label="提现周期" min-width="170px" show-overflow-tooltip />
          <el-table-column
            align="center"
            prop="effectiveDate"
            :label="'生效日期'"
            width="160"
          />
          <el-table-column
            align="center"
            prop="creationTime"
            :label="'创建日期'"
            width="160"
          />
          <el-table-column
            align="center"
            prop="time"
            :label="'操作'"
            fixed="right"
            width="100"
          >
            <template slot-scope="scope">
              <el-button
                type="text"
                size="small"
                style="color: #6962f6"
                class="button-s"
                @click="toFixed(scope.row.pkId)"
              >
                {{ '修改' }}
              </el-button>
              <el-button
                type="text"
                size="small"
                class="button-s"
                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="'添加'"
      :close-on-click-modal="false"
      :visible.sync="dialogVisible"
      width="40%"
      center
      :before-close="handleClose"
    >
      <el-form ref="form" :rules="rules" :model="form" label-width="auto">
        <el-row style="display: flex; justify-content: space-between">
          <el-col :span="12">
            <el-form-item label="选择币种" prop="pkAccount">
              <el-select
                v-model="form.pkAccount"
                clearable
                :placeholder="'请选择'"
              >
                <el-option
                  v-for="item in pubEnumsAccountList"
                  :key="item.pkId"
                  :label="item.accountName"
                  :value="item.pkId"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item
              :label="`最小手动提现(${getUser.currency.symbol})`"
              prop="minAmount"
            >
              <el-input
                v-model="form.minAmount"
                clearable
                placeholder=""
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="display: flex; justify-content: space-between">
          <el-col :span="10" style="margin-left: 50px">
            <el-form-item
              :label="`最大手动提现(${getUser.currency.symbol})`"
              prop="maxAmount"
            >
              <el-input
                v-model="form.maxAmount"
                clearable
                placeholder=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item
              :label="`最小自动提现(${getUser.currency.symbol})`"
              prop="lowerBound"
            >
              <el-input
                v-model="form.lowerBound"
                clearable
                placeholder=""
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="display: flex; justify-content: space-between">
          <el-col :span="10" style="margin-left: 50px">
            <el-form-item
              :label="`最大自动提现(${getUser.currency.symbol})`"
              prop="upperBound"
            >
              <el-input
                v-model="form.upperBound"
                clearable
                placeholder=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="自动提现" prop="isAuto">
              <el-select v-model="form.isAuto" clearable placeholder="">
                <el-option
                  v-for="item in agreeList"
                  :key="item.val"
                  :label="item.text"
                  :value="item.val"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="display: flex; justify-content: space-between">
          <el-col :span="10" style="margin-left: 50px">
            <el-form-item label="取整" prop="isRounding">
              <el-select v-model="form.isRounding" clearable placeholder="">
                <el-option
                  v-for="item in agreeList"
                  :key="item.val"
                  :label="item.text"
                  :value="item.val"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item :label="'提现次数'" prop="withdrawalNumber">
              <el-input
                v-model="form.withdrawalNumber"
                clearable
                placeholder=""
              />
            </el-form-item>
          </el-col>

        </el-row>
        <el-row style="display: flex; justify-content: space-between">
          <el-col :span="10" style="margin-left: 50px">
            <el-form-item label="提现周期" prop="enableStates">
              <el-select
                v-model="form.enableStates"
                placeholder=""
                clearable
                multiple
                @change="seleEnableState"
              >
                <el-option
                  v-for="item in cycleList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="10" style="margin-left: 7px">
            <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-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 { userCountryList } from '@/api/user'
import { withdrawalSave, withdrawalUpdate } from '@/api/configManage'
import { getToken } from '@/utils/auth'
import { mapGetters } from 'vuex'
import { stateFormat } from '@/utils/numberToCurrency'
import { getPubEnumsAccount } from '@/api/businesshandling'
export default {
  name: 'Bzpz',
  components: {
    topBar
  },
  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,
      // 提现周期
      cycleList: [],
      // 转换币
      pkAccountList: [],
      // 查询参数
      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: 'txpz',
      topList: [
        {
          name: '币种配置',
          path: 'bzpz'
        },
        {
          name: '消费配置',
          path: 'xfpz'
        },
        {
          name: '转账配置',
          path: 'zzpz'
        },
        {
          name: '提现配置',
          path: 'txpz'
        },
        {
          name: '提现个税',
          path: 'txgs'
        },
        {
          name: '提现手续费',
          path: 'txsxf'
        },
        {
          name: '特殊消费',
          path: 'specialMoney'
        }
      ],
      pubEnumsAccountList: [],
      form: {
        enableStates: []
      },
      rules: {
        minAmount: [
          { required: true, message: '请输入最小手动提现', trigger: 'blur' }
        ],
        maxAmount: [
          { required: true, message: '请输入最大手动提现', trigger: 'blur' }
        ],
        lowerBound: [
          { required: true, message: '请输入最小自动提现', trigger: 'blur' }
        ],
        upperBound: [
          { required: true, message: '请输入最大自动提现', trigger: 'blur' }
        ],
        withdrawalNumber: [
          { required: true, message: '请输入提现次数', trigger: 'blur' }
        ],
        isAuto: [
          { required: true, message: '请选择自动提现', trigger: 'change' }
        ],
        isRounding: [
          { required: true, message: '请选择取证', trigger: 'change' }
        ],
        enableStates: [
          { required: true, message: '请选择提现周期', trigger: 'change' }
        ],
        effectiveDate: [
          { required: true, message: '请选择生效时间', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    ...mapGetters(['getUser'])
  },
  mounted() {
    this.getOthers().finally(() => {
      this.getDataList()
    })
  },
  methods: {
    stateFormat,
    seleEnableState() {
      this.form.withdrawalAutoList = this.form.enableStates.map((item) => {
        return { withdrawalCycle: item }
      })
    },
    // 点击修改
    toFixed(id) {
      this.addOrEdit = false
      this.dialogVisible = true
      api.getWithdrawal(id).then((res) => {
        this.form = res.data
        // this.form.enableStates
        const arr = this.form.withdrawalAutoList.map((item) => {
          return item.withdrawalCycle
        })
        this.$set(this.form, 'enableStates', arr)
      })
    },
    // 新增
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = ''
          if (this.addOrEdit == true) {
            url = withdrawalSave
          } else {
            url = withdrawalUpdate
          }
          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()
            }
          })
        } else {
          return false
        }
      })
    },
    handleClose() {
      this.dialogVisible = false
      this.$refs['form'].clearValidate()
    },
    openDig() {
      this.addOrEdit = true
      this.dialogVisible = true
    },
    getOthers() {
      // 币种下拉选
      getPubEnumsAccount().then((res) => {
        this.pubEnumsAccountList = res.rows
      })
      // 获取转化币种列表
      api.pkBdAccountList().then((res) => {
        this.pkAccountList = res.rows
      })
      return new Promise((resolve, reject) => {
        Promise.allSettled([
          userCountryList(),
          api.getCycle()
        ]).then(([countryList, cycleList]) => {
          if (countryList.status === 'fulfilled') {
            this.countryList = countryList.value.data
          }
          if (cycleList.status === 'fulfilled') {
            this.cycleList = cycleList.value.data
          }
          resolve()
        }).catch((err) => {
          reject(err)
        })
      })
    },
    getDataList() {
      this.loading = true
      api.withdrawalList(Object.assign({}, this.queryParams)).then((res) => {
        res.rows.forEach((item) => {
          item.withdList = []
          this.countryList.forEach((child) => {
            if (item.pkCountry == child.pkCountry) {
              item.pkCountryName = child.name
              item.pkCountryIcon = child.nationalFlag2
            }
          })
          item.withdrawalAutoList.forEach((cld) => {
            this.cycleList.forEach((sld) => {
              if (sld.value == cld.withdrawalCycle) {
                item.withdList.push(sld.label)
              }
            })
          })
          item.withdList = item.withdList.join('/')
          // cycleList
        })
        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
    },
    // 删除
    handleDelete(id) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        api.delWithdrawal(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-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;
}
::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;
  }
}
</style>
