<template>
  <div>
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">

      <el-form-item label="打印状态">
        <el-select
          v-model="specReq.isPrint"
          :disabled="departmentReaderOnly"
          @change="changeDepartment()"
        >
          <el-option v-for="item in stateList" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="编码">
        <el-input v-model="specReq.code" placeholder="请输入编码" clearable></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="getData()">查询</el-button>
      </el-form-item>

      <el-form-item label="新打印数量">
        <el-input v-model="newCount" placeholder="请输入要新打印机编码数量" clearable></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="success" @click="printNewCodes()">新打印</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="dataListLoading" :data="data" style="width: 100%" @row-click="rowClick">
      <el-table-column prop="id" header-align="center" align="center" label="ID"></el-table-column>
      <el-table-column prop="code" header-align="center" align="center" label="编码"></el-table-column>
      <el-table-column prop="isPrint" header-align="center" align="center" label="是否打印">
        <template slot-scope="scope">
          <div class="conatnt-name">
            <span>{{ getStatusStr(scope.row.isPrint) }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="180" label="操作">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="mini"
            @click="handlePrint(scope.row)"
          >重新打印
          </el-button>
          
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="specReq.page"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="specReq.rows"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <device-type-detail v-if="isShowDetail" ref="deviceTypeDetail" @refreshDataList="getData"></device-type-detail>
  </div>
</template>
<script>
import { apiGetNewPrintCodes, apiGetCodes, apiGetRePrintCode } from "@/api/api_equipment";
import DeviceTypeDetail from './device-type-detail.vue'
export default {
  components: {
      DeviceTypeDetail
    },
  data() {
    
    return {
      isShowSearch: true,
      data: [],
      dataListLoading: false,
      newCount: null, 
      showHeader: false,
      totalPage: 1,
      departmentReaderOnly: false,
      specReq: {
        page: 1,
        rows: 10,
        id: null,
        isDelete: 0,
        isPrint: null,
        gmtCreate: null,
        gmtModified: null,
        code: null,
      },
      stateList:[
        {value: null, label: '全部'},
        {value: 0, label: '未打印'},
        {value: 1, label: '已打印'},
      ],
      isShowDetail: false
    };
  },
  created() {
    // this.handleGetImgCaptcha()
  },
  
  mounted() {
    this.getData();
  },
  methods: {
    printNewCodes(){
      if(!this.newCount || this.newCount == 0){
        this.$message.error('请输入大于0的打印数量')
        return
      }
      apiGetNewPrintCodes({}, this.newCount).then((data) => {
        console.log(data);
        if (data && data.code === 0) {
          this.getData()
          this.$message("发送打印请求成功");
          
        } else {
          this.$message.error("发送打印请求失败！" + data.msg);
        }
      });
    },
    changeDepartment(){
      this.specReq.page = 1;
      this.getData()
    },
    sizeChangeHandle(val) {
      this.specReq.page = 1
      this.specReq.rows = val
      this.getData();
    },
    currentChangeHandle(val) {
      this.specReq.page = val
      this.getData();
    },
    getStatusStr(status) {
      if (status < 2) {
        return this.stateList[status + 1].label;
      }
      return status;
    },
    handlePrint(val) {
      apiGetRePrintCode({}, val.code).then((data) => {
        console.log(data);
        if (data && data.code === 0) {
          this.getData()
          this.$message("发送打印请求成功");
          
        } else {
          this.$message.error("发送打印请求失败！" + data.msg);
        }
      });
    },
    
    getData() {
      console.log("获取数据")
      console.log(this.name)
      console.log(this.categorys)
      this.dataListLoading = true
      apiGetCodes(this.specReq).then((data) => {
        console.log(data);
        if (data && data.code === 0) {
          this.data = data.codes.list;
          this.totalPage = data.codes.totalCount
        } else {
          this.data = [];
        }
        this.dataListLoading = false;
      });
    },
    
    btnSearch() {
      this.specReq.page = 1
      this.getData();
    },

    rowClick(row, event, column) {},
  },
};
</script>

<style>
.el-row {
  margin-bottom: 20px;
}
.el-col {
  border-radius: 10px;
}
.bg-purple-dark {
  background: #99a9bf;
}
.bg-purple {
  background: #d3dce6;
}
.bg-purple-light {
  background: #e5f2f1;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
.div_height {
  height: 38px;
}
.div_width {
  width: 100%;
}
.width_100 {
  width: 100px;
}
.center {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);

  padding: 1em;
}
.flex {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  border: 1px solid;
}
.flex_son {
  width: 100%;
  height: 100%;
  border: 0x solid;
}
</style>