<!--
 * @Description: 新增/编辑弹框
 * @Author: Sugar.
 * @Date: 2023-11-24 13:55:00
 * @LastEditors: Sugar.
 * @LastEditTime: 22023-11-24 13:55:00
 * @FilePath: \cattle_webui\src\views\team\teamMr\AddOrEditDialog.vue
 * @Copyright: Copyright (c) 2016~2023 by Sugar., All Rights Reserved.
-->
<template>
  <el-dialog
    :title="title"
    :visible.sync="open"
    width="60%"
    append-to-body
    :close-on-click-modal="false"
    @close="cancel"
  >
    <div class="dialog">
      <el-form :model="form" ref="form" :rules="rules" label-width="150px">
        <div style="display: flex;">
          <el-form-item label="客户类型:" prop="bigCustomer">
            <span style="display: flex ; min-width: 300px;">{{ form.bigCustomer == 1 ? '普通客户' : form.bigCustomer == 2 ? '大客户' : '' }}</span>
          </el-form-item>
        </div>
        <div style="display: flex;">
          <el-form-item label="团队名称:" prop="name">
            <span style="display: flex ; min-width: 300px;">{{ form.name }}</span>
          </el-form-item>
          <el-form-item label="团队类型:" prop="type">
            <dict-tag :options="dict.type.team_type" :value="form.type"/>
          </el-form-item>
        </div>
        <div style="display: flex;" v-if="form.type == 6">
          <el-form-item label="OTA来源:" prop="source">
            <dict-tag :options="dict.type.order_form_type" :value="form.source"/>
        </el-form-item>
        </div>
        <div style="display: flex;">
          <el-form-item label="销售员:" prop="salerPerson">
            <span style="display: flex ; min-width: 300px;">{{ form.salerPerson }}</span>
          </el-form-item>
          <el-form-item label="销售员联系电话:" prop="salerMobile">
            <span style="display: flex ; min-width: 300px;">{{ form.salerMobile }}</span>
          </el-form-item>
        </div>
        <div style="display: flex;">
          <el-form-item label="客户负责人:" prop="contact">
            <span style="display: flex ; min-width: 300px;">{{ form.contact }}</span>
          </el-form-item>
          <el-form-item label="客户账号/联系电话:" prop="mobile">
            <span style="display: flex ; min-width: 300px;">{{ form.mobile }}</span>
          </el-form-item>
        </div>
        <el-form-item label="是否有合同:">
          <span>{{ form.haveContract==0?'无':'有' }}</span>
        </el-form-item>
        <!-- <el-form-item label="密码">
          <span>{{ form.password }}</span>
        </el-form-item> -->
        <el-form-item label="无合约原因:" >
          <span>{{ form.contractReason }}</span>
        </el-form-item>
        <el-form-item v-if="form.haveContract==1" label="合约信息:">
          <div style="display: flex;">
            <div 
              v-for="(item,index) in form.photoList" 
                :key="index"
                style="width: 100px; height: 100px;position: relative;border: 1px solid #999;border-radius: 5px;margin-right: 20px;">
                  <el-image 
                    style="width: 100%; height: 100%"
                    :src="item" 
                    :preview-src-list="form.photoList">
                  </el-image>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="审核备注:">
          <span style="color: #ccc;">(注意：驳回时请填写备注)</span>
        </el-form-item>
        <el-form-item label="" :error="errorText" prop="checkRemark">
          <span v-if="model != 'Audit'">{{ form.checkRemark}}</span>
          <el-input
            v-if="model == 'Audit'"
            v-model="form.checkRemark"
            type="textarea"
            placeholder="审核备注:"
            clearable
            style="width: 100%;"
            @input="checkRemarkFun"
          />
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer" v-if="model == 'Audit'">
      <div style="display: flex;justify-content: center;">
        <el-button
          type="danger"
          @click="submitForm(2)"
          v-loading.fullscreen.lock="loading"
          element-loading-text="提交中..."
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.8)"
        >
          <span v-if="loading">提交中...</span>
          <span v-else>驳回</span>
        </el-button>
        <el-button
          type="primary"
          @click="submitForm(1)"
          v-loading.fullscreen.lock="loading"
          element-loading-text="提交中..."
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.8)"
        >
          <span v-if="loading">提交中...</span>
          <span v-else>通过</span>
        </el-button>
      </div>
      
    </span>
  </el-dialog>
</template>

<script>
import { marketTeamAudit, getSelectById } from "@/api/team/teamMr";
import Editor from "@/components/Editor";
import { getToken } from "@/utils/auth";
export default {
  name: "addAndEdit",
  props: {
    dict: {
      type: Object,
      default: () => [],
    },
  },
  components: {
    Editor,
  },
  data() {
    return {
      title: "编辑",
      model: "EDIT",
      activeName: '01',
      open: false,
      loading: false,
      form: {
        id: undefined,
        type: "",
      },
      
      rules: {
        name: [{ required: false, message: "请输入团队名称", trigger: ["change","blur"] }],
        type: [{ required: false, message: "请选择团队类型", trigger: ["change","blur"] }],
        contact: [{ required: false, message: "请输入客户负责人", trigger: ["change","blur"] }],
        mobile: [{ required: false, message: "请输入客户账号/联系电话", trigger: ["change","blur"] }],
        salerPerson: [{ required: false, message: "请输入销售员", trigger: ["change","blur"] }],
        salerMobile: [{ required: false, message: "请输入销售员联系电话", trigger: ["change","blur"] }],
      },
      uploadObj: {
        url: process.env.VUE_APP_UPLOAD_FILE_API + "/upload/single/minio",
        Headers: { Authorization: "Bearer " + getToken() },
      },
      errorText: ''
    };
  },
  methods: {
    /**
     * 打开弹框
     * @date 2023-11-22
     * @param {any} obj
     * @returns {any}
     */
    openDialog(title, obj,type) {
      this.open = true;
      this.errorText = ''
      this.model = type
      this.title = title;
      this.reset();
      if (obj){
        this.title = title;
        this.getSelectByIdApi(obj);
      }else{
        this.title = title;
        this.$nextTick(() => {
          this.$refs["form"].clearValidate();
        });
      }
    },
    /** 获取详情 */
    getSelectByIdApi(row) {
      const id = row.id
      getSelectById(id).then(response => {
        const obj = response.data;
        this.$nextTick(() => {
          this.$nextTick(() => {
            this.form = {
              ...obj,
              photoList: obj.contractImg?obj.contractImg.split(','):[]
            }
          });
        });
      });
    },
    /**
     * 保存
     * @date 2023-11-22
     * @returns {any}
     */
    async submitForm(type) {
      try {
            if(type==2 && !this.form.checkRemark){
              this.errorText = '驳回请填写备注'
              return
            }else {
              this.errorText = ''
            }
            this.loading = true;
            const { code } = await marketTeamAudit({ 
              teamId: this.form.id,
              checkStatus: type,
              checkRemark: this.form.checkRemark,
            });
            if (code === 200) {
              this.$message.success("操作成功!");
              this.$emit("getList");
              this.cancel();
            }
          } catch (error) {
            console.error("errpr==",error)
          } finally {
            this.loading = false;
          }
    },
    /**
     * 重置
     * @date 2023-11-22
     * @returns {any}
     */
    reset() {
      this.form = {}
      if(this.$refs["form"]) {
        this.$refs["form"].clearValidate();
      }
    },
    /**
     * 关闭弹框
     * @date 2023-11-22
     * @returns {any}
     */
    cancel() {
      this.reset();
      this.open = false;
    },
    /**
     * 上传成功
     * @date 2023-11-22
     * @param {any} res
     * @returns {any}
     */
    handleAvatarSuccess(res) {
      if (res.code === 200) {
        // this.form.mainImg = res?.data?.url;
        this.$set(this.form, 'mainImg', res?.data?.url)
      }
    },
    /**
     * 上传文件之前之前
     * @date 2023-11-22
     * @param {any} file
     * @returns {any}
     */
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg" || "image/png";
      if (!isJPG) {
        this.$message.error("上传头像图片只能是jpg或png格式!");
      }
      return isJPG;
    },
    /**
     * 剧目海报上传成功
     * @date 2023-11-22
     * @param {any} res
     * @returns {any}
     */
    handlePhotoListSuccess(res) {
      if (res.code === 200) {
        let photo = {
          imageUrl: res?.data?.url,
          url: res?.data?.url,
          photoType: '2'
        }
        if(!this.form.photoList){
          this.form.photoList = []
        }
        // this.form.photoList.push(photo);
        this.$set(this.form.photoList, this.form.photoList.length, photo);
      }
    },
    handleRemove(file, fileList) {
      this.form.photoList.forEach((item, index) => {
        if(item.uid == file.uid){
          this.form.photoList.splice(index, 1)
        }
      })
    },
    checkRemarkFun(value) {
      if(value) {
        this.errorText = ''
      }
    }
  },
};
</script>

<style lang="scss" scoped>
.dialog {
  padding: 0 30px;
  max-height: 65vh;
  overflow-y: auto;
}
.dialog {
  padding: 0 30px;
  .upload-btn {
    width: 100px;
    height: 100px;
    background-color: #fbfdff;
    border: dashed 1px #c0ccda;
    border-radius: 5px;
    i {
      font-size: 30px;
      margin-top: 20px;
    }
    &-text {
      margin-top: -10px;
    }
  }
  .avatar {
    cursor: pointer;
  }
}
</style>
