<template lang="html">
  <div class="databaseAddVue pop-up">
    <div class="title">
      修改数据库
      <i class="close" @click="close()"></i>
    </div>
    <div class="main">
      <div class="list">
        <p class="item">
          <span>数据库用户名</span>:
          <input type="text" name="" v-model="data.dbUsername" spellcheck="false">
        </p>
        <p class="item">
          <span>数据库密码</span>:
          <input type="text" name="" v-model="data.dbPassword" spellcheck="false">
        </p>
        <p class="item">
          <span>数据库地址</span>:
          <input type="text" name="" v-model="data.dbHost" spellcheck="false">
        </p>
        <p class="item">
          <span>数据库端口</span>:
          <input type="text" name="" v-model="data.dbPort" spellcheck="false">
        </p>
        <p class="item">
          <span>表名</span>:
          <input type="text" name="" v-model="data.collection" spellcheck="false">
        </p>
        <p class="item">
          <span>数据库名</span>:
          <input type="text" name="" v-model="data.dbName" spellcheck="false">
        </p>
        <p class="item">
          <span>表的作用</span>:
          <input type="text" name="" v-model="data.effect" spellcheck="false">
        </p>
        <p class="item">
          <span>数据库类型</span>:
          <input type="text" name="" v-model="data.dbType" spellcheck="false">
        </p>
        <!-- <p class="item">
          <span>存储的什么数据</span>:
          <input type="text" name="" v-model="data.dataType" spellcheck="false">
        </p> -->
        <p class="item">
          <span>参数说明</span>:
          <!-- <input type="text" name="" v-model="data.other" spellcheck="false"> -->
          <select class="" name="" v-model="data.other">
            <option :value="n" v-for="n,i in paramData">{{ n }}</option>
          </select>
        </p>
        <p class="item">
          <span>渠道</span>:
          <input type="text" name="" v-model="data.pt" spellcheck="false">
        </p>
        <p class="item">
          <span>是否分表</span>:
          <select class="" name="" v-model="data.table">
            <option :value="true">是</option>
            <option :value="false">否</option>
          </select>
        </p>
        <p class="item">
          <span>分表种类</span>:
          <select class="" name="" v-model="data.tableKind">
            <option value="hash">哈希</option>
            <option value="month">月份</option>
          </select>
        </p>
      </div>
    </div>
    <div class="btns">
      <button type="button" class="btn3" @click="submit()">确认</button>
      <button type="button" class="btn3" @click="close()">放弃</button>
    </div>
  </div>
</template>

<script>
import { bus, requestUpdateServer } from '@/vuex/request.js'

export default {
  data () {
    return {
      data: {
        id: '',
        dbUsername: '',
        dbPassword: '',
        dbHost: '',
        dbPort: '',
        collection: '',
        dbName: '',
        effect: '',
        dbType: '',
        // dataType: '',
        other: '',
        pt: '',
        table: true,
        tableKind: 'hash'
      }
    }
  },
  props: ['childrenData', 'paramData'],
  mounted () {
    // 弹框弹出动画
    window.$(() => {
      bus.$emit('pop-up-show', '.databaseAddVue.pop-up')
    })
    // init
    for (let n in this.data) {
      this.data[n] = this.childrenData[n]
    }
  },
  methods: {
    // 关闭弹窗&动画
    close () {
      window.$(() => {
        bus.$emit('pop-up-close', '.databaseAddVue.pop-up')
        setTimeout(() => {
          this.$emit('close')
        }, 500)
      })
    },
    submit () {
      let params = {}
      for (let n in this.data) {
        params[n] = this.data[n]
      }
      let flag = true
      for (let n in params) {
        if (params[n] === '' && !/dbUsername|dbPassword/g.test(n)) {
          flag = false
          break
        }
      }
      if (!flag) {
        bus.$emit('alert', '存在未填写项！')
        return false
      }
      let data = params
      requestUpdateServer(params, data, (response) => {
        if (response.data) {
          if (response.data.state) {
            bus.$emit('alert', response.data.message)
            this.$emit('update')
            this.$emit('close')
          } else {
            bus.$emit('alert', response.data.message)
          }
        } else {
          bus.$emit('alert', response + '.')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.databaseAddVue{
  .main{
    .list{
      overflow: hidden;
      .item{
        width: 50%;
        float: left;
        margin-bottom: 20px;
        span{
          display: inline-block;
          width: 100px;
        }
        input, select{
          width: 200px;
          height: 31px;
          background-color: #2F363E;
          border: 1px solid #505A64;
          background-image: none;
        }
      }
    }
    .handle{
      line-height: 100px;
      padding-left: 50px;
      label.checkbox input[type=checkbox]+span{
        background-color: #262B31;
        box-shadow: 0 0 0 1px #262B31;
        margin-right: 10px;
      }
    }
  }
}
</style>
