<!-- 报损记录 -->
<template>
  <div>
    <template v-if="!detailVisible">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="queryPage()">
        <el-form-item label="产品名称">
          <el-input v-model="dataForm.name" placeholder="产品名称" clearable/>
        </el-form-item>
        <el-form-item label="合同号">
          <el-input v-model="dataForm.orderCode" placeholder="合同号" clearable/>
        </el-form-item>
        <el-form-item label="产品编号">
          <el-input v-model="dataForm.prodCode" placeholder="产品编号" clearable/>
        </el-form-item>
        <el-form-item>
          <el-button @click="queryPage()">查询</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="productName"
          header-align="center"
          align="center"
          width="160"
          :show-overflow-tooltip="true"
          label="产品名称">
        </el-table-column>
        <el-table-column
          prop="prodCode"
          header-align="center"
          align="center"
          width="160"
          :show-overflow-tooltip="true"
          label="产品编号">
        </el-table-column>
        <el-table-column
          prop="orderCode"
          header-align="center"
          align="center"
          width="160"
          :show-overflow-tooltip="true"
          label="合同号">
        </el-table-column>
        <el-table-column
          prop="techName"
          header-align="center"
          align="center"
          width="160"
          :show-overflow-tooltip="true"
          label="工艺名称">
        </el-table-column>
        <el-table-column
          prop="nodeName"
          header-align="center"
          align="center"
          width="160"
          :show-overflow-tooltip="true"
          label="报损工序">
        </el-table-column>
        <el-table-column
          prop="liableUserName"
          header-align="center"
          align="center"
          width="120"
          :show-overflow-tooltip="true"
          label="报损人">
        </el-table-column>
        <el-table-column
          prop="damageReason"
          header-align="center"
          align="center"
          width="120"
          :show-overflow-tooltip="true"
          label="损坏原因">
        </el-table-column>
        <el-table-column
          prop="damageType"
          header-align="center"
          align="center"
          :formatter="formatDamageType"
          label="损坏类型">
        </el-table-column>

        <el-table-column
          prop="createTime"
          header-align="center"
          align="center"
          min-width="160"
          label="报损时间">
        </el-table-column>
        <el-table-column
          prop="state"
          header-align="center"
          align="center"
          :formatter="formatState"
          min-width="120"
          label="状态">
        </el-table-column>
        <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          width="140"
          label="操作">
          <template slot-scope="scope">
            <el-button v-if="isAuth('prod:damage:info')" type="text" size="small" @click="detailHandle(scope.row.recordId)">查看</el-button>
            <el-button v-if="isAuth('prod:damage:again') && Number(scope.row.state) !== 2" type="text" size="small" @click="reScheduleHandle(scope.row.recordId)">重新排产</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>
    <detail v-if="detailVisible" ref="detail" @onChose="onChose"/>
  </div>
</template>

<script>
  import Detail from './damage-details'
  import { getDamageList, reSchedule } from '@/api/production'
  export default {
    name: 'damage',
    components: {Detail},
    data () {
      return {
        detailVisible: false,
        dataForm: {},
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: []
      }
    },
    created () {
      this.getDataList()
    },
    methods: {
      onChose () {
        this.detailVisible = false
      },
      // 查询
      queryPage () {
        this.pageIndex = 1
        this.getDataList()
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.addOrUpdateVisible = false
        // 接口调用
        let params = {
          'current': this.pageIndex,
          'size': this.pageSize,
          'productName': this.dataForm.name ? this.dataForm.name : null,
          'orderCode': this.dataForm.orderCode ? this.dataForm.orderCode : null,
          'prodCode': this.dataForm.prodCode ? this.dataForm.prodCode : null
        }
        getDamageList(params).then(({data}) => {
          if (data && data.code === '200') {
            this.dataList = data.data.records
            this.totalPage = Number(data.data.total)
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 损坏类型
      formatDamageType (row) {
        if (!row.damageType) return ''
        if (Number(row.damageType) === 1) return '操作损坏'
        if (Number(row.damageType) === 2) return '检验损坏'
        return ''
      },
      // 详情
      detailHandle (id) {
        this.detailVisible = true
        this.$nextTick(() => {
          this.$refs.detail.init(id)
        })
      },
      // 状态
      formatState (row) {
        if (!row.state) return ''
        if (Number(row.state) === 1) return '未重新排产'
        if (Number(row.state) === 2) return '已重新排产'
        return ''
      },
      // 重新排产
      reScheduleHandle (id) {
        reSchedule(id).then(({data}) => {
          if (data && data.code === '200') {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>
