<!-- 生产监控管理 -->
<template>
  <div class="production">
    <template v-if="!detailsVisible && !outsourceVisible && !purchaseVisible">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="queryPage()">
        <el-form-item label="合同号">
          <el-input v-model="dataForm.orderCode" placeholder="合同号" clearable/>
        </el-form-item>
        <el-form-item label="物料名称">
          <el-input v-model="dataForm.productName" placeholder="物料名称" clearable/>
        </el-form-item>
        <el-form-item label="状态">
          <el-select
            v-model="dataForm.state"
            remote
            placeholder="请选择">
            <el-option
              v-for="item in optionsState"
              :key="item.code"
              :label="item.value"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="queryPage()">查询</el-button>
          <el-button v-if="false" type="primary" disabled @click="createNewProduct()">创建新物料</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="productNumber"
          header-align="center"
          align="center"
          min-width="100"
          :show-tooltip-when-overflow="true"
          label="物料编号">
        </el-table-column>
        <el-table-column
          prop="mapNumber"
          header-align="center"
          align="center"
          width="120"
          :show-tooltip-when-overflow="true"
          label="图号">
        </el-table-column>
        <el-table-column
          prop="productName"
          header-align="center"
          align="center"
          min-width="160"
          :show-tooltip-when-overflow="true"
          label="物料名称">
        </el-table-column>
        <el-table-column
          prop="progressBar"
          header-align="center"
          align="center"
          min-width="100"
          label="生产进度">
          <template slot-scope="scope">
            <el-progress :percentage="scope.row.progressBar?Number(scope.row.progressBar):0"></el-progress>
          </template>
        </el-table-column>
        <el-table-column
          prop="deliveryDate"
          header-align="center"
          align="center"
          min-width="160"
          label="交期时间">
        </el-table-column>
        <el-table-column
          prop="warningState"
          header-align="center"
          align="center"
          min-width="60"
          label="预警状态">
          <template slot-scope="scope">
            <el-tag v-if="Number(scope.row.warningState) === 1">正常</el-tag>
            <el-tag type="warning" v-if="Number(scope.row.warningState) === 2">预警</el-tag>
            <el-tag type="danger" v-if="Number(scope.row.warningState) === 3">超期</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="contractNumber"
          header-align="center"
          align="center"
          min-width="120"
          :show-tooltip-when-overflow="true"
          label="合同号">
        </el-table-column>
        <el-table-column
          prop="customerName"
          header-align="center"
          align="center"
          min-width="160"
          :show-tooltip-when-overflow="true"
          label="客户名称">
        </el-table-column>
        <el-table-column
          fixed="right"
          prop="state"
          header-align="center"
          align="center"
          :formatter="formatState"
          label="状态">
        </el-table-column>
        <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          width="150"
          label="操作">
          <template slot-scope="scope">
            <el-button v-if="isAuth('prod:monitoring:info')" type="text" size="small" @click="detailHandle(scope.row)">查看</el-button>
            <el-button type="text" size="small" @click="printLabel(scope.row)">打印标签 + {{scope.row.printCnt}}</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>
    <!-- 弹窗, 查看 -->
    <prod-management-details v-if="detailsVisible" ref="details" @onChose="onChose" @refreshDataList="getDataList"/>
    <purchase-detail v-if="purchaseVisible" ref="purchase" @refreshDataList="getDataList" @onChose="onChose"/>
    <outsource-detail  v-if="outsourceVisible" ref="outsource" @refreshDataList="getDataList" @onChose="onChose"/>
    <!-- 打印标签 -->
    <print-label v-if="printLabelVisible" ref="printLabel" @onChose="onChose" @addOne="addOneClick"/>
  </div>
</template>

<script>
  import Detail from './monitoring-details'
  import { getMonitoringList } from '@/api/production'
  import PurchaseDetail from '@/views/modules/sale/purchase-detail'
  import OutsourceDetail from '@/views/modules/sale/outsource-detail'
  import ProdManagementDetails from '@/views/modules/production/prod-management-details'
  import PrintLabel from '@/views/modules/production/components/print-label.vue'
import CraftsDetail from '@/views/modules/tech/crafts-detail-dialog.vue'
export default {
    name: 'monitoring',
    components: {CraftsDetail, PrintLabel, ProdManagementDetails, OutsourceDetail, PurchaseDetail, Detail},
    data () {
      return {
        detailsVisible: false,
        purchaseVisible: false,
        outsourceVisible: false,
        printLabelVisible: false,
        dataForm: {},
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        optionsState: [
          {
            code: '3', value: '采购'
          },
          {
            code: '4', value: '委外'
          },
          {
            code: '5', value: '生产'
          }
        ]
      }
    },
    created () {
      this.getDataList()
    },
    methods: {
      onChose () {
        this.detailsVisible = false
        this.outsourceVisible = false
        this.purchaseVisible = false
        this.printLabelVisible = false
      },
      // 查询
      queryPage () {
        this.pageIndex = 1
        this.getDataList()
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        let params = {
          'current': this.pageIndex,
          'size': this.pageSize,
          'state': this.dataForm.state ? this.dataForm.state : null,
          'productName': this.dataForm.productName ? this.dataForm.productName : null,
          'orderCode': this.dataForm.orderCode ? this.dataForm.orderCode : null
        }
        getMonitoringList(params).then(({data}) => {
          if (data && data.code === '200') {
            this.dataList = data.data.records
            // 设置已打印标签数量的初始值
            if (this.dataList) {
              this.dataList.forEach((item) => {
                item.printCnt = 0
              })
            }
            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
      },
      // 新增 / 修改
      detail (id) {
        this.detailsVisible = true
        this.$nextTick(() => {
          this.$refs.details.init(id, 1)
        })
      },
      outsourceDetail (row) {
        this.outsourceVisible = true
        this.$nextTick(() => {
          this.$refs.outsource.init(row.tableId)
        })
      },
      purchaseDetail (row) {
        this.purchaseVisible = true
        this.$nextTick(() => {
          this.$refs.purchase.init(row.tableId)
        })
      },
      // 标签打印
      printLabel (row) {
        this.printLabelVisible = true
        this.$nextTick(() => {
          this.$refs.printLabel.init(row)
        })
      },
      // 创建新物料
      createNewProduct () {
        this.$message.warning('功能暂未开放')
      },
      closeDialogEvent () {
        this.detailsVisible = false
      },
      formatState (row) {
        if (!row.state) return ''
        const item1 = this.optionsState.find((item) => item.code === row.state.toString())
        return item1 ? item1.value : ''
      },
      detailHandle (row) {
        if (!row || !row.state) return
        let s = Number(row.state)
        switch (s) {
          case 3:
          // 调用采购详情页
            this.purchaseDetail(row)
            break
          case 4:
          // 调用委外详情页
            this.outsourceDetail(row)
            break
          default:
          // 详情
            this.detail(row.id, row.prodCode, true)
        }
      },
      addOneClick (row) {
        // this.dataList 中匹配 row.id，找到对应的数据
        this.dataList.forEach((item) => {
          if (item.id === row.id) {
            if (!item.printCnt) {
              item.printCnt = 0
            }
            item.printCnt += 1
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>

