<template>
  <div>
    <headerTitle title="基本信息"></headerTitle>
    <el-form
      label-width="120px"
      ref="form"
      :model="form"
      :rules="rules"
      style="margin-top: 30px"
    >
      <el-row>
        <el-col :span="8">
          <el-form-item label="客户名称：" prop="contactName">
            {{ detailData.contactName }}
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="	商机名称：" prop="name">
            {{ detailData.name }}
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="负责人名称：" prop="responsibleName">
            {{ detailData.responsibleName }}
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="商机来源：" prop="sourceCode">
            {{ detailData.sourceName }}
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="赢单率：" prop="winRate">
            {{ detailData.winRate ? detailData.winRate + '%' : '' }}
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="预算：" prop="budget">
            {{ detailData.budget ? detailData.budget + '万元' : '' }}
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="预计结单日期：" prop="expectedClosingDate">
            {{ detailData.expectedClosingDate }}
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="商机阶段：" prop="stageCode">
            {{ detailData.stageName }}
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="附件：" prop="files">
            <fileMain v-model="detailData.files" type="view"></fileMain>
            <!--            <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="8">
          <el-form-item label="计费方式：" prop="pricingWay">
            {{ detailData.pricingWay == 1 ? '按数量计费' : '按重量计费' }}
          </el-form-item>
        </el-col> -->
        <el-col :span="16">
          <el-form-item label="备注：" prop="remark">
            {{ detailData.remark }}
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <headerTitle title="物品清单"></headerTitle>

    <inventoryTable
      pageName="businessOpportunity"
      v-if="
        [
          'productionSupervisorApprove1',
          'technicianApprove',
          'techLeaderApprove',
          'productionSupervisorApprove2',
          'salesManagerApprove'
        ].includes(taskDefinitionKey)
      "
      :isCustomerMark="false"
      ref="inventoryTable"
      :isDeliveryDeadline="false"
      :isGuaranteePeriod="false"
      :isDiscount="false"
      :isProduceDeliveryDeadline="taskDefinitionKey === 'productionSupervisorApprove2'"
      :pricing-way="form.pricingWay"
      
    ></inventoryTable>

    <inventoryTabledetail
      v-if="
        ![
          'productionSupervisorApprove1',
          'technicianApprove',
          'techLeaderApprove',
          'productionSupervisorApprove2',
          'salesManagerApprove'
        ].includes(taskDefinitionKey)
      "
      ref="inventoryTabledetailRef"
      :isDeliveryDeadline="false"
      :isGuaranteePeriod="false"
      :isCustomerMark="false"
      :isDiscount="false"

      :pricingWay="detailData.pricingWay"
    ></inventoryTabledetail>
    <!-- <el-card
      shadow="never"
      header="竞品对手"
      body-style="padding: 22px 20px 0 20px;"
    >
      <ele-pro-table
        ref="table"
        :needPage="false"
        :columns="competAnalysisListcolumns1"
        :datasource="detailData.competAnalysisList"
        row-key="id"
      >
      </ele-pro-table>
    </el-card>
    <el-card
      shadow="never"
      header="关键人信息"
      body-style="padding: 22px 20px 0 20px;"
    >
      <ele-pro-table
        ref="table"
        :needPage="false"
        :columns="partyListcolumns"
        :datasource="detailData.partyList"
        row-key="id"
      >
      </ele-pro-table>
    </el-card> -->
  </div>
</template>

<script>
  import { getDetail } from '@/api/bpm/components/saleManage/businessOpportunity';
  import { getFile } from '@/api/system/file';
  import dictMixins from '@/mixins/dictMixins';
  // import inventoryTable from './saleGeneralityInventoryTable.vue';
  import inventoryTable from '@/BIZComponents/inventoryTable.vue';

  import inventoryTabledetail from '@/views/bpm/handleTask/components/contractBook/inventoryTabledetail.vue';
  // import fileMain from '@/components/addDoc/index.vue';
  import {contactDetail} from '@/api/bpm/components/saleManage/contact';
  export default {
    mixins: [dictMixins],
    components: {
      // fileMain,
      inventoryTable,
      inventoryTabledetail
    },
    props: {
      businessId: {
        default: ''
      },
      taskDefinitionKey: {
        default: ''
      }
    },
    data() {
      return {
        visible: false,
        customerMark:'',
        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'
          },
          {
            width: 120,
            prop: 'productCode',
            label: '编码',
            slot: 'productCode'
          },
          {
            width: 200,
            prop: 'productCategoryName',
            label: '类型',
            slot: 'productCategoryName'
          },
          {
            width: 160,
            prop: 'productBrand',
            label: '牌号',
            slot: 'productBrand'
          },
          {
            width: 120,
            prop: 'modelType',
            label: '型号',
            slot: 'modelType'
          },
          {
            width: 120,
            prop: 'specification',
            label: '规格',
            slot: 'specification'
          },

          {
            width: 160,
            prop: 'singlePrice',
            label: '单价',
            slot: 'singlePrice'
          },
          {
            width: 120,
            prop: 'totalCount',
            label: '数量',
            slot: 'totalCount'
          },
          {
            width: 120,
            prop: 'totalPrice',
            label: '销售总金额',
            slot: 'totalPrice',
            formatter: (_row, _column, cellValue) => {
              return _row.totalPrice ? _row.totalPrice + '元' : '';
            }
          },
          {
            width: 120,
            prop: 'measuringUnit',
            label: '计量单位',
            slot: 'measuringUnit'
          },
          {
            width: 120,
            prop: 'deliveryDays',
            label: '交期(天)',
            slot: 'deliveryDays'
          },
          {
            width: 200,
            prop: 'guaranteePeriod',
            label: '有效期',
            slot: 'guaranteePeriod',
            formatter: (_row, _column, cellValue) => {
              return (
                (_row.guaranteePeriod || '') + _row.guaranteePeriodUnitName
              );
            }
          },
          // {
          //   width: 120,
          //   prop: 'guaranteePeriodUnitCode',
          //   label: '',
          //   slot: 'guaranteePeriodUnitCode'
          // },
          {
            width: 240,
            prop: 'customerReqFiles',
            label: '客户需求',
            slot: 'customerReqFiles'
          },
          {
            width: 120,
            prop: 'technicalAnswerName',
            label: '技术答疑人',
            slot: 'technicalAnswerName'
          },
          {
            width: 220,
            prop: 'technicalParams',
            label: '技术参数',
            slot: 'technicalParams'
          },
          {
            width: 240,
            prop: 'technicalDrawings',
            label: '技术图纸',
            slot: 'technicalDrawings',
            formatter: (_row, _column, cellValue) => {
              return (
                (_row.guaranteePeriod || '') + _row.guaranteePeriodUnitName
              );
            }
          },
          {
            width: 220,
            prop: 'remark',
            label: '备注',
            slot: 'remark'
          }
        ]
      };
    },
    created() {
      this.getDetailData(this.businessId);
    },
    methods: {
      downloadFile(file) {
        getFile({ objectName: file.storePath }, file.name);
      },
      async getDetailData(id) {
        this.loading = true;
        const data = await getDetail(id);

        this.loading = false;
        this.detailData = data;
        this.getContactDetail()
        if (this.detailData) {
          this.$refs.inventoryTable &&
            this.$refs.inventoryTable.putTableValue(this.detailData);
          this.$refs.inventoryTabledetailRef &&
            this.$refs.inventoryTabledetailRef.putTableValue(this.detailData);
        }
      },
      //获取客户
      async getContactDetail(type) {
        const { base } = await contactDetail(this.detailData.contactId);
        this.customerMark = base.serialNo;
  
      },
      getValidate() {
        return Promise.all([
          new Promise((resolve, reject) => {
            this.$refs.inventoryTable.validateForm((valid) => {
              if (!valid) {
                reject(false);
              } else {
                resolve(true);
              }
            });
          })
        ]);
      },
      async getTableValue() {
        try {
          await this.getValidate();
          if (this.$refs.inventoryTable.getTableValue().length == 0) {
            this.$message.warning('物品清单不能为空');

            return;
          }

          this.detailData.totalPrice= this.$refs.inventoryTable.getPrice()[0]
          const commitData = {
            opportunity: this.detailData,
            competAnalysisList: this.detailData.competAnalysisList,
            partyList: this.detailData.partyList,
            productList: this.$refs.inventoryTable.getTableValue()
          };
          return commitData;

        } catch (error) {
          console.log(error);
          return false
        }
      }
    }
  };
</script>

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