<!-- 首件鉴定 -->
<template>
  <div>
    <template v-if="!updateVisible && !detailVisible">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="search()">
        <el-form-item label="物料名称">
          <el-input
            v-model="dataForm.productName"
            placeholder="请输入物料名称"
            clearable
          />
        </el-form-item>
        <el-form-item label="生产批次号">
          <el-input
            v-model="dataForm.batchNumber"
            placeholder="请输入批次号"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="search()">查询</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        style="width: 100%"
      >
        <el-table-column
          label="序号"
          type="index"
          width="50"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="authenticateCode"
          header-align="center"
          align="center"
          :show-tooltip-when-overflow="true"
          label="首件鉴定编码"
        >
        </el-table-column>
        <el-table-column
          prop="orderCode"
          header-align="center"
          align="center"
          :show-tooltip-when-overflow="true"
          label="任务单编码"
        >
        </el-table-column>
        <el-table-column
          prop="productName"
          header-align="center"
          align="center"
          :show-tooltip-when-overflow="true"
          label="物料名称"
        >
        </el-table-column>
        <el-table-column
          prop="productNumber"
          header-align="center"
          align="center"
          :show-tooltip-when-overflow="true"
          label="物料编号"
        >
        </el-table-column>
        <el-table-column
          prop="batchNumber"
          header-align="center"
          align="center"
          :show-tooltip-when-overflow="true"
          label="批次号"
        >
        </el-table-column>
        <el-table-column
          prop=""
          header-align="center"
          align="center"
          :show-tooltip-when-overflow="true"
          label="系统生产记录"
        >
        </el-table-column>
        <el-table-column
          prop="attachList1"
          header-align="center"
          align="center"
          :show-tooltip-when-overflow="true"
          label="首件生产流程记录表"
        >
          <template slot-scope="scope">
            <el-button
              :disabled="
                !scope.row.attachList1 || scope.row.attachList1.length === 0
              "
              type="text"
              size="small"
              @click="attachDetails(scope.row.attachList1)"
              >查看</el-button
            >
          </template>
        </el-table-column>
        <el-table-column
          prop="attachList2"
          header-align="center"
          align="center"
          :show-tooltip-when-overflow="true"
          label="首件生产过程原始记录"
        >
          <template slot-scope="scope">
            <el-button
              :disabled="
                !scope.row.attachList2 || scope.row.attachList2.length === 0
              "
              type="text"
              size="small"
              @click="attachDetails(scope.row.attachList2)"
              >查看</el-button
            >
          </template>
        </el-table-column>
        <el-table-column
          prop="attachList3"
          header-align="center"
          align="center"
          :show-tooltip-when-overflow="true"
          label="首件鉴定检验报告"
        >
          <template slot-scope="scope">
            <el-button
              :disabled="
                !scope.row.attachList3 || scope.row.attachList3.length === 0
              "
              type="text"
              size="small"
              @click="attachDetails(scope.row.attachList3)"
              >查看</el-button
            >
          </template>
        </el-table-column>
        <el-table-column
          prop="attachList4"
          header-align="center"
          align="center"
          :show-tooltip-when-overflow="true"
          label="首件鉴定审查报告"
        >
          <template slot-scope="scope">
            <el-button
              :disabled="
                !scope.row.attachList4 || scope.row.attachList4.length === 0
              "
              type="text"
              size="small"
              @click="attachDetails(scope.row.attachList4)"
              >查看</el-button
            >
          </template>
        </el-table-column>
        <el-table-column
          prop="attachList5"
          header-align="center"
          align="center"
          :show-tooltip-when-overflow="true"
          label="首件鉴定评审结论"
        >
          <template slot-scope="scope">
            <el-button
              :disabled="
                !scope.row.attachList5 || scope.row.attachList5.length === 0
              "
              type="text"
              size="small"
              @click="attachDetails(scope.row.attachList5)"
              >查看</el-button
            >
          </template>
        </el-table-column>
        <el-table-column
          prop="approveStateStr"
          header-align="center"
          align="center"
          :show-tooltip-when-overflow="true"
          label="评审状态"
        >
        </el-table-column>
        <el-table-column
          prop="approver"
          header-align="center"
          align="center"
          :show-tooltip-when-overflow="true"
          label="当前评审人"
        >
        </el-table-column>
        <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          width="150"
          label="操作"
        >
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="detailHandle(scope.row.authenticateId)">详情</el-button>
            <el-button
              v-if="isAuth('first:authenticate:update')"
              type="text"
              size="small"
              @click="updateHandle(scope.row)"
              >上传</el-button
            >
            <el-button
              v-if="isAuth('first:authenticate:update')"
              type="text"
              size="small"
              @click="deleteHandle(scope.row.authenticateId)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper"
      >
      </el-pagination>
    </template>
    <quality-first-update ref="update" v-if="updateVisible" @onChose="onChose" />
    <attach-detail-dialog ref="attachDetail" v-if="attachVisible" @onChose="onChose" />
    <first-detail ref="detail" v-if="detailVisible" @onChose="onChose" />
  </div>
</template>

<script>
import {getList, delFirst} from '@/api/quality'
import QualityFirstUpdate from './first-update'
import AttachDetailDialog from '../common/attach-detail-dialog'
import {approveStateOption} from '@/utils/enums'
import FirstDetail from '@/views/modules/quality/first-detail'
export default {
  name: 'quality-first',
  components: {FirstDetail, QualityFirstUpdate, AttachDetailDialog},
  data () {
    return {
      updateVisible: false,
      attachVisible: false,
      detailVisible: false,
      approveStateOption: approveStateOption,
      dataForm: {},
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false
    }
  },
  mounted () {},
  created () {
    this.search()
  },
  methods: {
    onChose () {
      this.updateVisible = false
      this.detailVisible = false
    },
    search () {
      this.pageIndex = 1
      this.getDataList()
    },
         // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    getDataList () {
      this.dataListLoading = true
      let param = {
        current: this.pageIndex,
        size: this.pageSize,
        batchNumber: this.dataForm.batchNumber,
        productName: this.dataForm.productName
      }
      getList(param).then(({ data }) => {
        if (data && data.code === '200') {
          this.dataList = data.data.records
          this.totalPage = Number(data.data.total)
          this.dataList.forEach(item => {
            let option = this.approveStateOption.find(t => t.value === item.approveState)
            if (option != null) {
              item.approveStateStr = option.label
            }
          })
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    attachDetails (attachList) {
      this.attachVisible = true
      this.$nextTick(() => {
        this.$refs.attachDetail.init(attachList)
      })
    },
    updateHandle (item) {
      this.updateVisible = true
      this.$nextTick(() => {
        this.$refs.update.init(item)
      })
    },
    detailHandle (id) {
      this.detailVisible = true
      this.$nextTick(() => {
        this.$refs.detail.init(id)
      })
    },
    deleteHandle (id) {
      this.$confirm('是否确认要删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delFirst([id]).then(({data}) => {
          if (data && data.code === '200') {
            this.$message({
              type: 'success',
              message: '删除成功!',
              duration: 1500,
              onClose: () => {
                this.search()
              }
            })
          } else {
            this.$message({
              type: 'error',
              message: data.msg
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
