<template>
  <!-- 新增编辑产品类别 -->
  <div class="addEditCategory">
    <el-dialog :title="title" :visible.sync="dialogVisible" :before-close="handleClose" width="700px" :append-to-body="true" :close-on-click-modal="false">
      <el-form :model="form" :rules="rules" ref="ruleForm" label-width="110px" class="demo-ruleForm">
        <el-form-item label="类别名称" prop="name" >
          <el-input v-model="form.name" placeholder="请输入类别名称"></el-input>
        </el-form-item>
        <el-form-item label="参考许可费率" prop="rate">
          <el-input v-model="form.licenseRate" placeholder="请输入参考许可费率(介于 0 ~ 1 之间)"></el-input>
        </el-form-item>
        <el-form-item label="管理人" prop="rate">
          <div class="manage">
            <el-button size="small" @click="showDialog(2)">选择</el-button>
            <div v-html="getNames(form.adminPersons)">
              
            </div>
          </div>
          
        </el-form-item>
        <el-form-item label="谁可以看" prop="rate">
            <div  v-for="item in $constants.showType" :key="item.value" >
              <div>
                <el-radio-group :value="form.showType" @input="changeShowType">
                  <el-radio :label="item.value">{{item.label}}</el-radio>
                </el-radio-group>
                <!-- <el-radio :checked="item.value == form.showType" :label="item.value" :value="form.showType" @input="changeShowType(item.value)">{{item.label}}</el-radio> -->
              </div>
              <div v-if="form.showType && item.value == form.showType && [0,1].indexOf(item.value)==-1" class="person" @click="showDialog(1)">
                <p>选择朋友</p>
                <p v-html="getNames(form.persons)">
                </p>
              </div>
            </div>
        </el-form-item>
        <el-form-item label="图片" prop="">
          <el-upload ref="upload"  action="#" :auto-upload="false"  :on-change="handleChange"  list-type="picture" :show-file-list="false">
            <span v-if="form.systemFileList&&form.systemFileList.length>0" class="avatar">
              <span class="deleteImg">
                <span> 
                  <i class="el-icon-zoom-in" @click.stop="handlePictureCardPreview"></i>
                  <i class="el-icon-delete" @click.stop="handleRemove"></i>
                </span>
              </span>
                <el-image ref="image" style="width:100%;height: 100%;" :src="form.systemFileList[0].guid?$commonJS.checkViewer(form.systemFileList[0].guid):form.systemFileList[0].url" :preview-src-list="form.systemFileList?form.systemFileList.map(item=>{return item.guid? $commonJS.checkViewer(item.guid):item.url}):[]"></el-image>
            </span>
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="描述" prop="">
          <el-input v-model="form.description" type="textarea" :rows="2" placeholder="请输入描述"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose" >取消</el-button>
        <el-button type="primary" :loading="btnLoading" @click="submit" >确定</el-button>
      </div>
    </el-dialog>

    <choose-person ref="choosePerson" @getPersonIds="getPersonIds"></choose-person>
  </div>
</template>

<script>
import choosePerson from '@/views/components/dialog/person.vue'
export default {
  components:{
    choosePerson
  },
  data() {
    return {
      // 弹窗的title
      title: "",
      // 控制弹窗显示
      dialogVisible: false,
      // 类别表单数据
      form: {},
      // 类别表单的校验规则
      rules: {
        name: [ { required: true, message: '请输入类别名称', trigger: 'blur' },],
      },
      //待上传的文件
      file:[],
      //选择类型是管理人还是谁都可以看
      chooseType:1,
      //按钮加载
      btnLoading:false,
    };
  },
  mounted() { },
  methods: {
    // 上传的change
    handleChange(file,fileList){
      this.$set(this.form,'systemFileList', [{url:file.url}])
      this.file = [file.raw]
    },
    // 点击已上传的文件
    handlePictureCardPreview(file) {
      this.$refs.image.showViewer = true
     },
    // 文件列表移除文件
    handleRemove(file) {
      this.file = []
      this.form.systemFileList = []//直接清空适用于单张图片
     },
    // 打开弹窗
    open(row) {
      this.form = JSON.parse(JSON.stringify(row))
      if(!this.form.showType && this.form.showType!=0){
        this.$set(this.form,'showType',0)
        // this.form.showType = 0
      }
      if (row.id) {
        this.title = '编辑产品类别'
      } else {
        this.title = '新增产品类别'
      }
      this.dialogVisible = true;
    },
    // 弹窗确定
    submit() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          var formData = new FormData()
          this.form.guids = (this.form.systemFileList||[]).map(item=>{
            return item.guid
          })
          formData.append('productCategory',JSON.stringify(this.form))
          if(this.file.length>0){
            this.file.forEach(item=>{
              formData.append("files", item);
            })
          }
          this.btnLoading = true
          if(this.form.id){
            this.$api.updateProductCategory(formData).then(response=>{
              this.$message.success('更新成功')
              this.$emit('getMessage','更新成功')
              this.btnLoading = false
              this.handleClose()
            }).catch(error=>{
              this.btnLoading = false
            })
          }else{
            this.$api.addProductCategory(formData).then(response=>{
              this.$message.success('添加成功')
              this.$emit('getMessage','')
              this.btnLoading = false
              this.handleClose()
            }).catch(error=>{
              this.btnLoading = false
            })
          }
          
        } else {
          
        }
      })
    },
    // 弹窗取消/关闭
    handleClose() {
      this.$refs.ruleForm.resetFields()
      this.dialogVisible = false;
    },
    //切换谁可以看类型
    changeShowType(val){
      this.form.showType1 = this.form.showType
      this.$set(this.form,'showType',val)
      this.$set(this.form,'persons',[])
      this.$set(this.form,'showPersonIds',[])
      if(this.form.showType == 2 || this.form.showType == 3){
        this.showDialog(1)
      }
      
    },
    //打开人员弹窗
    showDialog(type){
      this.chooseType = type
      if(type == 2){
        this.$refs.choosePerson.open(this.form.adminIds,this.form.adminPersons)
        return false
      }
      this.$refs.choosePerson.open(this.form.showPersonIds,this.form.persons)
    },
    //获取人员信息
    getPersonIds({personIds,persons}){
      if(this.chooseType == 2){
        if(personIds.length>0){
          this.$set(this.form,'adminPersons',persons)
          this.$set(this.form,'adminIds',personIds)
        }else{
          this.$set(this.form,'adminPersons',[])
          this.$set(this.form,'adminIds',[])
        }
        return false
      }
      if(personIds.length>0){
        this.$set(this.form,'showType',this.form.showType)
        this.$set(this.form,'persons',persons)
        this.$set(this.form,'showPersonIds',personIds)
      }else{
        if(this.form.showType == this.form.showType1){
          this.$set(this.form,'showType',0)
        }else{
          this.$set(this.form,'showType',this.form.showType1)
        }
        
        this.$set(this.form,'persons',[])
        this.$set(this.form,'showPersonIds',[])
      }
    },

    getNames(row){
      if(!row){
        return ''
      }
      var arr = row.map(item=>{
        return item.name
      })
      return arr.join('、')
    }
  },
};
</script>

<style lang="scss" scoped>
.manage{
  display: flex;
  align-items: center;
  height: 40px;
  div{
    width:100%;
    padding-left:10px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow:ellipsis;
  }
}
.person{
  height:50px;
  padding-left:30px;
  cursor: pointer;
  p{
    margin:0;
    height: 30px;
    line-height: 30px;
  }
  p:first-child{
    color: green;
  }
  p:last-child{
    font-size: 12px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow:ellipsis;
  }
}


.avatar-uploader-icon {
    background-color: #fbfdff;
    border: 1px dashed #c0ccda;
    font-size: 28px;
    color: #8c939d;
    width: 148px;
    height: 148px;
    line-height: 148px;
    text-align: center;
  }
 .avatar {
  
  position: relative;
    width: 148px;
    height: 148px;
    display: block;
  }
  .avatar:hover .deleteImg {
    display: block;
}
.deleteImg {
  display: none;
  font-size: 30px;
  width: 148px;
  height: 148px;
  background-color:black;
	opacity: 0.6; 
  position: absolute;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
  margin: auto;
  z-index: 999;
}
.deleteImg span i{
  margin-left: 10px;
  color: #fff;
}
.deleteImg span{
  display: flex;
  align-items:center;/*垂直居中*/
  justify-content: center;/*水平居中*/
  width:100%;
  height:100%;
}</style>
