<template>
  <div >
    <Modal id="staff_platform_update_modal" v-model="showMd" :title="title" :footer-hide="true" class-name="vertical-center-modal" :width="500">
      <div class="staff_platform_update">
        <Form ref="formSubmit" :model="formSubmit" :rules="formSubmitRule" :label-width="120" class="add-adminguild-form">
          <FormItem  label="类型：" prop="platform" class="typeChooseModal">
             <RadioGroup v-model="type" @on-change="changeRadio">
                <Radio :label="0">指定账号</Radio>
                <Radio :label="1">自动创建</Radio>
            </RadioGroup>
          </FormItem>
          <FormItem  label="绑定平台：" prop="platform" v-if="this.type == 1">
            <Select v-model="formSubmit.platform_id" placeholder="选择绑定平台" class="wid_347 fl zd1" filterable @on-change="choosePromoteData">
              <Option v-for="(item,index) in platformList" :value="item.id" :key="index" >{{item.name}}</Option>
            </Select>
          </FormItem>
          <div v-if="this.type == 0">
            <FormItem  label="绑定平台：" prop="platform_id" >
              <Select v-model="formSubmit.platform_id" placeholder="选择绑定平台" class="wid_347 fl zd1" filterable @on-change="choosePromoteData">
                <Option v-for="(item,index) in platformList" :value="item.id" :key="index" >{{item.name}}</Option>
              </Select>
            </FormItem>
            <FormItem  label="平台账号：" prop="platform" >
              <Input v-model="formSubmit.promote_account " placeholder="请输入账号"></Input>
            </FormItem>
             <FormItem  label="平台密码：" prop="platform" >
              <Input v-model="formSubmit.promote_password" type="password" placeholder="请输入密码"></Input>
            </FormItem>
          </div>
          <FormItem class="form_btn">
            <Button type="primary" @click="formConfirmClick('formSubmit')" :disabled="this.showConfirm">保存</Button>
            <Button class="mar_L_15 return_btn" @click="formCancelClick">取消</Button>
          </FormItem>
        </Form>
      </div>
    </Modal>
  </div>
</template>
<script>
export default {
  name: "staff_platform_update",
  props: {
    show: Boolean,
    value:Number,
    name: String,
  },
  data() {
    return {
      title: '',
      type:0,
      formSubmit: {
        appid:'',
        manager_id: 0,
        platform_id: '',
        promote_account :'',
        promote_password:''
      },
      formSubmitRule: {
        platform_id: [
          {
            type: 'number',
            required: true,
            message: "请选择绑定平台",
            trigger: "change"
          }
        ],
        promote_account:[
          { required: true, message: "请输入平台账号", trigger: "blur" },
        ],
         promote_password:[
          { required: true, message: "请输入平台密码", trigger: "blur" },
        ],
        platform_id:[
          { type:'number',required: true, message: "请选择绑定平台", trigger: "change" },
        ]
      },
      platformList: [],
      showConfirm:false
    };
  },
  computed: {
    showMd: {
      get(n) {
        this.title = '员工【' + this.name + '】绑定平台'
        if(this.show){
          this.getData()
          this.formSubmit.manager_id = this.value
        }else {
          this.formSubmit = {}
        }
        return this.show;
      },
      set(val) {
        this.$emit("update:show", val);
      }
    }
  },
  methods: {
    changeRadio(e){
      if(e == 1) {
        this.formSubmit = {}
      }
    },
    choosePromoteData(e){
      this.platformList.forEach(item =>{
        if(e == item.id) {
          this.formSubmit.appid = item.appid
        }
      })
    },
    getData () {
      this.$api.getPlatformList({page:1,limit:999}).then(data => {
        if (data.code == 200) {
          if (data.data.length > 0) {
            this.platformList = data.data
          } else {
            this.$Message.error({content: '公会未绑定平台，请先绑定公会平台', onClose: () => {
              this.showMd = false
            }})
          }
        }
      })
    },
    formConfirmClick (formname) {
      this.$refs[formname].validate(valid => {
        if(valid) {
          this.showConfirm = true
          this.$api.getManagerChannelBind(this.formSubmit)
            .then(response => {
              if (response.code == 200) {
                this.showMd = false
                 this.showConfirm = false
                this.$emit("platform-bind")
                this.$Message.success({content: response.msg})
              } else {
                this.showConfirm = false
                this.$Message.error(response.msg)
              }
          })
        }
      })
    },
    formCancelClick () {
      this.showMd = false
      this.formSubmit = {}
    }
  },
  created() {},
};
</script>
<style scoped>
.staff_platform_update {
  padding: 20px 15px;
}
.typeChooseModal .ivu-radio-wrapper {
  margin-right: 30px;
}
</style>


