<template>
  <div class="app-container">
    <div class="filter-container">

      <el-input size="mini" v-model="search.paperName" placeholder="纸张名称" style="width: 200px;" class="filter-item"/>
      <el-input size="mini" v-model="search.billCode" placeholder="施工单号" style="width: 200px;" class="filter-item"/>
      <el-input size="mini" v-model="search.preOutMan" placeholder="预出库人" style="width: 200px;" class="filter-item"/>
      <el-date-picker
        size="mini"
        v-model="searchTime"
        type="datetimerange"
        range-separator="-"
        value-format="yyyy-MM-dd HH:mm:ss"
        :default-time="['12:00:00','12:00:00']"
        start-placeholder="开始日期"
        end-placeholder="结束日期">
      </el-date-picker>
      <el-checkbox true-label="1" false-label="0" size="small" @change="refreshBtn" v-model="search.searchPaperLocked" label="查询已锁定" border></el-checkbox>

      <el-button size="mini" class="filter-item" type="primary" icon="el-icon-search" @click="searchData">
        搜索
      </el-button>
      <el-button
        size="mini"
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-refresh"
        @click="refreshBtn">
        刷新
      </el-button>
    </div>
    <div class="table-con">
      <el-table
        :data="tableData"
        v-loading="listLoading"
        style="width: 100%;margin-bottom: 20px;"
        @sort-change="changeSort"
        size="mini"
        row-key="id"
        border
      >
        <el-table-column
          label="#"
          align="center"
          type="index"
          :index="indexMethod">
        </el-table-column>
        <el-table-column label="操作" align="left" width="80" class-name="small-padding fixed-width">
          <template slot-scope="{row}">
            <el-button  @click="backToDeliver(row)" type="primary" size="mini">
              退回
            </el-button>
          </template>
        </el-table-column>
        <el-table-column
          width="120"
          label="纸张预出库时间">
          <template slot-scope="scope">
            <span>{{ scope.row.paperPreOutTime | getTime }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="状态" class-name="status-col" width="70">
          <template slot-scope="{row}">
            <el-tag size="mini" v-if="row.preOutStockUnlocked==0" type="success">
              已请购
            </el-tag>
            <el-tag size="mini" v-else-if="row.preOutStockUnlocked==1" type="warning">
              无需请购
            </el-tag>
            <el-tag size="mini" v-else-if="row.preOutStockUnlocked==2" type="danger">
              不再处理
            </el-tag>
            <el-tag size="mini" v-else-if="row.preOutStockUnlocked==10" type="success">
              完成
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="用纸"
          prop="actualPaperName"
          sortable="custom"
          width="140px"
        >
          <template show-overflow-tooltip slot-scope="{row}">
            <el-tooltip :content="row.actualPaperName" placement="top-start" effect="light">
              <span @click="getOrderStep(row.idPrintBill)" style="color: #409EFF;cursor: pointer"
                    class="ellipsis"> {{ row.actualPaperName }}</span>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column
          prop="printBillCode"
          sortable="custom"
          width="220px"
          label="施工单号-版名">
          <template show-overflow-tooltip slot-scope="{row}">
            <el-tooltip :content="getCodeName(row)" placement="top-start" effect="light">
              <span @click="handleDetail(row.idPrintBill)" style="color: #409EFF;cursor: pointer"
                    class="ellipsis"> {{ row.printBillCode }}-{{ row.breedName }}</span>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column
          width="90"
          label="大纸规格">
          <template slot-scope="scope">
            <span>{{ scope.row.wholePaperSize }}</span>
          </template>
        </el-table-column>
        <el-table-column
          width="120"
          show-overflow-tooltip
          prop="paperName"
          sortable="custom"
          label="纸张名称(原)">
          <template slot-scope="scope">
            <span>{{ scope.row.paperName }}</span>
          </template>
        </el-table-column>
        <el-table-column
          width="80"
          label="纸张开数">
          <template slot-scope="scope">
            <span>{{ scope.row.cutName }}</span>
          </template>
        </el-table-column>
        <el-table-column
          width="100"
          label="开纸尺寸">
          <template slot-scope="scope">
            <span>{{ scope.row.cutSize }}</span>
          </template>
        </el-table-column>
        <el-table-column
          width="80"
          show-overflow-tooltip
          label="版类型">
          <template slot-scope="scope">
            <span>{{ scope.row.breedTypeName }}</span>
          </template>
        </el-table-column>
        <el-table-column
          width="80"
          label="大纸张数">
          <template slot-scope="scope">
            <span>{{ scope.row.wholePaperCount }}</span>
          </template>
        </el-table-column>
        <el-table-column
          width="110"
          label="预出库锁定量">
          <template slot-scope="scope">
            <span>{{ scope.row.preOutStockLockQty }}</span>
          </template>
        </el-table-column>
        <el-table-column
          width="90"
          label="预出库人">
          <template slot-scope="scope">
            <span>{{ scope.row.preOutLockManName }}</span>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          label="客户名称">
          <template slot-scope="scope">
            <span>{{ scope.row.clientName }}</span>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip label="印刷机台">
          <template slot-scope="{row}">
            <span>{{ row.machineName }}</span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="请购状态" class-name="status-col" width="70">
          <template slot-scope="{row}">
            <el-tag size="mini" v-if="row.paperPreRequestFlag===0" type="success">
              未请购
            </el-tag>
            <el-tag size="mini" v-else-if="row.paperPreRequestFlag===1" type="warning">
              已请购
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="入库状态" class-name="status-col" width="70">
          <template slot-scope="{row}">
            <el-tag size="mini" v-if="row.paperStockInFlag===0" type="success">
              未入库
            </el-tag>
            <el-tag size="mini" v-else-if="row.paperStockInFlag===1" type="warning">
              已入库
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="领料状态" class-name="status-col" width="70">
          <template slot-scope="{row}">
            <el-tag size="mini" v-if="row.paperPickingFlag===0" type="success">
              未领料
            </el-tag>
            <el-tag size="mini" v-else-if="row.paperPickingFlag===1" type="warning">
              已领料
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="出库状态" class-name="status-col" width="70">
          <template slot-scope="{row}">
            <el-tag size="mini" v-if="row.paperStockOutFlag===0" type="success">
              未出库
            </el-tag>
            <el-tag size="mini" v-else-if="row.paperStockOutFlag===1" type="warning">
              已出库
            </el-tag>
          </template>
        </el-table-column>


        <!--        <el-table-column-->
        <!--          label="创建时间"-->
        <!--        >-->
        <!--          <template slot-scope="scope">-->
        <!--            <span v-if="scope.row.createTime">{{scope.row.createTime | getTime}}</span>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
      </el-table>
      <pagination v-show="listData.total>0" :total="listData.total" :page.sync="listData.page"
                  :limit.sync="listData.pageSize" @pagination="getTableInfo"/>
      <printbill-detail ref="printbillDetail"></printbill-detail>
      <print-step ref="printStep"></print-step>
    </div>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="500px"
    >
      <span>确认退回</span>
      <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button :disabled="noDoubleClick" type="primary" @click="confirmHandle">确 定</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
import {getPrintBreedList, preOutBackV2} from '@/api/printbill/preOut/preOut.js'
import PrintbillDetail from "@/components/printbillDetail/printbillDetail";
import printStep from "@/components/printbillDetail/printStep";

export default {
  name: "queryPreOut",
  components: {
    PrintbillDetail,
    printStep
  },
  data() {
    return {
      rowData:{},
      noDoubleClick:false,
      dialogVisible:false,
      searchTime: [],
      orderByString: "",
      listLoading: false,
      tableData: [],
      search: {
        preOutStockState: 2,
        searchPaperLocked:0,
        preOutTimeBegin:"",
        preOutTimeEnd:"",
        preOutMan:""
      },
      listData: {
        total: 0,
        page: 1,
        pageSize: 20
      }
    }
  },
  mounted() {
    this.getTableInfo();
  },
  methods: {
    changeSort(val) {
      // console.log(val)
      var _that = this
      if (val.order == 'ascending') {
        _that.orderByString = val.prop + ',' + 'asc'
      } else if (val.order == 'descending') {
        _that.orderByString = val.prop + ',' + 'desc'
      } else {
        _that.orderByString = ''
      }
      _that.listData.page = 1;
      _that.getTableInfo()
    },
    refreshBtn() {
      this.listData.page = 1;
      this.getTableInfo()
    },
    getOrderStep(id) {
      const _that = this;
      _that.$refs.printStep.idOrder = id;
      _that.$refs.printStep.drawer = true;
      _that.$refs.printStep.getStep();
    },
    handleDetail(id) {
      const _that = this;
      _that.$refs.printbillDetail.idOrder = id;
      _that.$refs.printbillDetail.printVisible = true;
      _that.$refs.printbillDetail.getDetail()
    },
    confirmHandle(){
      const _that = this;
      _that.noDoubleClick = true;
      var srcBreeds = [];

      srcBreeds.push({id: _that.rowData.id, preOutStockUnlocked: _that.rowData.preOutStockUnlocked})
      const data = {
        "srcBreeds": JSON.stringify(srcBreeds)
      }
      preOutBackV2(data).then(response => {
        console.log(response)
        _that.noDoubleClick = false;
        _that.dialogVisible = false;
        _that.$message.success('退回成功')
        _that.getTableInfo()
      }).catch(err => {
        _that.noDoubleClick = false;
        _that.dialogVisible = false;
        // _that.$message.error(err)
      })
    },
    backToDeliver(row) {
      const _that = this
      _that.rowData = row;
      _that.dialogVisible = true;
    },
    //获取表格数据
    getTableInfo() {
      const _that = this
      _that.listLoading = true;
      // console.log(_that.searchTime);
      if (_that.searchTime == null || _that.searchTime < 0) {
        _that.search.preOutTimeBegin = '';
        _that.search.preOutTimeEnd = '';
      } else {
        _that.search.preOutTimeBegin = _that.searchTime[0];
        _that.search.preOutTimeEnd = _that.searchTime[1];
      }
      var params = _that.search
      var page = {
        pageIndex: _that.listData.page,
        pageSize: _that.listData.pageSize,
      }
      var orderBy = _that.orderByString
      var data = {
        "params": JSON.stringify(params),
        'orderBy': orderBy,
        'page': JSON.stringify(page)
      }
      // console.log(data);
      getPrintBreedList(data).then(response => {
        // console.log("tableData",response)
        const data = response.data
        // //  组合表单数据
        _that.tableData = data
        _that.listData.total = response.total
        // console.log(_that.tableData)
        _that.listLoading = false
      }).catch(err => {
        _that.listLoading = false
      })
    },
    getCodeName(row) {
      return row.printBillCode + "-" + row.breedName
    },
    //搜索
    searchData() {
      this.listData.page = 1;
      this.getTableInfo()
    },
    //序号
    indexMethod(index) {
      return index + 1;
    }
  }
}
</script>
<style lang="scss" scoped>
.app-container {
  background-color: #f0f2f5;
  padding: 30px;
  min-height: calc(100vh - 84px);
}

.filter-container {
  margin-bottom: 15px;
}
</style>
