<template>
  <div v-loading="loading">
    <div class="white-box">
      <div class="filter-box">
        <filter-user :filter-types="filterTypes" @select-value="handleFilterUser"></filter-user>
      </div>
      <el-table ref="wrapper" :data="tableData" stripe style="width: 100%;" @selection-change="handleSelectionChange" :height="tool.getTableHeight()">
        <el-table-column fixed type="selection" width="55" v-if="tableHeaders"></el-table-column>
        <el-table-column v-for="(tableHeader, key) in tableHeaders" :key="key" :label="tableHeader.header"
            :width="tableHeader.other.width ? tableHeader.other.width : ''" :fixed="tableHeader.index=='USER_NAME' || tableHeader.index=='REAL_NAME' ?true:false">
          <template slot-scope="scope">
            <template v-if="scope.row[tableHeader.index].other.tag">
              <el-tag
                  :type="scope.row[tableHeader.index].other.tag.type ? scope.row[tableHeader.index].other.tag.type : null"
                  :size="scope.row[tableHeader.index].other.tag.size ? scope.row[tableHeader.index].other.tag.size : null"
                  :class="scope.row[tableHeader.index].other.tag.class ? scope.row[tableHeader.index].other.tag.class : null">
                {{scope.row[tableHeader.index].value}}
              </el-tag>
            </template>
            <template v-else>
              <template v-if="tableHeader.index === 'USER_NAME'">
<!--                <el-dropdown v-if="permission.hasPermission(`user/login-to-frontend`)">-->
                  <span class="el-dropdown-link">
                    {{scope.row.USER_NAME.value}} <!--<i class="el-icon-arrow-down el-icon&#45;&#45;right"></i>-->
                  </span>
<!--                  <el-dropdown-menu slot="dropdown">-->
<!--                    <el-dropdown-item command="login" v-show="scope.row.BTF_URL !== null">-->
<!--                      <a :href="`${frontendServer}/#/login-by-backend?${scope.row.BTF_URL}`" target="_blank" style="color:#606266;">-->
<!--                        Quick logon&lt;!&ndash; 快速登录 &ndash;&gt;-->
<!--                      </a>-->
<!--                    </el-dropdown-item>-->
<!--                  </el-dropdown-menu>-->
<!--                </el-dropdown>-->
<!--                <el-tag type="primary" size="small" class="no-border" v-if="!permission.hasPermission(`user/login-to-frontend`)">-->
<!--                  {{scope.row.USER_NAME.value}}-->
<!--                </el-tag>-->
              </template>
              <template v-else>
                <div v-html="scope.row[tableHeader.index].value"></div>
              </template>
            </template>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="Action" width="180"><!-- 操作 -->
          <template slot-scope="scope">
            <el-dropdown size="small" trigger="click">
              <el-button type="primary" size="small" @click.stop="">
                Action<!-- 操作该数据 --><i class="el-icon-arrow-down el-icon--right"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="handleModifyPassword(scope.row)" v-if="permission.hasPermission(`user/ba-modify-password`)">
                  Change Password<!-- 修改密码 -->
                </el-dropdown-item>
                <el-dropdown-item @click.native="handleModifyProfile(scope.row)" v-if="permission.hasPermission(`user/ba-modify-profile`)">
                  Modify personal data<!-- 修改个人资料 -->
                </el-dropdown-item>
                <el-dropdown-item @click.native="handleStatusActive(scope.row)" v-if="permission.hasPermission(`user/ba-modify-status`)">
                  Status activation<!-- 状态激活 -->
                </el-dropdown-item>
                <el-dropdown-item @click.native="handleStatusLock(scope.row)" v-if="permission.hasPermission(`user/ba-modify-status`)">
                  Status lock<!-- 状态锁定 -->
                </el-dropdown-item>
                <el-dropdown-item @click.native="handleIsModifyPassword(scope.row, 1)" v-if="permission.hasPermission(`user/ba-is-modify-password-status`)">
                  Open password modification<!-- 开启密码修改 -->
                </el-dropdown-item>
                <el-dropdown-item @click.native="handleIsModifyPassword(scope.row, 0)" v-if="permission.hasPermission(`user/ba-is-modify-password-status`)">
                  Turn off password modification<!-- 关闭密码修改 -->
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <div class="white-box-footer">
        <el-button type="success" size="small" @click="handleExport" v-show="permission.hasPermission(`user/ba-user-list`)">
          Export Excel
        </el-button>
        <pagination :total="totalCount" :page_size="pageSize" @size-change="handleSizeChange" @current-change="handleCurrentChange"></pagination>
      </div>
    </div>
    <el-dialog :title="formModifyPassword.typeName" :visible.sync="dialogModifyPasswordVisible" width="50%">
      <el-form ref="form" :model="formModifyPassword" label-width="120px" class="form-dialog">
        <el-form-item label="type"><!-- 类型 -->
          <el-select v-model="formModifyPassword.passwordType" placeholder="Please select a type"><!-- 请选择类型 -->
            <el-option v-for="(item,key) in passwordType" :label="item.label" :value="item.type" :key="key"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="password"><!-- 密码 -->
          <el-input placeholder="password" v-model="formModifyPassword.password"><!-- 密码 --></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleModifyPasswordSubmit" :loading="submitPasswordButtonStat">Submit<!-- 提交 --></el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-dialog :title="formModifyProfile.typeName" :visible.sync="dialogModifyProfileVisible" width="50%">
      <el-form ref="form" :model="formModifyProfile" label-width="150px" class="form-dialog">
        <el-form-item label="Member name"><!-- 会员姓名 -->
          <el-input v-model="formModifyProfile.realName"></el-input>
        </el-form-item>
<!--        <el-form-item label="Identity No.">&lt;!&ndash; 身份证号 &ndash;&gt;-->
<!--          <el-input v-model="formModifyProfile.idCard"></el-input>-->
<!--        </el-form-item>-->
        <el-form-item label="Phone Number"><!-- 手机号 -->
          <el-input v-model="formModifyProfile.mobile"></el-input>
        </el-form-item>
<!--        <el-form-item label="Bank name">&lt;!&ndash; 银行名称 &ndash;&gt;-->
<!--          <el-select v-model="formModifyProfile.openBank" placeholder="Please select a bank name">&lt;!&ndash; 请选择银行名称 &ndash;&gt;-->
<!--            <el-option v-for="(item,index) in allOpenBank" :key="index" :label="item.BANK_NAME" :value="item.BANK_CODE"></el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="Bank Address">&lt;!&ndash; 开户行地址 &ndash;&gt;-->
<!--          <el-input v-model="formModifyProfile.bankAddress"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="Bank account number">&lt;!&ndash; 银行帐号 &ndash;&gt;-->
<!--          <el-input v-model="formModifyProfile.bankNo"></el-input>-->
<!--        </el-form-item>-->
        <el-form-item>
          <el-button type="primary" @click="handleModifyProfileSubmit" :loading="submitProfileButtonStat">
            Submit<!-- 提交 -->
          </el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import network from '../../utils/network'
import tool from '../../utils/tool'
import {FRONTEND_SERVER} from '@/utils/config'
import baseInfo from '@/utils/baseInfo'
import FilterUser from '../../components/FilterUser'
import store from '../../utils/vuexStore'
import permission from '@/utils/permission'
import Pagination from '@/components/Pagination'
import filterHelper from '../../utils/filterHelper'

export default {
  name: 'ba-user-list',
  components: {FilterUser, Pagination},
  mounted () {
    let _this = this
    _this.getData()
    store.state.socket.onMessageCallback = this.onMessageCallback
  },
  data () {
    return {
      tableHeaders: null,
      tableData: null,
      loading: true,
      multipleSelection: [],
      currentPage: 1,
      totalPages: 1,
      totalCount: 1,
      pageSize: 20,
      frontendServer: FRONTEND_SERVER,
      tool: tool,
      permission: permission,
      filterTypes: null,
      filterModel: {},
      dialogVisible: false,
      formCloseLogin: {
        userName: null,
        typeName: 'Login management', // 登录管理
        type: null,
        isClose: 0,
        remark: '',
        areaSelected: null
      },
      regionDataPlus: store.state.regionInfo.regionData,
      apps: null,
      closeSwitch: null,
      submitButtonStat: false,
      dialogDecVisible: false,
      dialogModifyPasswordVisible: false,
      dialogModifyProfileVisible: false,
      formCloseDec: {
        userName: null,
        typeName: 'Manage entry', // 管理报单
        type: null,
        isClose: 0,
        remark: '',
        areaSelected: null
      },
      formModifyPassword: {
        userId: '',
        password: '',
        typeName: 'Change Password', // 修改密码
        passwordType: 'password'
        // remark: '',
      },
      formModifyProfile: {
        userId: '',
        typeName: 'Modify personal data', // 修改个人资料
        // nation: '',
        realName: '',
        // idCard: '',
        mobile: '',
        // openBank: '',
        // bankAddress: '',
        // bankNo: ''
      },
      passwordType: [
        {
          type: 'password',
          label: 'login password' // 登录密码
        },
        {
          type: 'payPassword',
          label: 'Payment password' // 支付密码
        }
      ],
      // allOpenBank: null,
      // allNation: null,
      submitDecButtonStat: false,
      submitPasswordButtonStat: false,
      submitProfileButtonStat: false,
      transferPropForm: {
        userIds: [],
        allowTransfer: true,
        transferProp: 100,
        withdrawProp: 0,
        remark: ''
      },
      closeUserData: null
    }
  },
  methods: {
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    handleCurrentChange (page) {
      this.getData(page, this.pageSize)
    },
    handleSizeChange (pageSize) {
      this.getData(this.currentPage, pageSize)
    },
    handleAdd () {
      this.$router.push('/user/ba-user-add')
    },
    handleFilterUser (filterData) {
      filterHelper.handleFilterUser(this, filterData)
    },
    handleFilter () {
      this.getData()
    },
    getData (page, pageSize, isLoading = true) {
      network.getPageData(this, 'user/ba-user-list', page, pageSize, this.filterModel, response => {
        this.filterTypes = response.filterTypes
      }, isLoading)
    },
    handleExport () {
      this.$confirm(`Are you sure you want to export the current data？`, 'Hint', { // (`确定要导出当前数据吗？`, '提示',
        confirmButtonText: 'confirm', // 确定
        cancelButtonText: 'cancel', // 取消
        type: 'warning'
      }).then(() => {
        return network.getData(`user/ba-user-list-export`, this.filterModel)
      }).then(response => {
        this.$message({
          message: response,
          type: 'success'
        })
      }).catch(response => {

      })
    },
    onMessageCallback (data) {
    },
    handleModifyPassword (row) {
      this.formModifyPassword.userId = row.USER_ID
      this.dialogModifyPasswordVisible = true
    },
    handleModifyProfile (row) {
      this.dialogModifyProfileVisible = true
      let vueObj = this
      network.getData('user/ba-profile-get', {id: row.USER_ID}).then(response => {
        vueObj.formModifyProfile = response.userInfo
        // vueObj.allOpenBank = response.allOpenBank
        // vueObj.allNation = response.allNation
      })
    },
    handleStatusActive (row) {
      network.postData('user/ba-modify-status',{userId: row.USER_ID, status: 1}).then(response => {
        this.$message({
          message: response,
          type: 'success'
        })
        this.getData(this.currentPage, this.pageSize)
      })
    },
    handleStatusLock (row) {
      network.postData('user/ba-modify-status',{userId: row.USER_ID, status: 0}).then(response => {
        this.$message({
          message: response,
          type: 'success'
        })
        this.getData(this.currentPage, this.pageSize)
      })
    },
    handleIsModifyPassword (row, status) {
      network.postData('user/ba-is-modify-password-status',{userId: row.USER_ID, status: status}).then(response => {
        this.$message({
          message: response,
          type: 'success'
        })
        this.getData(this.currentPage, this.pageSize)
      })
    },
    handleModifyPasswordSubmit () {
      this.submitPasswordButtonStat = true
      let path = 'user/ba-modify-password'
      network.postData(path, this.formModifyPassword).then(response => {
        this.$message({
          message: response,
          type: 'success'
        })
        this.getData(this.currentPage, this.pageSize)
        this.submitPasswordButtonStat = false
        this.dialogModifyPasswordVisible = false
      }).catch(response => {
        this.submitPasswordButtonStat = false
      })
    },
    handleModifyProfileSubmit () {
      this.submitProfileButtonStat = true
      let path = 'user/ba-modify-profile'
      network.postData(path, this.formModifyProfile).then(response => {
        this.$message({
          message: response,
          type: 'success'
        })
        this.getData(this.currentPage, this.pageSize)
        this.submitProfileButtonStat = false
        this.dialogModifyProfileVisible = false
      }).catch(response => {
        this.submitProfileButtonStat = false
      })
    }
  }
}

</script>
