<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"
    :model="isModal"
    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-if="activeComp == 'main'">
      <el-form ref="form" :model="form" label-width="120px" class="el-form-box">
        <headerTitle title="基本信息">
          <!-- <el-button
                  size="small"
                  type="primary"
                  icon="el-icon-s-grid"
                  class="ele-btn-icon"
                  @click="exportTable"
                >
                 导出
                </el-button> -->
        </headerTitle>
        <el-row>
          <el-col :span="8">
            <el-form-item label="采购计划单名称：" class="form_item_label" prop="planName">
              <el-input v-model="form.planName" disabled>
                <template slot="append" v-if="form.planName">
                  <el-button type="primary" @click="openPlanDetail(form)" title="查看详情" icon="el-icon-view"></el-button>
                </template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="询价单名称：" prop="inquiryName">
              <el-input v-model="form.inquiryName" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="acceptUnpack" label="是否接受拆单:">
              <el-select v-model="form.acceptUnpack" placeholder="请选择" disabled style="width: 100%">
                <el-option label="接受" :value="1"></el-option>
                <el-option label="不接受" :value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item prop="remark" label="备注：">
              <el-input type="textarea" v-model="form.remark" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="askFile" label="附件：">
              <fileMain v-model="form.files" type="view"></fileMain>
<!--              <el-link-->
<!--                v-if="form.files && form.files !== ''"-->
<!--                type="primary"-->
<!--                :underline="false"-->
<!--                @click="downloadFile(form.files)">-->
<!--                {{ form.files.name }}</el-link>-->
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <headerTitle title="物品清单" style="margin-top: 15px"></headerTitle>
      <ele-pro-table
        ref="table"
        :needPage="false"
        :columns="columns"
        :toolkit="[]"
        :datasource="form.detailList"
        row-key="id"
      >
        <template v-slot:files="{ row }">
          <fileMain v-model="row.files" type="view"></fileMain>
<!--          <div v-if="row.files && row.files?.length">-->
<!--            <el-link-->
<!--              v-for="link in row.files"-->
<!--              :key="link.id"-->
<!--              type="primary"-->
<!--              :underline="false"-->
<!--              @click="downloadFile(link)"-->
<!--            >-->
<!--              {{ link.name }}</el-link-->
<!--            >-->
<!--          </div>-->
        </template>
      </ele-pro-table>
      <headerTitle title="报价清单" style="margin-top: 15px"></headerTitle>
      <inquiryTable
        :radio="form.winnerId"
        v-for="item in form.supplierList"
        :key="item.supplierId"
        style="margin-top: 15px"
        status="Detail"
        :obj="item"
      ></inquiryTable>
    </div>
    <bpmDetail
      v-if="activeComp === 'bpm' && form.processInstanceId"
      :id="form.processInstanceId"
    ></bpmDetail>
    <div slot="footer" class="footer">
      <el-button @click="cancel">返回</el-button>
    </div>
    <planDetaillog ref="planDetaillogRef" :isModal="false"></planDetaillog>
  </ele-modal>
</template>

<script>
  import { getpurchaseinquiry } from '@/api/purchasingManage/inquiryManage';
  import { getFile } from '@/api/system/file';
  import dictMixins from '@/mixins/dictMixins';
  import { copyObj } from '@/utils/util';
  import inquiryTable from './inquiryTable.vue';
  import planDetaillog from '../../../purchasingManage/purchasePlanManage/components/detailDialog.vue';
  import bpmDetail from '@/views/bpm/processInstance/detail.vue';
  import fileMain from "@/components/addDoc/index.vue";
  import {
    contactQueryByCategoryIdsAPI,
  } from '@/api/saleManage/contact';
  export default {
    mixins: [dictMixins],
    components: {
      fileMain,
      inquiryTable,
      planDetaillog,bpmDetail
    },
    props: {
      isModal: {
        default: true
      }
    },
    data() {
      return {
        activeComp: 'main',
        tabOptions: [
          { key: 'main', name: '核价单详情' },
          { key: 'bpm', name: '流程详情' }
        ],
        visible: false,
        title: '详情',
        row: {},
        form: {},
        detailData: {},
        inventoryList: [],
        columns: [
          {
            width: 45,
            type: 'index',
            columnKey: 'index',
            align: 'center',
            fixed: 'left'
          },
          {
            minWidth: 100,
            label: '状态',
            fixed: 'left',
            formatter: (row, column) => {
              return row.isInquiry==1?'部分核价':row.isInquiry==2?'全部核价完成':row.isInquiry==3?'核价中':'未核价'
            },
            align: 'center'
          },
          {
            minWidth: 100,
            prop: 'productCategoryName',
            label: '分类',
            slot: 'productCategoryName',
            align: 'center',
            showOverflowTooltip: true,
          },
          {
            minWidth: 120,
            prop: 'productCode',
            label: '编码',
            slot: 'productCode',
            showOverflowTooltip: true,
            align: 'center',
          },
          {
            minWidth: 120,
            prop: 'productName',
            label: '名称',
            slot: 'productName',
            showOverflowTooltip: true,
            align: 'center',
          },

          {
            minWidth: 200,
            prop: 'supplierName',
            label: '供应商',
            showOverflowTooltip: true,
            align: 'center',
          },
          {
            minWidth: 80,
            prop: 'productBrand',
            label: '牌号',
            slot: 'productBrand',
            showOverflowTooltip: true,
            align: 'center',
          },
          {
            minWidth: 80,
            prop: 'totalCount',
            label: '数量',
            slot: 'totalCount',
            showOverflowTooltip: true,
            align: 'center',
          },
          {
            width: 150,
            prop: 'inquiryNum',
            label: '已核价数量',
            align: 'center'
          },
          {
            width: 120,
            prop: 'totalWeight',
            label: '重量',
            slot: 'totalWeight',
            align: 'center'
          },
          {
            minWidth: 220,
            prop: 'taskName',
            label: '工序',
            slot: 'taskName',
            align: 'center'
          },
          {
            width: 110,
            prop: 'batchNo',
            label: '批次号',
            slot: 'batchNo',
            align: 'center'
          },
          {
            prop: 'provenance',
            label: '产地',
            slot: 'provenance',
            align: 'center',
            minWidth: 200,
            showOverflowTooltip: true,
            formatter: (row, column) => {
       
              return row.provenance && row.provenance.length
                ? row.provenance
                    .map((item) => this.getDictValue('产地', item ))
                    .join(',')
                : '';
            }
          },
          {
            minWidth: 80,
            prop: 'measuringUnit',
            label: '单位',
            slot: 'measuringUnit',
            showOverflowTooltip: true,
            align: 'center',
          },
          {
            minWidth: 120,
            prop: 'modelType',
            label: '型号',
            slot: 'modelType',
            showOverflowTooltip: true,
            align: 'center',
          },

          {
            minWidth: 120,
            prop: 'specification',
            label: '规格',
            slot: 'specification',
            showOverflowTooltip: true,
            align: 'center',
          },
          // {
          //   width: 130,
          //   prop: 'modelType',
          //   label: '品牌',
          //   slot: 'modelType'
          // },
          {
            minWidth: 170,
            prop: 'expectReceiveDate',
            label: '到货日期',
            slot: 'expectReceiveDate',
            showOverflowTooltip: true,
            align: 'center',
          },
          {
            width: 120,
            prop: 'produceType',
            align: 'center',
            label: '属性类型',
            showOverflowTooltip: true,
            formatter: (row, column) => {
              return row.produceType && row.produceType.length
                ? row.produceType
                    .map((item) => this.getDictValue('生产类型', item + ''))
                    .join(',')
                : '';
            }
          },
          {
            minWidth: 120,
            prop: 'packingSpecification',
            align: 'center',
            label: '包装规格',
            showOverflowTooltip: true
          },
          {
            minWidth: 140,
            prop: 'files',
            label: '附件',
            slot: 'files',
            align: 'center',
          },
          {
            minWidth: 220,
            prop: 'remark',
            label: '备注',
            slot: 'remark',
            showOverflowTooltip: true,
            align: 'center',
          }
        ]
      };
    },
    created(){
      this.requestDict('产地');
      this.requestDict('生产类型');
    },
    methods: {
      // //导出
      // async exportTable() {
      //   this.loading = true;
      //   const response = await getExport(this.detailId);
      // },
      async open(row) {
        this.visible = true;
        this.getInquiryData(row.id);
        this.detailId = row.id;
      },

      cancel() {
        this.$nextTick(() => {
          // 关闭后，销毁所有的表单数据
          (this.form = {}),
            (this.otherForm = copyObj(this.otherFormDef)),
            (this.tableBankData = []);
          this.tableLinkData = [];
          this.visible = false;
        });
      },
      downloadFile(file) {
        getFile({ objectName: file.storePath }, file.name);
      },
      async getInquiryData(id) {
        this.loading = true;

        const data = await getpurchaseinquiry(id);
        this.loading = false;
        if (data) {
          let categoryIds=data.detailList.map(item=>item.productId)
          let supplierObj = await contactQueryByCategoryIdsAPI({ categoryIds });
          data.detailList.forEach((item) => {
            if(supplierObj[item.productId]){
              item.supplierName= supplierObj[item.productId].filter(val=>item.supplierIds.includes(val.id)).map(val1=>val1.name).toString()
            }
          });
          this.form = data;
          if (data.files && data.files.length > 0) {
            this.form.files = data.files[0];
          } else {
            this.form.files = null;
          }
        }
        // await this.getDetailData(data.requirementId);
      },
      //查看详情
      openPlanDetail(row) {
        this.$refs.planDetaillogRef.open(row);
      }
      // //获取需求详情
      // async getDetailData(id) {
      //   this.loading = true;
      //   let data = await getDetail(id);
      //   this.loading = false;
      //   if (data) {
      //     this.$nextTick(() => {
      //       this.inventoryList = copyObj(data.detailList);
      //     });
      //   }
      // }
    }
  };
</script>

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

  .headbox {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .amount {
      font-size: 14px;
      font-weight: bold;
    }
  }
</style>
