<!-- 发货管理 -->
<template>
  <div class="order">
    <template v-if="!noticeChangeVisible && !attachVisible && !arrivedVisible && !detailVisible">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="queryData()">
        <el-form-item label="名称" prop="productName">
          <el-input v-model="dataForm.productName" placeholder="名称" clearable></el-input>
        </el-form-item>
        <el-form-item label="任务单编码" prop="orderCode">
          <el-input v-model="dataForm.orderCode" placeholder="任务单编码" clearable></el-input>
        </el-form-item>
        <el-form-item label="生产状态" prop="state">
          <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="queryData()">查询</el-button>
          <el-button v-if="isAuth('order:deliver:noteChangeConfig')" type="primary" @click="setNoticeChangeHandel()">发货通知人设置</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
        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="140"
          :show-tooltip-when-overflow="true"
          label="任务单编码">
        </el-table-column>
        <el-table-column
          prop="customerName"
          header-align="center"
          align="center"
          min-width="140"
          :show-tooltip-when-overflow="true"
          label="客户名称">
        </el-table-column>
        <el-table-column
          fixed="right"
          prop="contractNumber"
          header-align="center"
          align="center"
          min-width="160"
          :show-tooltip-when-overflow="true"
          label="生产编号">
        </el-table-column>
        <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          min-width="80"
          :formatter="stateFormat"
          :show-tooltip-when-overflow="true"
          label="生产状态">
        </el-table-column>
        <el-table-column
          prop="productName"
          header-align="center"
          align="center"
          min-width="140"
          :show-tooltip-when-overflow="true"
          label="物料名称">
        </el-table-column>
        <el-table-column
          prop="cnt"
          header-align="center"
          align="center"
          min-width="120"
          :show-overflow-tooltip="true"
          label="数量">
        </el-table-column>
        <el-table-column
          prop="deliverCnt"
          header-align="center"
          align="center"
          min-width="160"
          :show-tooltip-when-overflow="true"
          label="送货数量">
        </el-table-column>
        <el-table-column
          prop="arriveCnt"
          header-align="center"
          align="center"
          min-width="160"
          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="160"
          label="批次号">
        </el-table-column>
        <el-table-column
          prop="state"
          header-align="center"
          align="center"
          min-width="160"
          label="状态">
        >
          <template slot-scope="scope">{{stateFormat(scope.row)}}</template>
        </el-table-column>
        <el-table-column
          prop="consignmentDate"
          header-align="center"
          align="center"
          min-width="160"
          label="发货时间">
        </el-table-column>
        <el-table-column
          prop="arriveDate"
          header-align="center"
          align="center"
          min-width="160"
          label="送达时间">
        </el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          label="签收附件">
          <template slot-scope="scope">
            <el-button :disabled="!scope.row.attachList || scope.row.attachList.length === 0" type="text" size="small" @click="attachDetails(scope.row)">查看</el-button>
          </template>
        </el-table-column>

        <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          width="160"
          label="操作">
          <template slot-scope="scope">
            <el-button v-if="isAuth('order:deliver:info') && Number(scope.row.state) <= 3" type="text" size="small" @click="detailHandle(scope.row)">查看</el-button>
            <el-button v-if="isAuth('order:deliver:send') && Number(scope.row.state) === 2" type="text" size="small" @click="sendHandle(scope.row.deliverId)">发货</el-button>
            <el-button v-if="isAuth('order:deliver:arrive') && Number(scope.row.state) === 3" type="text" size="small" @click="arriveHandle(scope.row.deliverId)">送达</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>
<!--    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>-->
<!--    <detail v-if="detailVisible" ref="detail"/>-->
    <!-- 文件预览 -->
    <attach-detail v-if="attachVisible" ref="attachDetail" @onChose="onChose"/>
    <!-- 发货通知设置弹窗 -->
    <notice-change-setting v-if="noticeChangeVisible" ref="noticeChangeSetting" @onChose="onChose"/>
    <!--送达弹窗 -->
    <dispatch-arrived v-if="arrivedVisible" ref="arrivedPage" @onChose="onChose" @refreshDataList="getDataList"/>
    <!-- 发货详情 -->
    <prod-management-details v-if="detailVisible" ref="detail" @onChose="onChose"/>
    <!-- 确定发货 -->
    <dispatching v-if="dispatchVisible" ref="dispatching" @refreshDataList="getDataList"/>
  </div>
</template>

<script>
import { getDispatchList } from '@/api/sale'
import PreviewComponent from '../common/preview-component'
import AttachDetail from '../common/attach-detail'
import NoticeChangeSetting from './dispatch-notice-change-setting'
import DispatchArrived from './dispatch-arrived'
import DispatchDetail from './dispatch-detail'
import ProdManagementDetails from '@/views/modules/production/prod-management-details'
import Dispatching from '@/views/modules/order/dispatching'
export default {
    // 发货管理
  name: 'dispatch',
  components: {
    Dispatching,
    ProdManagementDetails,
    AttachDetail,
    PreviewComponent,
    NoticeChangeSetting,
    DispatchArrived,
    DispatchDetail
  },
  created () {
    this.queryData()
  },
  data () {
    return {
      dataForm: {},
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      attachVisible: false,
      noticeChangeVisible: false,
      arrivedVisible: false,
      detailVisible: false,
      dispatchVisible: false,
      optionsState: [
        {
          code: null, value: '全部'
        },
        {
          code: '0', value: '待排产'
        },
        {
          code: '1', value: '生产中'
        },
        {
          code: '2', value: '已完成'
        },
        {
          code: '3', value: '发货中'
        },
        {
          code: '4', value: '已送达'
        }
      ]
    }
  },
  methods: {
    onChose () {
      this.attachVisible = false
      this.noticeChangeVisible = false
      this.arrivedVisible = false
      this.detailVisible = false
      this.dispatchVisible = false
    },
      // 查询
    queryData () {
      this.pageIndex = 1
      this.getDataList()
    },
      // 获取数据列表
    getDataList () {
      this.dataListLoading = true
      let params = {
        'current': this.pageIndex,
        'size': this.pageSize,
        'productName': this.dataForm.productName ? this.dataForm.productName : null,
        'orderCode': this.dataForm.orderCode ? this.dataForm.orderCode : null,
        'state': this.dataForm.state ? this.dataForm.state : null
      }
      getDispatchList(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
    },
    // 详情
    detailHandle (row) {
      this.detailVisible = true
      this.$nextTick(() => {
        this.$refs.detail.init(row.prodProductionId, 3)
      })
    },
    // 发货通知人设置
    setNoticeChangeHandel () {
      this.noticeChangeVisible = true
      this.$nextTick(() => {
        this.$refs.noticeChangeSetting.init()
      })
    },
    // 附件
    attachDetails (row) {
      this.attachVisible = true
      this.$nextTick(() => {
        this.$refs.attachDetail.init(row.attachList)
      })
    },
    // 送达
    arriveHandle (deliverId) {
      this.arrivedVisible = true
      this.$nextTick(() => {
        this.$refs.arrivedPage.init(deliverId)
      })
    },
    // 发货
    sendHandle (deliverId) {
      this.dispatchVisible = true
      this.$nextTick(() => {
        this.$refs.dispatching.init(deliverId)
      })
    },
    // 转换属性“状态”
    stateFormat (row) {
      if (this.optionsState) {
        for (let i = 0; i < this.optionsState.length; i++) {
          if (this.optionsState[i].code === row.state) {
            return this.optionsState[i].value
          }
        }
      }
    }
  }
}
</script>

<style scoped>

</style>
