<template>

  <div class="box-center">
    <!-- 头 -->
    <div class="app-top">
      <div class="app-titel" style="margin-top: 5px">
        <div class="label">批次号：</div>
        <div>
             <el-input v-model="batchId" maxlength="20" clearable></el-input>
        </div>
        <div style="margin-left: 20px">
          <el-button type="primary" @click="Screening()">筛选</el-button
          >
        </div>
        <div style="margin-left: 20px">
          <el-button @click="Screeningofempty()">清空筛选条件</el-button>
        </div>
         <div style="margin-left: 20px">
          <el-button v-if="equ_batch_add" @click="editorinfo()" type="primary"
            >添加批次</el-button
          >
        </div>
      </div>
    </div>

    <!-- 表格 -->
    <div class="cen-tab">
        <el-table
          :data="tableData"
          stripe
          style="width: 100%">
          <el-table-column
            prop="batchId"
            label="批次号"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="name"
            label="设备号"
             align="center"
           >
            <template slot-scope="scope">
                <p>{{scope.row.deviceNoStart}}:{{scope.row.deviceNoEnd}}</p>
              </template>
          </el-table-column>
          <el-table-column
            prop="realityEquipmentCount"
            label="应收设备数量"
             align="center">
          </el-table-column>

          <el-table-column
            prop="receivableEquipmentCount"
            label="实收设备数量"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="acceptanceNum"
            label="已验收设备数量"
             align="center"
           >
          </el-table-column>
          <el-table-column
            prop="createTime"
            label="创建时间"
             align="center">
          </el-table-column>

          <el-table-column
            prop="remark"
            label="备注"
            align="center"
          >
          </el-table-column>
         <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button v-if="equ_batch_edit" type="text" @click="clickbianji(scope.row)">修改</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div style="display: flex;justify-content:flex-end;margin-top: 10px;">
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="pageNum"
              :page-sizes="[10, 30, 50]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total">
            </el-pagination>
        </div>
    </div>



       <el-dialog :title='isshow' :visible.sync="dialogVisible" @closed='clear'>
      <el-form :model="addagentobj" label-position="labelPosition" :rules="rules"  ref="addagentobj" label-width="140px" style="width:60%; margin: 0 auto;">
           <el-form-item label="批次号" prop="batchId">
                <el-input v-model="addagentobj.batchId" maxlength="20" clearable></el-input>
          </el-form-item>
          <el-form-item label="应收设备数量" prop="realityEquipmentCount">
                <el-input v-model="addagentobj.realityEquipmentCount"  onkeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode)));" maxlength="5" clearable></el-input>
          </el-form-item>
           <el-form-item label="实收设备数量" prop="receivableEquipmentCount">
              <el-input v-model="addagentobj.receivableEquipmentCount" onkeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode)));" maxlength="5" clearable></el-input>
          </el-form-item>
          <el-form-item label="设备号开头" prop="deviceNoStart">
              <el-input v-model="addagentobj.deviceNoStart" clearable  maxlength="10"></el-input>
          </el-form-item>
          <el-form-item label="设备号结尾" prop="deviceNoEnd">
              <el-input v-model="addagentobj.deviceNoEnd" clearable  maxlength="10"></el-input>
          </el-form-item>
            <el-form-item label="备注：">
              <el-input type="textarea" v-model="addagentobj.remark"   maxlength="50" show-word-limit></el-input>
          </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editor()">保存</el-button>
      </div>
  </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      dialogVisible:false,
      batchId:'',
      tableData: [],
      currentPage4:1,
      addagentobj:{
        id:'',
        batchId:'',// 批次号
        realityEquipmentCount:'',// 应收数
        receivableEquipmentCount:'',// 实收数
        deviceNoStart:'',// 开头
        deviceNoEnd:'',// 结尾
        remark:''//备注
      },
      rules:{
        batchId: [
            { required: true, message: '请输入批次号', trigger: 'blur' },
          ],
          realityEquipmentCount: [
            { required: true, message: '请输入应收设备数量', trigger: 'blur' },
          ],
          receivableEquipmentCount: [
            { required: true, message: '请输入实收设备数量', trigger: 'blur' },
          ],
          deviceNoStart: [
            { required: true, message: '请输入设备号开头', trigger: 'blur' },
          ],
          deviceNoEnd: [
            { required: true, message: '请输入设备号结尾', trigger: 'blur' },
          ],
      },
      total:0,//总条数
      pageNum:1,
      pageSize:8,
      isshow:'绑定'

    };
  },
    computed: {
    ...mapGetters(["permissions"]),
  },
  created() {
    this.equ_batch_add = this.permissions["equ_batch_add"];
    this.equ_batch_edit = this.permissions["equ_batch_edit"];
  },
  mounted() {
    this.getcompanyList()
  },
  methods: {
    //編輯
    clickbianji(row){
      this.addagentobj.id=row.id;
      this.addagentobj.batchId=row.batchId;
      this.addagentobj.realityEquipmentCount=row.realityEquipmentCount;
      this.addagentobj.receivableEquipmentCount=row.receivableEquipmentCount;
      this.addagentobj.deviceNoStart=row.deviceNoStart;
      this.addagentobj.deviceNoEnd=row.deviceNoEnd;
      this.addagentobj.remark=row.remark;
      this.isshow='编辑';
      this.dialogVisible=true;
    },
    clear(){
      this.$refs.addagentobj.resetFields();
    },
    //添加
    editorinfo(){
      this.isshow='绑定';
      this.addagentobj.id='';
      this.addagentobj.batchId='';
      this.addagentobj.realityEquipmentCount='';
      this.addagentobj.receivableEquipmentCount='';
      this.addagentobj.deviceNoStart='';
      this.addagentobj.deviceNoEnd='';
      this.addagentobj.remark='';
      this.dialogVisible=true;
    },
    //确认添加
     editor(){
         this.$refs.addagentobj.validate((valid) => {
          if (valid) {
            if(this.isshow=='绑定'){
                this.$api.http.insert(this.addagentobj).then((res) => {
               if(res.code==0){
                    this.dialogVisible=false;
                    this.$refs.addagentobj.resetFields();
                    this.Screeningofempty()
                }else{
                  this.$message.error(res.message);
               }
             });
            }else{
              this.$api.http.insertupdate(this.addagentobj).then((res) => {
               if(res.code==0){
                    this.dialogVisible=false;
                    this.$refs.addagentobj.resetFields();
                    this.Screeningofempty()
                }else{
                  this.$message.error(res.message);
               }
             });
            }
          } else {
            return false;
          }
        });
    },
      //清空筛选
    Screeningofempty(){
        this.batchId='';
        this.pageNum=1
        this.getcompanyList()
    },
    //筛选
    Screening(){
      this.pageNum=1
      this.getcompanyList()
    },
       // 获取列表
    getcompanyList() {
      this.tableData=[];
      let parmest={
          current: this.pageNum,
          size: this.pageSize,
          batchId:this.batchId
      }
      this.$api.http.getequipmentbatch(parmest).then((res) => {
          console.log(res.data);
          this.tableData=res.data.records;
          this.total=res.data.total
        });
    },
      handleSizeChange(val) {
       this.pageSize=val;
        this.getcompanyList()
      },
      handleCurrentChange(val) {
        this.pageNum=val;
        this.getcompanyList()
      },
  },
};
</script>

<style scoped="scoped" lang="scss" >
.box-center {
  width: 100%;
  padding: 15px;
  min-width: 1000px;
  padding-bottom: 40px;
}
.cen-tab{
  width: 100%;
  padding: 15px;
  background: #FFFFFF;
  margin-top: 15px;
}
.tophove {
  color: #ffffff;
  background: #2671e2;
}
.app-top {
  width: 100%;
  background: #ffffff;
  box-shadow: 0px 0px 12px 0px rgba(0, 0, 0, 0.04);
  border-radius: 4px;
  padding-top: 15px;
  padding-bottom: 15px;
  .app-titel {
    width: 100%;
    display: flex;
    align-items: center;
    .label {
      font-size: 14px;
      font-weight: 400;
      color: #32363d;
      line-height: 14px;
      margin-left: 15px;
    }
    .toptimeqhuan {
      height: 30px;
      background: #ffffff;
      display: flex;
      align-items: center;
    }
    .toptimeqhuan div {
      padding-left: 20px;
      padding-right: 20px;
      text-align: center;
      line-height: 30px;
      font-size: 14px;
      margin-right: 15px;
      border-radius: 4px;
      border: 1px solid #e0e0e0;
    }
  }
}
</style>
