<template>
  <div>
    <p class="edit_page_title">{{platformList_type==='add'?'添加平台':'编辑平台'}}</p>
    <Form ref="editPlatform" :model="editPlatform" :rules="platform_rule" :label-width="170" class="add-admin-form">
        <FormItem label="平台名称：" prop="name">
        <Input v-model="editPlatform.name"  type="text" placeholder="请填写平台名称" class="wid_347 fl zd1" />
        <div class="clear"></div>
      </FormItem>
       <FormItem label="APPID：" prop="appid">
        <Input v-model="editPlatform.appid"  type="text" placeholder="请填写appid" class="wid_347 fl zd1" />
        <div class="clear"></div>
      </FormItem>
       <FormItem label="平台标识：" prop="ename" >
        <Input v-model="editPlatform.ename"  type="text" placeholder="平台的唯一标识，提交后不可修改" class="wid_347 fl zd1" />
        <div class="clear"></div>
      </FormItem>
       <FormItem label="注册接口：" prop="register">
        <Input v-model="editPlatform.api_register"  type="text" placeholder="请输入注册接口" class="wid_347 fl zd1" />
        <div class="clear"></div>
      </FormItem>
       <FormItem label="充值接口：" prop="recharge">
        <Input v-model="editPlatform.api_recharge"  type="text" placeholder="请输入充值接口" class="wid_347 fl zd1" />
        <div class="clear"></div>
      </FormItem>
       <FormItem label="新建推广员接口：" prop="create_promote">
        <Input v-model="editPlatform.api_create_promote"  type="text" placeholder="请输入新建推广员接口" class="wid_347 fl zd1" />
        <div class="clear"></div>
      </FormItem>
       <FormItem label="验证推广员接口：" prop=" verify_promote">
        <Input v-model="editPlatform.api_verify_promote"  type="text" placeholder="请验证推广员接口" class="wid_347 fl zd1" />
        <div class="clear"></div>
      </FormItem>
       <FormItem label="推广员可申请游戏接口：" prop="game">
        <Input v-model="editPlatform.api_game"  type="text" placeholder="请输入推广员可申请游戏接口" class="wid_347 fl zd1" />
        <div class="clear"></div>
      </FormItem>
       <FormItem label="推广员申请游戏接口：" prop="apply">
        <Input v-model="editPlatform.api_apply"  type="text" placeholder="请输入推广员申请游戏接口" class="wid_347 fl zd1" />
        <div class="clear"></div>
      </FormItem>
       <FormItem label="推广员已申请游戏接口：" prop="my">
        <Input v-model="editPlatform.api_my"  type="text" placeholder="请输入推广员已申请游戏接口" class="wid_347 fl zd1" />
        <div class="clear"></div>
      </FormItem>
       <FormItem label="推广员平台打包接口：" prop="package">
        <Input v-model="editPlatform.api_package"  type="text" placeholder="请输入推广员平台打包接口" class="wid_347 fl zd1" />
        <div class="clear"></div>
      </FormItem>
       <FormItem label="Token：" prop="token">
        <Input v-model="editPlatform.token"  type="text" placeholder="" class="wid_347 fl zd1" />
        <button class="reloadBtn" @click="getNewToken">刷新</button>
        <div class="clear"></div>
      </FormItem>
     <FormItem label="联系人：" prop="linkman">
        <Input v-model="editPlatform.linkman"  type="text" placeholder="请填联系人姓名" class="wid_347 fl zd1" />
        <div class="clear"></div>
      </FormItem>
       <FormItem label="联系电话：" prop="tel">
        <Input v-model="editPlatform.tel"  type="text" placeholder="请填写联系电话" class="wid_347 fl zd1" />
        <div class="clear"></div>
      </FormItem>
     
      <FormItem class="wid_440 form_btn">
        <Button type="primary" @click="editPlatformList('editPlatform')" >{{platformList_type==='add'?'确认':'保存'}}</Button>
        <Button class="mar_L_15 return_btn" @click="return_last">返回</Button>
      </FormItem>
    </Form>
  </div>
</template>
<script>
import { randomString } from '@/utils/utils'
export default {
  name: "edit_platformList",
  data() {
    return {
      editPlatform: {
       name:'',
       ename:'',
       api_register:'',
       api_recharge:'',
        api_game:'',
        api_apply:'',
        api_my:'',
        api_package:'',
       token:'',
       linkman:'',
       tel:'',
       appid:'',
       api_create_promote:'',
       api_verify_promote:''
      },
      platform_rule: {
        name:[
             { required: true, message: "请填写平台名称", trigger: "blur" }
        ],
        ename: [
          { required: true, message: "请填写平台标识", trigger: "blur" }
        ],  
        appid:[
           { required: true, message: "请填写appid", trigger: "blur" }
        ],
        api_register: [
          { required: true, message: "请填写注册接口", trigger: "blur" }
        ],
        api_recharge: [
          { required: true, message: "请填写充值接口", trigger: "blur" }
        ],
        api_create_promote: [
          { required: true, message: "请填写推广员接口", trigger: "blur" },
        ],
        api_verify_promote: [
          { required: true, message: "请验证推广员接口 ", trigger: "blur" },
        ],
        api_game: [
          { required: true, message: "请填写推广员可申请游戏接口", trigger: "blur" },
        ],
        api_apply: [
          { required: true, message: "请填写推广员申请游戏接口", trigger: "blur" }
        ],
        api_my: [
          { required: true, message: "请填写推广员已申请游戏接口", trigger: "blur" },
        ],
       
        api_package: [
          { required: true, message: "请填写推广员平台打包接口", trigger: "blur" },
        ],
       
        token: [
          { required: true, message: "请填写Token", trigger: "blur" },
        ]
      }
    };
  },
  props: {
    platformList_type: String,
    curInfo: Number,
    pindex:Number
  },
  methods:{
    editPlatformList(formname){ 
      this.$refs[formname].validate(valid => {
        if (valid) {
          const param = {
            api: {}
          };
          for (let [key, value] of Object.entries(this.editPlatform)) {
            if(key.indexOf('api_') == 0) {
              param.api[key.replace('api_','')] = value
            } else {
              param[key] = value
            }
          }
          let url = 'createPlatform'
          if (this.platformList_type === 'edit') {
            url = 'modifyPlatform'
          }
          this.$api[url](param)
            .then(response => {
              if (response.code == 200) {
                this.$Message.success({content: response.msg, onClose: () => {
                  this.$emit('return_last',this.pindex)
                }})
              } else {
                this.$Message.error(response.msg)
              }
            })
        }
      });
    },
    // 刷新按钮
    getNewToken() {
      this.editPlatform.token = randomString(64)
    },
    // 返回列表
    return_last(){
      this.$emit('return_last',this.pindex)
    },
    getPlatformDetail() {
      this.$api.getPlatformDetail({id:this.curInfo})
        .then(response => {
          if (response.code == 200) {
            const data = response.data
            this.editPlatform = data
            for (let [key, value] of Object.entries(data.api)) {
              this.editPlatform['api_' + key] = value
            }
          } else {
            this.$Message.error(response.msg)
          }
        })
    }
  },
  created(){
    this.getNewToken()
    this.$nextTick(()=>{
      if (!!this.curInfo && this.platformList_type === 'edit') {
        this.getPlatformDetail()
      }
    })
  }
};
</script>
<style lang="less">
.add-admin-form .platformLabel.ivu-form-item.ivu-form-item-required .ivu-form-item-label {
    width: 129px!important;
}
.add-admin-form .ivu-form-item-content {
    margin-left: 126px!important;
}
.reloadBtn {
    width: 56px;
    height: 32px;
    border: none;
    outline: none;
    line-height: 32px;
    border: 1px solid #3B82FF;
    border-radius: 4px;
    background: #fff;
    font-size: 14px;
    color: #3B82FF;
    margin-left: 30px;
    cursor: pointer;
}
</style>
