<!-- 任务处理中心 -->
<template>
  <div class="production">
    <template v-if="!detailsVisible && !outsourceVisible">
      <el-header style="font-size: large;text-align: center">任务处理中心</el-header>
      <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="orderCode"
          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
          prop="contractNumber"
          header-align="center"
          align="center"
          min-width="140"
          :show-tooltip-when-overflow="true"
          label="合同编号">
        </el-table-column>
        <el-table-column
          prop="productNumber"
          header-align="center"
          align="center"
          min-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="cnt"
          header-align="center"
          align="center"
          min-width="100"
          label="数量">
        </el-table-column>
        <el-table-column
          prop="isAppraise"
          header-align="center"
          align="center"
          :formatter="formatAppraise"
          label="是否鉴定">
        </el-table-column>
        <el-table-column
          prop="isFirst"
          header-align="center"
          align="center"
          :formatter="formatFirst"
          label="是否首件">
        </el-table-column>
        <el-table-column
          prop="deliveryDate"
          header-align="center"
          align="center"
          min-width="160"
          label="交期时间">
        </el-table-column>
        <el-table-column
          prop="batchNumber"
          header-align="center"
          align="center"
          min-width="140"
          :show-tooltip-when-overflow="true"
          label="批次号">
        </el-table-column>
        <el-table-column
          prop="notes"
          header-align="center"
          align="center"
          width="180"
          :show-tooltip-when-overflow="true"
          label="备注">
        </el-table-column>
        <el-table-column
          prop="inventoryCnt"
          header-align="center"
          align="center"
          min-width="140"
          label="仓库核对数量">
        </el-table-column>
        <el-table-column
          prop="prodState"
          header-align="center"
          align="center"
          width="120"
          :formatter="formatState"
          label="当前状态">
        </el-table-column>
        <el-table-column
          prop="state"
          header-align="center"
          align="center"
          :formatter="formatState2"
          label="处理状态">
        </el-table-column>
        <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          width="280"
          label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="check(scope.row.id)">核料</el-button>
            <el-button type="text" size="small" @click="check(scope.row.id)">转采购</el-button>
            <el-button type="text" size="small" @click="check(scope.row.id)">转委外</el-button>
            <el-button type="text" size="small" @click="check(scope.row.id)">试制前检查</el-button>
            <el-button v-if="isAuth('prod:production:plan')" type="text" size="small" @click="detail(scope.row.id, true)">排产</el-button>
<!--            <el-button v-if="isAuth('pur:commDetail:save') && Number(scope.row.outsourcing) === 0" type="text" size="small" @click="outsourceHandle(scope.row.id)">委外生产</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="detailsVisible" ref="details" @close="closeDialogEvent" @refreshDataList="getDataList" @onChose="onChose"/>
    <!-- 委外 -->
    <outsource v-if="outsourceVisible" ref="outsource" @refreshDataList="getDataList" @onChose="onChose"/>
  </div>
</template>

<script>
  import Detail from './scheduling-details'
  import Outsource from './scheduling-outsource'
  import { getSchedulingList, batchPlan } from '@/api/production'
  export default {
    name: 'scheduling',
    components: {Detail, Outsource},
    data () {
      return {
        detailsVisible: false,
        outsourceVisible: false,
        dataForm: {},
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        // 状态
        optionsProdState: [
          {
            code: '0',
            value: '待试制前检查'
          },
          {
            code: '1',
            value: '等待'
          },
          {
            code: '2',
            value: '待排产'
          },
          {
            code: '3',
            value: '生产中生产完成'
          },
          {
            code: '4',
            value: '生产完成'
          },
          {
            code: '5',
            value: '检验不合格完成'
          },
          {
            code: '6',
            value: '操作损坏完成'
          }
        ],
        // 处理状态
        optionsState: [
          {
            code: '1',
            value: '待核料'
          },
          {
            code: '2',
            value: '采购'
          },
          {
            code: '3',
            value: '委外'
          },
          {
            code: '4',
            value: '生产'
          }
        ],
        multipleSelection: []
      }
    },
    created () {
      this.getDataList()
    },
    methods: {
      onChose () {
        this.detailsVisible = false
        this.outsourceVisible = false
      },
      handleSelectionChange (val) {
        this.multipleSelection = val
      },
      // 批量排产
      batchPlan () {
        let ids = this.multipleSelection.map(item => item.id)
        // eslint-disable-next-line standard/object-curly-even-spacing
        batchPlan({ productionIds: ids}).then(({data}) => {
          if (data && data.code === '200') {
            this.$message({
              type: 'success',
              message: '批量排产成功!'
            })
            this.getDataList()
          } else {
            this.$message({
              type: 'error',
              message: data.msg
            })
          }
        })
      },
      // 查询
      queryPage () {
        this.pageIndex = 1
        this.getDataList()
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        let params = {
          'current': this.pageIndex,
          'size': this.pageSize,
          'customerName': this.dataForm.customerName ? this.dataForm.customerName : null,
          'orderCode': this.dataForm.orderCode ? this.dataForm.orderCode : null
        }
        getSchedulingList(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
      },
      // 新增 / 修改
      detail (id, disable) {
        this.detailsVisible = true
        this.$nextTick(() => {
          this.$refs.details.init(id, disable)
        })
      },
      // 转换属性：是否
      formatAppraise (row) {
        if (!row.isAppraise) return ''
        return row.isAppraise === '1' ? '否' : '是'
      },
      // 转换属性：是否
      formatFirst (row) {
        if (!row.isFirst) return ''
        return row.isFirst === '1' ? '否' : '是'
      },
      // 转换属性“当前状态”
      formatState (row) {
        if (!row.prodState) return ''
        const item1 = this.optionsProdState.find((item) => item.code === row.prodState.toString())
        return item1 ? item1.value : ''
      },
      // 转换属性“处理状态”
      formatState2 (row) {
        if (!row.state) {
          return ''
        }
        const item1 = this.optionsState.find((item) => item.code === row.state.toString())
        return item1 ? item1.value : ''
      },
      closeDialogEvent () {
        this.detailsVisible = false
      },
      // 委外生产
      outsourceHandle (id) {
        this.outsourceVisible = true
        let ids = []
        ids.push(id)
        this.$nextTick(() => {
          this.$refs.outsource.init(id, ids)
        })
      },
      // 批量委外生产
      outsourceHandle2 () {
        let ids = this.multipleSelection.map(item => item.id)
        this.outsourceVisible = true
        this.$nextTick(() => {
          this.$refs.outsource.init(-1, ids)
        })
      }
    }
  }
</script>

<style scoped>

</style>
