<template>
  <div>
    <el-badge :value="shoppingSize" :max="99" class="item">
      <el-popover placement="left" title="同类报告" width="750" trigger="hover">
        <el-table ref="shoppingTable" :data="shoppingCartData" border style="width: 100%;" height="350">
          <el-table-column align="center" label="报告类型" prop="bglx" min-width="30" />
          <el-table-column align="left" label="检验项目" prop="jyxmLabel" width="200">
            <template slot-scope="scope">
              <div style="text-decoration:underline;cursor:pointer;" @click="editChild(scope.row.id, scope.row.jianyanxiangmu, departmentId, scope.row.modelId)">
                {{ scope.row.jyxmLabel }}
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="处理人" prop="renlingren" min-width="40" />
          <el-table-column align="center" label="检验人员" prop="jianyanrenyuan" min-width="40" />
          <el-table-column align="center" label="办结状态" prop="flowstatus" min-width="30">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.flowstatus==='未办结'" type="danger">
                未办结
              </el-tag>
              <el-tag v-if="scope.row.flowstatus==='已办结'" type="success">
                已办结
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column property="" label="操作" align="center" width="80">
            <template slot-scope="scope">
              <el-button type="primary" size="mini" circle title="删除检验方案" icon="el-icon-minus" @click="delYsjl(scope.row.jianyanxiangmu, scope.row.jyxmLabel, scope.row.bglx, scope.row.id)" />
            </template>
          </el-table-column>
        </el-table>
        <el-button slot="reference" type="">
          <svg-icon icon-class="shoppingCard" />
        </el-button>
      </el-popover>
    </el-badge>
    <el-table :data="gridData" style="margin-top:10px;" row-key="title" height="350">
      <el-table-column type="index" :index="indexMethod" />
      <el-table-column property="title" label="名称" align="left" width="300" />
      <el-table-column property="" label="操作" align="center" width="179">
        <template v-if="scope.row.title !== '分项检测' && scope.row.title !== '无损检测'" slot-scope="scope">
          <el-button type="primary" size="mini" title="添加检验方案" circle icon="el-icon-check" @click="addToCart(scope.row)" />
          <el-button type="primary" size="mini" circle title="添加人员" icon="el-icon-plus" @click="addUserToCart(scope.row)" />
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :visible.sync="dialogVisible" title="指派（分项/无损）检验员" width="30%" append-to-body>
      <department-user :department-id="departmentId" @getUserValue="setUserId" />
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">
          取 消
        </el-button>
        <el-button type="primary" @click="addCart()">
          确 定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import DepartmentUser from '@/views/common/DepartmentUser'
import Utils from '../../utils/contact.js'
export default {
  components: { DepartmentUser },
  props: {
    info: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      gridData: [],
      sbzldm: this.info.sbzldm,
      shoppingCartData: [],
      shoppingSize: 0,
      childYsjl: {}, // 子报告信息
      departmentId: this.info.departmentId,
      type: this.info.type, // 标记是原始记录还是报告
      dialogVisible: false,
      jyfaParam: {
        bglx: '',
        ysjlId: this.info.ysjlId,
        type: '',
        jianyanxiangmu: '',
        userId: '',
        templateId: ''
      }
    }
  },
  created() {
    this.getJyfa()
    this.getJyfaInfo()
  },
  methods: {
    indexMethod(index) {
      return index + 1
    },
    setUserId(val) {
      this.jyfaParam.userId = val
    },
    getJyfa() {
      this.api({
        url: '/ysjl/getJyfa',
        method: 'get',
        params: {
          modelId: this.info.modelId,
          ysjlId: this.info.ysjlId,
          sbzldm: this.info.sbzldm,
          jylb: this.info.jylb,
          nblb: this.info.nblb,
          viewType: this.info.type
        }
      }).then(data => {
        if (this.info.sbzldm === '2000' && (this.info.nblb === 'DQ' || this.info.nblb === 'SCDQ')) {
          data.menusJSON.forEach((v, k) => {
            v.children.forEach((vs, ks) => {
              if (vs.value === 'dxt') {
                v.children.splice(ks, 1)
              }
            })
          })
        }
        this.gridData = data.menusJSON
      })
    },
    addCart() {
      if (this.dialogVisible) {
        if (this.jyfaParam.userId === '') {
          this.$message({ message: '请选择人员', type: 'error' })
          return false
        } else {
          this.dialogVisible = false
        }
      }
      this.api({
        url: '/ysjl/addJyfa',
        method: 'get',
        params: this.jyfaParam
      }).then(() => {
        this.getJyfaInfo()
        this.$emit('childMethod', 1)
      })
    },
    addToCart(row) {
      this.jyfaParam.bglx = row.bglx
      this.jyfaParam.jianyanxiangmu = row.value
      if (this.info.sbzldm === '8000') {
        if (row.value === 'aqfj') {
          this.jyfaParam.type = '工业管道安全附件与仪表检验记录'
        } else if (row.value === 'fjjy') {
          this.jyfaParam.type = '工业管道附加检查记录'
        } else {
          this.jyfaParam.type = row.title
        }
      } else {
        this.jyfaParam.type = row.title
      }
      this.jyfaParam.templateId = row.id
      this.jyfaParam.userId = this.$store.getters.userId
      this.addCart()
    },
    addUserToCart(row) {
      this.jyfaParam.bglx = row.bglx
      this.jyfaParam.jianyanxiangmu = row.value
      if (this.info.sbzldm === '8000') {
        if (row.value === 'aqfj') {
          this.jyfaParam.type = '工业管道安全附件与仪表检验记录'
        } else if (row.value === 'fjjy') {
          this.jyfaParam.type = '工业管道附加检查记录'
        } else {
          this.jyfaParam.type = row.title
        }
      }
      this.jyfaParam.templateId = row.id
      this.dialogVisible = true
    },
    getJyfaInfo() {
      this.api({
        url: '/ysjl/getJyfaInfo',
        method: 'get',
        params: {
          ysjlId: this.info.ysjlId
        }
      }).then(data => {
        const length = this.common.deepCopy(data.length)
        for (let i = length - 1; i >= 0; i--) {
          var jyfa = data[i]
          if ((jyfa.neibuleibie === 'DQ' || jyfa.neibuleibie === 'SCDQ') && jyfa.jyxmLabel.indexOf('压力容器') !== -1 && jyfa.jianyanxiangmu === 'dxt') {
            data.splice(i, 1)
          }
        }
        this.shoppingCartData = data
        this.shoppingSize = this.shoppingCartData.length
      })
    },
    // 删除检验方案
    /* delYsjl(jyxm, jianyanxiangmu, bglx, ysjlId) {
      this.$confirm('原始记录删除后将无法恢复，是否确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.common.delJyfa(jyxm, jianyanxiangmu, bglx, ysjlId, this.info.ysjlId, this.info.sbzldm, this.getJyfaInfo)
      }).catch(e => {
        this.getJyfaInfo()
      })
    },*/
    /**
     * 删除检验方案
     * @param jyxm  检验项目简写。例如：bhcd
     * @param jianyanxiangmu  检验项目。例如：壁厚测定
     * @param bglx  报告类型。例如：[2, 3]
     * @param ysjlId 无损或分项的原始记录Id
     */
    delYsjl(jyxm, jianyanxiangmu, bglx, ysjlId) {
      this.$confirm('原始记录删除后将无法恢复，是否确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.api({
          url: '/ysjl/delJyfa',
          method: 'get',
          params: {
            ysjlId: ysjlId,
            bglx: bglx,
            jianyanxiangmu: jianyanxiangmu
          }
        }).then(() => {
          Utils.$emit('modifyMajorInspectionItemBySub' + this.ysjl.parentId)
          Utils.$emit('ysjl-list')
          Utils.$emit('bggl-daiban-list')
          this.$message({ message: '删除成功', type: 'success' })
          this.getJyfaInfo()
        }).catch(e => {
          this.getJyfaInfo()
        })
      }).catch(e => {
      })
      this.$emit('childMethod', 1)
    },

    /**
     * 编辑检验方案
     * @param id
     * @param jianyanxiangmu
     * @param departmentId
     * @param type
     * @param nblb
     */
    editChild(id, jianyanxiangmu, departmentId, modelId) {
      this.api({
        url: '/template/getById',
        method: 'get',
        params: {
          id: modelId
        }
      }).then(data => {
        const queryParam = { id: id, jyxm: jianyanxiangmu, departmentId: departmentId }
        this.$router.push({ path: data.tempPath, query: queryParam })
        this.$parent.$parent.jyfaDialogVisible = false
      })
    }
  }
}
</script>
