<template>
  <div class="detail_contain">
    <p class="now_page_title">
      我的服务 / {{now_user==0||now_user==4?'注册发布的服务':'平台服务管理'}} /
      <span>服务编辑</span>
    </p>
    <div class="info_contain">
      <div class="edit_change" v-if="now_user==2">
        <div class="edit_change_box" @click="now_edit=0" :style="now_edit==0?{backgroundColor:'#e56600',color:'#ffffff'}:{}">
          <img v-if="now_edit==0" src="@/assets/imgs/nav_ic_xinxi_sel.png" alt="">
          <img v-if="now_edit!==0" src="@/assets/imgs/nav_ic_xinxi_nor.png" alt=""> 基本信息编辑</div>
        <div class="edit_change_box" @click="now_edit=1" :style="now_edit==1?{backgroundColor:'#e56600',color:'#ffffff'}:{}">
          <img v-if="now_edit==1" src="@/assets/imgs/nav_ic_anquan_sel.png" alt="">
          <img v-if="now_edit!==1" src="@/assets/imgs/nav_ic_anquan_nor.png" alt=""> 服务安全设置</div>
      </div>
      <el-form ref="form" :rules="rules" :model="form" label-width="0px" v-show="now_edit==0">
        <el-form-item prop="name">
          <p class="formname">服务名称：</p>
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item prop="desc">
          <p class="formname">服务描述：</p>
          <el-input type="textarea" v-model="form.desc"></el-input>
        </el-form-item>
        <el-form-item prop="area">
          <p class="formname">服务领域：</p>
          <el-select v-model="form.area" placeholder="请选择">
            <el-option
              v-for="item in area_arr"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="now_user==0"
        prop="origin"
        :rules="[
          { required: true, message: '请选择所属组织', trigger: 'change' },
        ]"
         >
          <p class="formname">所属组织：</p>
          <el-select v-model="form.origin" placeholder="请选择" :disabled="true">
            <el-option
              v-for="item in origin_arr"
              :key="item.department_id"
              :label="item.department_name"
              :value="item.department_id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <p class="formname">服务封面：</p>
          <upload-file
            :multiple="false"
            :max="1"
            type="cropper"
            :readOnly="false"
            fit="fill"
            :list="imgList"
            directory="service"
            @getNewList="getNewList"
            ref="servicepic"
          ></upload-file>
        </el-form-item>
        <el-form-item v-if="now_user==0" prop="code" :rules="[
          { required: true, message: '请输入接口编码', trigger: 'blur' },
        ]">
          <p class="formname">接口编码：</p>
          <el-input v-model="form.code"></el-input>
        </el-form-item>   
        <!-- <el-form-item v-if="now_user==2" prop="people" :rules="[
          { required: true, message: '请输入发布人', trigger: 'blur' },
        ]">
          <p class="formname">发布人：</p>
          <el-input v-model="form.people"></el-input>
        </el-form-item>   
        <el-form-item v-if="now_user==2" prop="phone" :rules="[
          { required: true, message: '请输入联系方式', trigger: 'blur' },
        ]">
          <p class="formname">联系方式：</p>
          <el-input v-model="form.phone"></el-input>
        </el-form-item>    -->
        <el-form-item prop="resource">
          <p class="formname">开放程度：</p>
          <el-radio-group v-model="form.resource">
            <el-radio :label="1">共享</el-radio>
            <el-radio :label="2">受限</el-radio>
            <el-radio :label="3">敏感</el-radio>
          </el-radio-group>
        </el-form-item>
        
        <el-form-item class="apass_button">
          <el-button type="primary" @click="onSubmit('form')" class="right">保存</el-button>
          <el-button class="right" @click="calcle">取消</el-button>
        </el-form-item>
      </el-form>

      <div class="safe_set"  v-show="now_edit==1">
        <div class="safe_title">
          <div v-for="(item,index) in safe_arr" :key="index+4000" 
          class="safe_select"
          @click="now_safe=index"
          :style="now_safe==index?{borderBottom: '4px solid #f5ab4c'}:{}"
          >{{item}}</div>
        </div>
        <div class="safe_box_select">  
          <div v-if="now_safe==0">
            <p class="formtitle">是否开启：</p>
            <div class="openbgc" @click="openstart" :style="!open?{backgroundImage:'url('+require('@/assets/imgs/btn_off_hov.png')+')'}:{}"></div>
            <p class="formtitle">最大连接数：</p>
            <el-input :disabled="!open" v-model="maxline" placeholder="请输入内容"></el-input>       
          </div>       

          <div v-if="now_safe==1">
            <p class="formtitle">是否开启：</p>
            <div class="openbgc" @click="openstart1" :style="!open1?{backgroundImage:'url('+require('@/assets/imgs/btn_off_hov.png')+')'}:{}"></div>
            <p class="formtitle">最大连接数：</p>
            <el-input :disabled="!open1" v-model="maxline1" placeholder="请输入内容"></el-input>     
            <div class="limitsd">
              <p class="formtitle">有效时间：</p>
              <el-input  :disabled="!open1" v-model="usetime1" placeholder="请输入内容"></el-input> 
              <el-select  :disabled="!open1" class="timeslect" v-model="timevalue" placeholder="请选择">
                <el-option
                  v-for="item in timeoptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </div>
          </div>  

          <div v-if="now_safe==2">
            <p class="formtitle">是否开启：</p>
            <div class="openbgc" @click="openstart2" :style="!open2?{backgroundImage:'url('+require('@/assets/imgs/btn_off_hov.png')+')'}:{}"></div>       
          </div>      
        </div>
        <div class="savebtn">
          <div class="btnsty" @click="calcle()">取消</div>
          <div class="btnsty" @click="saveInfo()">保存</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import uploadFile from '@/components/general/upload_file'
import { getRole } from "@/utils/common";
import { mapGetters,mapState } from 'vuex'
export default {
  props: {},
  components: {
    uploadFile
  },
  data() {
    return {
        form: {
          name: '',
          desc:'',
          area:'',
          origin:'',
          code:'',
          people:'',
          phone:'',
          resource: '',
          fileList:''
        },
        open:1,
        maxline:'',
        open1:0,
        maxline1:'',
        open2:0,
        usetime1:'',
        now_user: 2, //0：普通用户，1：组织管理员，2：超级管理员
        now_edit:0,//信息和安全切换
        now_safe:0,//切换安全
        imgList: [],
        timevalue:'',
        timeoptions:[
          {
            label:'day(天)',
            value:3,
          },
          {
            label:'hour(小时)',
            value:2,
          },
          {
            label:'min(分钟)',
            value:1,
          },
          {
            label:'second(秒)',
            value:0,
          },
        ],
        safe_arr:['熔断管理','速率限制','TLS加密','敏感字段追踪'],
        area_arr:[],
        origin_arr:[],
        rules:{
          name:[
            { required: true, message: '请输入服务名称', trigger: 'blur' },
          ],
          desc:[
            { required: true, message: '请输入服务描述', trigger: 'blur' },
            { max: 200, message: '长度小于200个字符', trigger: 'blur' }
          ],
          area:[
            { required: true, message: '请选择服务领域', trigger: 'change' },
          ],
          resource:[
            { required: true, message: '请选择开放程度', trigger: 'change' },
          ],
          
        }
    };
  },
  watch: {
    open(){
      this.maxline = ''
    },
    open1(){
      this.maxline1 = ''
      this.usetime1 = ''
      this.timevalue = ''
    },
  },
  computed: {
    ...mapGetters([
      'level',
    ]),
    ...mapState(['userInfo']),
  },
  created() {
    console.log(this.level);
    if(this.userInfo){
      this.now_user = this.level
    }else{
      this.getCurrentUser()
    }
    this.getOriginArr()
    this.getServiceInfo()
    this.get_service_arae()
  },
  mounted() {},
  methods: {
    getCurrentUser() {
      this.$api.user.getNowUser().then(({ data }) => {
        if (data.success == 1) {
          console.log("--- user info ---");
          console.log(data.data);
          console.log("--- user info ---");
          this.$store.commit("userInfofun", data.data);
          this.now_user = this.level
           console.log(this.level);
        } else {
          console.log(data.errMsg);
        }
      });
    },
    getOriginArr(){
      this.$http
            .get('/apaas/backmgt/department/list')
            .then(response => {
                let data = response.data.data
                this.origin_arr = data
            })
            .catch(function(response) {
                
            });
    },
    get_service_arae(){
      this.$http
            .get('/apaas/service/v3/service/manager/servarea')
            .then(response => {
                let data = response.data.data
                this.area_arr = data
            })
            .catch(function(response) {
                
            });
    },
    getServiceInfo() {
      this.$http
        .get("/apaas/service/v3/service/manager?service_id="+this.$route.params.id)
        .then((response) => {
          let data = response.data.data;
          console.log(data);
          this.imgList = []
          this.$set(this.form,'name',data.name)
          this.$set(this.form,'desc',data.descript)
          this.$set(this.form,'area',data.sectors)
          this.$set(this.form,'origin',data.organization)
          this.$set(this.form,'code',data.encode_method)
          this.$set(this.form,'people',data.register_user_info?data.register_user_info.user_name:data.register_user_info)
          this.$set(this.form,'phone',data.register_user_info?data.register_user_info.phone:data.register_user_info)
          this.$set(this.form,'resource',data.openness)
          this.$set(this.form,'fileList',data.cover)
          this.imgList = data.cover.split(';')
          this.open = data.service_safe_config?data.service_safe_config.fusing:data.service_safe_config
          this.open1 = data.service_safe_config?data.service_safe_config.req_intervals:data.service_safe_config
          this.maxline = data.service_safe_config?data.service_safe_config.max_connections:data.service_safe_config
          this.maxline1 = data.service_safe_config?data.service_safe_config.max_req_num:data.service_safe_config
          this.usetime1 = data.service_safe_config?data.service_safe_config.valid_time:data.service_safe_config
          this.timevalue = data.service_safe_config?data.service_safe_config.time_unit:data.service_safe_config
          this.open2 = data.service_safe_config?data.service_safe_config.tls:data.service_safe_config
        })
        .catch(function(response) {});
    },
    setService(){
      if(this.open){
        if(this.maxline){

        }else{
          this.$message.error('请输入最大连接数')
          return
        }
      }
      if(this.open1){
        if(!this.maxline1){
          this.$message.error('请输入最大连接数')
          return
        }
        if(!this.usetime1){
          this.$message.error('请输入有效时间')
          return
        }
        if(this.timevalue==''){
          this.$message.error('请选择时间')
          return
        }
        
      }
      var temp = {
        id:parseInt(this.$route.params.id),
        "name": this.form.name,
        "sectors": this.form.area,
        "organization": this.form.origin,
        "cover": this.form.fileList,
        "openness": this.form.resource,
        "descript": this.form.desc,
        "service_safe_config":{  // 安全配置 超管传入
            fusing:this.open,
            max_connections:parseInt(this.maxline),
            req_intervals:this.open1,
            max_req_num:parseInt(this.maxline1),
            valid_time:parseInt(this.usetime1),
            time_unit:this.timevalue,
            tls:this.open2,
          }
      }
      this.$http
        .put("/apaas/service/v3/service/manager",temp)
        .then((response) => {
         console.log(response);
         if(!response.data.success){
              this.$message.error(response.data.errMsg)
              return
          }
         this.$message.success('保存成功')
         this.getServiceInfo()
        })
        .catch(function(response) {

        });
    },
    calcle(){
      this.$router.back(-1)
    },
    saveInfo(){
      this.onSubmit('form')
    },
    openstart(){
      if(this.open){
        this.open = 0
      }else{
        this.open = 1
      }
    },
    openstart1(){
      if(this.open1){
        this.open1 = 0
      }else{
        this.open1 = 1
      }
    },
    openstart2(){
      if(this.open2){
        this.open2 = 0
      }else{
        this.open2 = 1
      }
    },
    removepic(){
      this.$refs.servicepic.remove_pic()
    },
      onSubmit(formName) {
        console.log('submit!');
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.setService()
          } else {
            this.$message.error('请填写完整表单')
            console.log('error submit!!');
            return false;
          }
        });
      },
      getNewList(val) {
        console.log(val);
        this.form.fileList = ""
        this.form.fileList = val.map((v,k)=>{
          return v.url
        }).join(';')
        console.log(this.form.fileList)
      }
  }
};
</script>


<style>
.info_contain .el-input__inner{
  background-color: #f7f8f9;
  width: 1022px;
}
.info_contain .el-textarea__inner{
  background-color: #f7f8f9;
  width: 1022px;
}
.limitsd .timeslect .el-input__inner{
  width: 150px;
  border-radius: 0 8px 8px 0;
  background-color: rgba(15, 38, 131, 1);
  color: rgba(248, 249, 253, 1);
  border: 0;
}
</style>

<style scoped>
.detail_contain {
  width: 100%;
  padding: 0 20px;
  height: calc(100% - 55px);
}
.info_contain {
  padding: 25px 20px;
  background-color: #fff;
  width: 100%;
  box-shadow: 0px 3px 6px 0px #f4f7fc;
  border-radius: 12px;
  min-height: calc(100% - 20px);
    margin-bottom: 20px;
    position: relative;
}
.now_page_title {
  margin: 15px 0;
  color: #898d9e;
}
.now_page_title span {
  color: #242c43;
}
.formname{
  color: #58617a;
}
.right{
  float: right;
  margin-left: 20px;
}
.removepic{
  display: inline-block;
  margin-top: -40px;
  float: left;
  margin-left: 180px;
  width: 76px;
	height: 32px;
	background-color: #495feb;
  border-radius: 8px;
  line-height: 32px;
  text-align: center;
  color: #e6ebfe;
  cursor: pointer;
}
.edit_change{
  width: 100%;
  border-bottom: 2px solid #f4f7fc;
  overflow: hidden;
}
.edit_change_box{
  width: 163px;
  height: 48px;
  line-height: 48px;
	background-color: #e3e5ef;
  border-radius: 8px;
  color: #8890a7;
  text-align: center;
  float: left;
  margin-right: 20px;
  font-weight: 600;
  margin-bottom: 25px;
  font-size: 16px;
  cursor: pointer;
}
.edit_change_box img{
  vertical-align: -3px;
  margin-right: 5px;
}
.safe_title{
    width: 100%;
    height: 65px;
    line-height: 65px;
    border-bottom: 2px solid #f4f7fc;
}
.safe_select{
    float: left;
    width: auto;
    margin-right: 40px;
    height: 65px;
    cursor: pointer;
}
.safe_box_select{
    padding: 30px 10px 10px 10px;
    width: 100%;
    height: 100%;
}
.openbgc{
  width: 56px;
	height: 23px;
  background-image: url('~@/assets/imgs/btn_on_hov.png');
  background-size:contain;
  cursor: pointer;
}
.formtitle{
  color: #58617a;
  margin: 15px 0;
}

.savebtn{
  position: absolute;
  right: 20px;
  bottom:20px;
  width: 278px;
  overflow: hidden;
}
.btnsty{
  width: 124px;
	height: 44px;
  line-height: 44px;
	background-color: rgba(195, 202, 248, 0.5);
	border-radius: 8px;
  color: rgba(15, 38, 131, 1);
  text-align: center;
  float: left;
  margin-right: 30px;
  cursor: pointer;
}
.savebtn .btnsty:nth-last-of-type(1){
  margin-right: 0px;
  background-color: rgba(15, 38, 131, 1);
  color: rgba(248, 249, 253, 1);
}
.limitsd{
  position: relative;
  width: 1022px;
}
.limitsd .timeslect{
  position: absolute;
  right: 0;
}
</style>
