<template>
  <div class="detail_contain">
    <p class="now_page_title">
      我的应用  /  应用仓库  /  应用详情  /  
      <span>应用编辑</span>
    </p>
    <div class="info_contain">
      <el-form ref="form" :rules="rules" :model="form" label-width="0px">
        <el-form-item prop="name">
          <p class="formname">应用名称：</p>
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item prop="vision">
          <p class="formname">应用版本：</p>
          <el-input v-model="form.vision"></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 prop="type">
          <p class="formname">应用类型：</p>
          <el-select v-model="form.type" placeholder="请选择">
            <el-option
              v-for="item in type_arr"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        
        <el-form-item prop="fileList">
          <p class="formname">应用封面：</p>
          <upload-file
            :multiple="false"
            :max="1"
            type="cropper"
            :readOnly="false"
            fit="fill"
            :list="imgList"
            @getNewList="getNewList"
            ref="servicepic"
          ></upload-file>
        </el-form-item> 

        <el-form-item
        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.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item prop="appdesc">
          <p class="formname">应用简介：</p>
          <el-input type="textarea" v-model="form.appdesc"></el-input>
        </el-form-item>
        
        <el-form-item prop="actiondesc">
          <p class="formname">功能简介：</p>
          <el-input type="textarea" v-model="form.actiondesc"></el-input>
        </el-form-item>

        <el-form-item prop="example">
          <p class="formname">应用场景示例：</p>
          <el-input type="textarea" v-model="form.example"></el-input>
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="onSubmit('form')" class="right">保存</el-button>
          <el-button class="right" @click="back_page()">取消</el-button>
        </el-form-item>
      </el-form>

      
    </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: '',
          area:'',
          origin:'',
          vision:'',
          type:'',
          appdesc:'',
          actiondesc:"",
          example:'',
          fileList:''
        },
        open:true,
        open1:false,
        open2:'true',
        now_user: 2, //0：普通用户，1：组织管理员，2：超级管理员
        imgList: [],   
        area_arr:[],
        type_arr:[],
        origin_arr:[],
        rules:{
          name:[
            { required: true, message: '请输入应用名称', trigger: 'blur' },
          ],
          vision:[
            { required: true, message: '请输入应用版本', trigger: 'blur' },
          ],
          appdesc:[
            { required: true, message: '请输入应用版本', trigger: 'blur' },
          ],
          actiondesc:[
            { required: true, message: '请输入应用版本', trigger: 'blur' },
          ],
          example:[
            { required: true, message: '请输入应用版本', trigger: 'blur' },
          ],
          desc:[
            { required: true, message: '请输入服务描述', trigger: 'blur' },
            { max: 200, message: '长度小于200个字符', trigger: 'blur' }
          ],
          area:[
            { required: true, message: '请选择业务领域', trigger: 'change' },
          ],
          type:[
            { required: true, message: '请选择应用类型', trigger: 'change' },
          ],
          fileList:[
              {required: true, message: '请上传图片', trigger: 'change'}
          ]
          
        }
    };
  },
  watch: {},
  computed: {},
  created() {
    this.getServiceInfo()
    this.getOriginArr()
    this.get_area()
    this.get_type()
  },
  mounted() {},
  methods: {
    back_page(){
      this.$router.back(-1)
    },
    getOriginArr(){
      this.$http
            .get('/apaas/hubApi/market/departments')
            .then(response => {
                let data = response.data.data
                this.origin_arr = data
            })
            .catch(function(response) {
                
            });
    },
    get_area(){
      this.$http
        .get("/apaas/hubApi/market/businessAreas")
        .then((response) => {
          if(response.data.success){
            let data = response.data.data;
            this.area_arr = data
          }
        })
        .catch(function(response) {});
    },
    get_type(){
      this.$http
        .get("/apaas/hubApi/market/appTypes")
        .then((response) => {
          if(response.data.success){
            let data = response.data.data;
            this.type_arr = data
          }
        })
        .catch(function(response) {});
    },
    getServiceInfo() {
      this.$http
        .get("/apaas/hubApi/market/appEditInfo?id="+this.$route.params.id)
        .then((response) => {
          if(response.data.success){
            let data = response.data.data;
            this.form = {
              name: data.app_name,
              area:data.ywly,
              origin:data.org,
              vision:data.version,
              type:data.type,
              appdesc:data.yyjj,
              actiondesc:data.gnjj,
              example:data.cjsl,
              fileList:data.logo
            }
            this.imgList = [data.logo]
          }
        })
        .catch(function(response) {});
    },
    removepic(){
      this.$refs.servicepic.remove_pic()
    },
    post_app_info(){
      // 声明一个 FormData 数据
      let formData = new FormData()
      // 利用 append 方法往 formData 中添加数据，第一个参数是键名，第二个参数是键值
      formData.append('name',this.form.name)
      formData.append('version',this.form.vision)
      formData.append('ywly',this.form.area)
      formData.append('type',this.form.type)
      formData.append('org',this.form.origin)
      formData.append('logo',this.form.fileList)
      formData.append('yyjj',this.form.appdesc)
      formData.append('gnjj',this.form.actiondesc)
      formData.append('cjsl',this.form.example)
      formData.append('app_id',this.$route.params.id)
      this.$http
            .post('/apaas/hubApi/market/app',formData)
            .then(response => {
                console.log(response);
              if(!response.data.success){
                this.$message.error(response.data.errMsg)
              return
              }
              this.$message.success('操作成功')
              this.back_page()
            })
            .catch(function(response) {
                
            });
    },
      onSubmit(formName) {
        console.log('submit!');
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.post_app_info()
          } else {
            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>
