<template>
  <div class="purchase-contract-review-content">
    <el-form ref="ruleForm" :key="key" :model="ruleForm" :rules="rules" label-width="150px" label-position="top" class="rule-form-orange">
      <!-- <cy-info-title style="margin-top: 10px;">审批结论</cy-info-title>
      <el-form-item label="审批结论" prop="purchContractApproval.approvalConclusion">
        <el-radio-group v-model="ruleForm.purchContractApproval.approvalConclusion" :disabled="isView">
          <el-radio label="pass">通过</el-radio>
          <el-radio label="back">退回</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        label="审批意见："
        style="width: 100%;"
        class="textarea-content"
        prop="purchContractApproval.approvalComment"
        :rules="[
          { required: ruleForm.purchContractApproval.approvalConclusion === 'back', message: '请输入审批意见', trigger: 'blur' }
        ]"
      >
        <el-input v-model="ruleForm.purchContractApproval.approvalComment" type="textarea" style="width: 100%;" rows="3" clearable :disabled="isView"></el-input>
      </el-form-item> -->

      <cy-info-title>补充信息</cy-info-title>
      <el-form-item label="合同名称">
        <el-input v-model="ruleForm.purchContractReplenishInfo.contractName" style="width: 100%;" disabled></el-input>
      </el-form-item>
      <el-form-item label="合同编号">
        <el-input v-model="ruleForm.purchContractReplenishInfo.contractNumber" style="width: 100%;" disabled></el-input>
      </el-form-item>
      <el-form-item label="工程量类别" prop="purchContractReplenishInfo.projectAmountCategory">
        <el-radio-group v-model="ruleForm.purchContractReplenishInfo.projectAmountCategory" :disabled="isView">
          <el-radio label="purchProjectAmountCategory_estimateAmount">概算量</el-radio>
          <el-radio label="purchProjectAmountCategory_fixedAmount">固定量</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="货物单价结算方式" prop="purchContractReplenishInfo.goodsPriceSettleMethod">
        <el-input v-model="ruleForm.purchContractReplenishInfo.goodsPriceSettleMethod" placeholder="请输入货物单价结算方式" style="width: 100%;" clearable :disabled="isView"></el-input>
      </el-form-item>

      <cy-info-title style="margin-top: 10px;">质量与验收</cy-info-title>
      <el-form-item label="质量标准" prop="purchContractQualityAccept.qualityCriteria" style="width: 100%;">
        <el-input v-model="ruleForm.purchContractQualityAccept.qualityCriteria" placeholder="请输入质量标准" type="textarea" rows="3" style="width: 100%;" clearable :disabled="isView"></el-input>
      </el-form-item>
      <el-form-item label="验收标准" prop="purchContractQualityAccept.acceptCriteria" style="width: 100%;">
        <el-input v-model="ruleForm.purchContractQualityAccept.acceptCriteria" placeholder="请输入验收标准" type="textarea" rows="3" style="width: 100%;" clearable :disabled="isView"></el-input>
      </el-form-item>

      <cy-info-title style="margin-top: 10px;">结算与付款</cy-info-title>
      <el-form-item label="结算方式" prop="approvalSettlePayment.settleMethod">
        <el-select v-model="ruleForm.approvalSettlePayment.settleMethod" :disabled="isView"
          placeholder="请选择结算方式" clearable style="width: 100%"
        >
          <el-option
            v-for="item in payMethodList"
            :key="item.code"
            :label="item.name"
            :value="item.code"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="付款时间及方式" prop="approvalSettlePayment.paymentMethod">
        <el-input v-model="ruleForm.approvalSettlePayment.paymentMethod" placeholder="请输入付款时间及方式" style="width: 100%;" clearable :disabled="isView"></el-input>
      </el-form-item>

      <cy-info-title style="margin-top: 10px;">交货规定</cy-info-title>
      <el-form-item label="交货地点" prop="purchContractDeliverClause.deliverSite">
        <el-input v-model="ruleForm.purchContractDeliverClause.deliverSite" placeholder="请输入交货地点" style="width: 100%;" clearable :disabled="isView"></el-input>
      </el-form-item>
      <el-form-item label="交货方式" prop="purchContractDeliverClause.deliverMethod">
        <el-input v-model="ruleForm.purchContractDeliverClause.deliverMethod" placeholder="请输入交货方式" style="width: 100%;" clearable :disabled="isView"></el-input>
      </el-form-item>
      <el-form-item label="交货日期" prop="purchContractDeliverClause.deliverDate">
        <el-input v-model="ruleForm.purchContractDeliverClause.deliverDate" placeholder="请输入交货日期" style="width: 100%;" clearable :disabled="isView"></el-input>
      </el-form-item>
      <el-form-item label="运费及保险费" prop="purchContractDeliverClause.transportPremium">
        <el-input v-model="ruleForm.purchContractDeliverClause.transportPremium" placeholder="请输入运费及保险费" style="width: 100%;" clearable :disabled="isView"></el-input>
      </el-form-item>

      <cy-info-title style="margin-top: 10px;">结算</cy-info-title>
      <el-form-item label="结算周期方式" prop="approvalSettleCycle.settleCycleMethod">
        <el-radio-group v-model="ruleForm.approvalSettleCycle.settleCycleMethod" :disabled="isView">
          <!-- <el-radio label="salesContractSettleCycleMethod_fixed">固定天数</el-radio>
          <el-radio label="salesContractSettleCycleMethod_natural">自然月</el-radio> -->
          <el-radio v-for="item in settleMethodList" :key="item.id" :label="item.code">{{ item.name }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="结算周期" prop="approvalSettleCycle.settleCycle" class="two-form-item">
        上一次结算后
        <el-input v-model="ruleForm.approvalSettleCycle.settleCycle" style="width: 100px;"  class="two-form-item" :disabled="isView" />
        {{ ruleForm.approvalSettleCycle.settleCycleMethod == 'salesContractSettleCycleMethod_fixed' ? '天' : '个自然月' }}
      </el-form-item>
      
      <cy-info-title style="margin-top: 10px;">需方收货人
        <span v-if="!isView" style="position: absolute; right: 10px; top: 0; font-weight: 400; color: #5ac4fc; cursor: pointer;" @click="ruleForm.userList.push({ name: '', idCard: '', mobile: '', id: '' })">新增</span>
      </cy-info-title>
      <div style="width: 100%;">
        <el-table
          :data="ruleForm.userList"
          style="width: 100%">
          <el-table-column label=" 序号" type="index"></el-table-column>
          <el-table-column prop="name" label="姓名">
            <template slot-scope="scope">
              <el-form-item label="" :prop="`userList.${scope.$index}.name`" style="width: 100%; margin-bottom: 15px;"
                :rules="[{ required: true, message: '请输入姓名', trigger: 'blur' }]"
              >
                <el-input type="textarea" autosize placeholder="请输入姓名" v-model="ruleForm.userList[scope.$index].name" style="width: 100%;" :disabled="isView" />
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column prop="idCard" label="身份证号">
            <template slot-scope="scope">
              <el-form-item label="" :prop="`userList.${scope.$index}.idCard`" style="width: 100%; margin-bottom: 15px;"
                :rules="[{ required: true, message: '请输入身份证号', trigger: 'blur' }]"
              >
                <el-input type="textarea" autosize placeholder="请输入身份证号" v-model="ruleForm.userList[scope.$index].idCard" style="width: 100%;" :disabled="isView" />
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column prop="content" label="联系方式">
            <template slot-scope="scope">
              <el-form-item label="" :prop="`userList.${scope.$index}.mobile`" style="width: 100%; margin-bottom: 15px;"
                :rules="[{ required: true, message: '请输入联系方式', trigger: 'blur' }]"
              >
                <el-input type="textarea" autosize placeholder="请输入联系方式" v-model="ruleForm.userList[scope.$index].mobile" style="width: 100%;" :disabled="isView" />
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="操作" width="200" v-if="!isView">
            <template slot-scope="scope">
              <el-button type="text" @click="ruleForm.userList.push({content: ''})">增加</el-button>
              <el-button type="text" @click="deletePerson(scope.$index)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      
      <cy-info-title style="margin-top: 10px;">增值税要求</cy-info-title>
      <el-form-item label="发票类型" prop="approvalVatInfo.receiptType">
        <el-select v-model="ruleForm.approvalVatInfo.receiptType" placeholder="请选择发票类型" clearable style="width: 100%" :disabled="isView">
          <el-option label="普通发票" value="invoice_type_general"></el-option>
          <el-option label="增值税专用发票" value="invoice_type_special"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="税率" prop="approvalVatInfo.taxRate">
        <el-input v-model="ruleForm.approvalVatInfo.taxRate" placeholder="请输入税率" style="width: 100%;" clearable :disabled="isView">
          <template slot="append">%</template>
        </el-input>
      </el-form-item>
      <el-form-item label="发票开户行" prop="approvalVatInfo.openBank">
        <el-input v-model="ruleForm.approvalVatInfo.openBank" placeholder="请输入发票开户行" style="width: 100%;" clearable :disabled="isView"></el-input>
      </el-form-item>
      <el-form-item label="发票账号" prop="approvalVatInfo.receiptAccount">
        <el-input v-model="ruleForm.approvalVatInfo.receiptAccount" placeholder="请输入发票账号" style="width: 100%;" clearable :disabled="isView"></el-input>
      </el-form-item>
      <el-form-item label="发票备注" prop="approvalVatInfo.receiptRemark">
        <el-input v-model="ruleForm.approvalVatInfo.receiptRemark" placeholder="请输入发票备注" style="width: 100%;" clearable :disabled="isView"></el-input>
      </el-form-item>

      <cy-info-title style="margin-top: 10px;">其他条款
        <span v-if="!isView" style="position: absolute; right: 10px; top: 0; font-weight: 400; color: #5ac4fc; cursor: pointer;" @click="ruleForm.purchContractOtherClause.push({ paymentCondition: '' })">新增</span>
      </cy-info-title>
      <div style="width: 100%;">
        <el-table
          :data="ruleForm.purchContractOtherClause"
          style="width: 100%">
          <el-table-column label=" 序号" type="index"></el-table-column>
          <el-table-column prop="paymentCondition" label="姓名">
            <template slot-scope="scope">
              <el-form-item label="" :prop="`purchContractOtherClause.${scope.$index}.paymentCondition`" style="width: 100%; margin-bottom: 15px;"
                :rules="[{ required: true, message: '请输入条件', trigger: 'blur' }]"
              >
                <el-input type="textarea" autosize placeholder="请输入条件" v-model="ruleForm.purchContractOtherClause[scope.$index].paymentCondition" style="width: 100%;" :disabled="isView" />
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="操作" width="200" v-if="!isView">
            <template slot-scope="scope">
              <el-button type="text" @click="ruleForm.purchContractOtherClause.push({paymentCondition: ''})">增加</el-button>
              <el-button type="text" @click="deleteCondition(scope.$index)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      
      <cy-info-title style="margin-top: 10px;">电子签信息</cy-info-title>
      <el-form-item label="审批结论" prop="purchContractApproval.approvalConclusion">
        <el-radio-group v-model="ruleForm.purchContractApproval.approvalConclusion" @change="setOtherInfo">
          <el-radio label="pass">通过</el-radio>
          <el-radio label="back">退回</el-radio>
        </el-radio-group>
      </el-form-item>
      <template v-if="ruleForm.purchContractApproval.approvalConclusion == 'pass'">
        <el-form-item label="合同签署方式" prop="purchContractApproval.signMode" style="width: 60%;"
          :rules="[
            { required: ruleForm.purchContractApproval.approvalConclusion == 'pass', message: '请选择合同签署方式', trigger: 'change' }
          ]">
          <!-- <el-checkbox v-model="ruleForm.purchContractApproval.signMode" @change="createFile" :true-label="1" :false-label="0" :disabled="isView">线上电子签</el-checkbox> -->
          <el-checkbox v-model="ruleForm.purchContractApproval.signMode" @change="createFile" :true-label="1" :false-label="''" :disabled="isView">线上电子签</el-checkbox>
        </el-form-item>
        <el-form-item label="贸易平台">
          <el-input v-model="ruleForm.purchContractApproval.platform.platformName" style="width: 100%;" disabled></el-input>
        </el-form-item>
        <el-form-item label="认证状态" prop="purchContractApproval.platform.authStatus">
          <el-input :value="ruleForm.purchContractApproval.platform.authStatus == 1 ? '已认证' : '未认证'" style="width: 100%;" disabled></el-input>
        </el-form-item>
        <el-form-item label="自动签署状态" prop="purchContractApproval.platform.autoSignStatus">
          <el-input :value="ruleForm.purchContractApproval.platform.autoSignStatus == 1 ? '已签署' : '未签署'" style="width: 100%;" disabled></el-input>
        </el-form-item>
        <el-form-item label="采购合同文件" prop="purchContractApproval.fileStorage" style="width: calc(66% - 15px);">
          <cy-upload
            ref="fileStorage"
            :upload-params="{ mouldName: 'reconsiderAdjustFile' }"
            :file-data="ruleForm.purchContractApproval.fileStorage"
            :upload-from-orange-status="true"
            placeholder="请上传附件"
            disabled
          />
        </el-form-item>
        <el-form-item label="上传合同文件" prop="purchContractApproval.upload"
          :rules="[
            { required: ruleForm.purchContractApproval.approvalConclusion === 'pass', message: '请选择上传合同文件', trigger: 'change' }
          ]">
          <el-radio-group v-model="ruleForm.purchContractApproval.upload" @change="changeUpload" :disabled="isView">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </template>

      <el-form-item
        label="审批意见："
        style="width: 100%;"
        class="textarea-content"
        prop="purchContractApproval.approvalComment"
        :rules="[
          { required: ruleForm.purchContractApproval.approvalConclusion === 'back', message: '请输入审批意见', trigger: 'blur' }
        ]"
      >
        <el-input v-model="ruleForm.purchContractApproval.approvalComment" type="textarea" style="width: 100%;" rows="3" clearable></el-input>
      </el-form-item>
      <div style="width: 100%;" v-if="ruleForm.purchContractApproval.upload == 1 && ruleForm.purchContractApproval.approvalConclusion == 'pass'">
        <el-table
          :data="ruleForm.purchContractApproval.uploadPurchContracts"
          style="width: 100%">
          <el-table-column label=" 序号" type="index"></el-table-column>
          <el-table-column prop="contractName" label="合同名称">
            <template slot-scope="scope">
              <el-form-item label="" :prop="`purchContractApproval.uploadPurchContracts.${scope.$index}.contractName`" style="width: 100%; margin-bottom: 15px;"
                :rules="[{ required: true, message: '请输入合同名称', trigger: 'blur' }]"
              >
                <el-input type="textarea" autosize placeholder="请输入合同名称" v-model="ruleForm.purchContractApproval.uploadPurchContracts[scope.$index].contractName" style="width: 100%;" :disabled="isView" />
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column prop="content" label="签署人">
            <template slot-scope="scope">
              <el-form-item label="" :prop="`purchContractApproval.uploadPurchContracts.${scope.$index}.personList`" style="width: 100%; margin-bottom: 15px;"
                :rules="[{ required: true, message: '请选择签署人', trigger: 'blur' }]"
              >
              <el-checkbox-group v-model="ruleForm.purchContractApproval.uploadPurchContracts[scope.$index].personList" :disabled="isView">
                <el-checkbox :label="`${ruleForm.purchContractApproval.platform.socialCreditCode}。${ruleForm.purchContractApproval.platform.platformName}`">{{ ruleForm.purchContractApproval.platform.platformName }}</el-checkbox>
                <el-checkbox :label="`${ruleForm.supplier.socialCreditCode}。${ruleForm.supplier.corporateName}`">{{ ruleForm.supplier.corporateName }}</el-checkbox>
              </el-checkbox-group>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column prop="content" label="签署文件">
            <template slot-scope="scope">
              <el-form-item label="" :prop="`purchContractApproval.uploadPurchContracts.${scope.$index}.fileId`" style="width: 100%; margin-bottom: 15px;"
                :rules="[{ required: true, message: '请上传PDF格式的文件', trigger: 'blur' }]"
              >
              <cy-upload
                :ref="`fileStorage${scope.$index}`"
                :upload-params="{ mouldName: 'purchaseContractUploadContractFile' }"
                :file-data="ruleForm.purchContractApproval.uploadPurchContracts[scope.$index].fileStorages"
                :upload-from-orange-status="true"
                placeholder="请上传PDF格式的文件"
                :disabled="isView"
                :isCallBack="true"
                @call-back="setFile(scope.$index, $event)"
              />
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="操作" width="200" v-if="!isView">
            <template slot-scope="scope">
              <el-button type="text" @click="ruleForm.purchContractApproval.uploadPurchContracts.push({contractName: '', fileId: '', fileStorages: [], id: '', personList: []})">增加</el-button>
              <el-button v-if="ruleForm.purchContractApproval && ruleForm.purchContractApproval.uploadPurchContracts && ruleForm.purchContractApproval.uploadPurchContracts.length > 1" type="text" @click="deleteContract(scope.$index)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-form>
  </div>
</template>

<script>
import request from '@/utils/request'
export default {
  components: {
    SignConfirmationContent: () => import('./SignConfirmationContent.vue')
  },
  props: ["isView", "info"],
  data() {
    const validateDigit = (rule, value, callback) => {
      if (!value && value !== 0) {
        callback(new Error('请输入税率'))
      } else {
        if(/^\d+(\.\d)?\d?$/g.test(value)) {
          callback()
          if(Number(value) > 100 || Number(value) < 0) {
            callback(new Error('请输入0-100范围内的数字'))
          } else {
            callback()
          }
        } else {
          callback(new Error('请输入保留两位小数的数字'))
        }
      }
    }
    return {
      key: 0,
      ruleForm: {
        purchContractBasicInfo: {},
        purchContractReplenishInfo: {},
        purchContractQualityAccept: {},
        approvalSettlePayment: {},
        purchContractDeliverClause: {},
        approvalSettleCycle: {},
        userList: [],
        approvalVatInfo: {},
        purchContractOtherClause: [],
        purchContractApproval: {
          platform: {}
        },
        visible: '',
        supplier: {}
      },
      rules: {
        'purchContractReplenishInfo.projectAmountCategory': [
          { required: true, message: '请选择工程量类别', trigger: 'blur' }
        ],
        'purchContractReplenishInfo.goodsPriceSettleMethod': [
          { required: true, message: '请输入货物单价结算方式', trigger: 'blur' }
        ],
        'purchContractQualityAccept.qualityCriteria': [
          { required: true, message: '请输入质量标准', trigger: 'blur' }
        ],
        'purchContractQualityAccept.acceptCriteria': [
          { required: true, message: '请输入验收标准', trigger: 'blur' }
        ],
        'approvalSettlePayment.settleMethod': [
          { required: true, message: '请选择结算方式', trigger: 'change' }
        ],
        'approvalSettlePayment.paymentMethod': [
          { required: true, message: '请输入付款时间及方式', trigger: 'blur' }
        ],
        'purchContractDeliverClause.deliverSite': [
          { required: true, message: '请输入交货地点', trigger: 'blur' }
        ],
        'purchContractDeliverClause.deliverMethod': [
          { required: true, message: '请输入交货方式', trigger: 'blur' }
        ],
        'purchContractDeliverClause.deliverDate': [
          { required: true, message: '请输入交货日期', trigger: 'blur' }
        ],
        'purchContractDeliverClause.transportPremium': [
          { required: true, message: '请输入运费及保险费', trigger: 'blur' }
        ],
        'approvalSettleCycle.settleCycleMethod': [
          { required: true, message: '请选择结算周期方式', trigger: 'change' }
        ],
        'approvalSettleCycle.settleCycle': [
          { required: true, message: '请输入结算规则', trigger: 'blur' }
        ],
        'approvalVatInfo.receiptType': [
          { required: true, message: '请选择发票类型', trigger: 'change' }
        ],
        'approvalVatInfo.taxRate': [
          { required: true, message: '请输入税率', trigger: 'blur' },
          { validator: validateDigit, trigger: 'blur' }
        ],
        'approvalVatInfo.openBank': [
          { required: true, message: '请输入发票开户行', trigger: 'blur' }
        ],
        'approvalVatInfo.receiptAccount': [
          { required: true, message: '请输入发票账号', trigger: 'blur' }
        ],
        'approvalVatInfo.receiptRemark': [
          { required: true, message: '请输入发票备注', trigger: 'blur' }
        ],
        'purchContractApproval.approvalConclusion': [
          { required: true, message: '请选择审批结论', trigger: 'blur' }
        ]
      },
      payMethodList: [],
      settleMethodList: []
    }
  },
  mounted() {
    if(!this.isView) {
      this.getDetail()
    }
    this.getStatusList()
    this.getMethodList()
  },
  methods: {
    getStatusList() {
      request({
        url: '/dictionary/do_findByParentCode',
        method: 'get',
        params: { parentCode: 'purchContractSettleMethod' }
      }).then(({ data }) => {
        this.payMethodList = data || []
      })
    },
    getMethodList() {
      request({
        url: '/dictionary/do_findByParentCode',
        method: 'get',
        params: { parentCode: 'salesContractSettleCycleMethod' }
      }).then(({ data }) => {
        this.settleMethodList = data || []
      })
    },
    getDetail() {
      request({
        url: '/purchContract/approval/add',
        params: {
          businessKey: this.$route.params.infoId
        },
        method: 'get'
      }).then(({ data }) => {
        if(data.purchContractApproval.fileStorage) {
          data.purchContractApproval.fileStorage = [data.purchContractApproval.fileStorage]
        } else {
          data.purchContractApproval.fileStorage = []
        }
        if(data.purchContractApproval.uploadPurchContracts) {
          data.purchContractApproval.uploadPurchContracts.forEach(item => {
            item.personList = []
            if(item.uploadPurchContractSigners) {
              item.uploadPurchContractSigners.forEach(i => {
                item.personList.push(`${i.signerSocialCreditCode}。${i.signerName}`)
              })
            }
            if(item.fileStorage) {
              item.fileStorages = [item.fileStorage]
              // item.fileStorage = [item.fileStorage]
            } else {
              item.fileStorages = []
              // item.fileStorage = []
            }
          })
        }
        Object.keys(this.ruleForm).forEach(key => {
          if(data[key]) {
            this.ruleForm[key] = data[key]
          }
        })
        console.log(this.ruleForm, 'ruleForm')
      })
    },
    changeUpload(val) {
      if(val == 1 && (!this.ruleForm.purchContractApproval.uploadPurchContracts || this.ruleForm.purchContractApproval.uploadPurchContracts.length === 0)) {
        this.ruleForm.purchContractApproval.uploadPurchContracts = []
        this.ruleForm.purchContractApproval.uploadPurchContracts.push({
          contractName: '',
          fileId: '',
          // fileStorage: [],
          fileStorages: [],
          id: '',
          personList: []
        })
      } else {
        this.ruleForm.purchContractApproval.uploadPurchContracts = []
      }
    },
    deleteContract(index) {
      this.ruleForm.purchContractApproval.uploadPurchContracts.splice(index, 1)
    },
    deleteCondition(index) {
      this.ruleForm.purchContractOtherClause.splice(index, 1)
    },
    deletePerson(index) {
      this.ruleForm.userList.splice(index, 1)
    },
    setFile(index, info) {
      this.ruleForm.purchContractApproval.uploadPurchContracts[index].fileId = this.$refs[`fileStorage${index}`].getFileLists[0].id
      // this.ruleForm.purchContractApproval.uploadPurchContracts[index].fileStorage = this.$refs[`fileStorage${index}`].getFileLists
      // this.ruleForm.purchContractApproval.uploadPurchContracts[index].fileStorage = info
    },
    validate() {
      return new Promise((resolve, reject) => {
        if(this.ruleForm.purchContractApproval.approvalConclusion == 'back') {
          this.$refs.ruleForm.validateField(['purchContractApproval.approvalComment'], flag => {
            if(!flag) {
              resolve()
            } else {
              reject()
            }
          })
        } else if(this.ruleForm.purchContractApproval.approvalConclusion == 'pass' || !this.ruleForm.purchContractApproval.approvalConclusion) {
          this.$refs.ruleForm.validate(flag => {
            if(flag) {
              resolve()
            } else {
              reject()
            }
          })
        }
        // this.$refs.ruleForm.validate(flag => {
        //   if(flag) {
        //     resolve()
        //   } else {
        //     reject()
        //   }
        // })
      })
    },
    createFile(val) {
      if(this.ruleForm.purchContractApproval.approvalConclusion == 'pass' && val == 1) {
        let data = { taskId: this.$route.params.taskId, businessKey: this.$route.params.infoId, ...this.ruleForm }
        data.visible = 0 
        data.purchContractApproval = {
          ...this.ruleForm.purchContractApproval
        }
        data.purchContractApproval.fileStorage = {}
        if(data.purchContractApproval.uploadPurchContracts && data.purchContractApproval.uploadPurchContracts.length > 0) {
          data.purchContractApproval.uploadPurchContracts.forEach(item => {
            item.uploadPurchContractSigners = item.personList.map(item => {
              return {
                signerName: item.split('。')[1],
                signerSocialCreditCode: item.split('。')[0]
              }
            })
            delete item.fileStorage
          })
        }
        request({
          url: '/purchContract/approval/makeContractPdf',
          method: 'post',
          data
        }).then(({ data }) => {
          this.ruleForm.purchContractApproval.fileStorage = []
          this.ruleForm.purchContractApproval.fileStorage.push(data)
        })
      } else {
        this.ruleForm.purchContractApproval.fileStorage = []
      }
    },
    setOtherInfo(val) {
      if(val == 'back') {
        this.ruleForm.purchContractApproval.signMode = ''
        this.ruleForm.purchContractApproval.fileStorage = []
        this.ruleForm.purchContractApproval.upload = ''
        this.ruleForm.purchContractApproval.uploadPurchContracts = []
      }
    }
  },
  watch: {
    'info.mathId': {
      handler(newV) {
        if (newV) {
          if(this.info.purchContractApproval.fileStorage) {
            this.info.purchContractApproval.fileStorage = [this.info.purchContractApproval.fileStorage]
          } else {
            this.info.purchContractApproval.fileStorage = []
          }
          if(this.info.purchContractApproval.uploadPurchContracts) {
            this.info.purchContractApproval.uploadPurchContracts.forEach(item => {
              item.personList = []
              if(item.uploadPurchContractSigners) {
                item.uploadPurchContractSigners.forEach(i => {
                  item.personList.push(`${i.signerSocialCreditCode}。${i.signerName}`)
                })
              }
              if(item.fileStorage) {
                item.fileStorages = [item.fileStorage]
              }
            })
          }
          Object.keys(this.ruleForm).forEach(key => {
            if(this.info[key] || this.info[key] === 0) {
              this.ruleForm[key] = this.info[key] || ''
              if(key == 'purchContractApproval' && !this.info[key].platform) {
                this.ruleForm[key].platform = {}
              }
            }
          })
        }
      },
      immediate: true
    }
  }
}
</script>

<style  scoped>

</style>
