<!-- 排产管理 -->
<template>
  <div class="production">
    <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.customerName" placeholder="客户名称" clearable/>
      </el-form-item>
      <el-form-item>
        <el-button @click="queryPage()">查询</el-button>
        <el-button @click="batchPlan()">批量排产</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="handleSelectionChange"
      style="width: 100%;">
       <el-table-column
        type="selection"
        width="50">
      </el-table-column>
      <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="140"
        :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="prodCode"
        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="isCompose"
        header-align="center"
        align="center"
        :formatter="formatBoolean"
        label="是否组合产品">
      </el-table-column>
      <el-table-column
        prop="state"
        header-align="center"
        align="center"
        :formatter="formatState"
        label="当前状态">
      </el-table-column>
      <el-table-column
        prop="notes"
        header-align="center"
        align="center"
        min-width="180"
        :show-overflow-tooltip="true"
        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:production:plan')" type="text" size="small" @click="detail(scope.row.id, true)">排产</el-button>
          <el-button v-if="false" type="text" size="small" disabled>委外生产</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>
    <!-- 弹窗, 新增 / 修改 -->
    <detail v-if="detailsVisible" ref="details" @refreshDataList="getDataList"/>
  </div>
</template>

<script>
  import Detail from './scheduling-details'
  import { getSchedulingList, batchPlan } from '@/api/production'
  export default {
    name: 'scheduling',
    components: {Detail},
    data () {
      return {
        detailsVisible: false,
        dataForm: {},
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        // 状态：1：待排产，2:生产中，3:生产完成
        optionsState: [
          {
            code: '1',
            value: '待排产'
          },
          {
            code: '2',
            value: '生产中'
          },
          {
            code: '3',
            value: '生产完成'
          }
        ],
        multipleSelection: []
      }
    },
    created () {
      this.getDataList()
    },
    methods: {
      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: '批量排产成功!'
            })
          } 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)
        })
      },
      // 转换属性：是否
      formatBoolean (row) {
        if (!row.isCompose) return ''
        return row.isCompose === '1' ? '是' : '否'
      },
      // 转换属性“是否组合产品”
      formatState (row) {
        if (!row.state) return ''
        const item1 = this.optionsState.find((item) => item.code === row.state.toString())
        return item1 ? item1.value : ''
      }
    }
  }
</script>

<style scoped>

</style>
