<template>
  <ele-modal
    custom-class="ele-dialog-form long-dialog-form"
    :centered="true"
    v-if="visible"
    :visible.sync="visible"
    :title="title"
    :close-on-click-modal="false"
    width="70%"
    @close="cancel"
  >
    <div class="switch">
      <div class="switch_left">
        <ul>
          <li
            v-for="item in tabOptions"
            :key="item.key"
            :class="{ active: activeComp == item.key }"
            @click="activeComp = item.key"
          >
            {{ item.name }}
          </li>
        </ul>
      </div>
    </div>
    <div v-show="activeComp === 'main'">
      <headerTitle title="基本信息" style="margin-top: 15px"></headerTitle>

      <el-form
        label-width="120px"
        ref="form"
        :model="form"
        :rules="rules"
       class="el-form-box"
      >
        <el-row>
          <el-col :span="8">
            <el-form-item label="客户名称：" prop="contactName">
              <el-input v-model="detailData.contactName" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="	商机名称：" prop="name">
              <el-input v-model="detailData.name" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="负责人名称：" prop="responsibleName">
              <el-input v-model="detailData.responsibleName" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="商机来源：" prop="sourceCode">
              <el-input v-model="detailData.sourceName" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="赢单率：" prop="winRate">
              <el-input v-model="detailData.winRate" disabled>
                <template slot="append">%</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="预算：" prop="budget">
              <el-input v-model="detailData.budget" disabled>
                <template slot="append">万元</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="预计结单日期：" prop="expectedClosingDate">
              <el-input v-model="detailData.expectedClosingDate" disabled>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="商机阶段：" prop="stageCode">
              <el-input v-model="detailData.stageName" disabled>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="附件：" prop="files">
              <div v-if="detailData.files && detailData.files?.length">
                <el-link
                  v-for="link in detailData.files"
                  :key="link.id"
                  type="primary"
                  :underline="false"
                  @click="downloadFile(link)"
                >
                  {{ link.name }}</el-link
                >
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="备注：" prop="remark">
              <el-input type="textarea" v-model="detailData.remark" disabled>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <headerTitle title="产品清单" style="margin-top: 15px"></headerTitle>
      <inventoryTabledetail
        ref="inventoryTabledetailRef"
        :isCustomerMark="false"
        :isDeliveryDeadline="false"
        :isGuaranteePeriod="false"
      ></inventoryTabledetail>

      <headerTitle title="竞品对手" style="margin-top: 15px"></headerTitle>

      <ele-pro-table
        ref="table"
        :needPage="false"
        :columns="competAnalysisListcolumns1"
        :datasource="detailData.competAnalysisList"
        row-key="id"
        :toolkit="[]"
      >
      </ele-pro-table>
      <headerTitle title="关键人信息" style="margin-top: 15px"></headerTitle>

      <ele-pro-table
        ref="table"
        :needPage="false"
        :columns="partyListcolumns"
        :datasource="detailData.partyList"
        row-key="id"
        :toolkit="[]"
      >
      </ele-pro-table>
    </div>
    <bpmDetail
      v-if="activeComp === 'bpm' && form.processInstanceId"
      :id="form.processInstanceId"
    ></bpmDetail>
    <div slot="footer" class="footer">
      <el-button @click="cancel">返回</el-button>
    </div>
  </ele-modal>
</template>

<script>
  import { getDetail } from '@/api/saleManage/businessOpportunity';
  import { getFile } from '@/api/system/file';
  import dictMixins from '@/mixins/dictMixins';
  import bpmDetail from '@/views/bpm/processInstance/detail.vue';
  import { copyObj } from '@/utils/util';
  import inventoryTabledetail from '@/views/contractManage/contractBook/components/inventoryTabledetail.vue';

  export default {
    mixins: [dictMixins],
    components: {
      bpmDetail,
      inventoryTabledetail
    },
    data() {
      return {
        activeComp: 'main',
        tabOptions: [
          { key: 'main', name: '商机详情' },
          { key: 'bpm', name: '流程详情' }
        ],
        visible: false,
        title: '详情',
        row: {},
        activeName: 'base',
        form: {},
        rules: {},
        detailData: {},
        competAnalysisListcolumns1: [
          {
            prop: 'name',
            label: '竞争对手名称',
            // sortable: 'custom',
            showOverflowTooltip: true,
            align: 'center',
            width: 300
          },
          {
            prop: 'analysis',
            label: '竞品分析',
            showOverflowTooltip: true,
            align: 'center'
          }
        ],
        partyListcolumns: [
          {
            prop: 'linkName',
            label: '关键人名称',
            // sortable: 'custom',
            showOverflowTooltip: true,
            align: 'center',
            minWidth: 110
          },
          {
            prop: 'linkMobilePhone',
            label: '联系方式',
            showOverflowTooltip: true,
            align: 'center',
            minWidth: 110
          },
          {
            prop: 'linkPost',
            label: '职位',
            showOverflowTooltip: true,
            align: 'center',
            minWidth: 110
          },
          {
            prop: 'attitudeName',
            label: '态度',
            showOverflowTooltip: true,
            align: 'center',
            minWidth: 110
          },
          {
            prop: 'influenceName',
            label: '影响力',
            showOverflowTooltip: true,
            align: 'center',
            minWidth: 110
          },
          {
            prop: 'goal',
            label: '项目目标/期望',
            showOverflowTooltip: true,
            align: 'center',
            minWidth: 110
          }
        ],
        competAnalysisListcolumns: [
          {
            width: 45,
            type: 'index',
            columnKey: 'index',
            align: 'center',
            fixed: 'left'
          },
          {
            width: 200,
            prop: 'productName',
            label: '名称',
            slot: 'productName',
            align: 'center',
          },
          {
            width: 120,
            prop: 'productCode',
            label: '编码',
            slot: 'productCode',
            align: 'center',
          },
          {
            width: 200,
            prop: 'productCategoryName',
            label: '类型',
            slot: 'productCategoryName',
            align: 'center',
          },
          {
            width: 160,
            prop: 'productBrand',
            label: '牌号',
            slot: 'productBrand',
            align: 'center',
          },
          {
            width: 120,
            prop: 'modelType',
            label: '型号',
            slot: 'modelType',
            align: 'center',
          },
          {
            width: 120,
            prop: 'specification',
            label: '规格',
            slot: 'specification',
            align: 'center',
          },

          {
            width: 160,
            prop: 'singlePrice',
            label: '单价',
            slot: 'singlePrice',
            align: 'center',
          },
          {
            width: 120,
            prop: 'totalCount',
            label: '数量',
            slot: 'totalCount',
            align: 'center',
          },
          {
            width: 120,
            prop: 'totalPrice',
            label: '销售总金额',
            slot: 'totalPrice',
            formatter: (_row, _column, cellValue) => {
              return _row.totalPrice + '元';
            },
            align: 'center',
          },
          {
            width: 120,
            prop: 'measuringUnit',
            label: '计量单位',
            slot: 'measuringUnit',
            align: 'center',
          },
          {
            width: 120,
            prop: 'deliveryDays',
            label: '交期(天)',
            slot: 'deliveryDays',
            align: 'center',
          },
          {
            width: 200,
            prop: 'guaranteePeriod',
            label: '质保期',
            slot: 'guaranteePeriod',
            formatter: (_row, _column, cellValue) => {
              return (_row.guaranteePeriod||'') + _row.guaranteePeriodUnitName;
            },
            align: 'center',
          },
          {
            width: 240,
            prop: 'customerReqFiles',
            label: '客户需求',
            slot: 'customerReqFiles',
            align: 'center',
          },
          {
            width: 120,
            prop: 'technicalAnswerName',
            label: '技术答疑人',
            slot: 'technicalAnswerName',
            align: 'center',
          },
          {
            width: 220,
            prop: 'technicalParams',
            label: '技术参数',
            slot: 'technicalParams',
            align: 'center',
          },
          {
            width: 240,
            prop: 'technicalDrawings',
            label: '技术图纸',
            slot: 'technicalDrawings',
            formatter: (_row, _column, cellValue) => {
              return (_row.guaranteePeriod||'') + _row.guaranteePeriodUnitName;
            },
            align: 'center',
          },
          {
            width: 220,
            prop: 'remark',
            label: '备注',
            slot: 'remark',
            align: 'center',
          }
        ],
        ifChiefList: [
          {
            value: 0,
            label: '否'
          },
          {
            value: 1,
            label: '是'
          }
        ]
      };
    },
    methods: {
      async open(row) {
        this.form = row;
        this.visible = true;
        this.getDetailData(row.id);
      },

      cancel() {
        this.$nextTick(() => {
          // 关闭后，销毁所有的表单数据
          (this.form = copyObj(this.formDef)),
            (this.otherForm = copyObj(this.otherFormDef)),
            (this.tableBankData = []);
          this.tableLinkData = [];
          this.visible = false;
        });
      },
      downloadFile(file) {
        getFile({ objectName: file.storePath }, file.name);
      },
      async getDetailData(id) {
        this.loading = true;
        const data = await getDetail(id);
        this.loading = false;
        if (data) {
          this.detailData = data;
          this.$nextTick(() => {
            this.$refs.inventoryTabledetailRef &&
              this.$refs.inventoryTabledetailRef.putTableValue(data);
          });
        }
      }
    }
  };
</script>

<style scoped lang="scss">
  .ele-dialog-form {
    .el-form-item {
      margin-bottom: 10px;
    }
  }
</style>
