<template>
  <el-dialog :title="title" :visible.sync="open" :width="dialogWidth"  custom-class="edit-dialog" @close="closeDialog">

    <el-form ref="form" :model="form" :rules="rules" label-position="top" v-loading="loading">
      <el-row :gutter="25">
        <el-col :span="12">
          <Select :label="$t('follow.followType')" prop="followType" :disabled="!contentEditable || !editAnbledFlad"
            :styleObj="{ width: '100%' }" @change="followTypeChange" :data="dict.type.crm_follow_type" required />
        </el-col>
        <el-col :span="12">
          <DatePicker type="date" :label="$t('follow.followTime')" prop="followTime" :disabled="!contentEditable"
            width="100%" format="yyyy-MM-dd HH:mm:ss" required />
        </el-col>
      </el-row>
      <el-row :gutter="25">
        <el-col :span="24">
          <!--          <Select :label="relatedLabel" prop="businessId" :disabled="!contentEditable || !editAnbledFlad"-->
          <!--                  filterable :remote="true" :remoteMethod="remoteMethod" @change="businessIdSelectChange"-->
          <!--                  :data="relatedList"-->
          <!--                  required showTip-->
          <!--                  :styleObj="{width: '100%'}"/>-->
          <!-- 关联客户 -->
          <PageSelect :label="relatedLabel" prop="businessId" @change="businessIdSelectChange"
            :disabled="!contentEditable || !editAnbledFlad" :list="getMethod" :pageSize="10" idName="value" clearable
            required :selfTop="1" :valueProp="valueProp" :labelProp="labelProp"
            :placeholder="$t('common.pleaseSelect') + relatedLabel" :defaultParams="{}" width="100%"
            ref="pageSelect" :formatLabel="formatLabel" :visibleGetData="true" class="account-select"
            :defaultData="defaultData" />
        </el-col>
      </el-row>
      <el-row :gutter="25">
        <el-col :span="12">
          <Select :label="$t('follow.contactPerson')" prop="contactPerson" :disabled="!contentEditable"
            :styleObj="{ width: '100%' }" @change="contactPersonSelectChange" :data="contactPersonList" clearable
            allow-create filterable required />
        </el-col>
        <el-col :span="12">
          <Input :label="$t('follow.contact')" required prop="contact" :disabled="!contentEditable" width="100%"
            :maxlength="50"></Input>
        </el-col>
      </el-row>
      <el-row :gutter="25">
        <el-col :span="12" v-if="!contentEditable">
          <Input :label="$t('follow.follower')" prop="follower" disabled required width="100%" />
        </el-col>
        <el-col :span="12">
          <Select :label="$t('follow.followWay')" prop="followWay" :disabled="!contentEditable"
            :data="dict.type.crm_follow_way" required />
        </el-col>
      </el-row>
      <el-row :gutter="25">
        <el-col :span="24">
          <Input type="textarea" :label="$t('follow.followDesc')" required prop="followDesc"
            :disabled="!contentEditable" :rows="3" width="100%" :maxlength="500"></Input>
        </el-col>
      </el-row>
      <el-row :gutter="25">
        <el-col :span="24">
          <el-form-item :label="$t('follow.followAttach')" label-width="20%">
            <!--<ImageUpload v-model="form.annex" :disabled="false" :isShowTip="true"></ImageUpload>-->

            <FileUpload v-model="form.annex" :isShowTip="false" :fileType="[]" :disabled="!contentEditable"
              :buttonText="$t('common.uploadFile')">
            </FileUpload>
            <!--<FileUpload v-model="form.annex" :isShowTip="false" disabled :buttonText="$t('common.uploadFile')"/>-->
          </el-form-item>
          <!--<el-form-item :label="$t('follow.followAttach')" label-width="20%" v-if="!contentEditable">-->
          <!--  <ImageUpload v-if="limitPic > 0" v-model="form.annex" :disabled="true" :isShowTip="false" :limit="limitPic"></ImageUpload>-->
          <!--  <el-empty v-else :description="$t('lead.noAnnexYet')" style="padding: 0;"></el-empty>-->
          <!--</el-form-item>-->
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" v-if="contentEditable">
      <el-button type="primary" @click="submitDataScope('form')" icon="el-icon-circle-check">{{ $t('common.confirm')
        }}</el-button>
      <el-button @click="cancel" icon="el-icon-remove-outline">{{ $t('common.cancel') }}</el-button>
    </div>
    <div slot="footer" class="dialog-footer" v-if="!contentEditable">
      <el-button @click="cancel" icon="el-icon-remove-outline">{{ $t('common.close') }}</el-button>
    </div>
  </el-dialog>

</template>

<script>
import { addFollow, getFollow, updateFollow } from "../../../api/business/crm/follow.js";
import Form from "../../../components/element/form/Form";
import ImageUpload from "../../../components/ImageUpload";
import ImagePreview from "../../../components/ImagePreview";
import Column from "../../../components/element/table/Column";
import { getRelatedContact } from "../../../api/business/crm/contact";
import { getRelatedLead, getRelatedLeadByLeadId, getRelatedLeadByQuery, getRelatedLeadByQueryPage } from "../../../api/business/crm/lead";
import { getRelatedAccount, getRelatedAccountByAccountId, getRelatedAccountByQuery, getRelatedAccountByQueryPage } from "../../../api/business/crm/account";
import { getRelatedProject, getRelatedProjectByProjectId, getRelatedProjectByQuery, getRelatedProjectByQueryPage } from "../../../api/business/crm/project";
import { parseTime } from "../../../utils/ruoyi";
import FileUpload from "@/components/FileUpload";
import PageSelect from '../../../components/element/form/items/PageSelect'

export default {
  components: { PageSelect, Form, Column, ImageUpload, ImagePreview, FileUpload },
  props: {
    dialogWidth: {
      type: String,
      default: '50%'
    },
    labelWidth: {
      type: String,
      default: '40%'
    },
    followId: {
      type: String,
      default: ''
    }
  },
  dicts: ['crm_follow_type', 'crm_follow_way'],
  data() {
    return {
      // 新增编辑表单参数
      form: {
        followType: "0",
        followPerson: "",
        followWay: "01",
        followTime: new Date(),
        followDesc: "",
        followImageSrc: "",
        contactPerson: '',
        contactPersonName: '',
        contact: "",
        telephone: "",
        followAttach: "",
        businessId: ''
      },
      // 编辑可能区分
      contentEditable: true,
      // 关联线索List
      relatedLeadList: [],
      // 关联客户List
      relatedAccountList: [],
      // 关联项目List
      relatedProjectList: [],
      // 关联List
      relatedList: [],
      // 关联Label
      relatedLabel: '',
      // 联系人
      contactPersonList: [],
      // 跟进方式和关联线索/客户/项目是否可以编辑
      editAnbledFlad: true,
      // 表单校验规则
      rules: {
        followTime: [
          { required: true, message: this.$t('common.pleaseSelect'), trigger: 'change' }
        ],
        businessId: [
          { required: true, message: this.$t('common.pleaseSelect'), trigger: 'change' }
        ],
        contactPerson: [
          { required: true, message: this.$t('common.pleaseSelect'), trigger: 'change' }
        ],
        contact: [
          { required: true, message: this.$t('common.pleaseInput'), trigger: 'change' }
        ],
        followWay: [
          { required: true, message: this.$t('common.pleaseSelect'), trigger: 'change' }
        ],
        followDesc: [
          { required: true, message: this.$t('common.pleaseInput'), trigger: 'blur' }
        ],
      },
      // 返回值
      returnRow: {
        returnCode: "",
      },
      open: false,
      title: "",
      limitPic: 0,
      addEditFlag: '',
      loading: false,
      defaultData: [],
      labelProp: "",
      valueProp: ""
    };
  },
  computed: {
  },

  created() {

  },
  mounted() {

  },
  para: {
    flag: null,
    followId: null,
    leadId: null,
    accountId: null,
    projectId: null
  },
  watch: {},
  methods: {
    /** 弹出窗口初期化  */
    async dialogItemInit(para) {
      this.loading = true;
      this.addEditFlag = para.flag;
      this.relatedList = [];
      this.contactPersonList = [];
      this.relatedLeadList = [];
      this.relatedAccountList = [];
      this.relatedProjectList = [];
      console.log("this.relatedList   start:", this.relatedList);

      if (para.leadId) {
        await this.getRelatedLeadListByLeadId(para.leadId);
      }
      if (para.accountId) {
        await this.getRelatedAccountListByAccountId(para.accountId);
      }
      if (para.projectId) {
        await this.getRelatedProjectListByProjectId(para.projectId);
      }

      if (this.addEditFlag == '1') {
        this.reset();
        this.contentEditable = true;
        this.editAnbledFlad = true;
        this.title = this.$t('follow.addFollowRecord');
        this.relatedList = this.relatedLeadList;

        this.relatedLabel = this.$t('follow.relatedLead');
        this.labelProp = "leadNo"
        this.valueProp = "leadId"
        if (para.leadId) {
          this.form.followType = '0';
          this.form.businessId = para.leadId;
          let selectItem = {
            value: this.form.businessId
          }
          this.businessIdSelectChange(selectItem);
          this.relatedList = this.relatedLeadList;
          this.relatedLabel = this.$t('follow.relatedLead');
          this.editAnbledFlad = false;
        }
        if (para.accountId) {
          this.labelProp = "accountNo"
          this.valueProp = "accountId"
          this.form.followType = '1';
          this.form.businessId = para.accountId;
          let selectItem = {
            value: this.form.businessId
          }
          this.businessIdSelectChange(selectItem);
          this.relatedList = this.relatedAccountList;
          this.relatedLabel = this.$t('follow.relatedAccount');
          this.editAnbledFlad = false;
        }
        if (para.projectId) {
          this.labelProp = "projectNo"
          this.valueProp = "projectId"
          this.form.followType = '2';
          this.form.businessId = para.projectId;
          let selectItem = {
            value: this.form.businessId
          }
          this.businessIdSelectChange(selectItem);
          this.relatedList = this.relatedProjectList;
          this.relatedLabel = this.$t('follow.relatedProject');
          this.editAnbledFlad = false;
        }
        this.open = true;
        console.log(this.form)
      }
      if (this.addEditFlag == '2') {
        this.reset();
        this.contentEditable = true;
        this.editAnbledFlad = false;
        this.title = this.$t('follow.editFollowRecord');
        this.getFollowById(para.followId);
      }
      if (this.addEditFlag == '3') {
        this.reset();
        this.contentEditable = false;
        this.editAnbledFlad = false;
        this.title = this.$t('follow.viewFollowRecord');
        this.getFollowById(para.followId);
        this.open = true;
      }
      setTimeout(() => {
        this.loading = false;

      }, 500);

      console.log("this.relatedList   end:", this.relatedList);
    },
    /** 获取所有相关的线索信息 */
    async getRelatedLeadList() {
      let relatedList = [];
      const response = await getRelatedLead();
      response.rows.forEach(item => {
        relatedList.push(
          {
            value: item.leadId,
            label: item.leadNo + ":[" + item.areaName + "] " + "[" + item.contactPerson + "] ",
            contactPerson: item.contactPerson,
            contact: item.contact
          }
        )
      })
      this.relatedLeadList = relatedList;
    },
    /** 获取所有相关的线索信息 */
    async getRelatedLeadListByLeadId(leadId) {
      let relatedList = [];
      let param = {
        leadId: leadId,
        language: this.$i18n.locale,
      }
      const response = await getRelatedLeadByLeadId(param);
      this.defaultData = response.rows
      console.log(this.defaultData)

      setTimeout(it=>{
        this.$refs.pageSelect.data = response.rows
        this.$refs.pageSelect.resSet(this.defaultData)
      },0)

      response.rows.forEach(item => {
        relatedList.push(
          {
            value: item.leadId,
            label: item.leadNo + ":[" + item.areaName + "] " + "[" + item.contactPerson + "] ",
            contactPerson: item.contactPerson,
            contact: item.contact
          }
        )
      })
      this.relatedLeadList = relatedList;
    },
    /** 获取所有相关的线索信息 */
    async getRelatedLeadListByQuery(query) {
      let relatedList = [];
      let param = {
        query: query,
        language: this.$i18n.locale,
      }
      const response = await getRelatedLeadByQuery(param);
      this.defaultData = response.rows
      // response.rows.forEach(item =>{
      //   relatedList.push(
      //     {
      //       value:item.leadId,
      //       label:item.leadNo + ":[" + item.areaName + "] " + "[" + item.contactPerson + "] ",
      //       contactPerson: item.contactPerson,
      //       contact: item.contact
      //     }
      //   )
      // })
      // this.relatedLeadList = relatedList;
    },


    /** 获取所有相关的线索信息 */
    async getRelatedAccountList() {
      let relatedList = [];
      await getRelatedAccount().then(response => {
        response.rows.forEach(item => {
          relatedList.push(
            {
              value: item.accountId,
              label: item.accountNo + ":[" + item.accountName + "] " + "[" + item.areaName + "] " + "[" + item.countryName + "] ",
            }
          )
        })
        this.relatedAccountList = relatedList
      });
    },
    /** 获取所有相关的客户信息 */
    async getRelatedAccountListByQuery(query) {
      let relatedList = [];
      let param = {
        query: query,
        language: this.$i18n.locale,
      }
      const response = await getRelatedAccountByQuery(param);
      response.rows.forEach(item => {
        relatedList.push(
          {
            value: item.accountId,
            label: item.accountNo + ":[" + item.accountName + "] " + "[" + item.areaName + "] " + "[" + item.countryName + "] ",
          }
        )
      })
      this.relatedAccountList = relatedList
    },
    /** 获取所有相关的客户信息 */
    async getRelatedAccountListByAccountId(accountId) {
      let relatedList = [];
      let param = {
        accountId: accountId,
        language: this.$i18n.locale,
      }
      const response = await getRelatedAccountByAccountId(param);
      this.defaultData = response.rows

      // setTimeout(it => {
      //   this.$refs.pageSelect.data = response.rows
      //   this.$refs.pageSelect.resSet(this.defaultData)
      // }, 50)

      response.rows.forEach(item => {
        relatedList.push(
          {
            value: item.accountId,
            label: item.accountNo + ":[" + item.accountName + "] " + "[" + item.areaName + "] " + "[" + item.countryName + "] ",
          }
        )
      })
      this.relatedAccountList = relatedList
    },
    /** 获取所有相关的项目信息 */
    async getRelatedProjectList() {
      let relatedList = [];
      await getRelatedProject().then(response => {
        response.rows.forEach(item => {
          relatedList.push(
            {
              value: item.projectId,
              label: item.projectNo + ":[" + item.projectName + "] " + "[" + item.countryName + "] " + "[" + item.city + "] ",
              accountId: item.accountId,
              contactPerson: item.contactPerson,
              contact: item.contact
            }
          )
        })
        this.relatedProjectList = relatedList
      });
    },
    /** 获取所有相关的项目信息 */
    async getRelatedProjectListByProjectId(projectId) {
      let relatedList = [];
      let param = {
        projectId: projectId,
        language: this.$i18n.locale,
      }
      const response = await getRelatedProjectByProjectId(param);
      this.defaultData = response.rows
      // setTimeout(it => {
      //   this.$refs.pageSelect.data = response.rows
      //   this.$refs.pageSelect.resSet(this.defaultData)
      // }, 50)
      response.rows.forEach(item => {
        relatedList.push(
          {
            value: item.projectId,
            label: item.projectNo + ":[" + item.projectName + "] " + "[" + item.countryName + "] " + "[" + item.city + "] ",
            accountId: item.accountId,
            contactPerson: item.contactPerson,
            contact: item.contact
          }
        )
      })
      this.relatedProjectList = relatedList
    },
    /** 获取所有相关的项目信息 */
    async getRelatedProjectListByQuery(query) {
      let relatedList = [];
      let param = {
        query: query,
        language: this.$i18n.locale,
      }
      const response = await getRelatedProjectByQuery(param);
      response.rows.forEach(item => {
        relatedList.push(
          {
            value: item.projectId,
            label: item.projectNo + ":[" + item.projectName + "] " + "[" + item.countryName + "] " + "[" + item.city + "] ",
            accountId: item.accountId,
            contactPerson: item.contactPerson,
            contact: item.contact
          }
        )
      })
      this.relatedProjectList = relatedList
    },

    // 跟进类别变化
    followTypeChange(selectFollowType) {
      this.defaultData = []
      this.$refs.pageSelect.data = []
      this.form.businessId = '';
      this.form.contactPerson = '';
      this.form.contact = "";
      this.contactPersonList = [];
      this.relatedList = []
      // let followType = this.form.followType;
      if (selectFollowType.value == '0') {
        this.relatedLabel = this.$t('follow.relatedLead');
        this.labelProp = "leadNo"
        this.valueProp = "leadId"
      }

      if (selectFollowType.value == '1') {
        this.relatedLabel = this.$t('follow.relatedAccount');
        this.labelProp = "accountNo"
        this.valueProp = "accountId"
      }

      if (selectFollowType.value == '2') {
        this.relatedLabel = this.$t('follow.relatedProject');
        this.labelProp = "projectNo"
        this.valueProp = "projectId"
      }
    },

    // 关联线索、客户、项目：编辑或者查看时的初期设定
    async businessIdSelectInit(value) {
      let followType = this.form.followType;
      let personList = [];
      // 关联线索的场合
      if (followType == '0') {
        for (let i = 0; i < this.relatedLeadList.length; i++) {
          let item = this.relatedLeadList[i];
          if (value == item.value) {
            personList.push(
              {
                value: "1",
                label: item.contactPerson,
                telephone: item.contact
              }
            );
            break;
          }
        }
        this.contactPersonList = personList;
      }

      // 关联客户的场合
      if (followType == '1') {
        let paramForContact = {
          accountId: value
        }
        await getRelatedContact(paramForContact).then(response => {
          response.rows.forEach(item => {
            personList.push(
              {
                value: item.contactId + "",
                label: item.contactName,
                telephone: item.phone
              }
            )
          })

          this.contactPersonList = personList;
        });
      }

      // 关联项目的场合
      if (followType == '2') {
        let accountId = '';
        let contactPerson = "";
        let contact = "";

        let index = 1;
        for (let i = 0; i < this.relatedProjectList.length; i++) {
          let item = this.relatedProjectList[i];
          if (value == item.value) {
            accountId = item.accountId;
            contactPerson = item.contactPerson;
            contact = item.contact;
            break;
          }
        }

        // 项目中的联系人、联系方式都有值的场合
        if (contactPerson && contact) {
          personList.push(
            {
              value: index + "",
              label: contactPerson,
              telephone: contact
            }
          )
          index++;
        }

        if (accountId) {
          let paramForContact = {
            accountId: accountId
          }
          getRelatedContact(paramForContact).then(response => {
            response.rows.forEach(item => {
              if (item.contactName == contactPerson && item.phone == contact) {
              } else {
                personList.push(
                  {
                    value: index + "",
                    label: item.contactName,
                    telephone: item.phone
                  }
                )
                index++;
              }
            })
          })
        }

        this.contactPersonList = personList;
      }
    },
    // 关联线索、客户、项目发生变化
    async businessIdSelectChange(selectItem) {
      let followType = this.form.followType;
      let personList = [];
      this.form.contactPerson = '';
      this.form.contact = "";
      console.log(selectItem)
      // 关联线索的场合
      if (followType == '0') {
        for (let i = 0; i < this.relatedLeadList.length; i++) {
          let item = this.relatedLeadList[i];
          if (selectItem.value == item.value) {
            personList.push(
              {
                value: "1",
                label: item.contactPerson,
                telephone: item.contact
              }
            );
            break;
          }
        }
        this.contactPersonList = personList;
        if (this.contactPersonList.length > 0) {
          this.form.contactPerson = this.contactPersonList[0].value;
          this.form.contact = this.contactPersonList[0].telephone;
        }
      }

      // 关联客户的场合
      if (followType == '1') {
        let paramForContact = {
          accountId: selectItem.value||selectItem[this.valueProp]
        }
        await getRelatedContact(paramForContact).then(response => {
          response.rows.forEach(item => {
            personList.push(
              {
                value: item.contactId + "",
                label: item.contactName,
                telephone: item.phone
              }
            )
          })

          this.contactPersonList = personList;
          if (this.contactPersonList.length > 0) {
            this.form.contactPerson = this.contactPersonList[0].value;
            this.form.contact = this.contactPersonList[0].telephone;
          }
        });
      }

      // 关联项目的场合
      if (followType == '2') {
        let accountId = '';
        let contactPerson = "";
        let contact = "";
        selectItem.value = selectItem.value||selectItem[this.valueProp]
        let index = 1;
        for (let i = 0; i < this.relatedProjectList.length; i++) {
          let item = this.relatedProjectList[i];
          if (selectItem.value == item.value) {
            accountId = item.accountId;
            contactPerson = item.contactPerson;
            contact = item.contact;
            break;
          }
        }

        // 项目中的联系人、联系方式都有值的场合
        if (contactPerson && contact) {
          personList.push(
            {
              value: index + "",
              label: contactPerson,
              telephone: contact
            }
          )
          index++;
        }

        if (accountId) {
          let paramForContact = {
            accountId: accountId
          }
          getRelatedContact(paramForContact).then(response => {
            response.rows.forEach(item => {
              if (item.contactName == contactPerson && item.phone == contact) {
              } else {
                personList.push(
                  {
                    value: index + "",
                    label: item.contactName,
                    telephone: item.phone
                  }
                )
                index++;
              }
            })
          })
        }

        this.contactPersonList = personList;
        if (this.contactPersonList.length > 0) {
          this.form.contactPerson = this.contactPersonList[0].value;
          this.form.contact = this.contactPersonList[0].telephone;
        }
      }

      // resetFields
      // this.$refs.form.clearValidate('businessId');
      // this.$refs.form.clearValidate('contactPerson');
      // this.$refs.form.clearValidate('contact');

      console.log(this.form)
    },
    // 联系人发生变化
    contactPersonSelectChange(selectPerson) {
      this.contactPersonList.forEach(item => {
        if (selectPerson && selectPerson.value == item.value) {
          this.form.contact = item.telephone;
        }
      });

    },
    /** 提交按钮（数据权限） */
    submitDataScope(form) {
      // 获取联系人名称
      for (let i = 0; i < this.contactPersonList.length; i++) {
        if (this.form.contactPerson == this.contactPersonList[i].value) {
          this.form.contactPersonName = this.contactPersonList[i].label;
        }
      }

      if (!this.form.contactPersonName) {
        this.form.contactPersonName = this.form.contactPerson;
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.addEditFlag == '1') {
            this.form.followId = undefined;
            addFollow(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('common.addSuccess'));
              this.openDataScope = false;
              this.open = false;
              this.returnRow.returnCode = "OK";
              this.$emit('returnRow', this.returnRow);

              this.loading = false;
            });
          }
          if (this.addEditFlag == '2') {
            updateFollow(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('common.editSuccess'));
              this.openDataScope = false;
              this.open = false;
              this.returnRow.returnCode = "OK";
              this.$emit('returnRow', this.returnRow);

              this.loading = false;
            });
          }

          if (this.form.followType == '2') {
            // 触发刷新
            this.$store.commit('refreshProject');
          }
        } else {
          return false;
        }
      });
    },
    // 取消按钮
    cancel() {
      this.reset();
      this.addEditFlag = undefined
      this.open = false;
      this.$emit("close")
    },
    // 表单重置
    reset() {
      this.form = {
        followType: "0",
        followPerson: "",
        followWay: "01",
        followTime: new Date(),
        followDesc: "",
        followImageSrc: "",
        contactPerson: '',
        contactPersonName: '',
        contact: "",
        telephone: "",
        followAttach: "",
        businessId: ""
      }
      this.resetForm("form");
    },
    // 根据跟进记录ID查询跟进信息
    getFollowById(followId) {
      getFollow(followId).then(response => {
        this.form = response.data;
        let businessId;

        this.limitPic = response.data?.tbFiles?.length || 0
        businessId = response.data.businessId;
        if (this.addEditFlag == '2' && response.data.delFlag == '1') {
          this.$message.error(this.$t('follow.deleted'));
          this.open = false;
          return false;
        }
        this.open = true;

        if (this.addEditFlag == '2' || this.addEditFlag == '3') {
          if (response.data.followType == '0') {
            this.getRelatedLeadListByLeadId(businessId).then(temp => {
              this.relatedList = this.relatedLeadList;
              this.relatedLabel = this.$t('follow.relatedLead');
              this.businessIdSelectInit(businessId);
              this.labelProp = "leadNo"
              this.valueProp = "leadId"
            });
          }
          if (response.data.followType == '1') {
            this.getRelatedAccountListByAccountId(businessId).then(temp => {
              this.relatedList = this.relatedAccountList;
              this.relatedLabel = this.$t('follow.relatedAccount');
              this.businessIdSelectInit(businessId);
              this.labelProp = "accountNo"
              this.valueProp = "accountId"
            });
          }
          if (response.data.followType == '2') {
            this.getRelatedProjectListByProjectId(businessId).then(temp => {
              this.relatedList = this.relatedProjectList;
              this.relatedLabel = this.$t('follow.relatedProject');
              this.businessIdSelectInit(businessId);
              this.labelProp = "projectNo"
              this.valueProp = "projectId"
            });
          }
        }
      });
    },
    // 关联线索/客户/项目的远程检索
    remoteMethod(query) {
      this.options = [];
      if (query !== '' && query.length > 2) {
        if (this.form.followType == '0') {
          this.getRelatedLeadListByQuery(query).then(item => {
            this.relatedList = this.relatedLeadList;
            this.relatedLabel = this.$t('follow.relatedLead');
          });

        }
        if (this.form.followType == '1') {
          this.getRelatedAccountListByQuery(query).then(item => {
            this.relatedList = this.relatedAccountList;
            this.relatedLabel = this.$t('follow.relatedAccount');
          });

        }
        if (this.form.followType == '2') {
          this.getRelatedProjectListByQuery(query).then(item => {
            this.relatedList = this.relatedProjectList;
            this.relatedLabel = this.$t('follow.relatedProject');
          });
        }
      }
    },
    //  methods end!

    getMethod(query) {
      let param = {
        query: this.form.businessId,
        language: this.$i18n.locale,
        pageSize: query.pageSize,
        pageNum: query.pageNum
      }
      if (this.form.followType == '0') {
        this.relatedLabel = this.$t('follow.relatedLead');
        return new Promise(function (resolve, reject) {
          getRelatedLeadByQueryPage(param).then(res => {
            resolve(res)
          }).catch(error => {
            reject()
          })
        });
      }
      if (this.form.followType == '1') {
        this.relatedLabel = this.$t('follow.relatedAccount');
        return new Promise(function (resolve, reject) {
          getRelatedAccountByQueryPage(param).then(res => {
            resolve(res)
          }).catch(error => {
            reject()
          })
        });
      }
      if (this.form.followType == '2') {
        this.relatedLabel = this.$t('follow.relatedProject');
        return new Promise(function (resolve, reject) {
          getRelatedProjectByQueryPage(query).then(res => {
            resolve(res)
          }).catch(error => {
            reject()
          })
        });
      }
    },
    formatLabel(leadNo, item) {
      if (this.form.followType == '0') {
        return item.leadNo + ":[" + item.areaName + "] " + "[" + item.contactPerson + "] "
      }
      if (this.form.followType == '1') {
        return item.accountNo + ":[" + item.accountName + "] " + "[" + item.areaName + "] " + "[" + item.countryName + "] ";
      }
      if (this.form.followType == '2') {
        return item.projectNo + ":[" + item.projectName + "] " + "[" + item.countryName + "] " + "[" + item.city + "] ";
      }

    },
    closeDialog(){
      this.defaultData = []
      this.$refs.pageSelect.data = []
    }
  },

}
</script>

<style scoped lang="scss">
::v-deep .edit-dialog {
  .el-dialog__body {
    min-height: 50vh;
    max-height: 75vh;
    overflow: auto;
    padding: 30px 50px;
  }

  .el-dialog__footer {
    padding: 10px 50px 20px 50px;
  }

  .el-form-item--medium .el-form-item__content,
  .el-form-item--medium .el-form-item__label {
    line-height: 25px;
  }

  .el-form--label-top .el-form-item__label {
    padding: 0;
  }

  .el-form .el-row:last-child .el-form-item {
    margin-bottom: 0;
  }

  .vue-treeselect .vue-treeselect__control {
    padding: 0 10px;
  }
}
</style>
