<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="count" placeholder="数量" style="width: 200px;" class="filter-item"
        @keyup.enter.native="handleFilter" />
      <el-select v-model="province" placeholder="省份" clearable style="width: 200px" class="filter-item">
        <el-option v-for="item in provinceList" :key="item" :label="item" :value="item" />
      </el-select>
      <el-select v-model="bank" placeholder="银行" clearable class="filter-item" style="width: 200px">
        <el-option v-for="item in bankList" :key="item" :label="item" :value="item" />
      </el-select>
      <el-button class="filter-item" type="primary" icon="el-icon-view" @click="generate">生成</el-button>
    </div>

    <el-table :data="peopleInfo" highlight-current-row fit style="width: 100%;">
      <el-table-column label="姓名" width="65px">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="身份证号" width="165px">
        <template slot-scope="scope">
          <span>{{ scope.row.IDCardNo }}</span>
        </template>
      </el-table-column>
      <el-table-column label="Sex" width="46px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.sex }}</span>
        </template>
      </el-table-column>
      <el-table-column label="生日" width="100px">
        <template slot-scope="scope">
          <span>{{ scope.row.birthday }}</span>
        </template>
      </el-table-column>
      <el-table-column label="通信地址">
        <template slot-scope="scope">
          <span>{{ scope.row.address }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号" width="110px">
        <template slot-scope="scope">
          <span>{{ scope.row.mobile }}</span>
        </template>
      </el-table-column>
      <el-table-column label="Email" width="150px">
        <template slot-scope="scope">
          <span>{{ scope.row.email }}</span>
        </template>
      </el-table-column>
      <el-table-column label="银行卡号" width="175px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.bankNo }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属银行" width="145px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.bankType }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <slot slot-scope="scope">
          <el-button type="primary" icon="el-icon-key" @click="bandingCardData(scope.row)">绑卡</el-button>
          <el-button type="primary" icon="el-icon-key" @click="bandingAddress(scope.row)">绑定收货地址</el-button>
        </slot>
      </el-table-column>
    </el-table>
    <!-- 绑卡弹框 -->
    <el-dialog title="绑定银行卡" :visible.sync="DialogVisible" width="40%" @close="closeAddDialog">
      <el-form ref="bindCardFormRef" :rules="rules" :model="bindCardForm" label-width="90px">
        <el-form-item label="环境名称:" prop="namespace">
          <el-select v-model="bindCardForm.namespace" placeholder="请选择环境" style="width:310px">
            <el-option v-for="item in namespaceList" :key="item.name" :label="item.name" :value="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="手机号:" prop="phoneNo" style="width:400px">
          <el-input v-model="bindCardForm.phoneNo"></el-input>
        </el-form-item>
        <el-form-item label="卡 号:" style="width:400px">
          <el-input v-model="bindCardForm.cardNo" :disabled=true></el-input>
        </el-form-item>
        <el-form-item label="银行Code:" style="width:400px">
          <el-input v-model="bindCardForm.bankCode" :disabled=true></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="DialogVisible=false">取 消</el-button>
        <el-button type="primary" @click="bindCard">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 绑定收获地址 -->
    <el-dialog title="绑定收货地址" :visible.sync="DialogVisibleAddress" width="40%" @close="closeAddDialog">
      <el-form ref="bindAddressRef" :rules="rules" :model="bindAddressForm" label-width="90px">
        <el-form-item label="环境名称:" prop="namespace" style="width:400px">
          <el-select v-model="bindAddressForm.namespace" placeholder="请选择环境" style="width:310px">
            <el-option v-for="item in namespaceList" :key="item.name" :label="item.name" :value="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="版本号:" prop="version" placeholder="请输入版本号" style="width:400px">
          <el-input v-model="bindAddressForm.version"></el-input>
        </el-form-item>
        <el-form-item label="手机号:" prop="phoneNo" placeholder="请输入手机号" style="width:400px">
          <el-input v-model="bindAddressForm.phoneNo"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="DialogVisibleAdd=false">取 消</el-button>
        <el-button type="primary" @click="bindAddress">确 定</el-button>
      </span>

    </el-dialog>
  </div>
</template>

<script>
import {
  getAll,
  getBankList,
  getProvinceList,
  bindingCard,
  bindingAddress
} from '@/api/testdata'
import { getNamespaceList } from '@/api/k8s'

export default {
  data() {
    return {
      tableKey: 0,
      total: 0,
      provinceList: [],
      bankList: [],
      peopleInfo: null,
      count: 5,
      province: null,
      bank: null,
      DialogVisible: false,
      bindCardForm: {
        namespace: window.sessionStorage.getItem('env')
      },
      namespaceList: [],
      rules: {
        namespace: [
          {
            required: true,
            message: '环境名称不能为空！',
            trigger: 'blur'
          }
        ],
        phoneNo: [
          {
            required: true,
            message: '手机号不能为空！',
            trigger: 'blur'
          }
        ],
        version: [
          {
            required: true,
            message: '版本号不能为空！',
            trigger: 'blur'
          }
        ]
      },
      bindAddressForm: {
        namespace: window.sessionStorage.getItem('env')
      },
      DialogVisibleAddress: false
    }
  },
  created() {
    this.getProvinceList()
    this.getBankList()
    this.generate()
  },
  methods: {
    getProvinceList() {
      getProvinceList().then((response) => {
        this.provinceList = response.data.data
      })
    },
    getBankList() {
      getBankList().then((response) => {
        this.bankList = response.data.data
      })
    },
    getAll(query) {
      getAll(query).then((res) => {
        this.peopleInfo = res.data.data
        this.total = res.data.count
      })
    },
    generate() {
      if (this.count <= 100) {
        this.getAll({
          count: this.count,
          province: this.province,
          bank: this.bank
        })
      } else {
        this.$message({
          message: '"数量"请输入1到100之间的整数',
          type: 'warning'
        })
      }
    },
    // 环境列表
    getNamespaceList() {
      getNamespaceList({ env: 'all' }).then((resp) => {
        this.namespaceList = resp.data.data
      })
    },
    bandingCardData(row) {
      var bankCode = row.bankType.split(' ')
      var newBankCode = bankCode[1]
      this.DialogVisible = true
      this.getNamespaceList()
      this.bindCardForm.cardNo = row.bankNo
      this.bindCardForm.bankCode = newBankCode
    },
    // 绑定银行卡
    bindCard() {
      this.$refs.bindCardFormRef.validate((valid) => {
        if (!valid) {
          return false
        } else {
          bindingCard(this.bindCardForm).then((resp) => {
            if (resp.data.success === 'true') {
              this.DialogVisibleAddress = false
              this.getAll()
              this.$message.success('绑卡成功！')
            } else {
              this.$message.error(resp.data.msg)
            }
          })
        }
      })
    },
    closeAddDialog() {
      this.$refs.bindCardFormRef.resetFields()
    },
    // 绑定收货地址
    bandingAddress() {
      this.DialogVisibleAddress = true
    },
    // 保存收货地址
    bindAddress() {
      var formData = new FormData()
      formData.set('namespace', this.bindAddressForm.namespace)
      formData.set('version', this.bindAddressForm.version)
      formData.set('phoneNo', this.bindAddressForm.phoneNo)
      this.$refs.bindAddressRef.validate((valid) => {
        if (!valid) {
          return false
        } else {
          bindingAddress(formData).then((resp) => {
            if (resp.data.success === true) {
              this.$message.success('地址绑定成功！')
              this.getAll()
              this.DialogVisibleAdd = false
            } else {
              this.$message.error(resp.data.msg)
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.app-container {
  padding: 20px;
}
.filter-container {
  padding-bottom: 10px;

  .filter-item {
    display: inline-block;
    vertical-align: middle;
    margin-bottom: 10px;
  }
}
</style>
