<template>
  <ele-modal
    custom-class="ele-dialog-form long-dialog-form"
    :centered="true"
    v-if="visible"
    :visible.sync="visible"
    title="转派"
    width="30%"
    :close-on-click-modal="false"
    :append-to-body="true"
    @close="cancel"
    :maxable="true"
    :resizable="true"
  >
    <el-form
      label-width="100px"
      ref="form"
      :model="form"
      :rules="rules"
      style="margin-top: 30px"
    >
      <el-form-item label="选择负责人" prop="responsibleId">
        <el-select v-model="form.responsibleId" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="footer">
      <el-button type="primary" @click="save">确认</el-button>
      <el-button @click="cancel">返回</el-button>
    </div>
  </ele-modal>
</template>

<script>
import {
  getDetail,
  UpdateInformation
} from '@/api/saleManage/businessOpportunity';
import { contactDetail } from '@/api/saleManage/contact';
let formDef = {
  id: '',
  contactName: '',
  contactId: '',
  budget: null,
  expectedClosingDate: '',
  sourceCode: '',
  sourceName: '',
  stageCode: '',
  stageName: '',
  winRate: null,
  name: '',
  remark: '',
  responsibleName: '',
  responsibleId: '',
  source: '',
  status: 1,
  pricingWay: 1,
  expectedSigningAmount: '',
  periodOfValidity: '',
  files: []
};
export default {
  data() {
    return {
      visible: false,
      rules: {
        responsibleId: [{ required: true, message: '请选择', trigger: 'blur' }]
      },

      // 提交状态
      loading: false,
      form: {
        ...formDef
      },
      contactData: {},
      options:[]
    };
  },
  created() {
  },
  methods: {
    async open(id) {
      this.options = []
      this.visible = true;
      this.form = await getDetail(id);
      this.form.responsibleId=''
      this.contactData = await contactDetail(this.form.contactId);
      if (this.contactData.base.salesmanId) {
        this.options = [
          {
            label: this.contactData.base.salesmanName,
            value: this.contactData.base.salesmanId
          }
        ];
      }
    },

    getValidate() {
      return Promise.all([
        new Promise((resolve, reject) => {
          this.$refs.form.validate((valid) => {
            if (!valid) {
              this.activeName = 'base';
              reject(false);
            } else {
              resolve(true);
            }
          });
        })
      ]);
    },
    async save() {
      try {
        await this.getValidate();
        // 表单验证通过，执行保存操作
        this.loading = true;
        this.form.responsibleName = this.contactData.base.salesmanName;
        const commitData = {
          opportunity: this.form,
          competAnalysisList: this.form.competAnalysisList,
          partyList: this.form.partyList,
          productList: this.form.productList
        };
        UpdateInformation(commitData)
          .then((res) => {
            this.loading = false;
            this.$message.success('转派成功');
            this.$emit('success');
            this.cancel();
          })
          .catch((e) => {
            //this.loading = false;
          });
      } catch (error) {
        // 表单验证未通过，不执行保存操作
      }
    },
    cancel() {
      this.$nextTick(() => {
        this.form = { ...formDef };
        this.visible = false;
      });
    }
  }
};
</script>
