<!--锅炉改造和重大修理-->
<template>
  <div class="app-container">
    <sticky style="margin-bottom: 10px;">
      <div class="sub-navbar">
        <el-button v-if="state === 'create'" type="success" icon="el-icon-edit" size="medium" @click="saveYsjl('add')">
          保存
        </el-button>
        <el-button v-if="state === 'update'" type="primary" icon="el-icon-edit" size="medium" @click="saveYsjl('upd')">
          更新
        </el-button>
        <el-button v-if="state === 'update'" type="success" icon="el-icon-upload2" size="medium" @click="saveYsjl('build')">
          生成报告
        </el-button>
        <el-button v-if="state !== 'create'" type="warning" icon="el-icon-view" size="medium" @click="common.viewYsjl(ysjl.id, 'YSJL')">
          预览
        </el-button>
      </div>
    </sticky>
    <el-tabs v-model="checkedTab" type="card">
      <el-tab-pane label="基本情况" name="first">
        <el-form ref="ysjl" :model="ysjl" class="el-form" label-position="right" label-width="130px">
          <fieldset>
            <legend>基本信息</legend>
            <el-row :gutter="25">
              <el-col v-if="state === 'create'" :span="14">
                <el-form-item label="记录编号">
                  <el-input v-model="jlbh1" style="width:80px" disabled />
                  <el-input v-model="jlbh2" style="width:80px" />
                  <el-input v-model="jlbh3" style="width:80px" disabled />-
                  <el-input v-model="jlbh4" style="width:85px" @blur="judgeRepeat(jlbh1 + jlbh2 + jlbh3 + '-' + jlbh4 , ysjl.id)" />
                </el-form-item>
              </el-col>
              <el-col v-if="state !== 'create'" :span="9">
                <el-form-item label="记录编号" prop="jilubianhao">
                  <el-input v-if="state !== 'create'" v-model="ysjl.jilubianhao" :disabled="edit" />
                </el-form-item>
              </el-col>
              <el-col v-if="isRepeat" :span="2" style="vertical-align: middle;padding-top: 3px;font-size: 15px;">
                <span style="color:red">编号重复</span>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="18">
                <el-form-item label="制造单位" prop="zhizaodanwei">
                  <el-input v-model="ysjl.zhizaodanwei" type="text" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="18">
                <el-form-item label="单位地址" prop="zhizaodanweiDizhi">
                  <el-input v-model="ysjl.zhizaodanweiDizhi" type="text" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="9">
                <el-form-item label="单位许可证编号" prop="zhizaodanweiCode">
                  <el-input v-model="ysjl.zhizaodanweiCode" :disabled="edit" />
                </el-form-item>
              </el-col>
              <el-col :span="9">
                <el-form-item label="产品类别" prop="shebeileibiedaima">
                  <el-select v-model="ysjl.shebeileibiedaima" placeholder="产品类别" style="width: 100%" @change="getSbpzList">
                    <el-option v-for="item in sblbList" :key="item.value" :label="item.label" :value="item.value" />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="9">
                <el-form-item label="产品名称" prop="shebeimingcheng">
                  <el-input v-model="ysjl.shebeimingcheng" />
                </el-form-item>
              </el-col>
              <el-col :span="9">
                <el-form-item label="产品品种" prop="shebeipinzhongdaima">
                  <el-select v-model="ysjl.shebeipinzhongdaima" placeholder="产品品种" style="width: 100%" @change="setSbpz">
                    <el-option v-for="item in sbpzList" :key="item.value" :label="item.label" :value="item.value" />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="9">
                <el-form-item label="材料牌号" prop="cailiaopaihao">
                  <el-input v-model="param.cailiaopaihao" />
                </el-form-item>
              </el-col>
              <el-col :span="9">
                <el-form-item label="产品标准" prop="chanpinbiaozhun">
                  <el-input v-model="param.chanpinbiaozhun" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="9">
                <el-form-item label="产品批号" prop="chanpinpihao">
                  <el-input v-model="param.chanpinpihao" type="text" />
                </el-form-item>
              </el-col>
              <el-col :span="9">
                <el-form-item label="产品编号" prop="chanpinbianhao">
                  <el-input v-model="ysjl.chanpinbianhao" type="text" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="9">
                <el-form-item label="产品规格" prop="guigexinghao">
                  <el-input v-model="ysjl.guigexinghao" />
                </el-form-item>
              </el-col>
              <el-col :span="9">
                <el-form-item label="管号" prop="guandaobianhao">
                  <el-input v-model="param.guandaobianhao" type="text" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="9">
                <el-form-item label="数量" prop="chanpinshuliang">
                  <el-input v-model="param.chanpinshuliang">
                    <template slot="append">
                      根
                    </template>
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="9">
                <el-form-item label="制造日期" prop="zhizaoriqi">
                  <el-input v-model="ysjl.zhizaoriqi" type="text" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="9">
                <el-form-item label="检验金额(元)" prop="jianyanheding">
                  <el-input-number v-model="ysjl.jianyanheding" :disabled="ysjl.retreatState === 2 && ysjl.jfState" :controls="false" style="width: 100%;" />
                </el-form-item>
              </el-col>
            </el-row>
          </fieldset>
          <fieldset>
            <legend>检查与校验</legend>
            <el-row :gutter="20" style="text-align:left;margin-left: 50px;font-size: 18px;color: #606266;line-height: 40px;font-weight: bolder;">
              &emsp;&emsp;按照《中华人民共和国特种设备安全法》《特种设备安全监察条例》的规定，
              该台（批）产品经本机构实施监督检验，安全性能符合 <el-input v-model="ysjl.jianyanyiju" style="width: 300px" />
              的要求，特发此证书。
            </el-row>
            <el-row :gutter="20" style="margin-top: 20px;">
              <el-col :span="9">
                <el-form-item label="检验日期" prop="jianyanjieshuriqi">
                  <el-date-picker v-model="ysjl.jianyanjieshuriqi" type="date" value-format="yyyy-MM-dd" placeholder="选择日期" style="width: 100%;" />
                </el-form-item>
              </el-col>
              <el-col :span="9">
                <el-form-item label="结论意见" prop="jianyanjielun">
                  <el-select v-model="ysjl.jianyanjielun" :disabled="edit" placeholder="请选择" style="width: 100%;">
                    <el-option label="符合" value="符合" />
                    <el-option label="不符合" value="不符合" />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="10">
              <el-col :span="9">
                <el-form-item label="检验人员" prop="jianyanrenyuan">
                  <el-input v-model="jianyanrenyuan" disabled />
                  <el-input v-show="false" v-model="ysjl.jianyanrenyuan" />
                </el-form-item>
              </el-col>
              <el-col :span="9">
                <sign-name :jianyanrenyuan="jianyanrenyuan" :ysjl="ysjl" @setSignValue="signNameValue" />
              </el-col>
            </el-row>
          </fieldset>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="检验项目表" name="second">
        <el-table ref="bgfyData" :data="bgfyData" :span-method="tableJs.mergeColRows" border stripe @row-click="onRowClick">
          <el-table-column type="selection" width="55" />
          <el-table-column type="index" align="center" width="50" label="序号" />
          <el-table-column align="center" width="100" label="检验项">
            <el-table-column align="center" prop="jianyanxiang" width="100" label="检验项" />
            <el-table-column align="center" prop="jianyanmu" width="100" label="检验目" />
            <el-table-column align="center" prop="jianyanneirong" max-width="380" label="检验内容" />
          </el-table-column>
          <el-table-column align="center" prop="xiangmuleibie" width="50" label="类别" />
          <el-table-column align="center" prop="gongzuojianzheng" label="工作见证">
            <template slot-scope="scope">
              <el-input v-model="scope.row.gongzuojianzheng" :disabled="edit" />
            </template>
          </el-table-column>
          <el-table-column align="center" prop="jianyanjieguo" label="检查结果">
            <template slot-scope="scope">
              <el-autocomplete v-model="scope.row.jianyanjieguo" :fetch-suggestions="querySearch" :disabled="edit" style="width: 100%;" />
            </template>
          </el-table-column>
          <el-table-column align="center" prop="querenriqi" width="170" label="日期">
            <template slot-scope="scope">
              <el-date-picker v-model="scope.row.querenriqi" type="date" value-format="yyyy-MM-dd" placeholder="选择日期" style="width: 100%;" />
            </template>
          </el-table-column>
        </el-table>
        <el-form class="el-form" style="margin-top: 10px;">
          <el-row :gutter="20">
            <el-col :span="18" style="height: 170px;">
              <el-form-item label="记事栏:" prop="beizhu">
                <el-input v-model="ysjl.beizhu" :rows="5" :disabled="edit" type="textarea" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="附件" name="third">
        <SliceUpload :renwu-id="ysjl.renwuId ? Number(ysjl.renwuId) : Number($route.query.renwuId)" :drag="true" type="gdfj" />
      </el-tab-pane>
    </el-tabs>
  </div>
</template>
<script>
import Sticky from '@/components/Sticky'
import SignName from '@/views/common/SignName'
import Utils from '@/utils/contact'
import merge from 'webpack-merge'
import SliceUpload from '@/components/SliceUpload'

export default {
  name: 'YlgdyjYsjl',
  components: { SignName, Sticky, SliceUpload },
  data() {
    return {
      jlbh1: 'GDJ',
      jlbh2: '01',
      jlbh3: '(' + new Date().getFullYear() + ')',
      jlbh4: '',
      ysjl: {},
      param: {},
      state: this.$route.query.state,
      jianyanrenyuan: '',
      departmentId: this.$route.query.departmentId,
      isRepeat: false,
      edit: false,
      bgfyData: [],
      tableData: [],
      checkedTab: 'first',
      sblbList: [],
      sbpzList: [],
      ysjlId: this.$route.query.id,
      jyjgOption: [{ value: '符合' }, { value: '不符合' }, { value: '无此项' }],
      sglbList: ['改造', '重大修理'],
      complexShow: false,
      // 表格合并相关
      // 参与列合并的字段，在这里增加即可
      cols: [
        {
          name: 'jianyanxiang', // 参与计算的列名，必须和el-table-column prop=''值一致
          getValue(row) { // 该列用于比较的值的获取方法
            return row.jianyanxiang
          }
        },
        {
          name: 'jianyanmu', // 参与计算的列名，必须和el-table-column prop=''值一致
          getValue(row) { // 该列用于比较的值的获取方法
            return row.jianyanmu
          }
        },
        {
          name: 'jianyanneirong', // 参与计算的列名，必须和el-table-column prop=''值一致
          getValue(row) { // 该列用于比较的值的获取方法
            return row.jianyanneirong
          }
        }
      ],
      // 参与行内合并的字段
      rows: [
        {
          name: 'jianyanxiang',
          getValue(row) {
            return row.jianyanxiang
          }
        },
        {
          name: 'jianyanmu',
          getValue(row) {
            return row.jianyanmu
          }
        },
        {
          name: 'jianyanneirong',
          getValue(row) {
            return row.jianyanneirong
          }
        }
      ]
    }
  },
  computed: {
    jilubianhao: function() {
      if (this.state !== 'create') {
        return this.ysjl.jilubianhao
      }
      return this.jlbh1 + this.jlbh2 + this.jlbh3 + '-' + this.jlbh4
    }
  },
  watch: {
    jilubianhao: function(newValue) {
      this.judgeRepeat(newValue, this.ysjl.id)
    }
  },
  created() {
    if (this.state === 'create') {
      this.init()
    } else {
      this.getYsjl(this.$route.query.id)
      this.getSbList('7000', undefined, 2)
    }
    // 查询最大记录编号
    if (!this.ysjl.jilubianhao) {
      this.getMaxBh()
    }
    this.getJyxm(1)
  },
  methods: {
    init() {
      this.api({
        url: '/ysjl/initYsjl',
        method: 'post',
        data: {
          modelId: this.$route.query.modelId,
          renwuId: this.$route.query.renwuId,
          neibuleibie: this.$route.query.neibuleibie,
          sblbdm: this.$route.query.sblbdm,
          sblbmc: this.$route.query.sblbmc,
          sbpzdm: this.$route.query.sbpzdm,
          sbpzmc: this.$route.query.sbpzmc
        }
      }).then(data => {
        this.ysjl = data.ysjl
        this.param = data.param
        this.ysjl.jianyanjieshuriqi = this.formatter.dateFormat('YYYY-MM-dd')
        this.ysjl.jianyanjielun = '符合'
        if (!this.ysjl.guigexinghao) {
          this.$set(this.ysjl, 'guigexinghao', 'Ø  ×    mm')
        }
        this.ysjl.jigouhezhunzhenghao = process.env.VUE_APP_HZZBH
        this.ysjl.beizhu = '抽查材料标志：           ；材料规格：            ；材料炉、批号：                         ；\n' +
            '抽查管道元件编号或埋弧焊管管号：        ；材料产品分析报告编号：            ；\n' +
            '抽查焊接工艺编号：                      ；返修工艺编号：             ；焊工号：            ；\n' +
            '抽查热处理测温仪表检定报告编号：        ；有效期：                   ；\n' +
            '抽查底片/数字图像管道元件编号或埋弧焊管管号：                 ；返修部位管号：             ；\n' +
            '埋弧焊管抽查的无损检测过程管子编号(至少1根)：                ；无损检测人员：            ；\n' +
            '抽查外观与几何尺寸管道元件编号或埋弧焊管管号（至少1根/台）：                       ；\n' +
            '现场抽查埋弧焊管力学性能试验过程报告编号：                    ；\n' +
            '耐压试验抽查管道元件编号或埋弧焊管管号（至少1根/台）：                     ；\n' +
            '抽查的产品标志管道元件编号或埋弧焊管管号(至少1根/台)：                    。'
        this.getSbList('7000', this.ysjl.shebeileibiedaima, 3)
      })
      this.$set(this.param, 'weihujianxiuqingkuangshuoming', '/')
      this.getSbList('7000', undefined, 2)
    },
    // 签名方法
    signNameValue(data) {
      this.ysjl.jianyanrenyuan = data.id
      this.jianyanrenyuan = data.name
      if (this.state !== 'create') {
        this.common.signName(this.ysjl.id, this.ysjl.jianyanrenyuan)
        Utils.$emit('ysjl-list')
      }
    },
    // 查询最大记录编号
    getMaxBh() {
      const aheadPart = this.jlbh1 + this.jlbh2 + this.jlbh3
      this.api({
        url: '/ysjl/gainCurrentBh',
        method: 'get',
        params: {
          aheadPart: aheadPart + '-',
          hinderPart: this.jlbh4
        }
      }).then(data => {
        this.jlbh4 = data
      })
    },
    getSbpzList(val) {
      let obj = {}
      obj = this.sblbList.find((item) => { // 这里的sblbList就是上面遍历的数据源
        return item.value === val // 筛选出匹配数据
      })
      if (obj) {
        this.ysjl.shebeileibie = obj.label
      }
      this.ysjl.shebeipinzhongdaima = ''
      this.ysjl.shebeipinzhong = ''
      if (val) {
        this.getSbList('7000', val, 3)
      }
    },
    setSbpz(val) {
      let obj = {}
      obj = this.sbpzList.find((item) => { // 这里的userList就是上面遍历的数据源
        return item.value === val // 筛选出匹配数据
      })
      this.ysjl.shebeipinzhong = obj.label
    },
    getSbList(sbzl, sblb, level) {
      this.api({
        url: '/sedirectory/getList',
        method: 'get',
        params: {
          sbzl: sbzl,
          sblb: sblb,
          level: level
        }
      }).then(data => {
        switch (level) {
          case 2:
            this.sblbList = data
            return ''
          case 3:
            this.sbpzList = data
            return ''
          default:
            this.sbzlList = data
            return ''
        }
      })
    },
    getYsjl(id) {
      this.api({
        url: '/ysjl/getYsjlById',
        method: 'get',
        params: {
          ysjlId: id
        }
      }).then(data => {
        this.ysjl = data.ysjl
        this.complexShow = true
        if (this.ysjl.jianyanrenyuan !== null && this.ysjl.jianyanrenyuan.length > 0) {
          this.jianyanrenyuan = this.common.convertCnName(this.ysjl.jianyanrenyuan)
        }
        this.getParam(this.ysjl.id, this.ysjl.shebeizhongleidaima)
        if (this.ysjl.shebeileibiedaima) {
          this.getSbList('7000', this.ysjl.shebeileibiedaima, 3)
        }
      })
    },
    getParam(ysjlId, sbzldm) {
      // 根据原始记录id查询参数
      this.api({
        url: '/ysjl/getParamByYsjlId',
        method: 'get',
        params: {
          ysjlId: ysjlId,
          sbzldm: sbzldm
        }
      }).then(data => {
        this.param = data
      })
    },
    // 查询检验项目
    getJyxm(sort) {
      let url = ''
      let data = {}
      if (this.state === 'create') {
        url = '/jyxm/getCyJyxm'
        data = {
          templateId: this.$route.query.modelId,
          order: sort
        }
      } else {
        url = '/jyxm/getCyJyxmByYsjl'
        data = {
          ysjlId: this.ysjlId,
          isReport: false,
          order: sort
        }
      }
      this.api({
        url: url,
        method: 'get',
        params: data
      }).then(data => {
        switch (sort) {
          case 1:
            this.bgfyData = data
            this.tableJs.getData(this.cols, this.rows, data)
            break
        }
      })
    },
    onRowClick(row) {
      this.$refs.bgfyData.toggleRowSelection(row)
    },
    /**
     * 获取表格中的id, 不为空的检验结果和备注
     */
    getProp: function(data) {
      const retData = []
      for (let i = 0; i < data.length; i++) {
        const retRow = {}
        retRow.id = data[i].id
        if (data[i].gongzuojianzheng) {
          retRow.gongzuojianzheng = data[i].gongzuojianzheng
        }
        if (data[i].jianyanjieguo) {
          retRow.jianyanjieguo = data[i].jianyanjieguo
        }
        if (data[i].querenriqi) {
          retRow.querenriqi = data[i].querenriqi
        }
        retData.push(retRow)
      }
      return retData
    },
    saveYsjl: function(operation) {
      if (operation === 'add') {
        if (this.ysjl.jlbh4) {
          this.$message({ message: '请先确认记录编号是否完整！', type: 'warning' })
          return false
        }
      }
      if (this.bgfyData.length === 0) {
        this.$message.warning('未获取到检验项目，请重新打开页面！')
        return false
      }
      if (operation === 'build' && !this.ysjl.jianyanheding) {
        this.$message.warning('请填写检验金额！')
        return false
      }
      this.tableData = []
      this.tableData.push(this.getProp(this.bgfyData))
      let url = ''
      this.ysjl.bglx = 1
      this.ysjl.cjState = 'ysjl'
      this.ysjl.zfState = '0'
      this.ysjl.jlprintState = 0
      if (operation === 'add') {
        url = '/ysjl/saveYsjl'
        this.ysjl.jilubianhao = this.jlbh1 + this.jlbh2 + this.jlbh3 + '-' + this.jlbh4
      } else if (operation === 'build') {
        if (!this.ysjl.jianyanrenyuan) {
          this.$message({
            type: 'error',
            message: '请先签名再生成报告!'
          })
          return false
        }
        url = '/ysjl/updateYsjl'
      } else if (operation === 'upd') {
        url = '/ysjl/updateYsjl'
      }
      if (this.isRepeat) {
        this.$message({ message: '记录编号重复，请先修改记录编号再保存', type: 'error' })
        return false
      } else {
        this.$refs['ysjl'].validate(valid => {
          if (valid) {
            this.api({
              url: url,
              method: 'post',
              data: {
                ysjl: this.ysjl,
                param: this.param,
                jyxm: JSON.stringify(this.tableData),
                flag: operation
              }
            }).then(data => {
              Utils.$emit('mission-list')
              Utils.$emit('task-list')
              Utils.$emit('ysjl-list')
              if (operation === 'add' && data !== '') {
                this.$message({ message: '保存成功', type: 'success' })
                this.ysjl.id = data
                this.param.ysjlId = data
                // 刷新路由网址
                this.$store.dispatch('delCurrentViews', {
                  view: this.$route,
                  $router: this.$router
                })
                this.$router.push({
                  path: '/ysjl/7000/jj/edit/' + this.ysjl.id,
                  query: { state: 'update', id: this.ysjl.id, departmentId: this.ysjl.departmentId, jianyanrenyuan: this.ysjl.jianyanrenyuan }
                })
              } else if (operation === 'upd') {
                this.$message({ message: '更新成功', type: 'success' })
                // 替换路由中的参数
                this.$router.push({
                  query: merge(this.$route.query, { 'jianyanrenyuan': this.ysjl.jianyanrenyuan })
                })
              } else {
                Utils.$emit('ysjl-yb-list')
                Utils.$emit('bggl-daiban-list')
                this.$message({ message: '生成报告成功', type: 'success' })
                this.$store.dispatch('delCurrentViews', {
                  view: this.$route,
                  $router: this.$router
                })
              }
              this.state = 'update'
            })
          }
        })
      }
    },
    judgeRepeat(bianhao, id) {
      this.api({
        url: '/ysjl/judgeRepeat',
        method: 'get',
        params: {
          ysjlId: id,
          bianhao: bianhao
        }
      }).then(data => {
        if (data > 0) {
          this.isRepeat = true
        } else {
          this.isRepeat = false
        }
      })
    },
    querySearch(queryString, cb) {
      cb(this.jyjgOption)
    }
  }
}
</script>

