<template>
  <div class="app-container">
    <sticky style="margin-bottom: 10px">
      <div class="sub-navbar">
        <template v-if="formState !== 'finish'">
          <el-button
            v-if="formState === 'create' || formState === 'newBuild'"
            type="success"
            icon="el-icon-edit"
            size="medium"
            @click="bjdSave(false)"
          >
            保存并核价
          </el-button>
          <template v-else>
            <el-button
              type="success"
              icon="el-icon-edit"
              size="medium"
              @click="bjdSave(false)"
            >
              更新/核价
            </el-button>
            <el-button
              type="warning"
              icon="el-icon-circle-check-outline"
              size="medium"
              @click="bjdSave(true)"
            >
              审核通过
            </el-button>
          </template>
          <template v-if="bjd.shebeizhongleidaima === 'F000'">
            <el-upload
              :show-file-list="false"
              :before-upload="beforeUpload"
              style="display: inline-flex"
              action=""
            >
              <el-button type="primary" size="medium" icon="el-icon-upload">
                导入数据
              </el-button>
            </el-upload>
          </template>
        </template>
        <template v-else>
          <el-button
            type="success"
            icon="el-icon-printer"
            size="medium"
            @click="viewHzd"
          >
            打印回执单
          </el-button>
          <el-button
            v-if="form==1"
            type="success"
            icon="el-icon-printer"
            size="medium"
            @click="modifyFn"
          >
            修改
          </el-button>
        </template>

        <el-button
          type="success"
          icon="el-icon-download"
          size="medium"
          style="margin-right: 10px"
          @click="common.downloadTemplate(templateName)"
        >
          下载导入模板
        </el-button>
      </div>
    </sticky>
    <el-tabs v-model="checkedTab" type="border-card">
      <el-tab-pane label="检验项目申请表" name="first">
        <el-form
          ref="bjd"
          :model="bjd"
          :rules="rules"
          class="small-space"
          label-position="right"
          label-width="120px"
        >
          <fieldset>
            <legend>设备种类</legend>
            <el-row :gutter="20">
              <el-col :span="9">
                <el-form-item label="检验类别:" prop="jianyanleibie">
                  <el-input v-model="jianyanleibie" type="text" disabled />
                  <el-input
                    v-show="false"
                    v-model="bjd.jianyanleibie"
                    type="text"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="9">
                <el-form-item label="设备种类:" prop="shebeizhonglei">
                  <el-input v-model="bjd.shebeizhonglei" disabled />
                </el-form-item>
              </el-col>
            </el-row>
          </fieldset>
          <fieldset>
            <legend>基本信息</legend>
            <el-row :gutter="20">
              <el-col :span="18">
                <el-form-item
                  :rules="[
                    {
                      required: true,
                      message: '使用单位不能为空',
                      trigger: 'change',
                    },
                  ]"
                  label="使用单位:"
                  prop="shiyongdanwei"
                >
                  <el-input v-model="bjd.shiyongdanwei" type="text" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="18">
                <el-form-item label="单位地址:" prop="shiyongdanweiDizhi">
                  <el-input v-model="bjd.shiyongdanweiDizhi" type="text" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="9">
                <el-form-item label="联系人:" prop="shiyongdanweiLianxiren">
                  <el-input v-model="bjd.shiyongdanweiLianxiren" type="text" />
                </el-form-item>
              </el-col>
              <el-col :span="9">
                <el-form-item
                  :rules="[
                    {
                      required: true,
                      message: '使用单位电话不能为空',
                      trigger: 'change',
                    },
                  ]"
                  label="联系电话:"
                  prop="shiyongdanweiDianhua"
                >
                  <el-input v-model="bjd.shiyongdanweiDianhua" type="text" />
                </el-form-item>
              </el-col>
            </el-row>
            <div v-if="bjd.shebeizhongleidaima !== 'F000'">
              <el-row v-if="bjd.shebeizhongleidaima === '1000'" :gutter="20">
                <el-col :span="18">
                  <el-form-item label="安装单位:" prop="shigongdanwei">
                    <el-input v-model="bjd.shigongdanwei" type="text" />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row v-else :gutter="20">
                <el-col :span="18">
                  <el-form-item label="施工单位:" prop="shigongdanwei">
                    <el-input v-model="bjd.shigongdanwei" type="text" />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row v-if="!(bjd.shebeizhongleidaima === '1000')" :gutter="20">
                <el-col :span="9">
                  <el-form-item label="联系人:" prop="shigongdanweiLianxiren">
                    <el-input
                      v-model="bjd.shigongdanweiLianxiren"
                      type="text"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="9">
                  <el-form-item label="联系电话:" prop="shigongdanweiDianhua">
                    <el-input v-model="bjd.shigongdanweiDianhua" type="text" />
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
            <div v-if="bjd.shebeizhongleidaima === '3000'">
              <el-row :gutter="20">
                <el-col :span="18">
                  <el-form-item
                    :rules="[
                      {
                        required: true,
                        message: '维保单位不能为空',
                        trigger: 'change',
                      },
                    ]"
                    label="维保单位:"
                    prop="weibaodanwei"
                  >
                    <el-input v-model="bjd.weibaodanwei" type="text" />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="20">
                <el-col :span="9">
                  <el-form-item label="联系人:" prop="weibaodanweiLianxiren">
                    <el-input v-model="bjd.weibaodanweiLianxiren" type="text" />
                  </el-form-item>
                </el-col>
                <el-col :span="9">
                  <el-form-item
                    :rules="[
                      {
                        required: true,
                        message: '维保单位电话不能为空',
                        trigger: 'change',
                      },
                    ]"
                    label="联系电话:"
                    prop="weibaodanweiDianhua"
                  >
                    <el-input v-model="bjd.weibaodanweiDianhua" type="text" />
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
            <el-row :gutter="20">
              <el-col :span="9">
                <el-form-item
                  :rules="[
                    {
                      required: true,
                      message: '区划名称不能为空',
                      trigger: 'change',
                    },
                  ]"
                  label="区划名称:"
                  prop="quhuadaima"
                >
                  <el-select
                    v-model="bjd.quhuadaima"
                    placeholder="请选择"
                    style="width: 240px"
                    @change="quhuaChange"
                  >
                    <el-option
                      v-for="item in areas"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="9">
                <el-form-item
                  :rules="[
                    {
                      required: true,
                      message: '检验科室不能为空',
                      trigger: 'change',
                    },
                  ]"
                  label="检验科室:"
                  prop="departmentId"
                >
                  <el-select
                    v-model="bjd.departmentId"
                    placeholder="请选择"
                    style="width: 240px"
                  >
                    <el-option
                      v-for="dept in depts"
                      :key="dept.id"
                      :label="dept.name"
                      :value="dept.id"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="9">
                <el-form-item
                  :rules="[
                    {
                      required: true,
                      message: '报检日期不能为空',
                      trigger: 'change',
                    },
                  ]"
                  label="报检日期:"
                  prop="baojianriqi"
                >
                  <el-date-picker
                    v-model="bjd.baojianriqi"
                    type="datetime"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    placeholder="选择日期"
                    style="width: 98%"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="9">
                <el-form-item
                  :rules="[
                    {
                      required: true,
                      message: '受理日期不能为空',
                      trigger: 'change',
                    },
                  ]"
                  label="受理日期:"
                  prop="shouliriqi"
                >
                  <el-date-picker
                    v-model="bjd.shouliriqi"
                    type="datetime"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    placeholder="选择日期"
                    style="width: 95%"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row v-if="!(bjd.shebeizhongleidaima === '1000')" :gutter="20">
              <el-col :span="18">
                <el-form-item label="备注:" prop="beizhu">
                  <el-input
                    v-model="bjd.bjdBeizhu"
                    :autosize="{ minRows: 2, maxRows: 2 }"
                    type="textarea"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <div v-if="formState === 'finish'" style="margin-top: 10px">
              <el-row>
                <el-col :span="9">
                  <el-form-item label="受理人:" prop="shouliren">
                    <el-input v-model="bjd.shouliren" type="text" />
                  </el-form-item>
                </el-col>
                <el-col :span="9">
                  <el-form-item label="最后修改人:" prop="updateBy">
                    <el-input v-model="bjd.updateBy" type="text" />
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
            <el-row :gutter="20">
              <el-col :span="9">
                <el-form-item
                  :rules="[
                    {
                      required: true,
                      message:
                        '报检人电话不能为空，该电话用于短信通知企业网上交费。',
                      trigger: 'change',
                    },
                  ]"
                  label="报检人电话:"
                  prop="baojiandianhua"
                >
                  <el-input v-model="bjd.baojiandianhua" type="text" />
                </el-form-item>
              </el-col>
            </el-row>
          </fieldset>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="申请检验设备明细表" name="second">
        <template v-if="formState !== 'finish'">
          <template v-if="sbzldm !== 'F000'">
            <el-button
              type="success"
              size="mini"
              icon="el-icon-edit"
              @click="openDialogParam()"
            >
              批量修改
            </el-button>
            <el-button
              type="success"
              size="mini"
              icon="el-icon-circle-plus-outline"
              @click="addRow"
            >
              查找添加
            </el-button>
          </template>
          <el-button
            type="success"
            size="mini"
            icon="el-icon-circle-plus-outline"
            @click="addRowN"
          >
            手动添加
          </el-button>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-remove-outline"
            @click="delRow()"
          >
            删除
          </el-button>
          <span
            style="margin-left: 20px; color: red"
          >如若【设备类别】相同，只编辑填写第一条即可！</span>
        </template>
        <el-switch
          v-model="fixedFlag"
          active-color="#13ce66"
          inactive-color="#ff4949"
          @change="editFixedHidden()"
        />
        <el-table
          ref="bjBaseList"
          v-loading="loading"
          v-adaptive="{ bottomOffset: 50 }"
          height="0"
          :data="tableData"
          :row-class-name="tableRowClassName"
          fit
          highlight-current-row
          border
          stripe
          style="width: 100%; margin-top: 5px"
          @row-click="onRowClick"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="40" />
          <el-table-column
            align="center"
            type="index"
            width="50"
            label="序号"
          />
          <el-table-column
            align="center"
            prop="neibuleibie"
            min-width="130"
            label="检验类别(项目)"
          >
            <template slot-scope="scope">
              <el-select v-model="scope.row.neibuleibie" placeholder="请选择">
                <el-option
                  v-for="item in jylbs"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code"
                />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="sblbpz"
            min-width="200"
            label="设备类别/品种"
          >
            <template slot-scope="scope">
              <el-cascader
                :ref="'cascader' + scope.$index"
                v-model="scope.row.sblbpz"
                :options="sblbList"
                style="width: 100%"
                @change="sblbpzChange(scope.$index)"
              />
            </template>
          </el-table-column>
          <!-- 通用：1000、2000、3000、4000、5000 -->
          <template
            v-if="
              sbzldm === '1000' ||
                sbzldm === '2000' ||
                sbzldm === '3000' ||
                sbzldm === '4000' ||
                sbzldm === '5000'
            "
          >
            <el-table-column
              v-if="sbzldm !== '1000'"
              align="center"
              prop="shebeimingcheng"
              min-width="120"
              label="设备名称"
            >
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.shebeimingcheng"
                  placeholder="请输入内容"
                />
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="danweineibubianhao"
              min-width="100"
              label="单位内编号"
            >
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.danweineibubianhao"
                  placeholder="请输入内容"
                />
              </template>
            </el-table-column>
            <el-table-column
              v-if="sbzldm !== '1000'"
              align="center"
              prop="zhucedaima"
              min-width="225"
              label="注册代码"
              sortable
            >
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.zhucedaima"
                  placeholder="请输入内容"
                />
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="shiyongdengjibianhao"
              min-width="135"
              label="使用登记编号"
            >
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.shiyongdengjibianhao"
                  placeholder="请输入内容"
                />
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="chanpibianhao"
              min-width="135"
              label="出厂编号"
            >
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.chanpinbianhao"
                  placeholder="请输入内容"
                />
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="guigexinghao"
              min-width="135"
              label="规格型号"
            >
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.guigexinghao"
                  placeholder="请输入内容"
                />
              </template>
            </el-table-column>
          </template>
          <!-- 压力容器：2000 -->
          <template v-if="sbzldm === '2000'">
            <el-table-column
              align="center"
              prop="xiacijianyanriqi"
              min-width="150"
              label="下次检验日期"
            >
              <template slot-scope="scope">
                <el-date-picker
                  v-model="scope.row.xiacijianyanriqi"
                  type="date"
                  value-format="yyyy-MM-dd"
                  style="width: 150px"
                />
              </template>
            </el-table-column>
          </template>
          <!-- 压力管道：8000 -->
          <template v-else-if="sbzldm === '8000'">
            <el-table-column
              align="center"
              prop="shebeimingcheng"
              min-width="120"
              label="装置名称"
            >
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.shebeimingcheng"
                  placeholder="请输入内容"
                />
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="shiyongdengjibianhao"
              min-width="175"
              label="使用登记编号"
            >
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.shiyongdengjibianhao"
                  placeholder="请输入内容"
                />
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="xiacijianyanriqi"
              min-width="150"
              label="到期时间"
            >
              <template slot-scope="scope">
                <el-date-picker
                  v-model="scope.row.xiacijianyanriqi"
                  type="date"
                  value-format="yyyy-MM-dd"
                  style="width: 150px"
                />
              </template>
            </el-table-column>
          </template>
          <!-- 安全阀：F000 -->
          <template v-else-if="sbzldm === 'F000'">
            <el-table-column
              align="center"
              prop="zhizaodanwei"
              min-width="200"
              label="制造单位"
            >
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.zhizaodanwei"
                  placeholder="请输入内容"
                />
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="zhizaoxukezhengbianhao"
              min-width="150"
              label="制造许可证编号"
            >
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.zhizaoxukezhengbianhao"
                  placeholder="请输入内容"
                />
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="chanpinbianhao"
              min-width="150"
              label="产品编号"
            >
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.chanpinbianhao"
                  placeholder="请输入内容"
                />
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="s35"
              min-width="200"
              label="安装位置"
            >
              <template slot-scope="scope">
                <el-input v-model="scope.row.s35" placeholder="请输入内容" />
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="s8"
              min-width="170"
              label="出厂日期"
            >
              <template slot-scope="scope">
                <el-date-picker
                  v-model="scope.row.s8"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="选择日期"
                  style="width: 100%"
                />
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="s16"
              min-width="120"
              label="安全阀类型"
            >
              <template slot-scope="scope">
                <el-input v-model="scope.row.s16" placeholder="请输入内容" />
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="s3"
              min-width="150"
              label="安全阀型号"
            >
              <template slot-scope="scope">
                <el-input v-model="scope.row.s3" placeholder="请输入内容" />
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="s4"
              min-width="120"
              label="公称通径"
            >
              <template slot-scope="scope">
                <el-input v-model="scope.row.s4" placeholder="请输入内容" />
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="s5"
              min-width="120"
              label="阀座口径(mm)"
            >
              <template slot-scope="scope">
                <el-input v-model="scope.row.s5" placeholder="请输入内容" />
              </template>
            </el-table-column>
            <el-table-column align="center" label="压力级别范围(MPa)">
              <el-table-column
                align="center"
                prop="s6"
                min-width="120"
                label="起始范围(MPa)"
              >
                <template slot-scope="scope">
                  <el-input v-model="scope.row.s6" placeholder="请输入内容" />
                </template>
              </el-table-column>
              <el-table-column
                align="center"
                prop="s7"
                min-width="120"
                label="结束范围(MPa)"
              >
                <template slot-scope="scope">
                  <el-input v-model="scope.row.s7" placeholder="请输入内容" />
                </template>
              </el-table-column>
            </el-table-column>
            <el-table-column
              align="center"
              prop="s36"
              min-width="120"
              label="设计压力"
            >
              <template slot-scope="scope">
                <el-input v-model="scope.row.s36" placeholder="请输入内容" />
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="s17"
              min-width="120"
              label="工作压力"
            >
              <template slot-scope="scope">
                <el-input v-model="scope.row.s17" placeholder="请输入内容" />
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="s2"
              min-width="120"
              label="工作介质"
            >
              <template slot-scope="scope">
                <el-input v-model="scope.row.s2" placeholder="请输入内容" />
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="s1"
              min-width="120"
              label="要求整定压力"
            >
              <template slot-scope="scope">
                <el-input v-model="scope.row.s1" placeholder="请输入内容" />
              </template>
            </el-table-column>
          </template>
          <!-- 第一次进来不对核定参数展示 后台进行信息提取后在做核价处理 -->
          <template v-if="formState !== 'create'">
            <template v-if="sbzldm === '1000'">
              <el-table-column
                align="center"
                prop="edingchuli"
                label="额定出力"
                fixed="right"
              >
                <template slot-scope="scope">
                  <el-input v-model="scope.row.s3" placeholder="请输入" />
                </template>
              </el-table-column>
              <el-table-column
                align="center"
                prop="edingyali"
                label="额定压力"
                fixed="right"
              >
                <template slot-scope="scope">
                  <el-input v-model="scope.row.s4" placeholder="请输入" />
                </template>
              </el-table-column>
              <el-table-column
                align="center"
                prop="chukouwendu"
                label="出口温度"
                fixed="right"
              >
                <template slot-scope="scope">
                  <el-input v-model="scope.row.s7" placeholder="请输入" />
                </template>
              </el-table-column>
            </template>
            <template v-if="sbzldm === '2000'">
              <el-table-column
                align="center"
                prop="rongji"
                label="容积"
                fixed="right"
              >
                <template slot-scope="scope">
                  <el-input v-model="scope.row.s5" placeholder="请输入" />
                </template>
              </el-table-column>
            </template>
            <template v-if="sbzldm === '3000'">
              <el-table-column
                align="center"
                prop="ceng"
                label="层"
                fixed="right"
              >
                <template slot-scope="scope">
                  <el-input
                    v-model="scope.row.s1"
                    placeholder="请输入"
                    @keyup.enter.native="beforeHejia"
                    @blur="beforeHejia"
                  />
                </template>
              </el-table-column>
              <el-table-column
                align="center"
                prop="quduanchangdu"
                label="区段长度"
                fixed="right"
              >
                <template slot-scope="scope">
                  <el-input
                    v-model="scope.row.s14"
                    placeholder="请输入"
                    @keyup.enter.native="beforeHejia"
                    @blur="beforeHejia"
                  />
                </template>
              </el-table-column>
              <el-table-column
                align="center"
                prop="tishenggaodu"
                label="提升高度"
                fixed="right"
              >
                <template slot-scope="scope">
                  <el-input
                    v-model="scope.row.s16"
                    placeholder="请输入"
                    @keyup.enter.native="beforeHejia"
                    @blur="beforeHejia"
                  />
                </template>
              </el-table-column>
              <el-table-column
                align="center"
                prop="gonggongjiaotong"
                label="公共交通"
                fixed="right"
              >
                <template slot-scope="scope">
                  <el-select
                    v-model="scope.row.ss1"
                    placeholder="请选择"
                    @change="beforeHejia"
                  >
                    <el-option label="×" value="0" />
                    <el-option label="√" value="1" />
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column
                align="center"
                prop="fujian"
                label="复检"
                fixed="right"
              >
                <template slot-scope="scope">
                  <el-select
                    v-model="scope.row.ss2"
                    placeholder="请选择"
                    @change="beforeHejia"
                  >
                    <el-option label="×" value="0" />
                    <el-option label="√" value="1" />
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column
                align="center"
                prop="xiansuqi"
                label="限速器"
                fixed="right"
              >
                <template slot-scope="scope">
                  <el-select
                    v-model="scope.row.ss3"
                    placeholder="请选择"
                    @change="beforeHejia"
                  >
                    <el-option label="不校验" value="0" />
                    <el-option label="校验" value="1" />
                  </el-select>
                </template>
              </el-table-column>
            </template>
            <template v-if="sbzldm === '4000'">
              <el-table-column
                align="center"
                prop="kuadu"
                label="跨度"
                fixed="right"
              >
                <template slot-scope="scope">
                  <el-input
                    v-model="scope.row.s1"
                    placeholder="请输入"
                    @keyup.enter.native="beforeHejia"
                    @blur="beforeHejia"
                  />
                </template>
              </el-table-column>
              <el-table-column
                align="center"
                prop="edingqizhongliang"
                label="额定起重量"
                fixed="right"
              >
                <template slot-scope="scope">
                  <el-input
                    v-model="scope.row.s2"
                    placeholder="请输入"
                    @keyup.enter.native="beforeHejia"
                    @blur="beforeHejia"
                  />
                </template>
              </el-table-column>
              <el-table-column
                align="center"
                prop="edingqizhongliju"
                label="额定起重力矩"
                fixed="right"
              >
                <template slot-scope="scope">
                  <el-input
                    v-model="scope.row.s3"
                    placeholder="请输入"
                    @keyup.enter.native="beforeHejia"
                    @blur="beforeHejia"
                  />
                </template>
              </el-table-column>
              <el-table-column
                align="center"
                prop="qishenggaodu"
                label="起升高度"
                fixed="right"
              >
                <template slot-scope="scope">
                  <el-input
                    v-model="scope.row.s4"
                    placeholder="请输入"
                    @keyup.enter.native="beforeHejia"
                    @blur="beforeHejia"
                  />
                </template>
              </el-table-column>
              <el-table-column
                align="center"
                prop="boweishu"
                label="存车容量"
                fixed="right"
              >
                <template slot-scope="scope">
                  <el-input v-model="scope.row.s12" placeholder="请输入" />
                </template>
              </el-table-column>
              <el-table-column
                align="center"
                prop="eliehuanjing"
                label="恶劣检验环境"
                fixed="right"
              >
                <template slot-scope="scope">
                  <el-select v-model="scope.row.ss1" placeholder="请选择">
                    <el-option label="×" value="0" />
                    <el-option label="√" value="1" />
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column
                align="center"
                prop="sijishi"
                label="有司机室"
                fixed="right"
              >
                <template slot-scope="scope">
                  <el-select
                    v-model="scope.row.ss2"
                    placeholder="请选择"
                    @change="beforeHejia"
                  >
                    <el-option label="×" value="0" />
                    <el-option label="√" value="1" />
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column
                align="center"
                prop="fugan"
                label="副杆"
                fixed="right"
              >
                <template slot-scope="scope">
                  <el-select v-model="scope.row.ss3" placeholder="请选择">
                    <el-option label="×" value="0" />
                    <el-option label="√" value="1" />
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column
                align="center"
                prop="fujian"
                label="复检"
                fixed="right"
              >
                <template slot-scope="scope">
                  <el-select
                    v-model="scope.row.ss4"
                    placeholder="请选择"
                    @change="beforeHejia"
                  >
                    <el-option label="×" value="0" />
                    <el-option label="√" value="1" />
                  </el-select>
                </template>
              </el-table-column>
            </template>
            <template v-if="sbzldm === '5000'">
              <el-table-column
                align="center"
                prop="edingqizhongliang"
                mini-width="100"
                label="额定起重量"
                fixed="right"
              >
                <template slot-scope="scope">
                  <el-input
                    v-model="scope.row.s1"
                    placeholder="请输入"
                    @keyup.enter.native="beforeHejia"
                    @blur="beforeHejia"
                  />
                </template>
              </el-table-column>
              <el-table-column
                align="center"
                prop="paizhaohaoma"
                mini-width="140"
                label="厂车牌照编号"
                fixed="right"
              >
                <template slot-scope="scope">
                  <el-input v-model="scope.row.s5" placeholder="请输入" />
                </template>
              </el-table-column>
              <el-table-column
                align="center"
                prop="ranliaozhonglei"
                mini-width="120"
                label="燃料种类"
                fixed="right"
              >
                <template slot-scope="scope">
                  <el-select v-model="scope.row.s6" placeholder="请选择">
                    <el-option label="汽油" value="汽油" />
                    <el-option label="柴油" value="柴油" />
                    <el-option label="电" value="电" />
                    <el-option label="液化气" value="液化气" />
                  </el-select>
                </template>
              </el-table-column>
            </template>
            <template v-if="sbzldm === '8000'">
              <el-table-column
                align="center"
                prop="guandaochangdu"
                min-width="175"
                label="管道长度(m)/数量(条)"
                fixed="right"
              >
                <template slot-scope="scope">
                  <el-input v-model="scope.row.s32" placeholder="请输入内容" />
                </template>
              </el-table-column>
            </template>
            <el-table-column
              v-if="formState !== 'create' && formState !== 'newBuild'"
              prop="xitongheding"
              min-width="90"
              label="标准费用"
              fixed="right"
            >
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.xitongheding"
                  placeholder="待核定"
                  disabled
                />
              </template>
            </el-table-column>
            <el-table-column
              v-if="formState !== 'create' && formState !== 'newBuild'"
              prop="datingheding"
              min-width="90"
              label="核定费用"
              fixed="right"
            >
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.datingheding"
                  placeholder="请输入"
                />
              </template>
            </el-table-column>
          </template>
        </el-table>
      </el-tab-pane>
      <!-- 修改检验类别/设备类别/设备品种方法 -->
      <el-dialog
        :title="editParamTitle"
        :visible.sync="dialogFormParam"
        @close="dialogFormParam = false"
      >
        <el-form
          ref="param"
          :model="param"
          class="small-space"
          label-position="right"
          label-width="120px"
        >
          <div v-if="formState !== 'finish'">
            <el-row :gutter="20">
              <el-col :span="8">
                <el-form-item label="检验类别:" prop="neibuleibie">
                  <el-select v-model="param.neibuleibie" placeholder="请选择">
                    <el-option
                      v-for="item in jylbs"
                      :key="item.code"
                      :label="item.name"
                      :value="item.code"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="设备类别:" prop="sblb">
                  <el-select
                    v-model="param.sblb"
                    value-key="value"
                    @change="sblbChange"
                  >
                    <el-option
                      v-for="item in sblbList"
                      :key="item.value"
                      :label="item.label"
                      :value="item"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="设备品种:" prop="sbpz">
                  <el-select v-model="param.sbpz" value-key="value">
                    <el-option
                      v-for="item in sbpzList"
                      :key="item.value"
                      :label="item.label"
                      :value="item"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="8">
                <el-form-item label="核定价格" prop="datingheding">
                  <el-input v-model="param.datingheding" />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="是否复检" prop="ss2">
                  <el-select
                    v-model="param.ss2"
                    placeholder="请选择"
                    @change="beforeHejia"
                  >
                    <el-option label="×" value="0" />
                    <el-option label="√" value="1" />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </div>
        </el-form>
        <div style="color: red">
          根据选中的条数进行修改，否则全部修改！
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormParam = false">
            取 消
          </el-button>
          <el-button type="primary" @click="editRow">
            确 定
          </el-button>
        </div>
      </el-dialog>
      <el-dialog
        title="设备列表"
        width="90%"
        :visible.sync="baseListVisible"
        @close="baseListVisible = false"
      >
        <el-input
          v-model="listQuery.shiyongdanwei"
          placeholder="使用单位名称"
          clearable
          style="width: 200px"
          @keyup.enter.native="getBaseList"
        />
        <el-input
          v-model="listQuery.shigongdanwei"
          placeholder="施工单位名称"
          clearable
          style="width: 200px"
          @keyup.enter.native="getBaseList"
        />
        <el-input
          v-model="listQuery.zhucedaima"
          placeholder="注册代码"
          clearable
          style="width: 220px"
          @keyup.enter.native="getBaseList"
        />
        <el-input
          v-model="listQuery.shiyongdengjibianhao"
          placeholder="使用登记证编号"
          clearable
          style="width: 160px"
          @keyup.enter.native="getBaseList"
        />
        <el-input
          v-model="listQuery.danweineibubianhao"
          placeholder="单位内部编号"
          clearable
          style="width: 160px"
          @keyup.enter.native="getBaseList"
        />
        <el-input
          v-model="listQuery.chanpinbianhao"
          placeholder="出厂编号"
          clearable
          style="width: 160px"
          @keyup.enter.native="getBaseList"
        />
        <el-button type="primary" icon="el-icon-search" @click="getBaseList">
          查询
        </el-button>
        <el-button type="info" icon="el-icon-clear" @click="clearSearch">
          清除
        </el-button>
        <el-button
          type="success"
          size="small"
          icon="el-icon-circle-plus-outline"
          @click="batchAddToBjBase"
        >
          添加
        </el-button>
        <el-table
          ref="baseTable"
          v-adaptive="{ bottomOffset: 50 }"
          height="0"
          :data="baseList"
          border
          fit
          highlight-current-row
          stripe
          :row-class-name="tableRowClassName"
          @selection-change="baseSelectionChange"
          @row-click="checkSelection"
        >
          <el-table-column type="selection" width="45" />
          <el-table-column align="center" label="序号" width="40">
            <template slot-scope="scope">
              <span v-text="getIndex(scope.$index)" />
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="设备种类"
            prop="shebeizhonglei"
            min-width="80"
          />
          <el-table-column
            align="center"
            label="使用单位"
            prop="shiyongdanwei"
            min-width="150"
          />
          <el-table-column
            :formatter="clearBlank"
            align="center"
            label="注册代码"
            prop="zhucedaima"
            min-width="180"
          />
          <el-table-column
            :formatter="clearBlank"
            align="center"
            label="使用登记证编号"
            prop="shiyongdengjibianhao"
            min-width="90"
          />
          <el-table-column
            :formatter="clearBlank"
            align="center"
            label="出厂编号"
            prop="chanpinbianhao"
            min-width="80"
          />
          <el-table-column
            :formatter="clearBlank"
            align="center"
            label="单位内部编号"
            prop="danweineibubianhao"
            min-width="60"
          />
          <el-table-column
            :formatter="clearBlank"
            align="center"
            label="下次检验日期"
            prop="xiacijianyanriqi"
            min-width="100"
          />
          <el-table-column
            :formatter="clearBlank"
            align="center"
            label="区划名称"
            prop="quhuamingcheng"
            min-width="50"
          />
          <el-table-column align="center" label="操作" min-width="50">
            <template slot-scope="scope">
              <el-button
                type="primary"
                size="small"
                icon="el-icon-circle-plus-outline"
                @click="addToBjBase(scope.$index)"
              />
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="totalCount > 0"
          :total="totalCount"
          :page-num.sync="listQuery.pageNum"
          :page-row.sync="listQuery.pageRow"
          @pagination="getBaseList"
        />
      </el-dialog>
      <el-dialog
        title="邮寄信息"
        width="800px"
        :visible.sync="formYJVisible"
        @close="formYJVisible = false"
      >
        <el-form
          ref="postInfo"
          :model="postInfo"
          label-position="right"
          label-width="120px"
        >
          <el-row :gutter="20">
            <el-col :span="22">
              <el-form-item label="使用单位:" prop="shiyongdanwei">
                <el-input
                  v-model="postInfo.shiyongdanwei"
                  type="text"
                  disabled
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="11">
              <el-form-item label="收件人姓名:" prop="nickname">
                <el-input v-model="postInfo.nickname" type="text" />
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item label="收件人电话:" prop="tel">
                <el-input v-model="postInfo.tel" type="text" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="22">
              <el-form-item label="收件人地址:" prop="addr">
                <el-input v-model="postInfo.addr" type="text" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="formYJVisible = false">取 消</el-button>
          <el-button type="primary" @click="savePostInfo()">确 定</el-button>
        </span>
      </el-dialog>
    </el-tabs>
    <iframe
      :src="printbutton"
      oncancel="onafterprint"
      frameborder="0"
      scrolling="no"
      height="0px"
      aria-disabled="true"
    />
  </div>
</template>
<script>
import Sticky from '@/components/Sticky'
import Utils from '../../utils/contact.js'
import Pagination from '@/components/Pagination'
// import XLSX from 'xlsx'
export default {
  name: 'AddProjectDJ',
  components: { Sticky, Pagination },
  data() {
    return {
      bjd: {
        id: '',
        departmentId: '',
        shiyongdanwei: '',
        shiyongdanweiDizhi: '',
        shiyongdanweiLianxiren: '',
        shiyongdanweiDianhua: '',
        shigongdanwei: '',
        shigongdanweiLianxiren: '',
        shigongdanweiDianhua: '',
        weibaodanwei: '',
        weibaodanweiLianxiren: '',
        weibaodanweiDianhua: '',
        jianyanleibie: 'DJ',
        shebeizhonglei: this.$route.params.sbzl,
        shebeizhongleidaima: this.$route.params.sbzldm,
        quhuamingcheng: '',
        quhuadaima: '',
        lingqufangshi: '自取',
        baojiandianhua: ''
      },
      param: {
        neibuleibie: '',
        shebeileibiedaima: '',
        sblb: '',
        sbpz: ''
      },
      sbzldm: this.$route.params.sbzldm,
      baseIds: '', // 省局设备主键
      tableData: [], // 参数列表
      jylbs: [], // 检验类别
      depts: [], // 检验科室
      areas: [], // 区划数组
      checkedTab: 'first',
      formState: this.$route.params.formState, // 验证是创建/修改/完成
      delRowIndex: [],
      selection: [],
      rules: {}, // 受理日期
      dialogFormParam: false,
      title: '核定价格',
      editParamTitle: '批量修改设备信息',
      sbpzList: [],
      baseListVisible: false,
      baseList: [],
      listQuery: {
        pageNum: 1, // 页码
        pageRow: 20, // 每页条数
        order: undefined, // 排序字段
        sort: undefined, // 排序方式
        shebeizhongleidaima: this.$route.params.sbzldm,
        shidaima: '3701'
      },
      jianyanleibie: '定期检验',
      totalCount: 0,
      baseSelection: [],
      formYJVisible: false,
      postInfo: {
        nickname: '',
        tel: '',
        addr: ''
      }, // 报告邮寄信息
      bjdDepartment: {
        shebeizhonglei: '',
        jianyanleibie: '',
        quhuamingcheng: ''
      },
      sblbList: [],
      printbutton: '',
      loading: false,
      templateName: '',
      fixedFlag: true,
      fixedWidth: '',
      isShengju: true,
      form: this.$route.params.form
    }
  },
  watch: {
    'bjd.shiyongdanwei': {
      handler(newVal, oldVal) {
        this.postInfo.shiyongdanwei = newVal
      },
      immediate: true
    }
  },
  created() {
    if (this.formState === 'newBuild') {
      this.isShengju = false
      this.tableData = []
      this.$set(this.bjd, 'shouliriqi', new Date())
      this.$set(this.bjd, 'baojianriqi', new Date())
      this.getSblb()
    } else if (this.formState === 'update' || this.formState === 'finish') {
      // 查询报检信息
      this.getBjdInfo()
    } else if (this.formState === 'create') {
      // 直接起草过来的数据
      this.getSblb()
      // 先加载当前的数据
      // this.tableData = this.$route.query.params
      this.getParamList(this.$route.query.params, true)
    }
    if (this.sbzldm === 'F000') {
      this.templateName = '安全阀报检导入模板.xlsx'
    }
    // 区划跟科室加载完成后读取关联数据
    if (this.formState === 'create' || this.formState === 'newBuild') {
      this.getBjdDepartment()
    }
    this.getJylb()
    this.getArea()
    this.getDepts()
  },
  methods: {
    getParamList(renwus, init) {
      this.apibjd({
        url: '/bjd/getParams',
        method: 'post',
        data: {
          paramList: renwus
        }
      }).then((data) => {
        this.tableData = data
        this.$refs.bjBaseList.doLayout()
        if (this.tableData !== null && this.tableData !== undefined) {
          // ***第一次起草
          if (init) {
            this.bjd = this.tableData[0] // 将第一条数据给报检单基本信息赋值
            this.$set(this.bjd, 'shouliriqi', new Date())
            this.$set(this.bjd, 'baojianriqi', new Date())
            this.bjd.lingqufangshi = '自取'
          }
          if (this.bjd.shebeizhongleidaima === '3000') {
            this.$set(this.bjd, 'baojiandianhua', this.bjd.weibaodanweiDianhua)
          }
          // 拿到省局主键
          for (let i = 0; i < this.tableData.length; i++) {
            // 初始化参数
            this.setDefaultValue(this.tableData[i])
          }
        }
      })
    },
    getBjdInfo() {
      this.apibjd({
        url: '/bjd/getBjdById',
        method: 'get',
        params: {
          bjdId: this.$route.params.id
        }
      }).then((data) => {
        this.bjd = data
        if (
          this.bjd.baojiandianhua === undefined ||
          this.bjd.baojiandianhua === null
        ) {
          this.bjd.baojiandianhua = this.bjd.weibaodanweiDianhua
        }
        this.getSblb()
        // 查询报检参数信息
        this.getParamById()
      })
    },
    getParamById() {
      this.apibjd({
        url: '/renwu/getParamByBjdId',
        method: 'get',
        params: {
          bjdId: this.bjd.id
        }
      }).then((data) => {
        this.tableData = data
        for (const tableObj of this.tableData) {
          const sblbpz = []
          // 如果设备类别代码不存在，或者设备类别代码是设备种类代码时
          const isNotExistSblbdm =
            !tableObj.shebeileibiedaima ||
            tableObj.shebeileibiedaima.substring(1, 2) === '0'
          // 如果存在注册代码
          if (isNotExistSblbdm && tableObj.zhucedaima) {
            // 如果注册代码上记录的设备类别代码是2010类型时，将调整为2100。
            if (tableObj.zhucedaima.substring(1, 2) === '0') {
              tableObj.shebeileibiedaima =
                tableObj.zhucedaima.substring(0, 1) +
                tableObj.zhucedaima.substring(2, 3) +
                '00'
            } else {
              // 正常从注册代码中提取并拼接为设备类别代码
              tableObj.shebeileibiedaima =
                tableObj.zhucedaima.substring(0, 2) + '00'
            }
          }
          // 如果设备品种代码不存在，或者设备品种代码是设备类别代码时
          const isNotExistSbpzdm =
            !tableObj.shebeipinzhongdaima ||
            tableObj.shebeipinzhongdaima.substring(2, 3) === '0'
          // 如果存在注册代码时
          if (isNotExistSbpzdm && tableObj.zhucedaima) {
            tableObj.shebeipinzhongdaima = tableObj.zhucedaima.substring(0, 4)
          }
          if (
            tableObj.shebeipinzhongdaima === '2140' ||
            tableObj.shebeipinzhongdaima === '2110' ||
            tableObj.shebeipinzhongdaima === '2120'
          ) {
            tableObj.shebeipinzhongdaima = '2170'
          }
          sblbpz.push(tableObj.shebeileibiedaima)
          sblbpz.push(tableObj.shebeipinzhongdaima)
          this.$set(tableObj, 'sblbpz', sblbpz)
        }
        this.$nextTick(() => {
          this.$refs.bjBaseList.doLayout()
        })
        if (this.formState !== 'finish') {
          this.beforeHejia()
        }
      })
    },
    // 报检设备设置默认值
    setDefaultValue(obj) {
      const sblbpz = []
      this.$set(obj, 'neibuleibie', 'DQ')
      if (this.bjd.shebeizhongleidaima === '1000') {
        this.$set(obj, 'neibuleibie', 'NB')
      }
      // 设备类别代码初始化（存在省局有设备代码是错误的情况）
      const testlb = new RegExp('^[1-9][1-9][0][0]$')
      const testpz = new RegExp('^[1-9][1-9][1-9][0]$')
      if (!testlb.test(obj.shebeileibiedaima)) {
        obj.shebeileibiedaima = this.sbzldm.substring(0, 1) + '100'
        if (obj.zhucedaima.length > 10) {
          const testzcdm = obj.zhucedaima.substring(0, 4)
          const testzcdma =
            testzcdm.substring(1, 2) === '0'
              ? testzcdm.substring(2, 3)
              : testzcdm.substring(1, 2)
          if (testzcdma != null) {
            obj.shebeipinzhongdaima = testzcdm
            if (testzcdma !== '0') {
              obj.shebeileibiedaima =
                this.sbzldm.substring(0, 1) + testzcdma + '00'
            }
          }
        }
      }
      if (!testpz.test(obj.shebeipinzhongdaima)) {
        obj.shebeipinzhongdaima =
          this.sbzldm.substring(0, 1) +
          obj.shebeileibiedaima.substring(1, 2) +
          '10'
      }
      if (obj.shebeipinzhongdaima !== '' && obj.shebeipinzhongdaima !== null) {
        if (
          obj.shebeipinzhongdaima === '2140' ||
          obj.shebeipinzhongdaima === '2110' ||
          obj.shebeipinzhongdaima === '2120'
        ) {
          obj.shebeipinzhongdaima = '2170'
        }
      }
      // 将关键信息默认‘/’
      if (
        obj.danweineibubianhao === 'null' ||
        obj.danweineibubianhao === '' ||
        obj.danweineibubianhao === null
      ) {
        obj.danweineibubianhao = '/'
      }
      if (
        obj.zhucedaima === 'null' ||
        obj.zhucedaima === '' ||
        obj.zhucedaima === null
      ) {
        obj.zhucedaima = '/'
      }
      if (
        obj.shiyongdengjibianhao === 'null' ||
        obj.shiyongdengjibianhao === '' ||
        obj.shiyongdengjibianhao === null
      ) {
        obj.shiyongdengjibianhao = '/'
      }
      // 增加核价默认值
      obj.ss1 = 0
      obj.ss2 = 0
      obj.ss3 = 0
      obj.ss4 = 0
      obj.ss5 = 0
      obj.ss6 = 0
      sblbpz.push(obj.shebeileibiedaima)
      sblbpz.push(obj.shebeipinzhongdaima)
      this.$set(obj, 'sblbpz', sblbpz)
    },
    getSblb() {
      this.getSblbList('2', this.sbzldm, undefined)
    },
    getSblbList(level, sbzl, sblb) {
      // 设备类别下拉菜单
      this.api({
        url: '/sedirectory/getList',
        method: 'get',
        params: {
          level: level,
          sbzl: sbzl,
          sblb: sblb
        }
      }).then((data) => {
        switch (level) {
          case '2':
            this.sblbList = data
            break
          case '3':
            this.sbpzList = data
            break
        }
      })
    },
    getJylb() {
      // 检验类别
      this.api({
        url: '/category/getCategoryList',
        method: 'get',
        params: {
          parentCode: 'DJ',
          species: this.bjd.shebeizhongleidaima
        }
      }).then((data) => {
        this.jylbs = data
      })
    },
    getArea() {
      // 查询区划
      this.api({
        url: '/area/getShiArea',
        method: 'get',
        params: {
          shidaima: '370100'
        }
      }).then((data) => {
        this.areas = data
      })
    },
    getDepts() {
      // 查询科室
      this.api({
        url: '/department/getDepartmentByCode',
        method: 'get',
        params: {
          code: this.bjd.shebeizhongleidaima
        }
      }).then((data) => {
        this.depts = data
      })
    },
    getBjdDepartment() {
      this.bjdDepartment.shebeizhonglei = this.bjd.shebeizhongleidaima
      this.bjdDepartment.jianyanleibie = this.bjd.jianyanleibie
      this.bjdDepartment.quhumingcheng = this.bjd.quhuadaima
      this.apibjd({
        url: '/bjdDepartment/getObj',
        method: 'get',
        params: this.bjdDepartment
      }).then((data) => {
        if (data != null) {
          this.bjdDepartment = data
          this.$set(this.bjd, 'departmentId', this.bjdDepartment.departmentId)
        }
      })
    },
    // 领取方式修改方法
    lqfsChange(val) {
      if (val === '邮寄') {
        this.formYJVisible = true
      }
    },
    quhuaChange(val) {
      const obj = this.areas.find((item) => {
        return item.value === val
      })
      this.bjd.quhuamingcheng = obj.label
    },
    tableRowClassName({ row, rowIndex }) {
      row.index = rowIndex
    },
    sblbChange(sblb) {
      this.getSblbList('3', this.bjd.shebeizhongleidaima, sblb.value)
    },
    handleSelectionChange(val) {
      this.selection = val
      this.delRowIndex = []
      for (let i = 0; i < val.length; i++) {
        this.delRowIndex.push(val[i].index)
      }
    },
    // 表格单击选中行
    onRowClick(row) {
      this.$refs.bjBaseList.toggleRowSelection(row)
    },
    addRowN: function() {
      let sblbpz = []
      let d = {}
      if (this.bjd.shebeizhongleidaima === 'F000') {
        sblbpz = ['7300', '7310']
        if (this.tableData.length === 0) {
          d = {
            neibuleibie: 'DQ',
            shebeileibie: '压力管道阀门',
            shebeileibiedaima: '7300',
            shebeipinzhong: '安全阀',
            shebeipinzhongdaima: '7310',
            sblbpz: sblbpz,
            shebeimingcheng: '',
            baseId: '',
            chanpinbianhao: '/',
            generateType: true
          }
          this.tableData.push(d)
        } else {
          this.tableData.push(this.tableData[this.tableData.length - 1])
        }
      } else {
        sblbpz.push(this.sbzldm.substr(0, 1) + '100')
        sblbpz.push(this.sbzldm.substr(0, 1) + '110')
        d = {
          shebeileibie: '',
          shebeileibiedaima: sblbpz[0],
          shebeipinzhong: '',
          shebeipinzhongdaima: sblbpz[1],
          sblbpz: sblbpz,
          shebeimingcheng: '',
          baseId: '',
          shebeidaima: '/',
          shiyongdengjibianhao: '/',
          chanpinbianhao: '/',
          generateType: true,
          localData: null
        }
        this.tableData.push(d)
      }
      setTimeout(() => {
        this.sblbpzChange(this.tableData.length - 1)
        this.$refs.bjBaseList.setCurrentRow(d)
      }, 10) // 用于延时渲染后选中这行
    },
    addRow: function() {
      // 添加行数据
      this.baseListVisible = true
    },
    getBaseList() {
      this.apibjd({
        url: '/se/list',
        method: 'get',
        params: this.listQuery
      }).then((data) => {
        this.baseList = data.list
        this.totalCount = data.totalCount
      })
    },
    clearSearch() {
      this.listQuery = {
        order: undefined, // 排序字段
        sort: undefined, // 排序方式
        shidaima: '3701'
      }
    },
    getIndex($index) {
      // 表格序号
      return (this.listQuery.pageNum - 1) * this.listQuery.pageRow + $index + 1
    },
    baseSelectionChange(val) {
      this.baseSelection = val
      this.addRowIndex = []
      for (let i = 0; i < val.length; i++) {
        this.addRowIndex.push(val[i].index)
      }
    },
    batchAddToBjBase() {
      if (this.addRowIndex !== null && this.addRowIndex.length > 0) {
        let baseIds = ''
        for (let i = 0; i < this.addRowIndex.length; i++) {
          for (let i = 0; i < this.tableData.length; i++) {
            if (
              this.tableData[i].zhucedaima ===
              this.baseList[this.addRowIndex[i]].zhucedaima
            ) {
              return true
            }
            if (
              this.tableData[i].shiyongdengjibianhao ===
              this.baseList[this.addRowIndex[i]].shiyongdengjibianhao
            ) {
              return true
            }
          }
          baseIds += this.baseList[this.addRowIndex[i]].id + ','
        }
        if (this.formState === 'update') {
          this.apibjd({
            url: '/bjd/relationParam',
            method: 'get',
            params: { baseIds: baseIds.substr(0, baseIds.length - 1) }
          }).then((data) => {
            if (data != null) {
              for (let j = 0; j < data.length; j++) {
                this.setDefaultValue(data[j])
                data[j].baseId = data[j].id
                data[j].id = '' // 设备为空可以防止执行update更改其他
                this.tableData.push(data[j])
              }
              this.$message({
                type: 'success',
                message: '已添加！'
              })
            }
          })
        }
      } else {
        this.$message({
          type: 'warning',
          message: '请选择要报检的数据！'
        })
      }
    },
    addToBjBase(index) {
      this.setDefaultValue(this.baseList[index])
      var flag = false
      for (let i = 0; i < this.tableData.length; i++) {
        if (this.tableData[i].zhucedaima === this.baseList[index].zhucedaima) {
          flag = true
        }
        if (
          this.tableData[i].shiyongdengjibianhao ===
          this.baseList[index].shiyongdengjibianhao
        ) {
          flag = true
        }
      }
      if (flag) {
        this.$confirm('该设备已存在，是否继续添加！！！', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.toBjd(index)
        })
      } else {
        this.toBjd(index)
      }
    },
    toBjd(index) {
      // 创建报检单----直接放到报检设备列表
      if (this.formState === 'create') {
        this.tableData.push(this.baseList[index])
        this.$message({
          type: 'success',
          message: '已添加！'
        })
      } else if (this.formState === 'update') {
        // 报检单修改----查询参数信息放到报检设备列表
        this.apibjd({
          url: '/bjd/relationParam',
          method: 'get',
          params: { baseIds: this.baseList[index].id }
        }).then((data) => {
          if (data != null) {
            this.setDefaultValue(data[0])
            data[0].baseId = data[0].id
            data[0].id = ''
            this.tableData.push(data[0])
            this.$message({
              type: 'success',
              message: '已添加！'
            })
          }
        })
      }
    },
    // 判断报检列表中是否包含选中的设备
    judgeContact(list, obj) {
      let flag = false
      for (let i = 0; i < list.length; i++) {
        if (list[i].id === obj.id) {
          flag = true
        }
      }
      return flag
    },
    checkSelection(row) {
      this.$refs.baseTable.toggleRowSelection(row)
    },
    clearBlank(row, column, cellValue) {
      if (cellValue !== null && cellValue !== '' && cellValue !== 'null') {
        return cellValue
      }
      return '/'
    },
    sblbpzChange(index) {
      const sblbpz = this.tableData[index].sblbpz
      this.tableData[index].shebeileibiedaima = sblbpz[0]
      this.tableData[index].shebeipinzhongdaima = sblbpz[1]
      this.sblbList.forEach((item) => {
        if (item.value === sblbpz[0]) {
          this.tableData[index].shebeileibie = item.label
          item.children.forEach((itemc) => {
            if (itemc.value === sblbpz[1]) {
              this.tableData[index].shebeipinzhong = itemc.label
            }
          })
        }
      })
    },
    editRow: function() {
      // 修改行数据
      if (this.delRowIndex.length === 0) {
        for (let i = 0; i < this.tableData.length; i++) {
          if (this.param.neibuleibie !== '') {
            this.tableData[i].neibuleibie = this.param.neibuleibie
          }
          if (this.param.sblb !== '') {
            this.tableData[i].shebeileibiedaima = this.param.sblb.value
            this.tableData[i].shebeileibie = this.param.sblb.label
          }
          if (this.param.sbpz !== '') {
            this.tableData[i].shebeipinzhongdaima = this.param.sbpz.value
            this.tableData[i].shebeipinzhong = this.param.sbpz.label
          }
          if (this.param.datingheding !== undefined) {
            this.tableData[i].datingheding = this.param.datingheding
          }
          if (this.param.ss2 !== undefined) {
            this.tableData[i].ss2 = this.param.ss2
          }
          const sblbpz = []
          sblbpz.push(this.tableData[i].shebeileibiedaima)
          sblbpz.push(this.tableData[i].shebeipinzhongdaima)
          this.tableData[i].sblbpz = sblbpz
        }
      } else {
        for (let i = 0; i < this.selection.length; i++) {
          if (this.param.neibuleibie !== '') {
            this.selection[i].neibuleibie = this.param.neibuleibie
          }
          if (this.param.sblb !== '') {
            this.tableData[i].shebeileibiedaima = this.param.sblb.value
            this.tableData[i].shebeileibie = this.param.sblb.label
          }
          if (this.param.sbpz !== '') {
            this.tableData[i].shebeipinzhongdaima = this.param.sbpz.value
            this.tableData[i].shebeipinzhong = this.param.sbpz.label
          }
          if (this.param.datingheding !== undefined) {
            this.selection[i].datingheding = this.param.datingheding
          }
          if (this.param.ss2 !== undefined) {
            this.tableData[i].ss2 = this.param.ss2
          }
          const sblbpz = []
          sblbpz.push(this.tableData[i].shebeileibiedaima)
          sblbpz.push(this.tableData[i].shebeipinzhongdaima)
          this.selection[i].sblbpz = sblbpz
        }
      }
      this.tableData.push()
      this.dialogFormParam = false
    },
    delRow: function() {
      // 删除行数据
      if (this.delRowIndex.length === 0) {
        this.$message({
          type: 'error',
          message: '请选中需要删除的数据！'
        })
        return false
      } else {
        this.$confirm('该条数据删除后无法恢复,是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            // 对delRowIndex进行排序，因为删除一个元素后index值会变
            this.delRowIndex.sort(function(x, y) {
              if (x < y) {
                return 1
              }
              if (x > y) {
                return -1
              }
              return 0
            })
            for (let i = 0; i < this.delRowIndex.length; i++) {
              // 在实际数据中进行删除
              if (this.formState !== 'create') {
                // 前提条件要满足非新建报检
                const delid = this.tableData[this.delRowIndex[i]].id
                if (delid != null) {
                  this.apibjd({
                    url: '/renwu/delRenwu',
                    method: 'delete',
                    params: { id: delid }
                  }).then((data) => {
                    // 不做处理
                  })
                }
              }
              this.tableData.splice(this.delRowIndex[i], 1)
            }
            this.$refs.bjBaseList.clearSelection()
            this.delRowIndex = []
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
      }
    },
    judgeNull(str) {
      if (!str || str.toString().replace(/(^s*)|(s*$)/g, '').length === 0) {
        return true
      } else {
        return false
      }
    },
    openDialogParam() {
      this.dialogFormParam = true
    },
    // 修改邮寄地址
    savePostInfo(operation) {
      this.$refs['postInfo'].validate((valid) => {
        if (valid) {
          this.apibjd({
            url: 'postInfo/save',
            method: 'post',
            data: this.postInfo
          }).then((data) => {
            this.$message({ message: '保存成功', type: 'success' })
          })
        }
      })
    },
    formTime(v) {
      const date = new Date(v)
      const y = date.getFullYear()
      let m = date.getMonth() + 1
      m = m < 10 ? '0' + m : m
      let d = date.getDate()
      d = d < 10 ? '0' + d : d
      const h = date.getHours()
      let minute = date.getMinutes()
      minute = minute < 10 ? '0' + minute : minute
      const seconds =
        date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
      const time =
        y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + seconds
      return time
    },
    // 报检单保存
    bjdSave(audit) {
      this.bjd.baojianriqi = this.formTime(this.bjd.baojianriqi)
      this.bjd.shouliriqi = this.formTime(this.bjd.shouliriqi)
      this.$refs['bjd'].validate((valid) => {
        if (valid) {
          if (this.judgeNull(this.bjd.quhuadaima)) {
            this.$message({
              type: 'error',
              message: '区划不能为空！'
            })
            return false
          }
          if (this.tableData === null || this.tableData.length === 0) {
            this.$message({
              type: 'error',
              message: '报检设备不能为空！'
            })
            return false
          } else {
            for (let i = 0; i < this.tableData.length; i++) {
              if (this.judgeNull(this.tableData[i].shebeipinzhongdaima)) {
                this.$message({
                  type: 'error',
                  message: '请通过批量修改完善设备类别及品种！！'
                })
                return false
              } else if (
                this.sbzldm !== '8000' &&
                this.judgeNull(this.tableData[i].zhucedaima) &&
                this.judgeNull(this.tableData[i].shiyongdengjibianhao) &&
                this.judgeNull(this.tableData[i].chanpinbianhao)
              ) {
                this.$message({
                  type: 'error',
                  message: '设备代码、使用登记编号、出厂编号不能同时为空！'
                })
                return false
              }
              if (this.judgeNull(this.tableData[i].shebeileibiedaima)) {
                if (this.sbzldm === 'F000') {
                  this.tableData[i].shebeileibiedaima = '7300'
                  this.tableData[i].shebeileibie = '压力管道阀门'
                } else {
                  this.tableData[i].shebeileibiedaima =
                    this.tableData[0].shebeileibiedaima
                  this.tableData[i].shebeileibie =
                    this.tableData[0].shebeileibie
                }
              }
              if (this.sbzldm === 'F000') {
                if (
                  !this.tableData[i].chanpinbianhao ||
                  this.tableData[i].chanpinbianhao === '/'
                ) {
                  this.$message({
                    type: 'error',
                    message: '产品编号不能为空！'
                  })
                  return false
                }
              } else if (
                (this.tableData[i].zhucedaima === '/' ||
                  this.judgeNull(this.tableData[i].zhucedaima)) &&
                this.tableData[i].shiyongdengjibianhao === '/'
              ) {
                this.$message({
                  type: 'error',
                  message: '注册代码、使用登记编号不能同时为空！'
                })
                return false
              }
              // 设置区划
              this.tableData[i].quhuadaima = this.bjd.quhuadaima
              this.tableData[i].quhuamingcheng = this.bjd.quhuamingcheng
            }
          }
          this.bjd.jianyanleibie = 'DJ'
          this.bjd.totalNum = this.tableData.length
          let url = ''
          if (this.formState === 'create' || this.formState === 'newBuild') {
            url = 'bjd/saveBjd'
          } else {
            url = 'bjd/updateBjd'
          }
          this.apibjd({
            url: url,
            method: 'post',
            data: {
              bjd: this.bjd,
              paramList: this.tableData,
              audit: audit,
              isShengju: this.isShengju
            }
          }).then((data) => {
            Utils.$emit('bjd-list')
            Utils.$emit('allot-list')
            if (audit) {
              this.$message({ message: '审核成功', type: 'success' })
              // 不是锅炉定检科室时，提示是否打印回执单
              if (this.$store.getters.departmentId !== 75) {
                this.$confirm('是否打印回执单?', '提示', {
                  confirmButtonText: '确定',
                  cancelButtonText: '取消',
                  type: 'warning'
                })
                  .then(() => {
                    this.printHzd()
                  })
                  .catch(() => {
                    this.$store.dispatch('delCurrentViews', {
                      view: this.$route,
                      $router: this.$router
                    })
                  })
              } else {
                this.deleteCurrentView()
              }
              // 更新省局设备状态
              this.apisjse({
                url: '/baseInfo/updateStateAndRiqiByIds',
                method: 'get',
                params: {
                  ids: this.$refs.tableData.selection
                    .map((info) => {
                      return info.seId
                    })
                    .join(','),
                  state: '1'
                }
              })
            } else {
              this.tableData = data.tableData
              for (const tableObj of this.tableData) {
                const sblbpz = []
                sblbpz.push(tableObj.shebeileibiedaima)
                sblbpz.push(tableObj.shebeipinzhongdaima)
                this.$set(tableObj, 'sblbpz', sblbpz)
              }
              if (
                this.formState === 'create' ||
                this.formState === 'newBuild'
              ) {
                this.$message({ message: '保存成功', type: 'success' })
                this.bjd = data.bjd
                this.formState = 'update'
                this.beforeHejia()
              } else {
                this.$message({ message: '修改成功', type: 'success' })
              }
            }
          })
        } else {
          this.$message({ type: 'error', message: '保存失败，存在必填项！' })
        }
      })
    },
    beforeUpload(file) {
      let importField = []
      if (this.sbzldm === 'F000') {
        importField = [
          [
            { label: '使用单位', value: 'shiyongdanwei' },
            { label: '使用单位地址', value: 'shiyongdanweiDizhi' },
            { label: '使用单位联系人', value: 'shiyongdanweiLianxiren' },
            { label: '使用单位电话', value: 'shiyongdanweiDianhua' },
            { label: '区划名称', value: 'quhuamingcheng' }
          ],
          [
            { label: '制造单位', value: 'zhizaodanwei' },
            { label: '制造许可证编号', value: 'zhizaoxukezhengbianhao' },
            { label: '产品编号', value: 'chanpinbianhao' },
            { label: '安装位置', value: 's35' },
            { label: '出厂日期', value: 's8' },
            { label: '安全阀类型', value: 's16' },
            { label: '安全阀型号', value: 's3' },
            { label: '公称通径', value: 's4' },
            { label: '阀座口径(mm)', value: 's5' },
            { label: '起始范围(MPa)', value: 's6' },
            { label: '结束范围(MPa)', value: 's7' },
            { label: '设计压力', value: 's36' },
            { label: '工作压力', value: 's17' },
            { label: '工作介质', value: 's2' },
            { label: '要求整定压力', value: 's1' },
            { label: '密封试验压力', value: 's20' },
            { label: '校验方式及介质', value: 's9' },
            { label: '校验介质温度(℃)', value: 's12' }
          ]
        ]
      }
      this.bjdController.beforeUpload(
        this,
        file,
        importField,
        this.sbzldm,
        this.bjd.bjType
      )
      return false
    },
    beforeHejia() {
      // 核定价格
      for (let i = 0; i < this.tableData.length; i++) {
        if (this.formState === 'finish') {
          this.$message.warning(
            '只允许更新核定费用，已审核的参数修改无效！！！'
          )
          return false
        }
        // 初始化参数
        // if (this.tableData[i].neibuleibie) {
        //   this.tableData[i].neibuleibie = 'DQ'
        // }
        if (this.sbzldm === '1000') {
          // 2
        } else if (this.sbzldm === '2000') {
          // 1
        } else if (this.sbzldm === '3000') {
          if (this.tableData[i].shebeileibiedaima !== undefined) {
            if (this.tableData[i].shebeileibiedaima.substring(0, 2) === '30') {
              const sblbdm = this.tableData[i].shebeileibiedaima.substring(
                2,
                3
              )
              this.tableData[i].shebeileibiedaima = '3' + sblbdm + '00'
            }
          }
          this.calculate.calculateDt(this.tableData[i])
        } else if (this.sbzldm === '4000') {
          if (this.tableData[i].shebeileibiedaima !== '') {
            if (this.tableData[i].shebeileibiedaima.substring(0, 2) === '40') {
              const sblbdm = this.tableData[i].shebeileibiedaima.substring(
                2,
                3
              )
              this.tableData[i].shebeileibiedaima = '4' + sblbdm + '00'
            }
          }
          this.calculate.calculateQZ(this.tableData[i])
        } else if (this.sbzldm === '5000') {
          if (this.tableData[i].shebeileibiedaima !== '') {
            if (this.tableData[i].shebeileibiedaima.substring(0, 2) === '50') {
              let sblbdm = this.tableData[i].shebeileibiedaima.substring(2, 3)
              if (sblbdm !== '2') {
                sblbdm = '1'
              }
              this.tableData[i].shebeileibiedaima = '5' + sblbdm + '00'
            }
          }
          this.calculate.calculateCC(this.tableData[i])
        }
      }
    },
    postInfoSave() {
      this.formYJVisible = true
      console.log('领取地址修改')
    },
    printHzd() {
      this.apibjd({
        url: '/bjd/printHzd',
        method: 'get',
        params: {
          bjdIds: this.bjd.id
        }
      })
        .then((data) => {
          this.preview({
            url: '/print/generatePdfListing',
            method: 'post',
            data: {
              info: data.bjd,
              paramList: data.renwu,
              viewType: 'HZD'
            }
          })
            .then((data) => {
              data = data.replace(/\\/g, '/')
              window.open(data)
              this.deleteCurrentView()
            })
            .catch(() => {
              this.deleteCurrentView()
            })
        })
        .catch(() => {
          this.deleteCurrentView()
        })
    },
    deleteCurrentView() {
      this.$store.dispatch('delCurrentViews', {
        view: this.$route,
        $router: this.$router
      })
    },
    viewHzd() {
      this.apibjd({
        url: '/bjd/printHzd',
        method: 'get',
        params: {
          bjdIds: this.bjd.id
        }
      }).then((data) => {
        this.preview({
          url: '/print/generatePdfListing',
          method: 'post',
          data: {
            info: data.bjd,
            paramList: data.renwu,
            viewType: 'HZD'
          }
        }).then((data) => {
          data = data.replace(/\\/g, '/')
          window.open(data)
        })
      })
    },
    editFixedHidden() {
      const els = document.getElementsByClassName('el-table__fixed-right')
      if (!this.fixedFlag) {
        this.fixedWidth = els[0].style.width
      }
      if (this.fixedFlag) {
        els[0].style.width = this.fixedWidth
      } else {
        els[0].style.width = '180px'
      }
    },
    // finish=>修改保存
    modifyFn() {
      this.apibjd({
        url: '/bjd/printHzd',
        method: 'put',
        params: {
          bjdIds: this.bjd
        }
      })
        .then((data) => {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
        })
    }
  }
}
</script>
