<template>
  <BasicLayout>
    <template #wrapper>
      <el-card class="box-card">
        <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
          <el-form-item label="网络" prop="networkId">

            <el-select v-model="queryParams.networkId" clearable placeholder="请选择网络">
              <el-option
                v-for="item in networkList"
                :key="'querynetwork' + item.id"
                :label="item.networkName"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="代币名称" prop="tokenName"><el-input
            v-model="queryParams.tokenName"
            placeholder="请输入代币名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              v-permisaction="['admin:wmToken:add']"
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
            >新增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              v-permisaction="['admin:wmToken:edit']"
              type="success"
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
            >修改
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              v-permisaction="['admin:wmToken:remove']"
              type="danger"
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
            >删除
            </el-button>
          </el-col>
        </el-row>

        <el-table v-loading="loading" :data="wmTokenList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" /><el-table-column
            label="网络"
            align="center"
            prop="networkId"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              {{ networkFormat(scope.row) }}
            </template>
          </el-table-column><el-table-column
            label="代币名称"
            align="center"
            prop="tokenName"
            :show-overflow-tooltip="true"
          /><el-table-column
            label="代币地址"
            align="center"
            prop="tokenAddress"
            :show-overflow-tooltip="true"
          /><el-table-column
            label="代币精度"
            align="center"
            prop="decimals"
            :show-overflow-tooltip="true"
          /><el-table-column
            label="开启自动转账"
            align="center"
            prop="isAuto"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <el-tag v-if="scope.row.isAuto === 1" type="success">开启</el-tag>
              <el-tag v-else type="danger">关闭</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="转账类型" align="center" width="100">
            <template slot-scope="scope">
              {{ transTypeFormat(scope.row) }}
            </template>
          </el-table-column>
          <el-table-column label="转账值" prop="transValue" align="center" width="100" />
          <el-table-column label="触发代币数" align="center" prop="triggerAmount" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">

              <el-button
                slot="reference"
                v-permisaction="['admin:wmToken:edit']"
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
              >修改
              </el-button>
              <el-button
                slot="reference"
                v-permisaction="['admin:wmToken:remove']"
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageIndex"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />

        <!-- 添加或修改对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px">
          <el-form ref="form" :model="form" :rules="rules" label-width="100px">

            <el-form-item label="网络id" prop="networkId">
              <el-select v-model="form.networkId" placeholder="请选择网络">
                <el-option
                  v-for="item in networkList"
                  :key="'network' + item.id"
                  :label="item.networkName"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="代币名称" prop="tokenName">
              <el-input v-model="form.tokenName" placeholder="代币名称" maxlength="20" show-word-limit />
            </el-form-item>
            <el-form-item label="代币地址" prop="tokenAddress">
              <el-input v-model="form.tokenAddress" placeholder="代币地址 请注意如果未填写则为主币！！" />
            </el-form-item>
            <el-form-item label="代币精度" prop="decimals">
              <el-input v-model="form.decimals" placeholder="代币精度" />
            </el-form-item>
            <el-form-item label="触发代币数" prop="triggerAmount">
              <el-input v-model="form.triggerAmount" placeholder="触发代币数" />
            </el-form-item>
            <el-form-item label="自动转账" prop="isAuto">
              <el-radio-group v-model="form.isAuto">
                <el-radio
                  v-for="item in isAutoOptions"
                  :key="'auto' + item.value"
                  :label="item.value"
                >{{
                  item.label
                }}</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item v-if="form.isAuto === 1" label="转账类型" prop="transType">
              <el-radio-group v-model="form.transType">
                <el-radio
                  v-for="item in transTypeOptions"
                  :key="'transtype' + item.value"
                  :label="item.value"
                >{{
                  item.label
                }}</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item
              v-if="form.isAuto === 1"
              :label="'转账' + (form.transtype === 1 ? '百分比' : '金额')"
              prop="transValue"
            >
              <el-input
                v-model="form.transValue"
                :placeholder="'请输入转账' + (form.transtype === 1 ? '百分比' : '金额')"
              />
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>
      </el-card>
    </template>
  </BasicLayout>
</template>

<script>
import { addWmToken, delWmToken, getWmToken, listWmToken, updateWmToken } from '@/api/admin/wm-token'
import { listWmNetwork } from '@/api/admin/wm-network'

export default {
  name: 'WmToken',
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      isEdit: false,
      // 类型数据字典
      typeOptions: [],
      transTypeOptions: [{
        label: '百分比',
        value: 1
      }, {
        label: '金额',
        value: 2
      }],
      isAutoOptions: [{
        label: '开启',
        value: 1
      }, {
        label: '关闭',
        value: 2
      }],
      wmTokenList: [],
      networkList: [],
      // 关系表类型

      // 查询参数
      queryParams: {
        pageIndex: 1,
        pageSize: 10,
        networkId: undefined,
        tokenName: undefined

      },
      // 表单参数
      form: {
      },
      // 表单校验
      rules: {
        networkId: [{ required: true, message: '网络不能为空', trigger: 'blur' }],
        tokenName: [{ required: true, message: '代币名称不能为空', trigger: 'blur' }],
        // tokenAddress: [{ required: true, message: '代币地址不能为空', trigger: 'blur' }],
        decimals: [{ required: true, message: '代币精度不能为空', trigger: 'blur' }],
        transType: [{ required: true, message: '转账类型不能为空', trigger: 'blur' }],
        transValue: [{ required: true, message: '转账值不能为空', trigger: 'blur' }],
        triggerAmount: [{ required: true, message: '触发代币数不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
    this.getNetworkList()
  },
  methods: {
    getNetworkList() {
      listWmNetwork({ pageIndex: 1, pageSize: 100 }).then(response => {
        this.networkList = response.data.list
      })
    },
    /** 查询参数列表 */
    getList() {
      this.loading = true
      listWmToken(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.wmTokenList = response.data.list
        this.total = response.data.count
        this.loading = false
      }
      )
    },
    networkFormat(row) {
      const netword = this.networkList.find(item => item.id === row.networkId)
      return netword ? netword.networkName : ''
    },
    transTypeFormat(row) {
      const transType = this.transTypeOptions.find(item => item.value === row.transType)
      return transType ? transType.label : ''
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {

        id: undefined,
        networkId: undefined,
        tokenName: undefined,
        tokenAddress: undefined,
        decimals: undefined,
        isAuto: 1,
        transType: undefined,
        transValue: undefined
      }
      this.resetForm('form')
    },
    getImgList: function() {
      this.form[this.fileIndex] = this.$refs['fileChoose'].resultList[0].fullUrl
    },
    fileClose: function() {
      this.fileOpen = false
    },
    // 关系
    // 文件
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageIndex = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加代币配置'
      this.isEdit = false
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const _this = this
      const id = row.id || this.ids

      this.$confirm('是否确认编辑编号为"' + id + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        _this.reset()
        return getWmToken(id)
      }).then(response => {
        _this.form = response.data
        _this.open = true
        _this.title = '修改代币配置'
        _this.isEdit = true
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.form.isAuto = this.form.isAuto ? Number(this.form.isAuto) : undefined
          this.form.decimals = this.form.decimals ? Number(this.form.decimals) : undefined
          this.form.triggerAmount = this.form.triggerAmount ? Number(this.form.triggerAmount) : undefined

          if (this.form.id !== undefined) {
            updateWmToken(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess(response.msg)
                this.open = false
                this.getList()
              } else {
                this.msgError(response.msg)
              }
            })
          } else {
            addWmToken(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess(response.msg)
                this.open = false
                this.getList()
              } else {
                this.msgError(response.msg)
              }
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      var Ids = (row.id && [row.id]) || this.ids

      this.$confirm('是否确认删除编号为"' + Ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delWmToken({ 'ids': Ids })
      }).then((response) => {
        if (response.code === 200) {
          this.msgSuccess(response.msg)
          this.open = false
          this.getList()
        } else {
          this.msgError(response.msg)
        }
      }).catch(function() {
      })
    }
  }
}
</script>
