<template>
	<div class="layout">
		<div class="title">
			<h3>授权链接</h3>
		</div>
		<div class="content">
			<a href="/T/wt_auth/goto_auth_url" target="_blank">获取授权(1)</a>
		</div>
		<div class="layout-content">
			<el-form :inline="true" :model="ruleForm" ref="ruleForm" class="demo-form-inline" label-position="right">
				<el-form-item label="授权状态" prop="authorizationStatus" class="fx_send">
					<el-select v-model="ruleForm.authorizationStatus" placeholder="请选择" clearable>
						<el-option v-for="(item,index) in authorizationStatus" :key="index" :value="item.value" :label="item.name"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="小程序名称" prop="name" class="fx_title">
					<el-input v-model="ruleForm.name" placeholder="请输入" clearable></el-input>
				</el-form-item>
				<el-form-item label="小程序类型" prop="type" class="fx_send">
					<el-select v-model="ruleForm.type" placeholder="请选择" clearable>
						<el-option v-for="(item,index) in appType" :key="index" :value="item.value" :label="item.name"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-button class="hoverBtn" type="primary" icon="el-icon-search" size="medium" @click.stop="search"></el-button>
				</el-form-item>
				<el-form-item>
					<el-button class="hoverBtn" type="primary" @click.stop="batchBasicSet">批量基础配置(2)</el-button>
				</el-form-item>
				<el-form-item>
					<el-button class="hoverBtn" type="primary" @click.stop="batchExtendSet">批量扩展配置</el-button>
				</el-form-item>
				<el-form-item>
					<el-button class="hoverBtn" type="primary" @click.stop="getAuthorizerList">拉取已授权帐号信息</el-button>
				</el-form-item>
			</el-form>
		</div>
		<table-list :ref="tk" :options="optionsTable"></table-list>
		<choose-app :ref="tKey" :t-key="tKey" :cb="getShopCb"></choose-app>
		<extend :ref="tKey1" :t-key="tKey1"></extend>
		<getcategory :ref="tKey2" :t-key="tKey2"></getcategory>
		<template-msg :ref="tKey3" :t-key="tKey3"></template-msg>
	</div>
</template>
<script>
import { mapState, mapActions } from "vuex";
import TableList from "../../core/comps/table/tableList.vue";
import { CacheData } from "./../../service/couponData/data.js";
import { configUrl } from "./../../utils/config.js";
import alertDialog from "../../core/comps/input/alertDialog.vue";
import extend from "./../../components/marketing/businessComp/extend.vue";
import templateMsg from "./../../components/marketing/businessComp/templateMsg.vue";
import chooseApp from "./../../components/marketing/businessComp/chooseApp.vue";
import getcategory from "./../../components/marketing/businessComp/getcategory.vue";
export default {
  name: "HomePage",
  components: {
    TableList,
    alertDialog,
    extend,
    chooseApp,
    getcategory,
    templateMsg
  },
  data() {
    return {
      listNavKey: "homePageNavKey",
      tk: "homePageKey",
      tKey: "extendApp",
      tKey1: "extend",
      tKey2: "getcategory",
      tKey3: "templateMsgList",
      dialogVisible: false,
      privacySetting:"",
      latestAuditstatus:"",
      ruleForm: {
        authorizationStatus: "",
        type: "",
        name: ""
      },
      optionsTable: {
        actions: [
          {
            key: "edit",
            val: "找回授权码", 
            action: this.getBack,
            
          }
          // {
          //   key: "edit",
          //   val: "配置隐私保护指引", 
          //   action: this.setprivacysetting,
          //   show: item => {
          //     return (item.type == 1 || item.type == 2)&&item.privacySettingStatus!=0 ? true : false;
          //   }
          // },
          // {
          //   key: "edit",
          //   val: "查询隐私保护指引",
          //   action: this.getPrivacySetting,
          //   show: item => {
          //     return (item.type == 1 || item.type == 2)&&item.privacySettingStatus==0 ? true : false;
          //   }
          // },
          // // {
          // //   key: "edit",
          // //   val: "查询最新审核状态",
          // //   action: this.getLatestAuditstatus,
          // // },
          // {
          //   key: "edit",
          //   val: "基础",
          //   action: this.searchFigure,
          //   show: item => {
          //     return item.type == 1 || item.type == 2 ? true : false;
          //   }
          // },
          // {
          //   key: "edit",
          //   val: "扩展(3)",
          //   action: this.conFigure,
          //   show: item => {
          //     return item.type == 1 || item.type == 2 ? true : false;
          //   }
          // },
          // {
          //   key: "edit",
          //   val: "类目",
          //   action: this.getCategory,
          //   show: item => {
          //     return item.type == 1 || item.type == 2 ? true : false;
          //   }
          // },
          // {
          //   key: "delete",
          //   val: "模板列表",
          //   action: this.templateList,
          //   show: item => {
          //     return item.type == 1 || item.type == 2 ? true : false;
          //   }
          // },
          // {
          //   key: "edit",
          //   val: "创建O",
          //   action: this.createPlatform,
          //   show: item => {
          //     return (item.type == 3)&&(!item.openAppid) || (item.type == 4)&&(!item.openAppid) ? true : false;
          //   }
          // },
          // {
          //   key: "bind",
          //   val: "绑定O",
          //   action: this.bindPlatform,
          //   show: item => {
          //     return (item.type == 1)&&(!item.openAppid) || (item.type == 2)&&(!item.openAppid) ? true : false;
          //   }
          // },
          // {
          //   key: "unbind",
          //   val: "解绑O",
          //   action: this.unbindPlatform,
          //   show: item => {
          //     return item.type == 1 || item.type == 2 ? true : false;
          //   }
          // },
          // {
          //   key: "edit",
          //   val: "获取O",
          //   action: this.getPlatform,
          //   show: item => {
          //     return (item.type == 1)&&(!item.openAppid) || (item.type == 2)&&(!item.openAppid) ? true : false;
          //   }
          // },
          // {
          //   key: "edit",
          //   val: "重置Quota",
          //   action: this.clearQuota,
          //   show: item => {
          //     return item.type == 3 || item.type == 4 ? true : false;
          //   }
          // }
        ],
        map: [
          { key: "headImg", val: "小程序图标", etype: "img" },
          { key: "name", val: "小程序名称" },
          { key: "type", val: "类型", convert: this.cAppType },
          { key: "currentVersion", val: "当前发布版本" },
          { key: "authorizerAppid", val: "appid" },
          {
            key: "authorizationStatus",
            val: "授权状态",
            convert: this.authStatus
          },
          { key: "baseStatus", val: "基础版本状态", convert: this.baseStatus },
          {
            key: "domainStatus",
            val: "服务器域名状态",
            convert: this.domainStatus
          },
          {
            key: "webdomainStatus",
            val: "业务域名状态",
            convert: this.webdomainStatus
          },
          {
            key: "templateStatus",
            val: "模板消息状态",
            convert: this.templateStatus
          },
          {
            key: "privacySettingStatus",
            val: "用户隐私状态",
            convert: this.setPrivacySettingStatus
          },
          { 
            key: "openAppid", 
            val: "开放平台AppId" 
          }
        ],
        getUrl: () => {
          let type = this.ruleForm.type;
          let authorizationStatus = this.ruleForm.authorizationStatus;
          let name = this.ruleForm.name;
          return (
            configUrl.weappList +
            "?type=" +
            type +
            "&authorizationStatus=" +
            authorizationStatus +
            "&name=" +
            name
          );
        },
        pageOption: {
          sizeKey: "pageSize",
          indexKey: "pageNum",
          index: 1,
          size: 10
        },
        analysis: res => {
          return {
            data: res.data.list,
            count: res.data.total
          };
        }
      }
    };
  },
  computed: {
    appType: () => {
      return CacheData.appType;
    },
    authorizationStatus: () => {
      return CacheData.authorizationStatus;
    },
    ...mapState({
      topNav: state => state.topNav.data, //将 this.topNav 映射为  this.$store.topNav.data
      channels: state => state.couponStore.channels,
      navigationInfo: state => state.topNav.navigationInfo
    })
  },
  methods: {
    //找回授权码
    getBack(item){
      this.getFetch(`T/wt_auth/retrieve?appId=${item.authorizerAppid}`).then(res=>{
        this.$message.success("找回成功！")
      }).catch(err=>{
        this.$message.error(err.message?err.message:err.data)
      })
    },
    //
    setprivacysetting(item){
      console.log(item);
      this.postFetch("/O/weappSet/setprivacysetting",{appId:item.authorizerAppid}).then(res=>{
        console.log(res,"res");
        this.search()
      }).catch(error => {
          this.$message({
            type: "error",
            message: error.data
          });
        });
    },
    getPrivacySetting(item){
      this.getFetch(`/O/weappSet/getPrivacySetting?appId=${item.authorizerAppid}`).then(res=>{
        this.privacySetting = JSON.stringify(res.data)

        
        this.$confirm( this.privacySetting,"查询隐私保护指引",{
           confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$message({
            type: 'success',
            message: '确定'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消'
          });          
        });
      }).catch(error => {
          this.$message({
            type: "error",
            message: error.data
          });
        });
    },
    getLatestAuditstatus(item){
      this.getFetch(`/O/weappCode/getLatestAuditstatus?appId=${item.authorizerAppid}`).then(res=>{
        this.latestAuditstatus = JSON.stringify(res.data)
        this.$confirm( this.latestAuditstatus,"查询隐私保护指引",{
           confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$message({
            type: 'success',
            message: '确定'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消'
          });          
        });
      }).catch(error => {
          this.$message({
            type: "error",
            message: error.data
          });
        });
    },
    //拉取当前所有已授权的帐号基本信息
    getAuthorizerList: function() {
      this.getFetch(configUrl.getAuthorizerList)
        .then(res => {
          console.log(res);
          this.$message({
            type: "success",
            message: "拉取当前所有已授权的帐号基本信息成功"
          });
        })
        .catch(error => {
          this.$message({
            type: "error",
            message: error.data
          });
        });
    },

    //获得模板列表
    templateList(item) {
      console.log(item);
      let parmers = JSON.stringify({
        appId: item.authorizerAppid
      });
      this.$refs[this.tKey3].openDialog(parmers);
    },

    //批量扩展配置
    batchExtendSet() {
      let parmers = JSON.stringify({
        flag: "extendJSON"
      });
      this.$refs[this.tKey1].openDialog(parmers);
    },
    //小程序的类型
    cAppType(col, item) {
      let res = CacheData.appType.filter(e => {
        return e.value == item.type;
      })[0];
      if (res) {
        return res.name;
      } else {
        return "小程序类型无效";
      }
      return res.name;
    },
    search: function() {
      let tk = this.$refs[this.tk];
      tk.search(tk.getParams().index);
    },

    /***
     * createPlatform 创建开放平台
     * bindPlatform   绑定开放平台
     * unbindPlatform 解绑开放平台
     */

    createPlatform(item) {
      let appId = item.authorizerAppid;
      this.getFetch(configUrl.createOpenPlatform + "?appId=" + appId)
        .then(res => {
          console.log(res);
          this.$message({
            type: "success",
            message: "创建微信开放平台成功"
          });
        })
        .catch(error => {
          this.$message({
            type: "error",
            message: error.data
          });
        });
    },
    // 绑定开放平台
    bindPlatform(item) {
      console.log(item);
      this.$prompt("请输入openAppId", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputPattern: /\S/,
        inputErrorMessage: "openAppId不能为空"
      })
        .then(({ value }) => {
          console.log(value);
          let appId = item.authorizerAppid;
          let openAppId = value;
          this.getFetch(
            configUrl.bindOpenPlatform +
              "?appId=" +
              appId +
              "&openAppId=" +
              openAppId
          )
            .then(res => {
              console.log(res);
              this.$message({
                type: "success",
                message: "绑定微信开放平台成功"
              });
            })
            .catch(error => {
              console.log(error);
              this.$message({
                type: "error",
                message: error.data
              });
            });
        })
        .catch(() => {
          this.$message({
            type: "error",
            message: "取消输入"
          });
        });
    },
    //解绑开放平台
    unbindPlatform(item) {
      console.log(item);
      this.$prompt("请输入openAppId", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputPattern: /\S/,
        inputErrorMessage: "openAppId不能为空"
      })
        .then(({ value }) => {
          console.log(value);
          let appId = item.authorizerAppid;
          let openAppId = value;
          this.getFetch(
            configUrl.unbindOpenPlatform +
              "?appId=" +
              appId +
              "&openAppId=" +
              openAppId
          )
            .then(res => {
              console.log(res);
              this.$message({
                type: "success",
                message: "解绑微信开放平台成功"
              });
            })
            .catch(error => {
              console.log(error);
              this.$message({
                type: "error",
                message: error.data
              });
            });
        })
        .catch(() => {
          this.$message({
            type: "error",
            message: "取消输入"
          });
        });
    },
    getPlatform(item) {
      let appId = item.authorizerAppid;
      let openAppId = "";
      this.getFetch(configUrl.getOpenPlatform + "?appId=" + appId)
        .then(res => {
          console.log(res);
          this.$message({
            type: "success",
            message: "获取微信开放平台Appid成功"
          });
        })
        .catch(error => {
          this.$message({
            type: "error",
            message: error.data
          });
        });
    },
    clearQuota(item) {
      let appId = item.authorizerAppid;
      let openAppId = "";
      this.getFetch(configUrl.clearQuota + "?appId=" + appId)
        .then(res => {
          console.log(res);
          this.$message({
            type: "success",
            message: "重置公众号Quota成功"
          });
        })
        .catch(error => {
          this.$message({
            type: "error",
            message: error.data
          });
        });
    },
    //选择小程序
    batchBasicSet(item) {
      let parmers = {
        type: this.ruleForm.type,
        from: "fromHomePage"
      };
      this.$refs[this.tKey].openDialog(JSON.stringify(parmers));
    },
    //选择小程序
    getShopCb(items) {
      this.shopTableData = [];
      items.data.map((item, index) => {
        let haveIf = false;
        this.shopTableData.map((item01, index01) => {
          if (item.id == item01.id) {
            haveIf = true;
          }
        });
        if (haveIf == false) {
          item.merchantName = item.name;
          item.merchantLinkPhone = item.linkPhone;
          this.shopTableData.push(item);
        }
      });
    },
    //小程序授权状态
    authStatus(col, item) {
      let res = CacheData.authorizationStatus.filter(e => {
        return e.value == item.authorizationStatus;
      })[0];
      if (res) {
        return res.name;
      } else {
        return "小程序授权状态无效";
      }
      return res.name;
    },
    //基础配置
    baseStatus(col, item) {
      let res = CacheData.baseStatus.filter(e => {
        return e.value == item.baseStatus;
      })[0];
      if (res) {
        return res.name;
      } else {
        return "暂无基础配置";
      }
      return res.name;
    },
    //服务器域名配置
    domainStatus(col, item) {
      let res = CacheData.domainStatus.filter(e => {
        return e.value == item.domainStatus;
      })[0];
      if (res) {
        return res.name;
      } else {
        return "暂无配置服务器域名";
      }
      return res.name;
    },
    //配置业务域名
    webdomainStatus(col, item) {
      let res = CacheData.webdomainStatus.filter(e => {
        return e.value == item.webdomainStatus;
      })[0];
      if (res) {
        return res.name;
      } else {
        return "暂无配置业务域名";
      }
      return res.name;
    },
    //小程序用户隐私状态
    setPrivacySettingStatus(col,item){
      let sing = ""
      if(!item.privacySettingStatus&&item.privacySettingStatus!=0){
        sing  = "暂无配置信息"
      }else if(item.privacySettingStatus===0){
        sing = "已设置"
      }else{
        sing = "设置失败"
      };
      
      return sing
    },
    //小程序模板消息
    templateStatus(col, item) {
      let res = CacheData.templateStatus.filter(e => {
        return e.value == item.templateStatus;
      })[0];
      if (res) {
        return res.name;
      } else {
        return "暂无配置模板消息";
      }
      return res.name;
    },
  
    //查看基础配置
    searchFigure(item) {
      let parmers = JSON.stringify({
        flag: "basicFigure",
        appId: item.authorizerAppid
      });
      this.$refs[this.tKey1].openDialog(parmers);
    },
    //查看小程序类目
    getCategory(item) {
      console.log(item);
      let parmers = JSON.stringify({
        appId: item.authorizerAppid
      });
      this.$refs[this.tKey2].openDialog(parmers);
    },
    //扩展配置
    conFigure(item) {
      let parmers = JSON.stringify({
        flag: "conFigure",
        appId: item.authorizerAppid
      });
      this.$refs[this.tKey1].openDialog(parmers);
    }
  }
};
</script>
<style scoped>
.tableContainer {
  width: 100%;
  margin-top: 30px;
}
.title {
  margin-bottom: 10px;
}
.title h3 {
  font-size: 18px;
  font-weight: bold;
}
.content span {
  font-size: 15px;
}
.content span:nth-of-type(1) {
  color: #19ce00;
  font-weight: bold;
  margin-right: 20px;
}
.content span:nth-of-type(2) {
  margin-right: 20px;
  cursor: pointer;
}
.layout-content {
  margin-top: 20px;
  background: #f6f6f6;
  padding: 29px 20px 0;
  border-radius: 6px;
}
.el-select-dropdown__item {
  display: block;
}
</style>
<style>
.el-message-box {
  width: 1000px;
  padding: 0 0 20px;
}
.el-message-box img {
  width: 200px;
  display: block;
  margin: 0 auto;
}
.el-message-box__header {
  text-align: center;
  font-size: 14px !important;
}
.el-message-box__btns button {
  width: 100px !important;
}
.el-message-box__title {
  font-size: 16px;
}
</style>

