<template>
  <div class="registerList">
    <el-card shadow="never"
             class="aui-card--fill">
      <div class="mod-demo__demo}">
        <el-form :inline="true"
                 :model="dataForm"
                 @keyup.enter.native="loadData()">
          <el-form-item>
            <el-input v-model="dataForm.name"
                      placeholder="组织名称">
            </el-input>
          </el-form-item>

          <el-form-item>
            <el-button @click="loadData()">查询</el-button>
          </el-form-item>
        </el-form>

        <c-table column-type=""
                 ref="table"
                 :url="tableUrl"
                 :params="tableParams"
                 keyword="RegisterInfo"
                 :operations="operations"
                 :tableHeight="tableHeight"
                 @init="init"
                 @delete="del"
                 @showToken="showToken">
        </c-table>
      </div>
    </el-card>
    <c-dialog :showFooter='false'
              :title="'Token信息'"
              :isNest="false"
              :visible="diaVisible"
              :dialogHeight="1.1"
              :width="40"
              @cancel="diaCancel">
      <el-form :inline="false"
               :model="tokenForm"
               ref="tokenForm"
               :label-width="'120px'">
        <div style="margin-top:20px">
          <el-form-item label="客户名称"
                        prop="customerName">
            <div class="item_width_1">
              <span>{{tokenForm.customerName}}</span></div>
          </el-form-item>
          <el-form-item label="开发token"
                        prop="componentAccessToken">
            <div class="item_width_1">
              <span>{{tokenForm.componentAccessToken}}</span></div>
          </el-form-item>

          <el-form-item label="工作端token"
                        prop="workAuthorizerToken">
            <div class="item_width_1">
              <span>{{tokenForm.workAuthorizerToken}}</span></div>
          </el-form-item>

          <el-form-item label="居民端token"
                        prop="resiAuthorizerToken">
            <div class="item_width_1">
              <span>{{tokenForm.resiAuthorizerToken}}</span></div>
          </el-form-item>

        </div>

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

<script>
import CDialog from '@c/CDialog'
import CTable from '@c/CTable'
import { mapGetters } from 'vuex'
import { Loading } from 'element-ui' // 引入Loading服务

let loading // 加载动画
export default {
  data () {
    return {
      dataForm: {
        name: '' // 组织名称
      },
      // 列表相关
      tableUrl:
        'https://epmet-cloud.elinkservice.cn/api/third/pacustomer/registerinfo',
      // tableUrl: 'https://nei.netease.com/api/apimock-v2/e3b1d0eb88e905f6c7ee559b2d6bb7ad/third/pacustomer/registerinfo',
      tableParams: {
        source: this.env
      },
      // 列表操作栏的操作项数组
      operations: [
        {
          lable: '初始化', // 按钮显示名称
          size: 'mini',
          style: 'margin: 0 6px;',
          type: 'text',
          slot: '',
          plain: false,
          methodName: 'init', // 回调方法名称
          isShow: (row) => {
            if (
              row.initState === 0 &&
              row.resiAuth === 1 &&
              row.workAuth === 1
            ) {
              return true
            } else {
              return false
            }
          }
        },
        {
          lable: '删除', // 按钮显示名称
          size: 'mini',
          style: 'margin: 0 6px;',
          type: 'text',
          slot: '',
          plain: false,
          methodName: 'delete', // 回调方法名称
          isShow: (row) => {
            if (this.env !== 'prod') {
              return true
            } else {
              return false
            }
          }
        },
        {
          lable: 'token', // 按钮显示名称
          size: 'mini',
          style: 'margin: 0 6px;',
          type: 'text',
          slot: '',
          plain: false,
          methodName: 'showToken', // 回调方法名称
          isShow: (row) => {
            // if (this.env !== 'prod') {
            return true
            // } else {
            //   return false
            // }
          }
        }
      ],

      // 初始化相关
      initUrl: '/oper/crm/customer/init',

      //token弹出框相关
      diaVisible: false,
      tokenForm: {
        customerId: '',
        customerName: '',
        componentAccessToken: '',
        resiAuthorizerToken: '',
        workAuthorizerToken: ''
      }
    }
  },
  components: {
    CTable, CDialog
  },
  activated () {
    this.$nextTick(() => {
      this.$refs.table.doLayout() // 解决表格错位
    })
  },
  mounted () {
    this.tableParams.source = this.env
    // eslint-disable-next-line
    this.loadData()
  },
  computed: {
    tableHeight () {
      return this.clientHeight - 60 - 80 - 80 - 50
    },
    ...mapGetters(['clientHeight', 'env'])
  },
  methods: {
    // 获取列表数据
    loadData () {
      this.$refs.table.loadData()
    },
    // 客户初始化
    init (row) {
      this.startLoading()
      const param = {
        customerId: row.customerId
      }
      window.app.ajax.post(
        this.initUrl,
        param,
        (data, rspMsg) => {
          this.endLoading()
          this.$message.success('初始化成功')
          this.loadData()
          // eslint-disable-next-line
        },
        (rspMsg, data) => {
          this.endLoading()
          this.$message.error(rspMsg)
        }
      )
    },

    //删除客户，开发和体验服显示
    del (row) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.startLoading()
        const url = 'https://epmet-cloud.elinkservice.cn/api/third/pacustomer/deletepubliccustomer'
        const param = {
          customerId: row.customerId,
          source: this.env
        }
        window.app.ajax.post(
          url,
          param,
          (data, rspMsg) => {
            this.endLoading()
            this.$message.success('删除成功')
            this.loadData()
            // eslint-disable-next-line
          },
          (rspMsg, data) => {
            this.endLoading()
            this.$message.error(rspMsg)
          }
        )
      })
    },

    //获取token，开发和体验服显示
    showToken (row) {
      this.startLoading()
      const url = 'https://epmet-cloud.elinkservice.cn/api/third/pacustomer/tokenlist'
      const param = {
        customerId: row.customerId
      }
      window.app.ajax.post(
        url,
        param,
        (data, rspMsg) => {
          this.endLoading()
          this.diaVisible = true
          this.$nextTick(() => {
            this.tokenForm = data
          })
          // eslint-disable-next-line
        },
        (rspMsg, data) => {
          this.endLoading()
          this.$message.error(rspMsg)
        }
      )
    },

    // 取消
    diaCancel () {
      this.diaVisible = false
    },

    // 开启加载动画
    startLoading () {
      loading = Loading.service({
        lock: true, // 是否锁定
        text: '正在加载……', // 加载中需要显示的文字
        background: 'rgba(0,0,0,.7)' // 背景颜色
      })
    },
    // 结束加载动画
    endLoading () {
      // clearTimeout(timer);
      if (loading) {
        loading.close()
      }
    }
  }
}
</script>
<style lang="css">
.aaa {
  height: 100px;
}
/* .register .el-table .el-table__header-wrapper {
  position: absolute;
  top: 0;
  left: 0;
}*/
/*
.register .el-table .el-table__fixed-body-wrapper {
  height: calc(100% - 44px);
  margin-top: 44px;
  overflow-y: auto !important;
} */
</style>
