<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="760px"
          :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="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="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="serviceType2"
            :label="'手续费类型'"
            width="150"
          >
            <template slot-scope="scope">
              <span v-if="scope.row.serviceType==1">按比例(%)</span>
            </template>
            <template slot-scope="scope">
              <span v-if="scope.row.serviceType==2">{{ `按金额(${getUser.currency.symbol})` }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="serviceRatio"
            :label="'手续费数值'"
            width="150"
          />
          <el-table-column align="center" prop="isReference" :label="'血缘'">
            <template slot-scope="scope">
              <div :class="scope.row.isReference ? 'isRed' : 'isGreen'">
                {{ scope.row.isReference | 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="multiple" label="倍数" />
          <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"
                class="button-s"
                style="color: #6962f6"
                @click.native.prevent="update(scope.row)"
              >
                {{ '修改' }}
              </el-button>
              <el-button
                class="button-s"
                type="text"
                size="small"
                style="color: #c73030"
                @click.native.prevent="deleteRow(scope.row)"
              >
                {{ '删除' }}
              </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="getTransferList"
    />

    <el-dialog
      :title="updateTitle"
      :visible.sync="updateShow"
      width="37%"
      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="10">
            <el-form-item
              :label="`最小转账金额(${getUser.currency.symbol})`"
              prop="minAmount"
            >
              <el-input
                v-model="form.minAmount"
                clearable
                placeholder=""
              />
            </el-form-item>
          </el-col>
          <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-row>
        <el-row style="display: flex; justify-content: space-between">
          <el-col :span="10">
            <el-form-item label="手续费" prop="serviceType">
              <el-radio-group v-model="form.serviceType">
                <el-radio
                  v-for="(item, index) in celltypeList"
                  :key="index"
                  :label="item.value"
                >{{ item.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="10" style="margin-left: 50px">
            <el-form-item :label="'手续费数值'" prop="serviceRatio">
              <el-input
                v-model="form.serviceRatio"
                clearable
                type="number"
                placeholder=""
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="display: flex; justify-content: space-between">
          <el-col :span="10">
            <el-form-item :label="'血缘'" prop="isReference">
              <el-select v-model="form.isReference" clearable placeholder="">
                <el-option
                  v-for="item in agreeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <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.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10" style="margin-left: 20px">
            <el-form-item label="倍数" prop="multiple">
              <el-input
                v-model="form.multiple"
                clearable
                placeholder=""
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-col :span="23" style="margin-left: 7px">
          <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-form>
      <div style="width: 100%; text-align: center">
        <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>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import topBar from '@/components/topBar'
import { userCountryList } from '@/api/user'
import { mapGetters } from 'vuex'
import {
  getTransferList,
  deleteTransfer,
  getOnetransferList,
  updateTransfer,
  saveTransfer
} from '@/api/configManage'
import { getcellTypeList } from '@/api/bonus'
import { stateFormat } from '@/utils/numberToCurrency'
export default {
  name: 'Txgs',
  components: {
    topBar
  },
  computed: {
    ...mapGetters(['getUser'])
  },
  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,
      moren: 'zzpz',
      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
      },
      celltypeList: [], // 手续费类型列表
      updateShow: false,
      addOrEdit: '', // 添加还是修改 true添加 false修改
      updateTitle: '',
      agreeList: [
        {
          label: '允许',
          value: 0
        },
        {
          label: '禁止',
          value: 1
        }
      ],
      form: {
        minAmount: '', // 最小转账金额
        maxAmount: '', // 最大转账金额
        serviceType: '', // 手续费类型
        serviceRatio: '', // 手续费数值
        isReference: '', // 血缘
        isRounding: '', // 取整
        multiple: '', // 倍数
        effectiveDate: '' // 生效时间
      },
      rules: {
        minAmount: [
          { required: true, message: '请输入最小转账金额', trigger: 'blur' }
        ],
        maxAmount: [
          { required: true, message: '请输入最大转账金额', trigger: 'blur' }
        ],
        serviceType: [
          { required: true, message: '请选择手续费类型', trigger: 'change' }
        ],
        serviceRatio: [
          { required: true, message: '请输入手续费数值', trigger: 'blur' }
        ],
        isReference: [{ required: true, message: '请选择', trigger: 'change' }],
        isRounding: [{ required: true, message: '请选择', trigger: 'change' }],
        multiple: [{ required: true, message: '请输入倍数', trigger: 'blur' }],
        effectiveDate: [
          { required: true, message: '请选择生效时间', trigger: 'change' }
        ]
      }
    }
  },
  mounted() {
    this.userCountryList().finally(() => {
      this.getTransferList()
    })
  },
  methods: {
    stateFormat,
    // 点击保存
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = ''
          if (this.addOrEdit == true) {
            url = saveTransfer
          } else {
            url = updateTransfer
          }
          url(this.form).then((res) => {
            if (res.code == 200) {
              this.updateShow = false
              this.$message({
                message: res.msg,
                type: 'success'
              })
              this.getTransferList()
            } else {
              this.$message({
                message: res.msg,
                type: 'warning'
              })
            }
          })
        }
      })
    },
    // 手续费类型列表
    getcellTypeList() {
      // getcellTypeList().then((res) => {
      //   this.celltypeList = res.data;
      // });
    },
    // 获取转账配置列表
    getTransferList() {
      this.loading = true
      getTransferList(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.serviceType2 = child2.label
              }
            })
          })
          this.tableData = res.rows
          this.total = res.total
          this.loading = false
        })
        .then(() => {
          this.celltypeList = [
            {
              value: 1,
              label: '按比率(%)'
            },
            { value: 2, label: `按金额(${this.getUser.currency.symbol})` }
          ] // 手续费类型列表
        })
    },
    // 添加
    adddate() {
      this.updateShow = true
      this.updateTitle = '添加'
      this.addOrEdit = true
    },
    // 获取国家
    userCountryList() {
      return new Promise((resolve, reject) => {
        userCountryList().then((res) => {
          this.countryList = res.data
          resolve(res.data)
        }).catch((err) => {
          reject(err)
        })
      })
    },
    // 修改
    update(row) {
      this.updateTitle = '修改'
      this.addOrEdit = false
      this.updateShow = true
      getOnetransferList(row.pkId).then((res) => {
        this.form = res.data
      })
    },
    // 删除
    deleteRow(row) {
      this.$confirm('确定删除?').then((_) => {
        deleteTransfer(row.pkId).then((res) => {
          if (res.code == 200) {
            this.$message({
              message: res.msg,
              type: 'success'
            })
            this.getTransferList()
          }
        })
      })
    },
    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 .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;
}
.tem {
  display: flex;
  align-items: center;
  justify-content: center;
  img {
    width: 28px;
    height: 20px;
    margin-right: 3px;
  }
}
.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;
      }
    }
  }
  .isRed {
    color: #ed1d25;
  }
  .isGreen {
    color: #1ab62b;
  }
}
</style>
