<!-- 配置管理 - 钱包配置 - 提现个税 -->
<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="adddate()"
          >{{ '添加' }}</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: '#eee' }"
          :row-class-name="tableRowClassName"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" />
          <el-table-column align="center" prop="jxmc" :label="'国家'" width="180">
            <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="taxCycle" :label="'提现周期/月'" />
          <el-table-column
            align="center"
            :formatter="stateFormat"
            prop="lowerBound"
            :label="`${'个税下界'}(${getUser.currency.symbol})`"
          />
          <el-table-column
            align="center"
            :formatter="stateFormat"
            prop="upperBound"
            :label="`${'个税上界'}(${getUser.currency.symbol})`"
            min-width="140"
          />
          <el-table-column align="center" prop="ratio" :label="'个税扣除'+'(%)'" />
          <el-table-column align="center" prop="kouchuType" :label="'扣除类型'" />
          <el-table-column align="center" prop="effectiveDate" :label="'生效日期'" />
          <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.native.prevent="update(scope.row)"
              >
                修改
              </el-button>
              <el-button
                type="text"
                size="small"
                style="color: #c73030"
                class="button-s"
                @click.native.prevent="deleteRow(scope.row)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getWithdrawalList"
      />
    </div>
    <el-dialog
      :title="updateTitle"
      :visible.sync="updateShow"
      width="60%"
      center
      :before-close="handleClose"
    >
      <el-form ref="form" :rules="rules" :model="form" label-width="120px" style="padding-bottom: 100px">
        <el-row style="padding-bottom: 20px">
          <el-col :span="7">
            <el-form-item label="提现周期(月)" prop="taxCycle">
              <el-input
                v-model="form.taxCycle"
                clearable
                placeholder="请输入提现周期(月)"
              />
            </el-form-item>
          </el-col>
          <el-col :span="7" style="margin-left: 45px">
            <el-form-item :label="'扣除类型'" prop="serviceType">
              <el-select v-model="form.serviceType" clearable>
                <el-option
                  v-for="item in cellTypeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="7" style="margin-left: 25px">
            <el-form-item :label="'生效日期'" prop="effectiveDate">
              <el-date-picker
                v-model="form.effectiveDate"
                type="datetime"
                :placeholder="'请选择'"
                value-format="yyyy-MM-dd HH:mm:ss"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-for="(item, i) in form.withdrawalTaxDetailsList" :key="i">
          <el-col :span="7">
            <el-form-item :label="`个税下界(${getUser.currency.symbol})`" prop="lowerBound">
              <el-input
                v-model="form.withdrawalTaxDetailsList[i].lowerBound"
                clearable
                :placeholder="`请输入个税下界(${getUser.currency.symbol})`"
              />
            </el-form-item>
          </el-col>
          <el-col :span="7" style="margin-left: 45px">
            <el-form-item :label="`个税上界(${getUser.currency.symbol})`" prop="upperBound">
              <el-input
                v-model="form.withdrawalTaxDetailsList[i].upperBound"
                clearable
                :placeholder="`请输入个税上界(${getUser.currency.symbol})`"
              />
            </el-form-item>
          </el-col>
          <el-col :span="7" style="margin-left: 25px">
            <el-form-item label="个税扣除(%)" prop="ratio">
              <el-input
                v-model="form.withdrawalTaxDetailsList[i].ratio"
                clearable
                placeholder="请输入个税扣除(%)"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col style="text-align: center">
            <el-button
              circle
              icon="el-icon-plus"
              @click="addList()"
            />
            <el-button
              circle
              icon="el-icon-minus"
              @click="subList()"
            />
          </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 { userCountryList } from '@/api/user'
import { getcellTypeList } from '@/api/bonus'
import { mapGetters } from 'vuex'
import {
  getWithdrawalList,
  deleteWithdrawal,
  saveWithdrawal,
  getOneWithdrawalList
} from '@/api/configManage'
import { stateFormat } from '@/utils/numberToCurrency'
export default {
  name: 'Txgs',
  components: {
    topBar
  },
  computed: {
    ...mapGetters(['getUser'])
  },
  data() {
    return {
      moren: 'txgs',
      topList: [
        {
          name: '币种配置',
          path: 'bzpz'
        },
        {
          name: '消费配置',
          path: 'xfpz'
        },
        {
          name: '转账配置',
          path: 'zzpz'
        },
        {
          name: '提现配置',
          path: 'txpz'
        },
        {
          name: '提现个税',
          path: 'txgs'
        },
        {
          name: '提现手续费',
          path: 'txsxf'
        },
        {
          name: '特殊消费',
          path: 'specialMoney'
        }
      ],
      countryList: [], // 国家列表
      tableData: [],
      loading: false,
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 50
      },
      ceshiList: [
        {
          value: 1,
          label: '1月'
        },
        {
          value: 2,
          label: '2月'
        }
      ],
      cellTypeList: [], // 扣除类型
      updateShow: false,
      addOrEdit: '', // 添加还是修改 true添加 false修改
      updateTitle: '',
      form: {
        serviceType: '', // 手续费类型
        taxCycle: '', // 提现周期/月
        effectiveDate: '', // 生效时间
        withdrawalTaxDetailsList: [
          {
            lowerBound: '', // 个税下界(￥)
            ratio: '', // 个税上界(￥)
            upperBound: '' // 个税扣除(%)}
          }
        ]
      },
      rules: {
        taxCycle: [
          { required: true, pattern: /^(1[0-2]|[1-9])$/, message: '请输入1-12月正整数', trigger: 'blur' }
        ],
        serviceType: [{ required: true, message: '请选择', trigger: 'change' }],
        effectiveDate: [
          { required: true, message: '请选择生效时间', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    // this.userCountryList()
    // this.getcellTypeList()
    this.initEnum().finally(() => {
      this.getWithdrawalList()
    })
  },
  methods: {
    stateFormat,
    // 计算类型枚举值
    getcellTypeList() {
      getcellTypeList().then((res) => {
        this.cellTypeList = res.data
      })
    },
    initEnum() {
      return new Promise((resolve, reject) => {
        Promise.allSettled([
          this.userCountryList(),
          this.getcellTypeList()
        ]).then(([countryList, cellTypeList]) => {
          if (countryList.status === 'fulfilled') {
            this.countryList = countryList.value.data
          }
          if (cellTypeList.status === 'fulfilled') {
            this.cellTypeList = cellTypeList.value.data
          }
          resolve()
        }).catch((err) => {
          reject(err)
        })
      })
    },
    addList() {
      this.form.withdrawalTaxDetailsList.push({
        lowerBound: '',
        ratio: '',
        upperBound: ''
      })
    },
    subList() {
      this.form.withdrawalTaxDetailsList.pop()
    },
    handleClose() {
      this.updateShow = false
      this.$refs['form'].clearValidate()
      this.$refs['form'].resetFields()
    },
    // 点击保存
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          saveWithdrawal(this.form).then((res) => {
            if (res.code == 200) {
              this.updateShow = false
              this.$message({
                message: res.msg,
                type: 'success'
              })
              this.getWithdrawalList()
            } else {
              this.$message({
                message: res.msg,
                type: 'warning'
              })
            }
          })
        }
      })
    },

    // 获取提现个税配置列表
    getWithdrawalList() {
      this.loading = true

      getWithdrawalList(this.queryParams).then((res) => {
        res.rows.forEach((element) => {
          this.countryList.forEach((child) => {
            if (element.pkCountry == child.pkCountry) {
              element.pkCountryName = child.name
              element.pkCountryIcon = child.nationalFlag2
            }
          })
          this.cellTypeList.forEach((child2) => {
            if (element.serviceType == child2.value) {
              element.kouchuType = child2.label
            }
          })
        })
        this.tableData = res.rows
        this.total = res.total
        this.loading = false
      })
    },
    // 添加
    adddate() {
      this.updateShow = true
      this.updateTitle = '添加'
      this.addOrEdit = true

      if (this.$refs.form) {
        this.$refs.form.clearValidate()
        this.$refs.form.resetFields()
        this.form.pkId = null
        this.form.withdrawalTaxDetailsList = [
          {
            lowerBound: '',
            ratio: '',
            upperBound: ''
          }
        ]
      }
    },
    // 获取国家
    userCountryList() {
      userCountryList().then((res) => {
        this.countryList = res.data
      })
    },
    // 修改
    update(row) {
      this.updateTitle = '修改'
      this.addOrEdit = false
      this.updateShow = true
      getOneWithdrawalList(row.pkId).then((res) => {
        this.form = res.data
      })
    },
    // 删除
    deleteRow(row) {
      this.$confirm('确定删除?').then((_) => {
        deleteWithdrawal(row.pkId).then((res) => {
          if (res.code == 200) {
            this.$message({
              message: res.msg,
              type: 'success'
            })
            this.getWithdrawalList()
          }
        })
      })
    },
    handleClose() {
      this.updateShow = false
      this.$refs['form'].clearValidate()
      this.$refs['form'].resetFields()
    },
    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 lang="scss" scoped>
  ::v-deep .pagination-container{
    padding: 10px 50px;
  }
  ::v-deep .el-input{
    width: 100%;
  }
  ::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;
  }
  .button-s{
    padding: 0 3px;
  }
::v-deep .el-table .warning-row {
  background: #f9f9f9;
}

::v-deep .el-table .success-row {
  background: #ffffff;
}
::v-deep .el-table thead {
  color: #000000;
}
.tem {
  display: flex;
  align-items: center;
  justify-content: center;
  img {
    width: 28px;
    height: 20px;
    margin-right: 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;
      }
    }
  }
  .isRed {
    color: #ed1d25;
  }
  .isGreen {
    color: #1ab62b;
  }
}
</style>
