<template>
  <div class="mod-transport">
    <!-- 搜索栏 -->
    <div class="search-bar">
      <el-form :inline="true" class="search-form" ref="searchForm" :model="searchForm" label-width="auto" size="small">
        <div class="input-row">
          <el-form-item label="波次号" class="search-form-item">
            <el-input type="text" v-model="searchForm.waveNo" placeholder="请输入波次号"></el-input>
          </el-form-item>
          <el-form-item label="备注名" class="search-form-item">
            <el-input type="text" v-model="searchForm.waveName" placeholder="请输入备注名"></el-input>
          </el-form-item>
          <el-form-item label="生产状态"  :label-width=" lang === 'en' ? '145px' : '85px'">
            <el-select
              v-model="searchForm.productionStatus"
              clearable
              placeholder="请选择生产状态"
              size="small">
              <el-option key="0" label="未生成" value="0"></el-option>
              <el-option key="1" label="已生产" value="1"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="运单号状态"  :label-width=" lang === 'en' ? '145px' : '85px'">
            <el-select
              v-model="searchForm.expressBillStatus"
              clearable
              placeholder="请选择运单号状态"
              size="small">
              <el-option key="0" label="未获取" value="0"></el-option>
              <el-option key="1" label="已获取" value="1"></el-option>
            </el-select>
          </el-form-item>
          <!--<el-form-item label="发货状态"  :label-width=" lang === 'en' ? '145px' : '85px'">
            <el-select
              v-model="searchForm.scanStatus"
              clearable
              placeholder="请选择发货状态"
              size="small">
              <el-option key="0" label="未扫描" value="0"></el-option>
              <el-option key="1" label="部分扫描" value="1"></el-option>
              <el-option key="2" label="全部已扫描" value="2"></el-option>
            </el-select>
          </el-form-item>-->
          <el-form-item label="生成时间"   :label-width=" lang === 'en' ? '145px' : '85px'">
            <el-date-picker
              size="small"
              v-model="dateRange"
              type="datetimerange"
              :range-separator="this.$i18n.t('time.tip')"
              value-format="yyyy-MM-dd HH:mm:ss"
              :start-placeholder="this.$i18n.t('time.start')"
              :end-placeholder="this.$i18n.t('time.end')"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <div class="default-btn primary-btn" @click="searchChange(true)">{{$t('crud.searchBtn')}}</div>
            <div class="default-btn" @click="resetSearchForm('searchForm')">{{$t('shop.resetMap')}}</div>
          </el-form-item>
        </div>
      </el-form>
    </div>

    <div class="main-container">
      <!-- 表格 -->
      <div class="table-con transport-table">
        <el-table
          ref="waveTable"
          :data="dataList"
          border
          header-cell-class-name="table-header"
          row-class-name="table-row-low"
          style="width: 100%">
          <el-table-column
            align="center"
            prop="waveNo"
            label="波次号">
            <template slot-scope="scope">
              <span class="table-cell-text">{{ scope.row.waveNo }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="备注名"
            align="center"
            prop="waveName">
            <template slot-scope="scope">
              <span>{{scope.row.waveName }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="生产状态"
            align="center"
            prop="productionStatus">
            <template slot-scope="scope">
              <span v-if="scope.row.productionStatus == 0">未生产</span>
              <span v-else-if="scope.row.productionStatus == 1">已生产</span>
              <span v-else>未知</span>
            </template>
          </el-table-column>

          <el-table-column
            align="center"
            prop="orderNum"
            label="订单数量"
          >
            <template slot-scope="scope">
              <span>{{scope.row.orderNum }}</span>
            </template>
          </el-table-column>

          <el-table-column
            align="center"
            label="生成时间"
            prop="createTime"
          >
            <template slot-scope="scope">
              <span>{{scope.row.createTime }}</span>
            </template>
          </el-table-column>

          <el-table-column
            align="center"
            prop="expressBillStatus"
            label="运单号状态"
          >
            <template slot-scope="scope">
              <span v-if="scope.row.expressBillStatus == 0">未获取</span>
              <span v-else-if="scope.row.expressBillStatus == 1">已获取</span>
              <span v-else>未知</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="scanStatus"
            label="发货状态"
          >
            <template slot-scope="scope">
              <span v-if="scope.row.scanStatus == 0">未发货</span>
              <span v-else-if="scope.row.scanStatus == 1">部分发货({{scope.row.scanNum}}/{{scope.row.orderNum}})</span>
              <span v-else-if="scope.row.scanStatus == 2">全部已发货</span>
              <span v-else>未知</span>
            </template>
          </el-table-column>

          <el-table-column
            align="center"
            :label="$t('crud.menu')"
            width="150"
          >
            <template slot-scope="scope">
              <div style="display: inline-block;float: left;">
<!--                  <el-button size="mini" @click="downloadWavePrintOrderPdf(scope.row)" :loading="wavePrintOrderPdfDownloadLoading" :disabled="scope.row.productionStatus == 0">下载PDF包</el-button>-->
<!--                  <el-button size="mini" :loading="wavePrintOrderInfoDownloadLoading" @click="downloadWavePrintOrderInfo(scope.row)" :disabled="scope.row.productionStatus == 0">下载订单信息</el-button>-->
<!--                  <el-button size="mini" @click="openScanDialog(scope.row)" :disabled="scope.row.productionStatus == 0">扫描发货</el-button>-->
              </div>
              <div
                class="default-btn text-btn operate-btn"
                @click="openWaveDetailDialog(scope.row)">详情</div>
              <div
                class="default-btn text-btn"
                @click="openWaveModifyDialog(scope.row)">
                编辑
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 分页 -->
      <el-pagination
        v-if="dataList.length"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>
    <!-- 波次详情 -->
    <wave-print-order ref="wavePrintOrder" @refressWaveList="getDataList"></wave-print-order>

<!--    <order-scan-delivery ref="orderScanDelivery" @refreshDataList="getDataList"></order-scan-delivery>-->

    <!-- 修改波次 -->
    <el-dialog
      title="修改波次"
      :visible.sync="modifyVisible"
      v-if="modifyVisible"
      width="40%"
      center
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form :model="modifyWave">
        <el-form-item label="波次号" label-width="100px">
          <span autocomplete="off">{{modifyWave.waveNo}}</span>
        </el-form-item>
        <el-form-item label="备注名" label-width="100px">
          <el-input v-model="modifyWave.waveName" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="modifyHandle(modifyWave)">确 认</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import {downloadXls} from "@/utils/excel";
  import {downloadZip} from "@/utils/zip";
  import OrderScanDelivery from '@/components/order-scan-delivery';
  export default {
    components: {OrderScanDelivery},
    data () {
      return {
        theData: null, // 保存上次点击查询的请求条件

        dataForm: {
          transName: ''
        },
        dataList: [],
        dataListLoading: false,
        dataListSelections: [],
        modifyVisible: false,
        modifyWave: {},
        lang: localStorage.getItem('lang'),
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 10 // 每页显示多少条
        },
        searchForm: {},
        dateRange: [],
        selectAll: false,
        wavePrintOrderInfoDownloadLoading: false,
        wavePrintOrderPdfDownloadLoading: false,
        wavePrintOrderVisible: false

      }
    },
    created () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList (page, newData = false) {
        this.dataListLoading = true
        if (newData || !this.theData) {
          this.searchForm
          this.searchForm.startTime = this.dateRange === null ? null : this.dateRange[0], // 开始时间
          this.searchForm.endTime = this.dateRange === null ? null : this.dateRange[1], // 结束时间
          this.theData = JSON.parse(JSON.stringify(this.searchForm))
        }
        this.$http({
          url: this.$http.adornUrl('/wave/wave/page'),
          method: 'get',
          params: this.$http.adornParams(
            Object.assign(
              {
                current: page == null ? this.page.currentPage : page.currentPage,
                size: page == null ? this.page.pageSize : page.pageSize
              },
              this.theData
            )
          )
        }).then(({data}) => {
          this.dataList = data.records
          this.page.total = data.total
          this.dataListLoading = false
        })
      },
      // 新增 / 修改
      openWaveDetailDialog (wave) {
        this.$refs.wavePrintOrder.init(wave)
      },
      openWaveModifyDialog (wave) {
        this.modifyVisible = true;
        this.modifyWave = wave;
      },
      downloadWavePrintOrderInfo(wave){
        this.wavePrintOrderInfoDownloadLoading = true;
        this.$http({
          url: this.$http.adornUrl('/wave/wave/downloadWavePrintOrderInfo'),
          method: 'get',
          params: this.$http.adornParams({waveId: wave.waveId, printChannel: wave.printChannel}),
          responseType: 'blob'
        }).then(({data}) => {
          this.wavePrintOrderInfoDownloadLoading = false;
          let fileName = wave.waveNo + "_" + wave.waveName + "_订单信息";
          downloadXls(data, fileName + ".xlsx");
        }).catch((error) => {
          this.wavePrintOrderInfoDownloadLoading = false;
        })
      },
      downloadWavePrintOrderPdf(wave){
        this.wavePrintOrderPdfDownloadLoading = true;
        let dataBody = {"printChannel": wave.printChannel, "waveId": wave.waveId};
        this.$http({
          url: this.$http.adornUrl('/wave/wave/downloadWavePrintOrderPdf'),
          method: 'get',
          params: this.$http.adornParams(dataBody),
          responseType: 'blob'
        }).then(({data}) => {
          this.wavePrintOrderPdfDownloadLoading = false;
          let fileName = wave.waveNo + "_" + wave.waveName;
          downloadZip(data, fileName + ".zip");
        }).catch((error) => {
          this.wavePrintOrderPdfDownloadLoading = false;
        })
      },
      /**
       * 全选按钮
       */
      handleSelectAll () {
        if (this.$refs.transportTable.selection.length < this.dataList.length) {
          this.selectAll = true
        } else {
          this.selectAll = false
        }
        this.$refs.waveTable.toggleAllSelection()
      },
      // 修改
      modifyHandle (row) {
        this.$http({
          url: this.$http.adornUrl(`/wave/wave/modify`),
          method: 'POST',
          data: this.$http.adornData(row)
        }).then(({data}) => {
          this.modifyVisible = false;
          if(data){
            this.$message({
              message: "修改成功",
              type: 'success'
            })
            this.getDataList(this.page);
          }else{
            this.$message({
              message: "修改失败",
              type: 'error'
            })
          }
        })
      },
      // 删除
      deleteHandle (row) {
        this.$confirm('确定要删除波次吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl(`/wave/wave/deleteWave`),
            method: 'POST',
            data: this.$http.adornData(row)
          }).then(({data}) => {
            if(data){
              this.$message({
                message: "删除成功",
                type: 'success'
              })
              this.getDataList(this.page);
            }else{
              this.$message({
                message: "删除失败",
                type: 'error'
              })
            }
          })
        })
      },

      // 条件查询
      searchChange (newData = false) {
        this.page.currentPage = 1
        this.getDataList(this.page, newData)
      },
      openScanDialog(row){
        this.$refs.orderScanDelivery.init(row);
      },

      /**
       * 重置表单
       * @param {String} formName 表单名称
       */
      resetSearchForm (formName) {
        this.$refs[formName].resetFields()
        this.searchForm = {}
      },
      handleSizeChange (val) {
        this.page.pageSize = val
        this.getDataList()
      },
      handleCurrentChange (val) {
        this.page.currentPage = val
        this.getDataList()
      }
    }
  }
</script>

<style lang="scss" scoped>

</style>
