<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2019-03-20 14:36:37
 * @LastEditors: 无尘
 * @LastEditTime: 2020-09-25 14:29:44
 -->
<template>
  <div class="daily-set-wrap ">
    <div class="company-set-content border-box">
      <div class="company-title m-b-20 flex flex-space-between">
        <div class="line-h-32"><span class="color-303133 font-14 font-w-600">企业微信基本信息</span><el-button class="m-l-20" type="text" :loading="loadRefesh" @click="refeshData">同步刷新 </el-button></div>
      </div>
      <div class="company-info-body">
        <el-form ref="form" :model="form" label-width="105px">
          <el-form-item label="小程序版本">
            <span class="font-14 color-303133">{{ companyObj.smallVersion || '免费版' }}</span>
          </el-form-item>
          <el-form-item label="企业名称">
            <span class="font-14 color-303133 p-r-10">{{ companyObj.wxEnterpriseName || '--' }}</span> <el-tag> {{ companyObj.corpType == 'verified' ? '已认证' : '未认证' }}</el-tag><span v-if="companyObj.corpType == 'verified'" class="p-l-10 font-12 color-909399">认证到期 {{ companyObj.verifiedEndTime | timeStampToYmd }}</span>
          </el-form-item>
          <el-form-item label="企业ID">
            <span class="font-14 color-303133">{{ companyObj.corpId || '--' }}</span>
          </el-form-item>
          <el-form-item label="行业类型">
            <span class="font-14 color-303133">{{ companyObj.corpIndustry || '--' }} / {{ companyObj.corpSubIndustry || '--' }}</span>
          </el-form-item>
          <el-form-item label="人员规模">
            <span class="font-14 color-303133">{{ companyObj.corpScale || '0人'}}</span>
          </el-form-item>
          <el-form-item label="地址">
            <span class="font-14 color-303133">{{ companyObj.location || '--' }}</span>
          </el-form-item>
        </el-form>
      </div>
      <div class="company-title m-b-20 m-t-30"><span class="color-303133 font-14 font-w-600">客户密钥维护</span></div>
      <div class="company-info-body secret-body">
        <div class="m-b-20 secret-cell flex">
          <div class=" secret-cell-title">
            <el-tooltip class="item" effect="dark" content="用于获取登录小程序用户的手机号/姓名/部门信息。必填项" placement="top-start"> <span  class="font-14 color-606266 common-bottom-border">自建应用：</span></el-tooltip>
          </div>
          <div>
            <div class="self-app-info" v-for="(item, index) in tableData" :key="index+'app'">
              <div class="self-app-item secret-name flex flex-space-between">
                <div><span class="font-14 color-303133 font-w-600 secret-name-span">{{ item.secretName	 || '--'}}</span><span class="font-12 color-606266 p-l-15">编辑时间：{{ item.updateTime | formatTimeStamp}}</span></div>
                <div>
                 <el-button type="text" :loading="loadApp" @click ="refreshApp(item)">同步刷新</el-button> <el-button type="text" @click="toEdit(item)">编辑</el-button><el-button type="text" @click="toDel(item)">删除</el-button>
                </div>
              </div>
              <div class="m-t-10">
                <el-form ref="form" label-width="110px">
                  <el-form-item label="Agentld">
                    <span class="font-14 color-303133">{{item.agentId}}</span>
                  </el-form-item>
                  <el-form-item label="Secret">
                    <span class="font-14 color-303133">{{item.secretVal}}</span><span class="p-l-15 font-14 color-303133"><span :class="['iconfont', item.checkFlag ? 'color-49c958 iconchenggong' : 'color-f83431 iconshibai']"></span><span class="p-l-10">{{ item.checkFlag ? '校验成功' : '校验失败' }}</span></span>
                  </el-form-item>
                  <el-form-item label="应用可见范围">
                    <span v-for="(child, key) in item.displayRange" :key="key+'child'" class="font-14 color-303133 inline-block">{{child.name}} {{ item.displayRange.length > 1 ? '；':''}}</span>
                  </el-form-item>
                </el-form>
              </div>
            </div>
            <div v-if="!tableData.length" class="add-self-app font-14 color-606266 cursor-pointer" @click="addSecret"><span class="iconfont icontianjiajiahaowubiankuang font-12"></span><span class="p-l-10">添加</span></div>
          </div>
        </div>
      </div>
    </div>
    <add-self-app v-if="addShow" :edit-row="editRow" @closeText="closeText" @submitText="submitText"></add-self-app>
  </div>
</template>
<script>
import addSelfApp from '@/components/set/add-self-app.vue';
import fetch from '@/api/enterprise.js';
const { wxEnterpriseRefresh, wxEnterpriseDetail, getSelfApp, delSelfApp, refreshSelfApp } = fetch;
// import { _debounce } from '@/common/js/public';
import showMsg from '@/common/js/showmsg';
export default {
  name: 'EnterpriseSetPage',
  components: {
    addSelfApp
  },
  data() {
    return {
      checkShow: true,
      tipText: '请在企业密钥维护中输入对应的secret',
      wxEnterpriseId: localStorage.getItem('haoBanUser') ? JSON.parse(localStorage.getItem('haoBanUser')).wxEnterpriseId : '',
      companyObj: {
        smallVersion: '',
        corpName: '',
        corpId: '',
        contactFlag: false,
        verifiedEndTime: '',
        corpIndustry: '',
        corpSubIndustry: '',
        corpUserMax: '',
        location: ''
      },
      loadRefesh: false,
      tableData: [],
      addShow: false,
      editRow: {},
      loadApp: false
    };
  },
  mounted() {
    const that = this;
    that.$emit('showTab', '/enterpriseSet');
    document.documentElement.style.backgroundColor = '#f0f2f5';
    that.getData();
    that.getApp();
  },
  destroyed() {
    document.documentElement.style.backgroundColor = '#fff';
  },
  methods: {
    /**
     * @description: 添加
     * @author: 无尘
     */
    addSecret() {
      const that = this;
      that.addShow = true;
      that.editRow = {};
    },
    /**
     * @description: 编辑
     * @param {Object} row
     * @author: 无尘
     */
    toEdit(row) {
      const that = this;
      that.addShow = true;
      that.editRow = JSON.parse(JSON.stringify(row));
    },
    closeText() {
      const that = this;
      that.addShow = false;
      that.editRow = {};
    },
    submitText() {
      const that = this;
      that.addShow = false;
      that.editRow = {};
      that.getApp();
    },
    /**
     * @description: 刷新app
     * @param {Object} row
     * @author: 无尘
     */
    refreshApp(row) {
      const that = this;
      that.loadApp = true;
      let para = {
        secretId: row.secretId
      };
      refreshSelfApp(para)
        .then(res => {
          that.loadApp = false;
          if (res.code == '0000') {
            showMsg.showmsg('刷新成功', 'success');
            that.getApp();
          }else {
            showMsg.showmsg(res.message, 'warning');
            that.getApp();
          }
        })
        .catch(function(error) {
          that.loadApp = false;
        });
    },
    /**
     * @description: 删除
     * @param {Object} row
     * @author: 无尘
     */
    toDel(row) {
      const that = this;
      that
        .$confirm('确定删除当前自建应用?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .then(() => {
          that.postDel( row);
        })
        .catch(() => {});
    },
    postDel(row) {
      const that = this;
      let para = {
        secretId: row.secretId
      };
      delSelfApp(para)
        .then(res => {
          if (res.code == '0000') {
            showMsg.showmsg('删除成功', 'success');
            that.getApp();
          }else {
            that.$message({
              message: res.message || '接口异常',
              type: 'warning'
            });
            that.getApp();
          }
        })
        .catch(function(error) {

        });
    },

    refeshData() {
      const that = this;
      that.refreshData();
    },
    /**
     * @description: 刷新数据
     * @author: 无尘
     */
    refreshData() {
      const that = this;
      that.loadRefesh = true;
      let para = {
        wxEnterpriseId: that.wxEnterpriseId
      };
      wxEnterpriseRefresh(para)
        .then(res => {
          that.loadRefesh = false;
          if (res.code == '0000') {
            showMsg.showmsg('刷新成功', 'success');
          }
          that.getData();
        })
        .catch(function(error) {
          that.loadRefesh = false;
        });
    },
    /**
     * @description: 获取自建应用
     * @author: 无尘
     */
    getApp() {
      const that = this;
      let para = {
        secretType: 3 // 1 客户联系secret 2通讯录secret 3好办助手 4会员小程序
      };
      getSelfApp(para)
        .then(res => {
          that.tableData = res.result || [];
        })
        .catch(function(error) {

        });
    },
    /**
     * @description: 获取数据
     * @author: 无尘
     */
    getData() {
      const that = this;
      let para = {
        wxEnterpriseId: that.wxEnterpriseId
      };
      wxEnterpriseDetail(para)
        .then(res => {
          that.companyObj = res.result || {};
        })
        .catch(function(error) {

        });
    }
  }
};
</script>
<style type="text/less" lang="less" scoped>
.color-c0c4cc {
  display: inline-block;
  line-height: 34px;
  padding-left: 10px;
  color: #c0c4cc;
}
.color-49c958 {
  color: #49c958;
}
.color-f83431 {
  color: #f83431;
}
.line-h-32 {
  line-height: 32px;
}
.condition-tips {
  display: inline-block;
  vertical-align: middle;
  line-height: 32px;
}

.w-127 {
  width: 127px;
}

.w-399 {
  width: 399px;
}

.p-l-5 {
  padding-left: 5px;
}

.p-l-15 {
  padding-left: 15px;
}

.p-l-130 {
  padding-left: 130px;
}

.p-l-137 {
  padding-left: 137px;
}

.m-l-20 {
  margin-left: 20px;
}

.m-l-37 {
  margin-left: 37px;
}

.m-l-137 {
  margin-left: 137px;
}
.m-b-20 {
  margin-bottom: 20px;
}
.m-t-30 {
  margin-top: 30px;
}
.secret-cell {
  position: relative;
  .secret-cell-title {
    height: 32px;
    line-height: 32px;
  }
  .common-bottom-border {
    cursor: pointer;
    padding-bottom: 2px;
    border-bottom: 1px dashed #2f54eb;
  }
  .self-app-info {
    width: 595px;
    margin-left: 12px;
    .self-app-item {
      height: 32px;
      line-height: 32px;
      &:nth-child(2) {
        margin-top: 14px;
      }
      &.secret-name {
        border-bottom: 1px solid #e4e7ed;
      }
      .app-item-title {
        display: inline-block;
        width: 100px;
        text-align: right;
        margin-right: 10px;
      }
      .secret-name-span {
        display: inline-block;
        max-width: 100px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        vertical-align: middle;
      }
    }
  }
  .add-self-app {
    width: 98px;
    height: 32px;
    line-height: 32px;
    margin-left: 20px;
    text-align: center;
    border: 1px solid #c0c4cc;
    &:hover {
      border-color: #2f54eb;
      span {
        color: #2f54eb;
      }
    }
  }
}
.add-cell {
  height: 43px;
  line-height: 43px;
  border-bottom: 1px solid #dcdfe6;
  &:hover {
    .add-cell-txt {
      color: #597ef7;
    }
  }
}

.daily-set-wrap {
  height: 100%;
  .company-set-content {
    padding: 20px;
    .table-condition-left {
      font-size: 0;
    }
  }
  .el-form {
    .el-form-item {
      margin-bottom: 4px;
    }
    /deep/ .el-form-item__label {
      color: #606266;
    }
  }
  .depart-item-content {
    width: 213px;
    height: 32px;
    overflow: hidden;
    white-space: nowrap;
    border-radius: 4px;
    border: 1px solid #dcdfe6;
    cursor: pointer;
  }
  .item-cell-select {
    /deep/ .el-select__tags {
      white-space: nowrap;
      overflow: hidden;
    }
  }
  .depart-cell {
    position: relative;
    margin: 0 24px 24px 0;
    .el-icon-circle-close {
      position: absolute;
      top: -10px;
      right: -10px;
      font-size: 16px;
      color: #808995;
      cursor: pointer;
    }
  }
}
</style>
