<template>
  <div class="app-container">
    <div class="filter-container">
      <sticky style="margin-bottom: 10px;">
        <div class="sub-navbar">
          <el-button type="danger" icon="el-icon-back" @click="withDraw">
            撤回报告
          </el-button>
        </div>
      </sticky>
      <el-form>
        <el-form-item>
          <el-input v-model="listQuery.baogaobianhao" placeholder="报告编号" clearable style="width: 200px" @keyup.enter.native="handleFilter" />
          <el-input v-model="listQuery.zhucedaima" placeholder="注册代码" clearable style="width: 200px" @keyup.enter.native="handleFilter" />
          <el-input v-model="listQuery.shiyongdanwei" placeholder="使用单位" clearable style="width: 300px" @keyup.enter.native="handleFilter" />
          <el-select v-model="listQuery.ownRoles" placeholder="请选择所处角色" style="width: 165px" clearable @change="changeOwnRoles">
            <el-option v-for="item in statusList" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
          <el-button type="primary" size="small" icon="el-icon-search" @click="handleFilter">
            查询
          </el-button>
          <el-button type="info" size="small" icon="el-icon-close" @click="clearQuery">
            清空条件
          </el-button>
          <br>
          <span class="radio-group-label">查看类型：</span>
          <el-radio-group v-model="listQuery.createBy" @change="handleFilter">
            <el-radio :label="$store.getters.userId">
              只看创建人
            </el-radio>
            <el-radio :label="undefined">
              全部
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      ref="list"
      v-adaptive="{bottomOffset: 50}"
      height="0"
      :data="list"
      :default-sort="{prop:'baogaobianhao', order:'descending'}"
      border
      fit
      highlight-current-row
      stripe
      width="100%"
      size="small"
      @row-click="onRowClick"
      @selection-change="handleSelectionChange"
      @sort-change="sortChange"
    >
      <el-table-column type="selection" width="40" />
      <el-table-column fixed="left" align="center" label="序号" width="50">
        <template slot-scope="scope">
          <span v-text="getIndex(scope.$index)" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="报告编号" prop="baogaobianhao" width="180" sortable="custom" />
      <el-table-column :formatter="formatter.formatterCategory" align="center" label="检验类别" prop="neibuleibie" width="100" />
      <template v-if="$store.getters.departmentId === 69 || $store.getters.departmentId === 86">
        <el-table-column v-if="$store.getters.departmentId === 69" align="center" label="使用单位" prop="shiyongdanwei" width="200" />
        <el-table-column v-if="$store.getters.departmentId === 86" align="center" label="安装单位" prop="shigongdanwei" width="200" />
        <el-table-column align="center" label="产品编号/出厂编号" prop="chanpinbianhao" width="130" />
        <el-table-column align="center" label="制造单位" prop="zhizaodanwei" width="200" />
      </template>
      <template v-else-if="$store.getters.departmentId === 74">
        <el-table-column align="center" label="制造单位" prop="zhizaodanwei" width="200" />
        <el-table-column align="center" label="设备代码" prop="shebeidaima" width="200" />
        <el-table-column align="center" label="锅炉型号" prop="guigexinghao" width="200" />
        <el-table-column align="center" label="产品编号/出厂编号" prop="chanpinbianhao" width="130" />
      </template>
      <template v-else>
        <el-table-column align="center" label="使用登记证号" prop="shiyongdengjibianhao" width="150" sortable="custom" />
        <el-table-column align="center" label="注册代码" prop="zhucedaima" width="170" sortable="custom" />
        <el-table-column align="center" label="使用单位" prop="shiyongdanwei" width="200" />
        <el-table-column align="center" label="产品名称/设备名称" prop="shebeimingcheng" width="130" />
        <el-table-column align="center" label="制造单位" prop="zhizaodanwei" width="200" />
        <el-table-column align="center" label="产品编号/出厂编号" prop="chanpinbianhao" width="130" />
        <el-table-column align="center" label="单位内编号" prop="danweineibubianhao" width="130" sortable="custom" />
      </template>
      <el-table-column :formatter="formatter.getChineseName" align="center" label="检验人员" prop="jianyanrenyuan" width="140" />
      <el-table-column align="center" label="检验日期" prop="jianyanjieshuriqi" width="110" sortable="custom" />
      <el-table-column v-if="$store.getters.departmentId !== 74" align="center" label="下次检验日期" prop="xiacijianyanriqi" width="130" sortable="custom" />
      <el-table-column align="center" label="检验结论" prop="jianyanjielun" width="120" />
      <el-table-column :formatter="formatterHuanjie" fixed="right" align="center" label="当前环节" prop="flowstatus" width="95" />
      <el-table-column fixed="right" align="center" label="报告类型" prop="inspectionItems" width="95">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.bglx===1">
            主报告
          </el-tag>
          <el-tag v-if="scope.row.bglx===2" type="success">
            {{ scope.row.inspectionItems }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column fixed="right" align="center" label="操作" prop="" width="100">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-view" title="预览" circle @click="common.viewYsjl(scope.row.id, 'JYBG')" />
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="totalCount>0" :total="totalCount" :page-num.sync="listQuery.pageNum" :page-row.sync="listQuery.pageRow" @pagination="getList" />
  </div>
</template>

<script>
import Sticky from '@/components/Sticky'
import Pagination from '@/components/Pagination'
import Utils from '../../utils/contact.js'
export default {
  name: 'OwnDoneReport',
  components: { Sticky, Pagination },
  data() {
    return {
      totalCount: 0,
      list: [],
      listQuery: {
        pageNum: 1, // 页码
        pageRow: 20, // 每页条数
        order: undefined, // 排序字段
        sort: undefined, // 排序方式
        createBy: this.$store.getters.departmentId === 70 ? this.$store.getters.userId : undefined,
        hasChild: true, // 是否包含无损
        ownRoles: undefined, // 角色
        link: 7 // 已办报告环节
      },
      multipleSelection: [],
      sbzlList: [],
      statusList: [
        {
          value: 1,
          label: '检验人员'
        }, {
          value: 2,
          label: '审核人员'
        }/*, {
          value: 3,
          label: '审批人员'
        }*/
      ],
      userList: this.$store.getters.allUser,
      userId: this.$store.getters.userId
    }
  },
  created() {
    this.getList()
    this.getSbzlList()
  },
  mounted() {
    const that = this
    this.common.$on('bggl-yiban-list', function() {
      that.handleFilter()
    })
  },
  methods: {
    getList() {
      this.listQuery.cjState = 'bggl'
      this.api({
        url: '/ysjl/getList',
        method: 'get',
        params: this.listQuery
      }).then(data => {
        this.list = data.list
        this.totalCount = data.totalCount
        this.common.switchInspection(this.list)
      })
    },
    sortChange(column) {
      this.listQuery.order = column.prop
      this.listQuery.sort = column.order.replace('ending', '')
      this.getList()
    },
    getIndex($index) {
      // 表格序号
      return (this.listQuery.pageNum - 1) * this.listQuery.pageRow + $index + 1
    },
    handleFilter() {
      // 查询事件
      this.listQuery.pageNum = 1
      this.getList()
    },
    onRowClick(row) {
      this.$refs.list.toggleRowSelection(row)
    },
    handleSelectionChange: function(val) {
      this.multipleSelection = val
    },
    getSbzlList() {
      this.api({
        url: '/sedirectory/getList',
        method: 'get',
        params: {
          sbzl: undefined,
          level: '1'
        }
      }).then(data => {
        this.sbzlList = data
      })
    },
    formatterHuanjie(row) {
      switch (row.flowstatus) {
        case 1:
          return '起草报告'
        case 2:
          return '审核报告'
        case 3:
          return '审批报告'
        case 4:
          return '办结报告'
        default:
          return '未知'
      }
    },
    clearQuery() {
      this.listQuery = {
        order: undefined, // 排序字段
        sort: undefined, // 排序方式
        createBy: this.$store.getters.departmentId === 70 ? this.$store.getters.userId : undefined,
        hasChild: true, // 是否包含无损
        ownRoles: undefined, // 角色
        link: 7 // 已办报告环节
      }
      this.getList()
    },
    createQrcode() {
      let ids = ''
      for (let i = 0; i < this.multipleSelection.length; i++) {
        if (i === 0) {
          ids = this.multipleSelection[i].id
        } else {
          ids += ',' + this.multipleSelection[i].id
        }
      }
      this.api({
        url: '/ysjl/createQrcode',
        method: 'get',
        params: {
          ysjlIds: ids
        }
      }).then(() => {
        this.$message({
          message: '生成成功！',
          type: 'success'
        })
      })
    },
    /**
     * 撤回报告
     * 只能撤回流转状态的报告并且创建人或者检验人包括当前用户的报告
     */
    withDraw() {
      const withDraw = []
      this.$refs.list.selection.forEach(l => {
        if ((l.flowstatus === 2 || l.flowstatus === 3) && (l.createBy === this.$store.getters.userId.toString() || l.jianyanrenyuan.indexOf(this.$store.getters.userId.toString()) !== -1)) {
          withDraw.push(l)
        }
      })
      if (!this.$refs.list.selection.length || !withDraw.length) {
        this.$message.warning('请至少选择一份流转中的报告')
        return false
      }
      this.api({
        url: '/ysjl/withdraw',
        method: 'post',
        data: this.$refs.list.selection.map(ysjl => ysjl.id)
      }).then(data => {
        if (data) {
          this.getList()
          Utils.$emit('bggl-daiban-list')
          this.$message.success('撤回成功！')
        } else {
          this.$message.error('撤回失败！')
        }
      })
    },
    changeOwnRoles(val) {
      if (val === 2 || val === 3) {
        this.listQuery.createBy = undefined
      }
    }
  }
}
</script>

<style scoped>
  .app-container {
    padding-top: 5px;
    padding-bottom: 0px;
  }
</style>
