<!--
  @description:招标项目/采购项目-合同签订
  @Modifier:zyy
  @routerPath:
  @date:2021/4/21-17:52
-->
<template>
  <div>
    <section class="title p0-16 clearfix">
      <template>
        <div class="title-back pointer" @click="back">
          <i class="iconfont icon-fanhui mr10" />协议执行
        </div>
      </template>
    </section>
    <div class="p0-16">
      <el-form
        ref="form"
        :model="formData"
        :rules="rules"
        :inline-message="true"
      >
        <table class="tableData from_table">
          <tr>
            <td colspan="4" style="background: #fff;border:none">
              <div class="page-title" style="display: inline-block">
                1.协议信息
              </div>
              <el-button style="margin-left:16px;" @click="addProjectInfo">
                添加
              </el-button>
            </td>
          </tr>
          <tr>
            <td>
              <el-form-item
                ref="focusContractNo"
                show-word-limit
                label="协议编号"
                prop="focusContractNo"
              />
            </td>
            <td>
              <gx-input v-model.trim="formData.focusContractNo" placeholder="协议编号" disabled />
            </td>
            <td>
              <el-form-item
                ref="focusContractName"
                show-word-limit
                label="协议名称"
                prop="focusContractName"
              />
            </td>
            <td>
              <gx-input v-model.trim="formData.focusContractName" placeholder="协议名称" disabled />
            </td>
          </tr>
          <tr>
            <td colspan="4" style="background: #fff;border:none">
              <div class="page-title">
                2.合同基本信息
              </div>
            </td>
          </tr>
          <tr>
            <td>
              <el-form-item
                ref="contractNo"
                show-word-limit
                label="合同编号"
                prop="contractNo"
              />
            </td>
            <td>
              <gx-input v-model.trim="formData.contractNo" gx-maxlength="varchar100" placeholder="合同编号" />
            </td>
            <td>
              <el-form-item
                ref="contractName"
                show-word-limit
                label="合同名称"
                prop="contractName"
              />
            </td>
            <td>
              <gx-input v-model.trim="formData.contractName" gx-maxlength="varchar100" placeholder="合同名称" />
            </td>
          </tr>
          <tr>
            <td>
              <el-form-item
                ref="contractTotalMoney"
                show-word-limit
                label="合同金额(元)"
                prop="contractTotalMoney"
              />
            </td>
            <td>
              <!--金额组件-->
              <money
                :thousands="true"
                :money-value="formData"
                :unit-stust="true"
                :great="true"
                name="contractTotalMoney"
              />
            </td>
            <td />
            <td />
          </tr>
          <tr>
            <td colspan="4" style="background: #fff;border:none">
              <div class="page-title">
                3.合同供方
              </div>
            </td>
          </tr>
          <template v-for="(item, index) in formData.supplyContractPartyVo">
            <tr :key="index+'departName'">
              <td>
                <el-form-item
                  show-word-limit
                  label="单位名称"
                  :prop="'supplyContractPartyVo.'+ index +'.departName'"
                  :rules="rules.departName"
                />
              </td>
              <td>
                <gx-input v-model.trim="item.departName" gx-maxlength="varchar200" placeholder="单位名称" disabled />
              </td>
              <td>
                <el-form-item
                  show-word-limit
                  label="社会统一信用代码"
                  :prop="'supplyContractPartyVo.'+ index +'.unified'"
                  :rules="rules.unified"
                />
              </td>
              <td>
                <gx-input v-model.trim="item.unified" gx-maxlength="varchar200" placeholder="社会统一信用代码" disabled />
              </td>
            </tr>
            <tr :key="index+'corporation'">
              <td>
                <el-form-item
                  show-word-limit
                  label="法定代表人"
                  :prop="'supplyContractPartyVo.'+ index +'.corporation'"
                  :rules="rules.corporation"
                />
              </td>
              <td>
                <gx-input v-model.trim="item.corporation" gx-maxlength="varchar50" placeholder="法定代表人" />
              </td>
              <td>
                <el-form-item
                  show-word-limit
                  label="税号"
                  :prop="'supplyContractPartyVo.'+ index +'.taxNo'"
                  :rules="rules.taxNo"
                />
              </td>
              <td>
                <gx-input v-model.trim="item.taxNo" gx-maxlength="varchar200" placeholder="税号" />
              </td>
            </tr>
            <tr :key="index+'bankName'">
              <td>
                <el-form-item
                  show-word-limit
                  label="开户行"
                  :prop="'supplyContractPartyVo.'+ index +'.bankName'"
                  :rules="rules.bankName"
                />
              </td>
              <td>
                <gx-input v-model.trim="item.bankName" gx-maxlength="varchar200" placeholder="开户行" />
              </td>
              <td>
                <el-form-item
                  show-word-limit
                  label="账号"
                  :prop="'supplyContractPartyVo.'+ index +'.bankNo'"
                  :rules="rules.bankNo"
                />
              </td>
              <td>
                <gx-input v-model.trim="item.bankNo" gx-maxlength="varchar100" placeholder="账号" />
              </td>
            </tr>
            <tr :key="index+'address'">
              <td>
                <el-form-item
                  show-word-limit
                  label="单位地址"
                  :prop="'supplyContractPartyVo.'+ index +'.address'"
                  :rules="rules.address"
                />
              </td>
              <td colspan="3">
                <gx-input v-model.trim="item.address" gx-maxlength="varchar200" placeholder="单位地址" />
              </td>
            </tr>
            <tr :key="index+'agent'">
              <td>
                <el-form-item
                  show-word-limit
                  label="委托代理人"
                  :prop="'supplyContractPartyVo.'+ index +'.agent'"
                  :rules="rules.agent"
                />
              </td>
              <td>
                <gx-input v-model.trim="item.agent" gx-maxlength="varchar200" placeholder="委托代理人" />
              </td>
              <td>
                <el-form-item
                  show-word-limit
                  label="电话"
                  :prop="'supplyContractPartyVo.'+ index +'.tel'"
                  :rules="rules.tel"
                />
              </td>
              <td>
                <gx-input v-model.trim="item.tel" gx-maxlength="varchar20" placeholder="电话" />
              </td>
            </tr>
            <tr :key="index+'bankNo'">
              <td>
                <el-form-item
                  show-word-limit
                  label="传真"
                  :prop="'supplyContractPartyVo.'+ index +'.fax'"
                  :rules="rules.fax"
                />
              </td>
              <td>
                <gx-input v-model.trim="item.fax" gx-maxlength="varchar200" placeholder="传真" />
              </td>
              <td>
                <el-form-item
                  show-word-limit
                  label="邮箱"
                  :prop="'supplyContractPartyVo.'+ index +'.mail'"
                  :rules="rules.mail"
                />
              </td>
              <td>
                <gx-input v-model.trim="item.mail" gx-maxlength="varchar100" placeholder="邮箱" />
              </td>
            </tr>
          </template>
          <tr>
            <td colspan="4" style="background: #fff;border:none">
              <div class="page-title">
                4.合同需方
              </div>
            </td>
          </tr>
          <template v-for="(item, index) in formData.demandContractPartyVo">
            <tr :key="index+'demandDepartName'">
              <td>
                <el-form-item
                  show-word-limit
                  label="单位名称"
                  :prop="'demandContractPartyVo.'+ index +'.departName'"
                  :rules="rules.departName"
                />
              </td>
              <td>
                <gx-input
                  v-model="item.departName"
                  placeholder="请选择"
                  gx-maxlength="varchar200"
                  style="width:calc(100% - 180px)"
                  disabled
                />
                <el-button type="primary" @click="organSelect">
                  选择
                </el-button>
                <el-button @click="organTree('clear')">
                  清空
                </el-button>
              </td>
              <td>
                <el-form-item
                  show-word-limit
                  label="社会统一信用代码"
                  :prop="'demandContractPartyVo.'+ index +'.unified'"
                  :rules="rules.unified"
                />
              </td>
              <td>
                <gx-input v-model.trim="item.unified" gx-maxlength="varchar200" placeholder="社会统一信用代码" />
              </td>
            </tr>
            <tr :key="index+'demandCorporation'">
              <td>
                <el-form-item
                  show-word-limit
                  label="法定代表人"
                  :prop="'demandContractPartyVo.'+ index +'.corporation'"
                  :rules="rules.corporation"
                />
              </td>
              <td>
                <gx-input v-model.trim="item.corporation" gx-maxlength="varchar50" placeholder="法人" />
              </td>
              <td>
                <el-form-item
                  show-word-limit
                  label="税号"
                  :prop="'demandContractPartyVo.'+ index +'.taxNo'"
                  :rules="rules.taxNo"
                />
              </td>
              <td>
                <gx-input v-model.trim="item.taxNo" gx-maxlength="varchar200" placeholder="税号" />
              </td>
            </tr>
            <tr :key="index+'demandBankName'">
              <td>
                <el-form-item
                  show-word-limit
                  label="开户行"
                  :prop="'demandContractPartyVo.'+ index +'.bankName'"
                  :rules="rules.bankName"
                />
              </td>
              <td>
                <gx-input v-model.trim="item.bankName" gx-maxlength="varchar200" placeholder="开户行" />
              </td>
              <td>
                <el-form-item
                  show-word-limit
                  label="账号"
                  :prop="'demandContractPartyVo.'+ index +'.bankNo'"
                  :rules="rules.bankNo"
                />
              </td>
              <td>
                <gx-input v-model.trim="item.bankNo" gx-maxlength="varchar100" placeholder="账号" />
              </td>
            </tr>
            <tr :key="index+'demandAddress'">
              <td>
                <el-form-item
                  show-word-limit
                  label="单位地址"
                  :prop="'demandContractPartyVo.'+ index +'.address'"
                  :rules="rules.address"
                />
              </td>
              <td colspan="3">
                <gx-input v-model.trim="item.address" gx-maxlength="varchar200" placeholder="单位地址" />
              </td>
            </tr>
            <tr :key="index+'demandAgent'">
              <td>
                <el-form-item
                  show-word-limit
                  label="委托代理人"
                  :prop="'demandContractPartyVo.'+ index +'.agent'"
                  :rules="rules.agent"
                />
              </td>
              <td>
                <gx-input v-model.trim="item.agent" gx-maxlength="varchar200" placeholder="委托代理人" />
              </td>
              <td>
                <el-form-item
                  show-word-limit
                  label="电话"
                  :prop="'demandContractPartyVo.'+ index +'.tel'"
                  :rules="rules.tel"
                />
              </td>
              <td>
                <gx-input v-model.trim="item.tel" gx-maxlength="varchar20" placeholder="电话" />
              </td>
            </tr>
            <tr :key="index+'demandBankNo'">
              <td>
                <el-form-item
                  show-word-limit
                  label="传真"
                  :prop="'demandContractPartyVo.'+ index +'.fax'"
                  :rules="rules.fax"
                />
              </td>
              <td>
                <gx-input v-model.trim="item.fax" gx-maxlength="varchar200" placeholder="传真" />
              </td>
              <td>
                <el-form-item
                  ref="contractPartyVo.supplyMail"
                  show-word-limit
                  label="邮箱"
                  :prop="'demandContractPartyVo.'+ index +'.mail'"
                  :rules="rules.mail"
                />
              </td>
              <td>
                <gx-input v-model.trim="item.mail" gx-maxlength="varchar100" placeholder="邮箱" />
              </td>
            </tr>
          </template>
        </table>
        <div class="page-title" style="margin-top: 10px;margin-bottom: 10px">
          5.合同标的物
        </div>
        <el-table :data="formData.detailList">
          <el-table-column type="index" label="序号" min-width="60" align="center" />
          <el-table-column header-align="center" label="产品名称" min-width="180" prop="productName" align="center" />
          <el-table-column header-align="center" label="物资规格" min-width="180" prop="productSpecs" align="center" />
          <el-table-column header-align="center" label="单位" prop="unit" align="center">
            <template slot-scope="scope">
              <span>{{ filterDictText(purchaseUnitList,scope.row.unit ) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            header-align="center"
            align="center"
            width="180"
            prop="quantity"
            label="数量"
          >
            <template #header>
              <span class="procurement_orange-color">*</span>
              <span>数量</span>
            </template>
            <template slot-scope="scope">
              <el-form-item
                :prop="'detailList.'+ scope.$index +'.quantity'"
                :rules="[
                  { required: true, message: '请输入', trigger: 'blur' }
                ]"
                style="margin-bottom: 0"
              >
                <el-input v-model="scope.row.quantity" @input="getSummaries()" />
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column :key="formData.contractSource !='01' ? 'payP1' : 'payP2' " header-align="center" width="220px" label="单价（元）">
            <template #header>
              <span class="procurement_orange-color">*</span>
              <span>单价（元）</span>
            </template>
            <template slot-scope="scope">
              <el-form-item
                :prop="'detailList.'+ scope.$index +'.price'"
                :rules="[{ required: true, message: '请输入', trigger: 'blur' }]"
                style="margin-bottom: 0"
              >
                <!--招标可编辑-->
                <money v-if="formData.contractSource ==='01'" key="price" :money-value="scope.row" name="price" @input="getSummaries()" />
                <span v-else>{{ scope.row.price?moneyFormat(parseFloat(scope.row.price).toFixed(2)):scope.row.price }}</span>
              </el-form-item>
            </template>
          </el-table-column>
          <!-- <el-table-column header-align="center" label="单价（元）" prop="price" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.price?moneyFormat(parseFloat(scope.row.price).toFixed(2)):scope.row.price }}</span>
            </template>
          </el-table-column>-->
          <el-table-column header-align="center" label="总价（元）" prop="totalPrice" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.totalPrice?moneyFormat(parseFloat(scope.row.totalPrice).toFixed(2)):scope.row.totalPrice }}</span>
            </template>
          </el-table-column>
          <el-table-column
            header-align="center"
            align="left"
            prop="offerRemarks"
            label="备注"
            min-width="180"
          />
          <el-table-column
            align="center"
            width="70"
            label="操作"
          >
            <template slot-scope="scope">
              <el-link
                type="primary"
                @click="deleteRow(scope.$index)"
              >
                [移除]
              </el-link>
            </template>
          </el-table-column>
        </el-table>
        <div class="page-title" style="margin-top: 10px;margin-bottom: 10px">
          6.月度计划
          <el-button style="margin-left:16px;" @click="addPlan">
            添加
          </el-button>
        </div>
        <el-table ref="detailPurchasePlanList" :data="formData.detailPurchasePlanList" @selection-change="selectionChange">
          <el-table-column align="center" type="selection" width="55" />
          <el-table-column type="index" label="序号" min-width="60" align="center" />
          <el-table-column header-align="center" label="产品名称" min-width="180" prop="productName" align="center" />
          <el-table-column header-align="center" label="单位" prop="unit" align="center">
            <template slot-scope="scope">
              <span>{{ filterDictText(purchaseUnitList,scope.row.unit ) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            header-align="center"
            align="center"
            width="180"
            prop="quantity"
            label="数量"
          />
          <el-table-column header-align="center" label="预算单价（元）" prop="price" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.price?moneyFormat(parseFloat(scope.row.price).toFixed(2)):scope.row.price }}</span>
            </template>
          </el-table-column>
          <el-table-column header-align="center" label="预算总价（元）" prop="totalPrice" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.totalPrice?moneyFormat(parseFloat(scope.row.totalPrice).toFixed(2)):scope.row.totalPrice }}</span>
            </template>
          </el-table-column>
          <el-table-column
            header-align="center"
            align="left"
            prop="offerRemarks"
            label="备注"
            min-width="180"
          />
        </el-table>
      </el-form>
    </div>
    <div>
      <div class="but_footer">
        <div class="but_footer-h">
          <div class="but_footer-but">
            <el-button :loading="confirmLoading" @click="saveData('save')">
              暂存，进入下一步
            </el-button>
            <!--<el-button type="primary" :loading="confirmLoading" @click="submitData('form')">
              提交
            </el-button>-->
          </div>
        </div>
      </div>
    </div>
    <!-- 项目信息-选择弹框 -->
    <purchaseAgreementDialog
      ref="purchaseAgreementDialog"
      @dialogDataInfo="dialogDataInfo"
    />
    <purchasePlanDialog
      ref="purchasePlanDialog"
      @dialogDataInfo2="dialogDataInfo2"
    />
    <!-- 所属机构弹框 -->
    <organTree-dialog
      ref="organTree"
      :single-choice="true"
      @call-back="organTree"
    />
  </div>
</template>
<script>
/* 接口*/
import { getBidtCalibrationTenderContractData, saveBidtCalibrationTenderContractData } from '@/api/contractManagement/index'// 招标项目-编辑初始化数据 招标项目-签订保存
/* 公用方法*/
import { moneyFormat, filterDictText } from '@assets/js/utils'// 常用方法
import { validateInput, characters1_50, checkLinktel, email, bankCardNumber } from '@/assets/js/validate'// 校验输入内容
// import { validFn } from '@assets/js/utils'// 常用方法
/* 混合开发方法-参数字段*/
// import { initGroupMixin } from '@/mixins/initGroupMixin'
import { procurementGroupMixins } from '@/views/procurement/mixins/procurementGrounpMixins'
/* 组件*/
import purchaseAgreementDialog from './purchaseAgreement-dialog' // 协议信息-选择弹框
import purchasePlanDialog from './purchasePlan-dialog' // 月度计划-选择弹框
import organTreeDialog from '@/views/common/user/organizationTree/organTree-dialog' // 组织机构选择
import money from '@/components/common/money/money.vue'
export default {
  name: 'ContractSigning',
  components: {
    money,
    purchaseAgreementDialog, // 协议信息-选择弹框
    purchasePlanDialog, // 月度计划
    'organTree-dialog': organTreeDialog // 组织机构选择
  },
  mixins: [procurementGroupMixins],
  data() {
    return {
      summaries: 0, // 总额
      confirmLoading: false,
      contractSource: '', // 合同来源00采购01招标
      purchaseUnitList: [],
      moneyFormat: moneyFormat, // 千分位转换
      id: '', // 合同详情id
      selectionList: [], // 选中的月度计划
      formData: {
        contractSource: '', // 合同来源00采购01招标
        isFocus: '00', // 00 采购合同，01框架合同
        noticeId: '', // 通知书id
        focusContractName: '', // 协议名称
        focusContractNo: '', // 协议编号
        tenderId: '', // 项目id
        focusContractId: '', // 协议采购的id
        contractNo: '', // 合同编号
        contractName: '', // 合同名称
        contractTotalMoney: '', // 合同金额
        // 供方
        supplyContractPartyVo: [
          {
            departName: '', // 中标人名称/供方单位
            departId: '', // 供方人id
            unified: '', // 供方统一社会信用代码
            tel: '', // 供方电话
            corporation: '', // 供方法人
            bankName: '', // 供方开户行
            mail: '', // 供方邮箱
            address: '', // 供方地址
            agent: '', // 供方代理人
            fax: '', // 供方传真
            bankNo: '', // 供方银行账号
            taxNo: '' // 供方税号
          }
        ],
        // 需方
        demandContractPartyVo: [
          {
            departId: '', // 需方机构id
            departName: '', // 需方机构名称
            unified: '', // 需方统一社会信用代码
            corporation: '', // 需方法人
            tel: '', // 需方电话
            bankName: '', // 需方开户行
            mail: '', // 需方邮箱
            address: '', // 需方地址
            agent: '', // 需方代理人
            fax: '', // 需方传真
            bankNo: '', // 需方银行账号
            taxNo: '' // 需方税号
          }
        ],
        detailList: [], // 合同标的
        detailPurchasePlanList: [] // 月度计划
      },
      rules: {
        deliveryTime: [{ required: true, message: '不能为空', trigger: 'change' }], // 供货期限
        deliveryAddress: [{ required: true, message: '不能为空', trigger: 'blur' }], // 供货期限
        contractName: [{ required: true, message: '不能为空', trigger: 'blur' }], // 项目编号
        focusContractName: [{ required: true, validator: validateInput, trigger: 'blur' }], // 协议名称
        focusContractNo: [{ required: true, validator: validateInput, trigger: 'blur' }], // 协议编号
        packNo: [{ required: true, validator: validateInput, trigger: 'blur' }], // 标段编号
        packName: [{ required: true, validator: validateInput, trigger: 'blur' }], // 标段名称
        contractNo: [{ required: true, validator: validateInput, trigger: 'blur' }], // 合同编号
        // contractTotalMoney: [{ required: true, message: '请填写金额', trigger: 'blur' }], // 金额
        'departName': [{ required: true, validator: validateInput, trigger: 'blur' }], // 中标人名称
        'unified': [{ required: true, validator: validateInput, trigger: 'blur' }], // 统一社会信用代码
        'tel': [{ required: true, validator: characters1_50, trigger: 'blur' }], // 法人
        'corporation': [{ required: true, validator: checkLinktel, trigger: 'blur' }], // 电话
        'bankName': [{ required: true, validator: validateInput, trigger: 'blur' }], // 开户行
        'mail': [{ required: true, validator: email, trigger: 'blur' }], // 邮箱
        'address': [{ required: true, validator: validateInput, trigger: 'blur' }], // 地址
        'agent': [{ required: true, validator: validateInput, trigger: 'blur' }], // 代理人
        // 'fax': [{ required: true, validator: validateInput, trigger: 'blur' }], // 传真
        'bankNo': [{ required: true, validator: bankCardNumber, trigger: 'blur' }], // 银行账号
        'taxNo': [{ required: true, validator: validateInput, trigger: 'blur' }], // 税号
        'departId': [{ required: true, validator: validateInput, trigger: 'blur' }] // 机构id
      }
    }
  },
  created() {
    // 字典信息
    this.initGroup()
    if (this.$route.params.id) { // 编辑
      this.id = this.$route.params.id// 合同Id
      this.initData(this.$route.params.id)
    }
  },
  methods: {
    filterDictText,
    // 获取字典方法
    initGroup() {
      const data = ['purchaseUnit']
      this.$store.dispatch('getGroupType', data).then(res => {
        this.purchaseUnitList = res.bidTypeData.purchaseUnitList
      })
    },
    // 编辑初始化数据
    initData(id) {
      const data = {
        id: id
      }
      getBidtCalibrationTenderContractData(data).then(response => {
        this.formData = response.data
        this.$nextTick(() => {
          for (let i = 0; i < this.formData.detailPurchasePlanList.length; i++) {
            this.$refs.detailPurchasePlanList.toggleRowSelection(this.formData.detailPurchasePlanList[i], true)
          }
        })
      }).catch(() => {
        this.confirmLoading = false
      })
    },
    // 处理时间
    handleTime(val) {
    },
    // 返回
    back() {
      this.$router.push({
        path: '/contractManagement/purchaseAgreement-list'
      })
    },
    // 关联项目
    addProjectInfo() {
      this.$refs.purchaseAgreementDialog.openProjectInfoDialog()
    },
    addPlan() {
      this.$refs.purchasePlanDialog.openProjectInfoDialog()
    },
    // 关联项目信息
    dialogDataInfo(data) {
      this.formData.noticeId = data.noticeId// 通知书id
      this.formData.contractSource = data.contractSource // 合同来源00采购01招标
      this.formData.focusContractName = data.focusContractName // 协议名称
      this.formData.focusContractNo = data.focusContractNo // 协议编号
      this.formData.tenderId = data.id // 项目id
      this.formData.focusContractId = data.focusContractId // 协议采购的合同id
      // this.formData.contractTotalMoney = data.contractMoney // 合同金额
      this.formData.supplyContractPartyVo[0].departId = data.supplyContractPartyVo[0].departId// 供方机构id
      this.formData.supplyContractPartyVo[0].departName = data.supplyContractPartyVo[0].departName// 供方单位/中标人名称
      this.formData.supplyContractPartyVo[0].unified = data.supplyContractPartyVo[0].unified // 供方统一社会信用代码
      this.formData.supplyContractPartyVo[0].corporation = data.supplyContractPartyVo[0].corporation // 供方法人
      this.formData.supplyContractPartyVo[0].bankName = data.supplyContractPartyVo[0].bankName // 供方开户行
      this.formData.supplyContractPartyVo[0].tel = data.supplyContractPartyVo[0].tel // 供方电话
      this.formData.supplyContractPartyVo[0].mail = data.supplyContractPartyVo[0].mail // 供方邮箱
      this.formData.supplyContractPartyVo[0].address = data.supplyContractPartyVo[0].address // 供方地址
      this.formData.supplyContractPartyVo[0].agent = data.supplyContractPartyVo[0].agent // 供方代理人
      this.formData.supplyContractPartyVo[0].fax = data.supplyContractPartyVo[0].fax // 供方传真
      this.formData.supplyContractPartyVo[0].bankNo = data.supplyContractPartyVo[0].bankNo // 供方账号
      this.formData.supplyContractPartyVo[0].taxNo = data.supplyContractPartyVo[0].taxNo // 供方税号
      this.formData.demandContractPartyVo[0].departName = data.demandContractPartyVo[0].departName // 需方机构名称
      this.formData.demandContractPartyVo[0].departId = data.demandContractPartyVo[0].departId // 需方机构id
      this.formData.demandContractPartyVo[0].unified = data.demandContractPartyVo[0].unified // 需方统一社会信用代码
      this.formData.detailList = data.detailList // 协议标的物
    },
    dialogDataInfo2(data) {
      this.formData.detailPurchasePlanList = data.planDetailList // 月度计划
    },
    // 选择机构
    organSelect() {
      this.$refs.organTree.open({
        title: '选择机构', // 弹框标题
        dialog: true, // 弹窗状态
        selectCodes: this.formData.demandContractPartyVo[0].departId && this.formData.demandContractPartyVo[0].departId.split(',')
      })
    },
    // 选择/清空机构
    organTree(data) {
      // 清空
      if (data === 'clear') {
        this.formData.demandContractPartyVo[0].departId = '' // 需方机构id
        this.formData.demandContractPartyVo[0].departName = '' // 需方机构名称
      } else {
        // 选择
        this.$set(this.formData.demandContractPartyVo[0], 'departName', data.labels)
        this.$set(this.formData.demandContractPartyVo[0], 'departId', data.keys)
        this.$set(this.formData.demandContractPartyVo[0], 'unified', data.orgCodes)
      }
    },
    deleteRow(index) {
      this.$confirm('确认移除此行?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn',
        type: 'warning'
      }).then(() => {
        this.formData.detailList.splice(index, 1)
      }).catch(() => {
      })
    },
    // 金额总计
    getSummaries() {
      let contractBudget = 0
      this.formData.detailList.map(item => {
        if (item.quantity && item.price) {
          item.totalPrice = Number(item.price * item.quantity).toFixed(2)
        } else {
          item.totalPrice = 0
        }
        contractBudget += Number(item.totalPrice)
      })
      this.summaries = contractBudget.toFixed(2)
      this.formData.contractTotalMoney = this.summaries
    },
    selectionChange(list) {
      this.selectionList = list
    },
    // 保存
    saveData(type) {
      this.confirmLoading = true
      if (type === 'save') {
        this.status = '00'
      } else {
        this.status = '01'
      }
      const data = {
        ...this.formData,
        contractTypeNo: '02', // 协议采购
        contractTypeName: '协议采购', // 协议采购
        useStatus: this.status // 00保存，01提交
      }
      data.detailPurchasePlanList = this.selectionList
      this.$refs.form.validate(valid => {
        if (valid) {
          saveBidtCalibrationTenderContractData(data).then(response => {
            this.confirmLoading = false
            this.$message({
              type: 'success',
              message: '保存成功'
            })
            this.$router.push({
              path: '/contractManagement/purchaseAgreementToolMake',
              query: {
                id: response.data // 合同id
              }
            })
          }).catch(() => {
            this.confirmLoading = false
          })
        } else {
          this.confirmLoading = false
          return false
        }
      })
    },
    // 提交
    submitData(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.saveData()
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>

</style>
