<template>
  <el-dialog
    :title="modalTitle"
    :visible.sync="modalVisible"
    :width="formLabelWidth"
    top="2vh"
    @close="handleCancel">
    <div class="mobile">
        <el-form label-width="90px" :model="form" :rules="rules" ref="from" class="from">
            <el-form-item label="企业类型:" prop="customerType">
                <el-select v-model="form.customerInfoForm.companyType" :disabled="limit.isRead" placeholder="客户类型" clearable size="small">
                  <el-option
                    v-for="dict in companyTypeOptions"
                    :key="dict.guid"
                    :label="dict.typeName"
                    :value="dict.guid"
                  />
                </el-select>
            </el-form-item>
            <el-form-item label="企业名称:">
                <el-input type="text" v-model="form.customerCompanyInfoForm.companyName" :disabled="limit.isRead"></el-input>
            </el-form-item>
            <el-form-item label="机构代码证:">
                <el-input type="text" v-model="form.customerCompanyInfoForm.orgCode" :disabled="limit.isRead"></el-input>
            </el-form-item>
            <el-form-item label="联系人:">
                <el-input type="text" v-model="form.customerCompanyInfoForm.principals" :disabled="limit.isRead"></el-input>
            </el-form-item>
            <el-form-item label="电话:">
                <el-input type="text" v-model="form.customerCompanyInfoForm.phone" :disabled="limit.isRead"></el-input>
            </el-form-item>
            <el-form-item label="邮箱:">
                <el-input type="text" v-model="form.customerCompanyInfoForm.email" :disabled="limit.isRead"></el-input>
            </el-form-item>
            <el-form-item label="企业地址:">
                <el-input type="text" v-model="form.customerCompanyInfoForm.companyAddress" :disabled="limit.isRead"></el-input>
            </el-form-item>
            <el-divider content-position="left">企业图片</el-divider>
            <div class="uploadImg">
               <el-upload
                 v-if="!limit.isRead"
                 accept=".jpg, .png"
                 :headers="upload.headers"
                 :action="upload.url"
                 :disabled="upload.isUploading"
                 :on-progress="handleFileUploadProgress"
                 :on-success="handleFileSuccess"
                 :file-list="fileList">
                 <el-button size="small" type="primary">点击上传</el-button>
                 <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
               </el-upload>
               <ul class="uploadImgList">
                 <li v-for="(item,index) in form.customerCompanyInfoForm.companyImages" :key="index">
                   <img :src="imgRequest + item + '?imageView2/1/w/200/h/200'">
                   <span class="del" @click="handelImgRemove(index)" v-if="!limit.isRead"><i class="el-icon-delete-solid"></i></span>
                 </li>
               </ul>
            </div>
        </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancel">取 消</el-button>
      <el-button type="primary" @click="submit" v-if="!limit.isRead">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getToken } from "@/utils/auth";
import { deepClone } from '@/utils/index'
import { addCompany , updateCompany , companyType } from "@/api/customerManage/company";
export default {
  name: 'addMobiel',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: 'title'
    },
    action: {
      type: String,
      default: 'add'
    },
    selectRow: {
      type: Object,
      default:null
    }
  },
  watch: {
    visible(newVal, oldVal) {
      this.modalVisible = newVal;
    },
    title(newVal, oldVal) {
      this.modalTitle = newVal;
    },
    action(newVal, oldVal) {
      this.modalAction = newVal;
    },
    selectRow(newVal, oldVal) {
      this.modalSelectRow = newVal;
    }
  },
  data() {
    return {
      modalTitle: this.title,
      modalVisible: this.visible,
      modalAction: this.action,
      modalSelectRow: this.selectRow,
      formLabelWidth: '1024px',
      //窗口权限
      limit: {
        isRead: false, //是否只读
        isAdd: false //是否可写
      },
      upload: {
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/upload/fileImg",
      },
      companyTypeOptions: [],
      fileList:[],
      //验证
      rules: {
        typeName: [{ required: true, message: '类别名称不能为空!', trigger: 'blur' }]
      },
      //表单
      form: {
        customerCompanyInfoForm:{
          companyImages:[]
        },
        customerInfoForm:{
          companyType:''
        }
      }
    };
  },
  created() {
    //客户状态字典
    this.getCompanyType();
    switch (this.modalAction) {
      case 'add':
        this.limit.isAdd = true;
        break;
      case 'edit':
        this.limit.isAdd = true;
        this.initDatas(this.modalSelectRow)
        break;
      case 'view':
        this.limit.isRead = true;
        this.initDatas(this.modalSelectRow)
        break;
      default:
        break;
    }
  },
  methods: {
    /** 企业类型列表 */
    getCompanyType(){
      companyType().then(res => {
          if(res.retHead.errCode == 0){
            this.companyTypeOptions = res.retBody
          }
        }
      );
    },
    //数据初始化
    initDatas(datas){
      this.form = {
        customerCompanyInfoForm:{
          companyAddress: datas.companyAddress,
          companyImages: datas.companyImages.split(';'),
          companyName: datas.companyName,
          email: datas.email,
          guid:datas.guid,
          orgCode: datas.orgCode,
          phone: datas.phone,
          principals: datas.principals,
        },
        customerInfoForm:{
          companyType:datas.companyType
        }
      }
    },
    //表单提交
    submit() {
      this.$refs.from.validate(valid => {
        if (valid) {
          let params = deepClone(this.form)
          params.customerCompanyInfoForm.companyImages = params.customerCompanyInfoForm.companyImages.join(';')
          console.log(params)
          if(this.modalAction === 'add'){
            addCompany(params).then(res => {
                if (res.retHead.errCode === 0) {
                  this.msgSuccess("操作成功");
                  this.modalVisible = false
                 	this.$emit('closeModal', true);
                }
            });
          }else{
            updateCompany(params).then(res => {
                if (res.retHead.errCode === 0) {
                  this.msgSuccess("操作成功");
                  this.modalVisible = false
                 	this.$emit('closeModal', true);
                }
            });
          }
        } else {
          return false;
        }
      });
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
       this.upload.isUploading = true;
    },
    // 上传成功处理
    handleFileSuccess(response, file, fileList){
       this.fileList = []
       this.form.customerCompanyInfoForm.companyImages.push(response.retBody)
       this.upload.isUploading = false;
    },
    // 图片删除
    handelImgRemove(i){
      this.form.customerCompanyInfoForm.companyImages.splice(i,1)
    },
    //窗口关闭
    handleCancel() {
      this.$emit('closeModal', false);
    }
  }
};
</script>

<style lang="scss" scoped>
  .hc-box{
    border:1px solid #e9e9e9;
    margin-bottom:20px;
    .hc-box-header{
        height:40px;
        line-height:40px;
        background:#fafafa;
        font-weight:bold;
        color:#333;
        padding:0 15px;
        .action{
          margin-left:10px;
          color:#304FFE
        }
    }
  }
  .uploadImg{
    padding:0 30px 30px 30px;
    .uploadImgList{
      list-style:none;
      margin:0;
      padding:15px 0;
      display:flex;
      flex-flow: row;
      li{
        position:relative;
        border:1px solid #ddd;
        border-radius:6px;
        width:120px;
        height:120px;
        margin-right:13px;
        img{
          width:100%;
          height:100%;
          border-radius:6px;
        }
        .del{
          display:none;
          position:absolute;
          top: -9px;
          right: -9px;
          cursor:pointer;
          i{
            font-size:20px;
            color:red;
          }
        }
        &:hover{
          .del{
            display:block;
          }
        }
      }
    }
  }
</style>
