<template xmlns:v-slot="http://www.w3.org/1999/XSL/Transform">
  <div>
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      v-dialogDrag
      width="1300px"
      @close="close"
      @keyup.enter.native=""
      :visible.sync="visible">
      <el-row>
        <el-col :span="24">
          <el-tabs v-model="activeName" type="border-card" @tab-click="tabHandleClick">
            <el-tab-pane label="项目登记详情" name="programDetail">
              <el-form size="middle" :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
                       label-width="220px" @submit.native.prevent>
                <el-divider content-position="left"><i class="el-icon-document"></i> 选择合同</el-divider>
                <el-row  :gutter="15">
                  <el-col :span="24">
                    <el-form-item label="选择合同" prop="contractName"
                                  :rules="[
                          {required: true, message:'合同不能为空', trigger:'blur'},
                          {required: true, message:'合同不能为空', trigger:'change'}
                 ]">
                      <el-input size="medium" :disabled="method!=='add'" :readonly="true" @focus="openContractForm()" v-model="inputForm.contractName" placeholder="请选择合同">
                        <el-button slot="append" icon="el-icon-search" @click="openContractForm()"></el-button>
                      </el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="合同名称" prop="contractName"
                                  :rules="[
                 ]">
                      <el-input :disabled="true" v-model="inputForm.contractName" placeholder="请填写合同名称"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="合同金额(元)" prop="amount"
                                  :rules="[
                 ]">
                      <!--              <el-input v-model="inputForm.amount" placeholder="请填写合同金额"></el-input>-->
                      <el-input-number
                        :disabled="true"
                        v-model="inputForm.amount"
                        controls-position="right"
                        :controls="false"
                        style="width:100%;"
                        :precision="2"
                        placeholder="请填写合同金额"
                        :step="0.01"
                        :min="0"
                        clearable>
                      </el-input-number>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="委托方" prop="clientName"
                                  :rules="[
                 ]">
                      <el-input :disabled="true" v-model="inputForm.clientName" placeholder="请填写委托方"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="合同类型" prop="contractType"
                                  :rules="[
                 ]">
                      <el-select :disabled="true" v-model="inputForm.contractType" placeholder="请选择合同类型" clearable style="width: 100%;">
                        <el-option
                          v-for="item in $dictUtils.getDictList('contract_type')"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value">
                        </el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-divider content-position="left"><i class="el-icon-document"></i> 项目基本情况信息</el-divider>
                <el-row  :gutter="15">
                  <el-col :span="12">
                    <el-form-item label="项目名称" prop="name"
                                  :rules="[
                          {required: true, message:'项目名称不能为空', trigger:'blur'}
                 ]">
                      <el-input maxlength="64" v-model="inputForm.name" placeholder="请输入项目名称" clearable></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="项目编号" prop="no"
                                  :rules="[
                 ]">
                      <el-input :disabled="true" v-model="inputForm.no" placeholder="请输入项目编号" clearable></el-input>
                    </el-form-item>
                  </el-col>
                  <!--          <el-col :span="12">-->
                  <!--            <el-form-item label="项目类别" prop="projectType"-->
                  <!--                          :rules="[-->
                  <!--                          {required: true, message:'项目类别不能为空', trigger:'blur'}-->
                  <!--                 ]">-->
                  <!--              <el-radio v-model="inputForm.projectType" label="1" size="small" style="margin-right: 20px">资评报字</el-radio>-->
                  <!--              <el-radio v-model="inputForm.projectType" label="2" size="small" style="margin-right: 20px">资评咨字</el-radio>-->
                  <!--            </el-form-item>-->
                  <!--          </el-col>-->
                  <el-col :span="12">
                    <el-form-item label="项目类型" prop="projectMould"
                                  :rules="[
                          {required: true, message:'项目类型不能为空', trigger:'blur'}
                 ]">
                      <el-select v-model="inputForm.projectMould" placeholder="请选择项目类型" clearable style="width: 100%;">
                        <el-option
                          v-for="item in $dictUtils.getDictList('program_registered_type')"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value">
                        </el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="立项号" prop="approvalNo"
                                  :rules="[
                 ]">
                      <el-input :disabled="true" maxlength="64" max v-model="inputForm.approvalNo" placeholder="请输入立项号"clearable></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="报告类型" prop="reportType"
                                  :rules="[
                          {required: true, message:'报告类型不能为空', trigger:'blur'}
                 ]">
                      <el-select v-model="inputForm.reportType" :disabled="method!=='add'" placeholder="请选择报告类型" clearable style="width: 100%;">
                        <el-option
                          v-for="item in $dictUtils.getDictList('program_registered_report_type')"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value">
                        </el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="评估目的" prop="assessmentObjective"
                                  :rules="[
                 ]">
                      <el-input v-model="inputForm.assessmentObjective" placeholder="请输入评估目的"clearable></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="评估方法" prop="assessmentWay"
                                  :rules="[
                 ]">
                      <el-input v-model="inputForm.assessmentWay" placeholder="请输入评估方法"clearable></el-input>
                    </el-form-item>
                  </el-col>

                  <el-col :span="12">
                    <el-form-item label="评估基准日" prop="assessmentDate"
                                  :rules="[
                          {required: true, message:'评估基准日不能为空', trigger:'blur'}
                 ]">
                      <el-date-picker
                        v-model="inputForm.assessmentDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择评估基准日"
                        style="width:100%"
                        placement="bottom-start"
                        clearable>
                      </el-date-picker>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="评估对象" prop="assessmentObject"
                                  :rules="[
                          {required: true, message:'评估对象不能为空', trigger:'blur'}
                 ]">
                      <el-select v-model="inputForm.assessmentObject" placeholder="请输入评估对象" clearable style="width: 100%;">
                        <el-option
                          v-for="item in $dictUtils.getDictList('project_assessment_object')"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value">
                        </el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="执业机构" prop="company"
                                  :rules="[
                 ]">
                      <el-input :readonly="true" v-model="inputForm.company" placeholder="请输入执业机构" clearable></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="项目所在地" prop="location"
                                  :rules="[
                          {required: true, message:'项目所在地不能为空', trigger:'blur'}
                 ]">
                      <SelectTree
                        ref="areaTree"
                        :props="{
                      value: 'id',             // ID字段名
                      label: 'name',         // 显示名称
                      children: 'children'    // 子级字段名
                    }"
                        url="/sys/area/treeData"
                        :value="inputForm.location"
                        :clearable="true"
                        size="medium"
                        :accordion="true"
                        @getValue="(value) => {inputForm.location=value}"/>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="委托人" prop="propertyHolderName"
                                  :rules="[
                          {required: true, message:'委托人不能为空', trigger:'blur'},
                          {required: true, message:'委托人不能为空', trigger:'change'}
                 ]">
                      <el-input v-model="inputForm.propertyHolderName" @focus="openWorkClientForm('2')" placeholder="请输入委托人" clearable></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="企业类型" prop="enterpriseType"
                                  :rules="[
                 ]">
                      <el-input maxlength="64" v-model="inputForm.enterpriseType" placeholder="请输入企业类型"clearable></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="是否首次承做" prop="isFirst"
                                  :rules="[
                          {required: true, message:'是否首次承做不能为空', trigger:'blur'}
                 ]">
                      <el-radio v-model="inputForm.isFirst" v-for="item in $dictUtils.getDictList('yes_no')" :label="item.value" size="small" style="margin-right: 20px">
                        {{item.label}}</el-radio>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12" v-if="inputForm.isFirst === '0'">
                    <el-form-item label="已连续承做几年" prop="yearNum"
                                  :rules="[
                          {required: true, message:'已连续承做几年不能为空', trigger:'blur'}
                 ]">
                      <el-input-number
                        v-model="inputForm.yearNum"
                        controls-position="right"
                        style="width:100%"
                        :precision="1"
                        :step="0.1"
                        :min="0"
                        placeholder="请输入已连续承做几年"
                        :controls="false"
                        clearable>
                      </el-input-number>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="项目来源" prop="projectSource"
                                  :rules="[
                          {required: true, message:'项目来源不能为空', trigger:'blur'}
                 ]">
                      <el-select v-model="inputForm.projectSource" placeholder="请选择项目来源" clearable style="width: 100%;">
                        <el-option
                          v-for="item in $dictUtils.getDictList('program_registered_source')"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value">
                        </el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="被评估企业" prop="assessmentEnterpriseName"
                                  :rules="[
                          {required: true, message:'被评估企业不能为空', trigger:'blur'},
                          {required: true, message:'被评估企业不能为空', trigger:'change'}
                 ]">
                      <el-input v-model="inputForm.assessmentEnterpriseName" @focus="openWorkClientForm('3')" placeholder="请输入被评估企业"clearable></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="联系人及电话" prop="linkNum"
                                  :rules="[
                          {required: true, message:'联系人及电话不能为空', trigger:'blur'}
                 ]">
                      <el-input maxlength="64" v-model="inputForm.linkNum" placeholder="请输入联系人及电话"clearable></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="委托人与被评估单位关系" prop="relationship"
                                  :rules="[
                 ]">
                      <el-input v-model="inputForm.relationship" placeholder="请输入委托人与被评估单位关系"clearable></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="评估费用核算年度" prop="checkYear"
                                  :rules="[
                          {required: true, message:'评估费用核算年度不能为空', trigger:'blur'}
                 ]">
                      <el-date-picker
                        v-model="inputForm.checkYear"
                        type="year"
                        value-format="yyyy"
                        placeholder="选择评估费用核算年度"
                        style="width:100%"
                        placement="bottom-start"
                        clearable>
                      </el-date-picker>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="项目负责人" prop="projectManager"
                                  :rules="[
                          {required: true, message:'项目负责人不能为空', trigger:'blur'}
                 ]">
                      <SelectUserTree
                        ref="companyTree"
                        size="medium"
                        :props="{
                  value: 'id',             // ID字段名
                  label: 'name',         // 显示名称
                  children: 'children'    // 子级字段名
                }"
                        :url="`/sys/user/treeUserDataAllOffice?type=2`"
                        :value="inputForm.projectManager"
                        :clearable="true"
                        :accordion="true"
                        @getValue="(value) => {inputForm.projectManager=value}"/>
                    </el-form-item>
                  </el-col>

                  <!--          <el-col :span="12">-->
                  <!--            <el-form-item label="所属行业" prop="industry"-->
                  <!--                          :rules="[-->
                  <!--                 ]">-->
                  <!--              <el-input maxlength="64" v-model="inputForm.industry" placeholder="请输入所属行业"clearable></el-input>-->
                  <!--            </el-form-item>-->
                  <!--          </el-col>-->

                  <el-col :span="12">
                    <el-form-item label="风险等级" prop="riskLevel"
                                  :rules="[
                          {required: true, message:'风险等级不能为空', trigger:'blur'}
                 ]">
                      <el-radio v-model="inputForm.riskLevel" v-for="item in $dictUtils.getDictList('program_registered_risk_level')" :label="item.value" size="small" style="margin-right: 20px">
                        {{item.label}}</el-radio>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="风险项目类型" prop="delegateProjectType"
                                  :rules="[
                          {required: true, message:'风险项目类型不能为空', trigger:'blur'}
                 ]">
                      <el-select v-model="inputForm.delegateProjectType" placeholder="请选择风险项目类型" clearable style="width: 100%">
                        <el-option-group
                          v-for="(type,index) in typeDictList"
                          :key="type.id"
                          :label="type.type">
                          <el-option
                            v-for="item in typeDictList[index].childrenList"
                            :key="item.id"
                            :label="item.type"
                            :value="item.id">
                          </el-option>
                        </el-option-group>
                      </el-select>
                    </el-form-item>
                  </el-col>

                  <el-col :span="12">
                    <el-form-item label="评估收费总额(元)" prop="estimate"
                                  :rules="[
                 ]">
                      <el-input-number
                        v-model="inputForm.estimate"
                        controls-position="right"
                        style="width:100%;"
                        maxlength="15"
                        :precision="2"
                        placeholder="请输入评估收费总额(元)"
                        :controls="false"
                        :step="0.01"
                        :min="0"
                        clearable>
                      </el-input-number>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="计划完成时间" prop="planEndTime"
                                  :rules="[
                 ]">
                      <el-date-picker
                        v-model="inputForm.planEndTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择计划完成时间"
                        style="width:100%"
                        placement="bottom-start"
                        clearable>
                      </el-date-picker>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="计划使用人数" prop="useNum"
                                  :rules="[
                 ]">
                      <el-input-number
                        v-model="inputForm.useNum"
                        controls-position="right"
                        style="width:100%"
                        maxlength="15"
                        :precision="0"
                        :min="0"
                        placeholder="请输入计划使用人数"
                        :controls="false"
                        clearable>
                      </el-input-number>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="约定书（份）" prop="appointment"
                                  :rules="[
                 ]">
                      <el-input-number
                        v-model="inputForm.appointment"
                        controls-position="right"
                        style="width:100%"
                        maxlength="15"
                        :precision="0"
                        :min="0"
                        placeholder="请输入约定书（份）"
                        :controls="false"
                        clearable>
                      </el-input-number>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="预算工时" prop="workHours"
                                  :rules="[
                 ]">
                      <el-input-number
                        v-model="inputForm.workHours"
                        controls-position="right"
                        style="width:100%"
                        maxlength="15"
                        :precision="1"
                        :step="0.1"
                        :min="0"
                        placeholder="请输入预算工时"
                        :controls="false"
                        clearable>
                      </el-input-number>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="预计现场工作起止日期" prop="workBeginAndEndDate"
                                  :rules="[
                 ]">
                      <el-date-picker
                        size="medium"
                        v-model="inputForm.workBeginAndEndDate"
                        type="daterange"
                        range-separator="至"
                        style="width: 100%  "
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        placement="bottom-start"
                        clearabl>
                      </el-date-picker>
                    </el-form-item>
                  </el-col>

                  <el-col :span="12">
                    <el-form-item label="预计出报告时间" prop="reportingDate"
                                  :rules="[
                          {required: true, message:'预计出报告时间不能为空', trigger:'blur'}
                 ]">
                      <el-date-picker
                        v-model="inputForm.reportingDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择预计出报告时间"
                        style="width:100%"
                        placement="bottom-start"
                        clearable>
                      </el-date-picker>
                    </el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="备注" prop="remarks"
                                  :rules="[
                 ]">
                      <el-input type="textarea" v-model="inputForm.remarks" maxlength="500" placeholder="请输入备注" show-word-limit></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-divider content-position="left"><i class="el-icon-document"></i>
                  委托方联系人和项目直接对接人
                  <el-button style="margin-left: 20px" type="primary" :disabled="method==='view'" size="mini" @click="openContactForm('0')" plain>
                    新增
                  </el-button>
                </el-divider>
                <el-row  :gutter="15" >
                  <vxe-table
                    border
                    show-overflow
                    ref="clientTable"
                    class="vxe-table-element"
                    :data="inputForm.clientList"
                    style="margin-left: 5em"
                    @cell-click=""
                    @edit-closed=""
                    highlight-current-row
                    :edit-config="{trigger: 'click', mode: 'cell', showStatus: true, autoClear: true}"
                  >
                    <vxe-table-column field="contacts" title="联系人" :edit-render="{}">
                      <template v-slot:edit="scope">
                        <el-input :disabled="true" v-model="scope.row.contacts" ></el-input>
                      </template>
                    </vxe-table-column>
                    <vxe-table-column field="clientType" title="人员类型" :edit-render="{name: '$select', options: clientTypes}">
                      <template v-slot:edit="scope">
                        <vxe-select v-model="scope.row.clientType"  transfer>
                          <vxe-option
                            v-for="item in clientTypes"
                            :key="item.value"
                            :value="item.value"
                            :label="item.label">
                          </vxe-option>
                        </vxe-select>
                      </template>
                    </vxe-table-column>
                    <vxe-table-column field="linkMobilePhoneFirst" title="联系方式1" :edit-render="{}">
                      <template v-slot:edit="scope">
                        <el-input :disabled="true" v-model="scope.row.linkMobilePhoneFirst" ></el-input>
                      </template>
                    </vxe-table-column>
                    <vxe-table-column field="linkMobilePhoneSecoed" title="联系方式2" :edit-render="{}">
                      <template v-slot:edit="scope">
                        <el-input :disabled="true" v-model="scope.row.linkMobilePhoneSecoed" ></el-input>
                      </template>
                    </vxe-table-column>
                    <vxe-table-column field="job" title="职位" :edit-render="{}">
                      <template v-slot:edit="scope">
                        <el-input v-model="scope.row.job" ></el-input>
                      </template>
                    </vxe-table-column>
                    <vxe-table-column field="remarks" title="备注" :edit-render="{}">
                      <template v-slot:edit="scope">
                        <el-input v-model="scope.row.remarks" ></el-input>
                      </template>
                    </vxe-table-column>
                    <vxe-table-column title="操作" width="100">
                      <template v-slot="scope">
                        <el-button size="mini" type="danger" @click="removeEvent(scope.row,scope.$rowIndex,'client')">删除</el-button>
                      </template>
                    </vxe-table-column>
                  </vxe-table>
                </el-row>


                <el-divider content-position="left"><i class="el-icon-document"></i>
                  项目组成员
                  <el-button style="margin-left: 20px" type="primary" :disabled="method==='view'" size="mini" @click="insertEvent('member')" plain>
                    新增
                  </el-button>
                </el-divider>
                <el-row  :gutter="15">
                  <vxe-table
                    border
                    show-overflow
                    show-footer
                    :footer-method="footerMethod"
                    ref="memberTable"
                    class="vxe-table-element"
                    :data="inputForm.members"
                    style="margin-left: 5em"
                    @cell-click=""
                    @edit-closed=""
                    highlight-current-row
                    :edit-config="{trigger: 'click', mode: 'cell', showStatus: true, autoClear: true}"
                  >
                    <vxe-table-column field="name" title="姓名" :edit-render="{}">
                      <template v-slot:edit="scope">
                        <el-input v-model="scope.row.name" ></el-input>
                      </template>
                    </vxe-table-column>
                    <vxe-table-column field="workHours" title="预计工时（时）" :edit-render="{name: '$input'}">
                      <template v-slot:edit="scope">
                        <vxe-input
                          v-model="scope.row.workHours"
                          type="float"
                          digits="1"
                          placeholder="请输入预计工时（时）"
                          clearable>
                        </vxe-input>
                      </template>
                    </vxe-table-column>
                    <vxe-table-column field="type" title="人员类型" :edit-render="{name: '$select', options: $dictUtils.getDictList('program_registered_member_type')}">
                      <template v-slot:edit="scope">
                        <vxe-select v-model="scope.row.type" placeholder="请选择人员类型" clearable style="width: 100%;" transfer>
                          <vxe-option
                            v-for="item in $dictUtils.getDictList('program_registered_member_type')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </vxe-option>
                        </vxe-select>
                      </template>
                    </vxe-table-column>
                    <vxe-table-column field="job" title="项目职务" :edit-render="{name: '$select', options: $dictUtils.getDictList('program_registered_member_position')}">
                      <template v-slot:edit="scope">
                        <vxe-select v-model="scope.row.job" placeholder="请选择项目职务" clearable style="width: 100%;" transfer>
                          <vxe-option
                            v-for="item in $dictUtils.getDictList('program_registered_member_position')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </vxe-option>
                        </vxe-select>
                      </template>
                    </vxe-table-column>
                    <vxe-table-column title="操作" width="100">
                      <template v-slot="scope">
                        <el-button size="mini" type="danger" @click="removeEvent(scope.row,scope.$rowIndex,'member')">删除</el-button>
                      </template>
                    </vxe-table-column>
                  </vxe-table>
                </el-row>
              </el-form>
              <!--        附件-->
              <UpLoadComponent ref="uploadComponent"></UpLoadComponent>
            </el-tab-pane>
            <el-tab-pane label="项目归档" name="archive" >
              <ProjectArchiveForm ref="projectArchiveForm" :formReadOnly="true"></ProjectArchiveForm>
            </el-tab-pane>
          </el-tabs>
        </el-col>
      </el-row>
      <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="close()" icon="el-icon-circle-close">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" icon="el-icon-circle-check" v-noMoreClick>确定</el-button>
    </span>
    </el-dialog>
    <ContractForm  ref="contractForm" @getContract="getContract"></ContractForm>
    <WorkClientForm  ref="workClientForm" @getWorkClient="getWorkClient"></WorkClientForm>
    <ContactForm  ref="contactForm" @getContact="getContact"></ContactForm>
  </div>
</template>

<script>
  import ProgramTypeDictService from '@/api/program/ProgramTypeDictService'
  import ContractForm from './ContractForm'
  import WorkClientForm from './WorkClientForm'
  import ContactForm from './ContactForm'
  import ProjectArchiveForm from './ProjectArchiveForm'
  import UpLoadComponent from '@/views/common/UpLoadComponent'
  import SelectUserTree from '@/views/modules/utils/treeUserSelect'
  import ProgramProjectListInfoService from '@/api/program/ProgramProjectListInfoService'
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  import XEUtils from 'xe-utils'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        typeDictList: [],
        activeName: 'programDetail',
        inputForm: {
          id: '', // 主键值（新增不传，修改必传）
          no: '', // 编号（新增不传，修改必传）
          contractId: '', // 合同id
          contractName: '', // 合同名称
          client: '', // 委托方
          clientName: '', // 委托方名称
          amount: '', // 合同金额
          contractType: '', // 合同类别
          projectMould: '', // 项目类型
          name: '', // 项目名称
          projectType: '', // 项目类型
          approvalNo: '', // 立项号
          company: '', // 执业机构
          propertyHolder: '', // 产权持有人id
          propertyHolderName: '', // 产权持有人姓名
          location: '', // 项目所在地
          isFirst: '', // 是否首次承建
          yearNum: '', // 承做年数
          checkYear: '', // 评估费用核算年度
          projectManager: '', // 项目经理
          reportType: '', // 报告类型
          industry: '', // 所属行业
          enterpriseType: '', // 企业类型
          riskLevel: '', // 风险等级
          projectSource: '', // 项目来源
          estimate: '', // 预估收入（元）
          planEndTime: '', // 计划完成时间
          useNum: '', // 计划使用人数
          appointment: '', // 约定书（份）
          workHours: '', // 预算工时
          remarks: '', // 备注
          assessmentEnterprise: '', // 被评估企业
          assessmentEnterpriseName: '', // 被评估企业名称
          linkNum: '', // 联系人及电话
          relationship: '', // 委托人与被评估单位关系
          assessmentWay: '', // 评估方式
          assessmentObjective: '', // 评估目的
          assessmentDate: '', // 评估基准日
          num: '', // 评估服务收费总额
          projectDevelopment: '', // 项目开发人
          delegateProjectType: '', // 项目类型
          assessmentObject: '', // 评估对象
          workBeginAndEndDate: [], // 预计现场工作起止时间
          workBeginDate: '', // 预计现场工作开始时间
          workEndDate: '', // 预计现场工作结束时间
          reportingDate: '', // 预计出报告日期
          isHave: '', // 本所是否具备承接胜任能力
          isInfluence: '', // 是否影响本所乘坐人员独立性
          mode: '', // 项目承接方式
          status: '', // 状态
          procInsId: '', // 流程id
          processDefinitionId: '', // 流程信息
          links: [], // 联系人列表
          members: [], // 成员列表
          files: [], // 附件信息

          contactList: [], // 被评估企业对接联系人
          equityList: [], // 产权持有人联系人
          clientList: [] // 项目直接对接联系人
        },
        clientTypes: [
          {value: 1, label: '委托人'},
          {value: 2, label: '项目对接人'}
        ],
        archiveId: ''
      }
    },
    programTypeDictService: null,
    programProjectListInfoService: null,
    created () {
      this.programTypeDictService = new ProgramTypeDictService()
      this.programProjectListInfoService = new ProgramProjectListInfoService()
    },
    computed: {
      archiveIdA: {
        get () {
          return this.archiveId
        },
        set (val) {
          this.archiveId = val
        }
      }
    },
    components: {
      ContractForm,
      SelectUserTree,
      UpLoadComponent,
      WorkClientForm,
      ContactForm,
      SelectTree,
      ProjectArchiveForm
    },
    methods: {
      init (method, id, archiveId) {
        this.archiveIdA = archiveId
        this.method = method
        this.inputForm = {
          id: '', // 主键值（新增不传，修改必传）
          no: '', // 编号（新增不传，修改必传）
          contractId: '', // 合同id
          contractName: '', // 合同名称
          client: '', // 委托方
          clientName: '', // 委托方名称
          amount: '', // 合同金额
          contractType: '', // 合同类型
          name: '', // 项目名称
          projectType: '', // 项目类别
          projectMould: '', // 项目类型
          company: '江苏兴光资产评估有限公司', // 执业机构
          propertyHolder: '', // 产权持有人
          propertyHolderName: '', // 产权持有人姓名
          location: '', // 项目所在地
          isFirst: '', // 是否首次承建
          yearNum: '', // 承做年数
          checkYear: '', // 评估费用核算年度
          projectManager: '', // 项目经理
          reportType: '', // 报告类型
          industry: '', // 所属行业
          enterpriseType: '', // 企业类型
          riskLevel: '', // 风险等级
          projectSource: '', // 项目来源
          estimate: '', // 预估收入（元）
          planEndTime: '', // 计划完成时间
          useNum: '', // 计划使用人数
          appointment: '', // 约定书（份）
          workHours: '', // 预算工时
          remarks: '', // 备注
          assessmentEnterprise: '', // 被评估企业
          assessmentEnterpriseName: '', // 被评估企业名称
          linkNum: '', // 联系人及电话
          relationship: '', // 委托人与被评估单位关系
          assessmentWay: '', // 评估方式
          assessmentObjective: '', // 评估目的
          assessmentDate: '', // 评估基准日
          num: '', // 评估服务收费总额
          projectDevelopment: '', // 项目开发人
          delegateProjectType: '', // 项目类型
          assessmentObject: '', // 评估对象
          workBeginAndEndDate: [], // 预计现场工作起止时间
          workBeginDate: '', // 预计现场工作开始时间
          workEndDate: '', // 预计现场工作结束时间
          reportingDate: '', // 预计出报告日期
          isHave: '', // 本所是否具备承接胜任能力
          isInfluence: '', // 是否影响本所乘坐人员独立性
          mode: '', // 项目承接方式
          status: '', // 状态
          procInsId: '', // 流程id
          processDefinitionId: '', // 流程信息
          links: [], // 联系人列表
          members: [], // 成员列表
          files: [], // 附件信息

          contactList: [], // 被评估企业对接联系人
          equityList: [], // 产权持有人联系人
          clientList: [] // 项目直接对接联系人
        }
        this.getTypeList()
        if (method === 'add') {
          this.title = `项目登记`
        } else if (method === 'edit') {
          this.inputForm.id = id
          this.title = '修改项目数据'
        } else if (method === 'view') {
          this.inputForm.id = id
          this.title = '查看项目数据'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.programProjectListInfoService.queryById(this.inputForm.id).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data)
              this.inputForm = JSON.parse(JSON.stringify(this.inputForm))
              if (!this.commonJS.isEmpty(this.inputForm.workBeginDate) && !this.commonJS.isEmpty(this.inputForm.workEndDate)) {
                this.inputForm.workBeginAndEndDate = []
                this.inputForm.workBeginAndEndDate[0] = this.inputForm.workBeginDate
                this.inputForm.workBeginAndEndDate[1] = this.inputForm.workEndDate
              }
              this.inputForm.clientList = []
              this.inputForm.equityList = []
              this.inputForm.contactList = []
              if (this.commonJS.isEmpty(this.inputForm.members)) {
                this.inputForm.members = []
              }
              this.inputForm.links.forEach(item => {
                if (item.type === '0') {
                  this.inputForm.clientList.push(item)
                }
                if (item.type === '2') {
                  this.inputForm.equityList.push(item)
                }
                if (item.type === '3') {
                  this.inputForm.contactList.push(item)
                }
              })
              if (this.commonJS.isEmpty(this.inputForm.files)) {
                this.inputForm.files = []
              }
              this.$refs.uploadComponent.newUpload(method, this.inputForm.files, 'program')
              this.loading = false
            })
          }
          if (method !== 'edit' && method !== 'view') {
            this.$refs.uploadComponent.newUpload(method, [], 'program')
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.inputForm.links = []
        if (!this.commonJS.isEmpty(this.inputForm.clientList)) {
          this.inputForm.clientList.forEach(item => {
            if (this.commonJS.isEmpty(item.clientType)) {
              this.$message.error('"委托方联系人和项目直接对接人"中的人员类型不可以为空')
              throw new Error()
            }
            item.type = '0'
            this.inputForm.links.push(item)
          })
        } else {
          this.$message.error('至少填写一条"委托方联系人和项目直接对接人"信息')
          return
        }
        if (this.commonJS.isEmpty(this.inputForm.members)) {
          this.$message.error('至少填写一条"项目组成员"信息')
          return
        }
        if (!this.commonJS.isEmpty(this.inputForm.workBeginAndEndDate)) {
          this.inputForm.workBeginDate = this.inputForm.workBeginAndEndDate[0]
          this.inputForm.workEndDate = this.inputForm.workBeginAndEndDate[1]
        }
        if (this.inputForm.isFirst === '1') {
          this.inputForm.yearNum = ''
        }
        this.inputForm.status = '5'
        this.inputForm.createContractNo = '1'
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            if (this.$refs.uploadComponent.checkProgress()) {
              this.loading = false
              return
            }
            if (this.commonJS.isEmpty(this.inputForm.files)) {
              this.inputForm.files = []
            }
            this.inputForm.files = this.$refs.uploadComponent.getDataList()
            this.programProjectListInfoService.save2022(this.inputForm).then(({data}) => {
              this.close()
              this.$message.success(data)
              this.$emit('refreshDataList')
              this.loading = false
            }).catch(() => {
              this.loading = false
            })
          }
        })
      },
      getContract (row) {
        this.inputForm.contractId = row.id // 合同id
        this.inputForm.contractName = row.name // 合同名称
        this.inputForm.client = row.clientId // 委托方
        this.inputForm.clientName = row.clientName // 委托方名称
        this.inputForm.amount = row.contractAmount // 合同金额
        this.inputForm.contractType = row.contractType // 合同类型
        this.clearClientList()
        this.$forceUpdate()
      },
      openContractForm () {
        if (!this.commonJS.isEmpty(this.inputForm.contractId)) {
          this.$refs.contractForm.init(this.inputForm.contractId)
        } else {
          this.$refs.contractForm.init()
        }
      },
      openContactForm (deputy) {
        if (deputy === '0') {
          if (this.commonJS.isEmpty(this.inputForm.client) && this.commonJS.isEmpty(this.inputForm.propertyHolder)) {
            this.$message.error('请先填写合同委托方或委托人')
          } else {
            let ids = []
            if (this.inputForm.client === this.inputForm.propertyHolder) {
              ids.push(this.inputForm.client)
            } else if (this.commonJS.isEmpty(this.inputForm.client)) {
              ids.push(this.inputForm.propertyHolder)
            } else if (this.commonJS.isEmpty(this.inputForm.propertyHolder)) {
              ids.push(this.inputForm.client)
            } else {
              ids.push(this.inputForm.propertyHolder)
              ids.push(this.inputForm.client)
            }
            this.$nextTick(() => {
              this.$refs.contactForm.init(ids, deputy)
            })
          }
        }
      },
      getContact (rows, deputy) {
        if (deputy === '0') {
          rows.forEach(item => {
            this.inputForm.clientList.push(item)
          })
        }
        this.$forceUpdate()
      },
      openWorkClientForm (deputy) {
        if (!this.commonJS.isEmpty(this.inputForm.propertyHolder)) {
          this.$refs.workClientForm.init(this.inputForm.propertyHolder, deputy)
        } else {
          this.$refs.workClientForm.init(null, deputy)
        }
      },
      getWorkClient (row, deputy) {
        if (deputy === '2') {
          this.inputForm.propertyHolderName = row.name
          this.inputForm.propertyHolder = row.id
          this.clearEquityList()
        }
        if (deputy === '3') {
          this.inputForm.assessmentEnterpriseName = row.name
          this.inputForm.assessmentEnterprise = row.id
          this.clearContactList()
        }
        this.$forceUpdate()
      },
      close () {
        this.inputForm.clientList = []
        this.inputForm.equityList = []
        this.inputForm.contactList = []
        this.inputForm.members = []
        this.$refs.uploadComponent.clearUpload()
        this.$refs.inputForm.resetFields()
        this.visible = false
      },
      getTypeList () {
        this.programTypeDictService.getList().then(({data}) => {
          this.typeDictList = data
        })
      },
      // 删除
      removeEvent (row, rowIndex, type) {
        if (type === 'member') {
          this.$refs.memberTable.remove(row)
          this.inputForm.members.splice(rowIndex, 1)
        }
        if (type === 'contact') {
          this.$refs.contactTable.remove(row)
          this.inputForm.contactList.splice(rowIndex, 1)
        }
        if (type === 'equity') {
          this.$refs.equityTable.remove(row)
          this.inputForm.equityList.splice(rowIndex, 1)
        }
        if (type === 'client') {
          this.$refs.clientTable.remove(row)
          this.inputForm.clientList.splice(rowIndex, 1)
        }
      },
      // 新增
      // eslint-disable-next-line no-dupe-keys
      async insertEvent (type) {
        if (type === 'member') {
          await this.$refs.memberTable.insert().then((data) => {
            this.inputForm.members.push(data)
          })
        }
        if (type === 'contact') {
          await this.$refs.contactTable.insert().then((data) => {
            this.inputForm.contactList.push(data)
          })
        }
        if (type === 'equity') {
          await this.$refs.equityTable.insert().then((data) => {
            this.inputForm.equityList.push(data)
          })
        }
        if (type === 'client') {
          await this.$refs.clientTable.insert().then((data) => {
            this.inputForm.clientList.push(data)
          })
        }
      },
      clearContactList () {
        // 被评估企业对接联系人列表清除
        this.inputForm.contactList = []
        this.$forceUpdate()
      },
      clearEquityList () {
        // 产权持有人联系人列表清除
        this.inputForm.equityList = []
        this.$forceUpdate()
      },
      clearClientList () {
        // 项目直接对接联系人列表清除
        this.inputForm.clientList = []
        this.$forceUpdate()
      },
      footerMethod ({ columns, data }) {
        const footerData = [
          columns.map((column, columnIndex) => {
            if (columnIndex === 0) {
              return '工时总计'
            }
            if (['workHours'].includes(column.property)) {
              // eslint-disable-next-line no-undef
              return XEUtils.sum(data, column.property)
            }
            return null
          })
        ]
        return footerData
      },
      tabHandleClick (event) {
        if (event.name === 'archive') {
          if (this.commonJS.isNotEmpty(this.archiveIdA)) {
            this.$refs.projectArchiveForm.init('view', this.archiveIdA)
          }
        }
      }
    }
  }
</script>
<style scoped>
  /deep/ .el-input-number .el-input__inner {
    text-align: left;
  }
</style>
