<template>
  <div class="edit-add-detail">
    <div class="crumbs">
      <Breadcrumb
        :breadcrumbOpt="breadcrumbOpt"
        svg-contrName="#icon-bill"
        @breadcrumb-click="handleClose"
      />
    </div>
    <div class="form-content" v-loading="loading">
      <el-form
        ref="formInfo"
        :model="parameter"
        label-width="auto"
        label-position="right"
        size="small"
        :rules="rules"
      >
        <div class="rows" @click="handleOver">
          <div class="rows-left">
            <div class="rows-hr"></div>
          </div>
          <div class="rows-title">
            <div class="text-overflow">基本信息</div>
          </div>
          <i
            :class="!flexAuto ? 'el-icon-arrow-right' : 'el-icon-arrow-down'"
            style="margin-left: 10px"
          ></i>
        </div>
        <div :class="!flexAuto ? 'auto' : 'classNone'">
          <el-row :gutter="24">
            <el-col :span="8">
              <el-form-item label="合同编号" prop="contrCode">
                <el-input
                  clearable
                  v-model="parameter.contrCode"
                  placeholder="请输入合同编号"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="合同名称" prop="contrName">
                <el-input
                  clearable
                  v-model="parameter.contrName"
                  placeholder="请输入合同名称"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="合同类型" prop="contrMethod">
                <dict-select
                  dictCode="htgl-htlx"
                  :hasDefaultOption="true"
                  style="width: 100%"
                  v-model="parameter.contrMethod"
                  placeholder="请选择合同类型"
                />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="24">
            <el-col :span="8">
              <el-form-item label="合同签订人" prop="matCompanyClientName">
                <el-input
                  v-model="parameter.matCompanyClientName"
                  clearable
                  placeholder="请输入合同签订人"
                ></el-input>
                <!-- <EipUserDialog
                  :title="'合同签订人'"
                  :single="true"
                  :matCategoryId="params.matCategoryId"
                  v-model="parameter.affiliatedCompanyName"
                  :config="{
                    id: 'parameter.affiliatedCompanyId',
                    name: 'parameter.affiliatedCompanyName',
                  }"
                  :searchPlaceholder="'选择合同签订人'"
                  @input="handleSelectorInput"
                ></EipUserDialog> -->
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="合同开始日期" prop="contractStartDate">
                <el-date-picker
                  v-model="parameter.contractStartDate"
                  contrMethod="date"
                  :picker-options="pickerOptionsStart"
                  value-format="yyyy-MM-dd"
                  style="width: 100%"
                  placeholder="请选择合同开始日期"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="合同结束日期" prop="contractEndDate">
                <el-date-picker
                  v-model="parameter.contractEndDate"
                  contrMethod="date"
                  :picker-options="pickerOptionsEnd"
                  value-format="yyyy-MM-dd"
                  style="width: 100%"
                  placeholder="请选择合同开始日期"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="8">
              <el-form-item label="采购方" prop="matCompanyName">
                <el-input
                  clearable
                  v-model="parameter.matCompanyName"
                  placeholder="请输入采购方"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="采购方签章联系人"
                prop="matCompanyUserName"
              >
                <el-input
                  clearable
                  v-model="parameter.matCompanyUserName"
                  placeholder="请输入采购方签章联系人"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="采购方联系方式" prop="matCompanyUserPhone">
                <el-input
                  clearable
                  type="number"
                  v-model="parameter.matCompanyUserPhone"
                  placeholder="请输入采购方联系方式"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="8">
              <el-form-item label="供货方" prop="vendorName">
                <el-input
                  clearable
                  @focus="openVendor"
                  v-model="parameter.vendorName"
                  prefix-icon="el-icon-search"
                  placeholder="请输入供货方"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="供货方签章联系人" prop="vendorUserName">
                <el-input
                  clearable
                  v-model="parameter.vendorUserName"
                  placeholder="请输入供货方签章联系人"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="供货方联系方式" prop="vendorUserPhone">
                <el-input
                  clearable
                  type="number"
                  v-model="parameter.vendorUserPhone"
                  placeholder="请输入供货方联系方式"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="8">
              <el-form-item label="付款方式" prop="payMethod">
                <dict-select
                  dictCode="cglx-fkfs"
                  :hasDefaultOption="true"
                  clearable
                  style="width: 100%"
                  v-model="parameter.payMethod"
                  placeholder="请选择付款方式"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="是否代采" prop="isAgencyProcure">
                <dict-select
                  dictCode="cgsq-sfdc"
                  :hasDefaultOption="true"
                  clearable
                  style="width: 100%"
                  v-model="parameter.isAgencyProcure"
                  placeholder="请选择是否代采"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="是否含税" prop="isTax">
                <dict-select
                  dictCode="cgsq-bjfs"
                  :hasDefaultOption="true"
                  clearable
                  style="width: 100%"
                  v-model="parameter.isTax"
                  placeholder="请选择是否含税"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="项目类型" prop="noticeType">
                <dict-select
                dictCode="bdlx"
                :hasDefaultOption="true"
                clearable
                style="width: 100%"
                v-model="parameter.noticeType"
                placeholder="请选择项目类型"
              />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="合同签订日期" prop="signData">
                <el-date-picker
                  v-model="parameter.signData"
                  contrMethod="date"
                  :picker-options="pickerOptions"
                  value-format="yyyy-MM-dd"
                  style="width: 100%"
                  placeholder="请选择合同签订日期"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="合同金额(元)" prop="contractAmount">
                <el-input
                  clearable
                  v-model="parameter.contractAmount"
                  placeholder="选择明细自动计算合同金额"
                ></el-input>
              </el-form-item> 
              </el-col>
          </el-row>

          <el-row :gutter="24">
            
            
            </el-row>
          <div class="rows">
            <div class="rows-left">
              <div class="rows-hr"></div>
            </div>
            <div class="rows-title">
              <div class="text-overflow">相关文件</div>
            </div>
          </div>
          <div>
            <el-upload
              class="upload-demo"
              ref="upload"
              :headers="headers"
              :action="action"
              :file-list="parameter.accessoryList"
              :multiple="false"
              accept=".png,.jpg,.jpeg,.xlsx,.xls,.pdf,.docx,.doc,.zip,.rar"
              :on-progress="handleProgress"
              :show-file-list="false"
              :before-upload="beforeUpload"
              :on-success="handleSuccess"
            >
              <el-button
                icon="el-icon-upload"
                slot="trigger"
                size="small"
                type="primary"
                >上传文件</el-button
              >
              <span></span>
            </el-upload>
          </div>
          <el-table
            class="down"
            :data="parameter.accessoryList"
            border
            stripe
            style="width: 100%; margin-top: 20px"
          >
            <el-table-column prop="name" label="文件名称"></el-table-column>
            <el-table-column prop="size" label="文件大小">
              <template slot-scope="scope">
                <span>{{ scope.row.fileSize }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="createTime" label="上传时间">
              <template slot-scope="scope">
                <div>
                  {{ scope.row.createTime }}
                </div>
              </template>
            </el-table-column>
            <el-table-column width="150px" label="操作">
              <template slot-scope="scope">
                <el-button size="small" type="text">
                  <a
                    target="_blank"
                    @click="downloadFile(scope.row.name, scope.row.url)"
                    >下载</a
                  >
                </el-button>
                <el-button
                  size="small"
                  type="text"
                  @click="deleteHandle(scope.$index)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </div>

        <div class="rows">
          <div class="rows-left">
            <div class="rows-hr"></div>
          </div>
          <div class="rows-title">
            <div class="text-overflow">物料信息</div>
          </div>
          <el-button
            type="primary"
            style="margin-left: 15px"
            icon="el-icon-thumb"
            size="mini"
            @click="open"
            >添加明细</el-button
          >
        </div>
        <el-table
          ref="singleTable"
          :data="parameter.detail"
          style="width: 100%"
          v-if="parameter.noticeType==1"
          highlight-current-row
          :cell-style="{'text-align': 'center'}"
          :header-cell-style="{'text-align': 'center'}"
        >
          <el-table-column fixed type="index" width="55" label="序号" />
          <el-table-column
            prop="matCode"
            show-overflow-tooltip
            min-width="120px"
            align="center"
            label="企业物料编号"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="matPlatcode"
            show-overflow-tooltip
            min-width="120px"
            align="center"
            label="平台物料编号"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="matName"
            show-overflow-tooltip
            min-width="100px"
            align="center"
            label="物料名称"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="matMaterial"
            show-overflow-tooltip
            min-width="100px"
            align="center"
            label="物料牌号"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="matSpec"
            show-overflow-tooltip
            min-width="110px"
            align="center"
            label="规格型号"
            resizable
          >
          </el-table-column>

          <el-table-column
            prop="planCode"
            show-overflow-tooltip
            min-width="120px"
            align="center"
            label="计划编号"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="pendingTime"
            show-overflow-tooltip
            min-width="100px"
            align="center"
            label="挂账时间"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="acceptanceTime"
            show-overflow-tooltip
            min-width="100px"
            v-if="parameter.payMethod == 2"
            align="center"
            label="承兑时间"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="num"
            show-overflow-tooltip
            min-width="100px"
            align="center"
            label="数量"
            resizable
          >
            <template slot-scope="scope">
              <el-input
                clearable
                type="Number"
                :min="0"
                v-if="parameter.contrMethod == 1"
                v-model="scope.row.num"
                placeholder="数量"
              ></el-input>
              <div v-else>{{ scope.row.num }}</div>
            </template>
          </el-table-column>
          <el-table-column
            prop="matUnit"
            show-overflow-tooltip
            min-width="100px"
            align="center"
            label="计量单位"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="taxInprice"
            show-overflow-tooltip
            min-width="100px"
            align="center"
            label="含税金额(元)"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="taxInvalue"
            show-overflow-tooltip
            min-width="100px"
            align="center"
            label="含税总额(元)"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="contrPrice"
            show-overflow-tooltip
            min-width="100px"
            align="center"
            label="不含税金额(元)"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="contrAmount"
            show-overflow-tooltip
            min-width="100px"
            align="center"
            label="不含税总额(元)"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="taxValue"
            show-overflow-tooltip
            min-width="100px"
            align="center"
            label="税额(元)"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="demandDate"
            show-overflow-tooltip
            min-width="120px"
            align="center"
            label="需求日期"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="matBrand"
            show-overflow-tooltip
            min-width="100px"
            align="center"
            label="品牌"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="matBaseExt"
            show-overflow-tooltip
            min-width="100px"
            align="center"
            label="基础扩展"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="matOtherExt"
            show-overflow-tooltip
            min-width="100px"
            align="center"
            label="其他扩展"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="taxRate"
            show-overflow-tooltip
            min-width="100px"
            align="center"
            label="税率(%)"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="unloadAddress"
            show-overflow-tooltip
            min-width="130px"
            align="center"
            label="送货地址"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="purNote"
            show-overflow-tooltip
            min-width="130px"
            align="center"
            label="企业补充说明"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="tenderExplain"
            show-overflow-tooltip
            min-width="130px"
            align="center"
            label="成交人补充说明"
            resizable
          >
          </el-table-column>
          <el-table-column
            fixed="right"
            align="right"
            label="操作"
            width="90px"
            :index="999"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="danger"
                circle
                icon="el-icon-close"
                @click="delTable(scope.$index, scope.row)"
              />
            </template>
          </el-table-column>
        </el-table>
        <el-table
          ref="singleTable"
          :data="parameter.detail"
          style="width: 100%"
          v-if="parameter.noticeType==2"
          highlight-current-row
          :cell-style="{'text-align': 'center'}"
          :header-cell-style="{'text-align': 'center'}"
        >
          <el-table-column fixed type="index" width="55" label="序号" />
          <el-table-column
            prop="projectLedgerCode"
            show-overflow-tooltip
            min-width="100px"
            align="center"
            label="项目编号"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="projectLedgerName"
            show-overflow-tooltip
            min-width="100px"
            align="center"
            label="项目名称"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="matName"
            show-overflow-tooltip
            min-width="100px"
            align="center"
            label="名称"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="num"
            show-overflow-tooltip
            min-width="100px"
            align="center"
            label="数量"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="taxInprice"
            show-overflow-tooltip
            min-width="100px"
            align="center"
            label="含税金额(元)"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="taxInvalue"
            show-overflow-tooltip
            min-width="100px"
            align="center"
            label="含税总额(元)"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="contrPrice"
            show-overflow-tooltip
            min-width="100px"
            align="center"
            label="不含税金额(元)"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="contrAmount"
            show-overflow-tooltip
            min-width="100px"
            align="center"
            label="不含税总额(元)"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="taxValue"
            show-overflow-tooltip
            min-width="100px"
            align="center"
            label="税额(元)"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="matBaseExt"
            show-overflow-tooltip
            min-width="100px"
            align="center"
            label="服务内容"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="matOtherExt"
            show-overflow-tooltip
            min-width="100px"
            align="center"
            label="服务范围"
            resizable
          >
          </el-table-column>
          <el-table-column
              prop="serviceStartTime"
              show-overflow-tooltip
              min-width="100px"
              align="center"
              label="开始时间"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="serviceEndTime"
              show-overflow-tooltip
              min-width="100px"
              align="center"
              label="结束时间"
              resizable
            >
            </el-table-column>
          <el-table-column
            prop="taxRate"
            show-overflow-tooltip
            min-width="100px"
            align="center"
            label="税率(%)"
            resizable
          >
          </el-table-column>
          <el-table-column
            prop="tenderExplain"
            show-overflow-tooltip
            min-width="130px"
            align="center"
            label="成交人补充说明"
            resizable
          >
          </el-table-column>
          <el-table-column
            fixed="right"
            align="right"
            label="操作"
            width="90px"
            :index="999"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="danger"
                circle
                icon="el-icon-close"
                @click="delTable(scope.$index, scope.row)"
              />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
    </div>
    <div slot="footer" class="footer">
      <el-button @click="handleClose">{{ $t('domestic.cancel') }}</el-button>
      <el-button type="primary" @click="handleSave" :loading="loading">{{
        $t('domestic.save')
      }}</el-button>
    </div>
    <!-- 选择成交供应商 -->
    <vendorDialog
      ref="vendor"
      @addDetail="getVendor"
      :useOrgList="vendorList"
    ></vendorDialog>
    <!-- 获取成交明细 -->
    <contracDetailedDialog
      ref="contrac"
      @addDetail="getDetails"
      :useOrgList="detailsList"
    ></contracDetailedDialog>
  </div>
</template>

<script>
import Breadcrumb from '@/components/Breadcrumb/index.vue'
import vendorDialog from './vendorDialog'
import utils from '@/utils.js'
import contracDetailedDialog from './contracDetailedDialog'
import {mapState} from 'vuex'
import EipUserDialog from '@/components/dialog/EipUserDialog.vue'
export default {
  contrName: 'contractPreparationAddDialog',
  components: {Breadcrumb, vendorDialog, contracDetailedDialog, EipUserDialog},
  props: {
    editData: Object,
  },
  data() {
    return {
      pickerOptions: {
        disabledDate: (time) => {
          if (this.parameter.contractEndDate) {
            // eslint-disable-next-line max-len
            return (
              time.getTime() <=
              new Date(this.basicInfo.createDate).getTime() - 86400000
            )
          }
          return (
            time.getTime() <=
            new Date(this.basicInfo.createDate).getTime() - 86400000
          )
        },
      },
      pickerOptionsStart: {
        disabledDate: (time) => {
          if (this.parameter.contractEndDate) {
            // eslint-disable-next-line max-len
            return (
              time.getTime() >
                new Date(this.parameter.contractEndDate).getTime() ||
              time.getTime() <=
                new Date(this.basicInfo.createDate).getTime() - 86400000
            )
          }
          return (
            time.getTime() <=
            new Date(this.basicInfo.createDate).getTime() - 86400000
          )
        },
      },
      // 结束时间限制
      pickerOptionsEnd: {
        disabledDate: (time) => {
          if (this.parameter.contractStartDate) {
            return (
              time.getTime() <
              new Date(this.parameter.contractStartDate).getTime()
            )
          }
          return (
            time.getTime() <=
            new Date(this.basicInfo.createDate).getTime() - 86400000
          )
        },
      },
      basicInfo: {
        // 创建时间 此时间可以通过后端接口取服务器时间 或者 用 new Date().getTime()
        createDate: new Date().getTime(),
      },
      breadcrumbOpt: [{breadcrumbName: '合同编制'}, {breadcrumbName: '新增'}],
      rules: {
        noticeType: [
          {
            required: true,
            message: '请选择项目类型',
            trigger: 'blur',
          },
        ],
        contractAmount: [
          {
            required: true,
            message: '请选择明细',
            trigger: 'blur',
          },
        ],
        signData: [
          {
            required: true,
            message: '请选择签订日期',
            trigger: 'blur',
          },
        ],
        isAgencyProcure: [
          {
            required: true,
            message: '请选择是否代采',
            trigger: 'blur',
          },
        ],
        payMethod: [
          {
            required: true,
            message: '请选择付款方式',
            trigger: 'blur',
          },
        ],
        matCompanyUserName: [
          {
            required: true,
            message: '请输入采购方签章联系人',
            trigger: 'blur',
          },
        ],
        matCompanyName: [
          {
            required: true,
            message: '请输入采购方',
            trigger: 'blur',
          },
        ],
        vendorUserPhone: [
          {
            required: true,
            message: '请输入联系电话',
            trigger: 'blur',
          },
          {
            pattern: /^1[3456789]\d{9}$/,
            message: '手机号码格式不正确',
            trigger: 'blur',
          },
        ],
        contrCode: [
          {
            required: true,
            message: '请输入合同编号',
            trigger: 'blur',
          },
        ],
        contrName: [
          {
            required: true,
            message: '请输入合同名称',
            trigger: 'blur',
          },
        ],
        contrMethod: [
          {
            required: true,
            message: '请选择合同类型',
            trigger: 'blur',
          },
        ],
        thirdPartyCompanyName: [
          {
            required: true,
            message: '请输入处置方',
            trigger: 'blur',
          },
        ],
        vendorName: [
          {
            required: true,
            message: '请输入供货方',
            trigger: 'blur',
          },
        ],
        vendorUserName: [
          {
            required: true,
            message: '请输入供货方签章联系人',
            trigger: 'blur',
          },
        ],
        contractStartDate: [
          {
            required: true,
            message: '请选择合同开始日期',
            trigger: 'blur',
          },
        ],
        contractEndDate: [
          {
            required: true,
            message: '请选择合同结束日期',
            trigger: 'blur',
          },
        ],
        isTax: [
          {
            required: true,
            message: '请选择是否含税',
            trigger: 'blur',
          },
        ],
      },
      loading: false,
      flexAuto: true,
      parameter: {
        detail: [],
        vendorName: '',
        contractAmount: 0,
        contractStartDate: '',
        contractEndDate: '',
        accessoryList: [],
        isAgencyProcure: 0,
        noticeType:1,
        payMethod: '',
        isTax:'1',
        contrCode: '',
        matCompanyName: '',
        matCompanyCode: '',
        matCompanyId: '',
        signData: '',
        status: '1',
        vendorUserId:''
      },
      vendorList: [],
      detailsList: [],
    }
  },
  created() {
    this.currentUser = JSON.parse(sessionStorage.getItem('currentUserDetail'))
    this.parameter.matCompanyName = this.currentUser.org[0].orgName
    this.parameter.matCompanyCode = this.currentUser.org[0].orgCode
    this.parameter.matCompanyId = this.currentUser.org[0].orgId
    this.parameter.matCompanyClientName = this.currentUser.user.fullname
    this.parameter.matCompanyClientId = this.currentUser.user.userId
    this.parameter.matCompanyUserName = this.currentUser.user.fullname
    this.parameter.matCompanyUserId = this.currentUser.user.userId
    let moment = require('moment')
    this.parameter.signData = moment().format('YYYY-MM-DD')
    this.addModifyBtn()
  },
  mounted() {},
  methods: {
    // 附件上传
    handleSuccess(res, file, fileList) {
      this.loading = false
      this.parameter.accessoryList.push(res.value)
    },
    beforeUpload(file) {
      let acceptList = ['xlsx', 'xls','png','jpg','jpeg','xlsx','xls','pdf','docx','doc','zip','rar']
      // 根据文件名获取文件的后缀名
      let fileType = file.name.split('.').pop().toLowerCase()
      // 判断文件格式是否符合要求
      if (acceptList.indexOf(fileType) === -1) {
        this.$message.error('请上传 .png,.jpg,.jpeg,.xlsx,.xls,pdf,.docx,.doc,.zip,.rar 格式的文件 !')
        return false
      }
    },
    // 删除明细
    delTable(index, row) {
      this.parameter.detail.splice(index, 1)
    },
    getCode() {
      this.$http
        .get('${portal}/sys/identity/v1/getNextIdByAlias?alias=htgl_htbh')
        .then((res) => {
          if (res.data.state) {
            this.parameter.contrCode = res.data.value
          }
        })
    },
    // 获取字典值翻译
    getDictName(code, value) {
      return utils.getDictName(code, value)
    },
    //编辑
    addModifyBtn() {
      if (this.$route.query.editRows) {
        this.breadcrumbOpt[1].breadcrumbName = '编辑'
        this.modifychoData(this.$route.query.editRows)
      } else {
        this.breadcrumbOpt[1].breadcrumbName = '新增'
        this.getCode()
      }
    },
    //获取修改回显数据
    modifychoData(val) {
      this.$http
        .post('${portal}/bizContract/v1/getById?id=' + val.id)
        .then((res) => {
          console.log(res)
          if (res.data.state) {
            this.parameter = res.data.value
            this.parameter.detail = this.parameter.itemList
            this.vendorList = []
            this.vendorList.push({
              vendorCode: this.parameter.vendorCode,
              vendorId: this.parameter.vendorId,
              vendorName: this.parameter.vendorName,
            })
          }
        })
    },
    //下载按钮回调
    downloadFile(name, url) {
      console.log(name, 'name')
      var a = document.createElement('a')
      var event = new MouseEvent('click')
      a.download = name
      a.target = '_blank'
      a.href = url
      a.dispatchEvent(event)
    },
    // 删除
    deleteHandle(index) {
      this.$confirm('是否确认删除该附件', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.parameter.accessoryList.splice(index, 1)
      })
    },
    // 关闭//面包屑点击
    handleClose() {
      this.$router.push({
        name: 'contractPreparation',
      })

      this.parameter = {}
    },
    handleProgress() {
      this.loading = true
    },
    // 保存
    handleSave() {
      this.parameter.itemList.forEach(item=>{
        if(item.applicationId!=undefined){
          item.purchasingApplicationId = item.applicationId
        }
        if(item.applicationDetailld!=undefined){
          item.purchasingApplicationDetailId = item.applicationDetailld
        }
      })
      this.$refs.formInfo.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$http
            .post('${portal}/bizContract/v1/saveOrUpdateContr', this.parameter)
            .then((res) => {
              console.log(res)
              this.loading = false
              if (res.data.state) {
                this.$message.success(res.data.message)
                this.handleClose()
              }
            })
        }
      })
    },
    handleOver() {
      this.flexAuto = !this.flexAuto
    },
    // 打开供应商弹窗
    openVendor() {
      this.$refs.vendor.add()
    },
    // 获取供应商
    getVendor(data) {
      this.vendorList = data
      this.parameter.vendorName = data[0].vendorName
      this.parameter.vendorId = data[0].vendorId
      this.parameter.vendorCode = data[0].vendorCode
      this.$nextTick(() => {
        this.$refs.formInfo.clearValidate('vendorName')
      })
    },
    // 打开明细弹窗
    open() {
      if (this.parameter.vendorName == '') {
        this.$message.warning('请选择供货方')
        return
      }
      if (this.parameter.payMethod == '') {
        this.$message.warning('请选择付款方式')
        return
      }
      this.$refs.contrac.add(this.parameter)
    },
    // 获取中标明细
    getDetails(data) {
      this.detailsList = data
      if (this.parameter.detail.length == 0) {
        this.parameter.detail = data
      }
      let sum = 0
      this.parameter.detail.forEach((item) => {
        if (item.isTax == '1') {
          item.taxInprice = item.winBidPrice
          item.taxInvalue = item.winBidAmount
          item.contrPrice = (item.winBidPrice/(1 + (item.taxRate / 100))).toFixed(4)
          item.contrAmount = (item.contrPrice * item.matNum).toFixed(2)
          item.taxValue = (
            (item.taxInprice * item.matNum * (item.taxRate / 100)) /
            (1 + item.taxRate / 100)
          ).toFixed(2)
          sum += Number(item.taxInvalue)
          this.parameter.contractAmount = sum.toFixed(2)
        }
        if (item.isTax == '2') {
          item.contrPrice = item.winBidPrice
          item.contrAmount = item.winBidAmount
          item.taxInprice = (
            item.winBidPrice *
            (item.taxRate / 100 + 1)
          ).toFixed(4)
          item.taxInvalue = (item.taxInprice * item.matNum).toFixed(2)
          item.taxValue = (
            (item.taxInprice * item.matNum * (item.taxRate / 100)) /
            (1 + item.taxRate / 100)
          ).toFixed(2)
          sum += Number(item.taxInvalue)
          this.parameter.contractAmount = sum.toFixed(2)
        }
        item.matCompanyCode = this.parameter.matCompanyCode
        item.matCompanyId = this.parameter.matCompanyId
        item.matCompanyName = this.parameter.matCompanyName
        item.winNoticeDetailId = item.id
        item.num = item.matNum
        item.vendorNote  = item.tenderExplain
        delete item.id
      })

      this.parameter.itemList = this.parameter.detail
    },
  },
  computed: {
    ...mapState({
      headers: (state) => {
        return {Authorization: `Bearer ${state.login.currentUser.token}`}
      },
      action: function () {
        return window.context.portal + '/biz/universal/v1/minioUpload'
      },
    }),
  },
}
</script>

<style lang="scss" scoped>
.auto {
  display: none;
}
.classNone {
  display: block;
}
.rows:first-of-type {
  cursor: pointer;
}
.rows {
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ebebeb;
  margin-bottom: 16px;
  padding: 10px 0;
  .rows-left {
    margin-right: 10px;
    .rows-hr {
      width: 3px;
      height: 18px;
      background-color: #0e6eff;
      border-radius: 2px;
    }
  }
  .rows-title {
    .text-overflow {
      width: 100%;
      font-size: 16px;
    }
  }
}
</style>
