<template>
    <div>
      <r-search ref="searchalt" :searchData="searchData" :searchForm="searchForm" :searchHandle="searchHandle" />
      <r-table :isIndex="true" :tableData="tableData" :tableCols="tableCols" v-loading="load" :isPagination="true" :tablePage="pagination" @refresh="init()" />
      <r-form dialogWidth="500px" labelWidth="120px" :isHandle="true" :formCols="formCols" :formHandle="formHandle" :formRules="formRules" :formData="formData" ref="elForm" />
    </div>
  </template>
  
  <script>
  export default {
    data () {
      return {
        list:[],
        load:false,
        pagination: { size: 10, page: 1, total: 0 },
        searchData: {
          keyWord:"",
          batteryStatus:'-1',
          batteryLockStatus:'-1',
          batteryModelId:'',
        },
        searchForm: [
          { type: "input",label: "关键词", prop: "keyWord",placeholder:'客户名称、电池编号、使用人'},
          { type: "select",label: "电池状态", prop: "batteryStatus",options:[{label:'正常',value:'0'},{label:'故障',value:'1'},{label:'全部',value:'-1'}]},
          { type: "select",label: "电池锁定状态", prop: "batteryLockStatus",options:[{label:'正常',value:'0'},{label:'锁定',value:'1'},{label:'全部',value:'-1'}]},
        ],
        searchHandle: [
          {type: "info",icon: "el-icon-refresh", handle: e => this.init(true) },
          {label: "搜索",type: "primary",icon: "el-icon-search",handle: e => this.seach()},
        ],
        tableData: [],
        tableCols: [
          { label: "电池编号", prop: "batteryNo",width:200},
          { label: "电池型号", prop: "batteryModelName",width:150},
          { label: "是否备件库", prop: "isSpares",formatter:e=>e.isSpares?'是':'否',width:120},
          { label: "所属订单", prop: "byOrderCode",width:150},
          { label: "所属出库单号", prop: "byStockCode",width:150},
          { label: "客户类型", prop: "customerType",width:150,formatter:e=>e.customerType==0?'经销商':'代理商',},
          { label: "所属客户", prop: "customerName",width: 200},
          { label: "所属代理", prop: "parentCustomerName",width:200},
          { label: "使用人", prop: "usePersionName",width:120},
          { label: "电池状态", prop: "batteryStatus",formatter:e=>e.batteryStatus==0?'正常':'故障'},
          { label: "电池锁定状态", prop: "batteryLockStatus",formatter:e=>e.batteryLockStatus==0?'正常':'已锁定',width:120,isDisabled:e=>e.batteryLockStatus?true:false},
          { label: "操作",type: "button",width: 300,
            btnList: [
              {label: "手动解锁",type: "primary",size: "mini",isShow:e=>e.batteryLockStatus==1?true:false,handle: (row) => this.getLock(row)},
              {label: "查看备件更换记录",type: "danger",size: "mini",handle: (row) => this.getChange(row)},
            ]
          },
        ],
        formCols: [
        ],
        formRules:{
        },
        formData: {
        },
        formHandle: [
        ],
      }
    },
    mounted () {
  
    },
    created() {
      this.$http.post('/battery/model/list').then(res=>{
          let lst=res.data;
          let arr=[]
          lst.forEach((obj)=>{
              let list={label:obj.batteryModel,value:obj.id}
              arr.push(list)
          });
          this.searchForm.push({type:'select', label: "电池型号", prop: "batteryModelId",options:arr})
      })
      this.init();
    },
    methods: {
      init(isref=false,param={}) {
      //   this.load=true;
        let data=Object.assign(this.pagination,param)
        this.$http.get('/battery/getBatteryListByCustomer',data).then(res=>{
            console.log(res.data);
        //   this.pagination.pageSize=res.data.pageSize
        //   this.pagination.page=res.data.currPage
        //   this.pagination.total=res.data.totalCount
        //   this.tableData=res.data.list;
        })
      },
      seach(){
        this.init(false,this.searchData)
      },
  
      getLock(e){
        console.log(e);
        this.$confirm('您确定要解锁吗？','提示').then(()=>{
          this.$http.post('/battery/settinglock',{id:String(e.id)}).then(res=>{
            if (res.data==1) {
              this.$msg.success('解锁成功！')
              this.init()
            }else{
              this.$msg.error(res.msg)
            }
          })
        })
      },
      getChange(e){
        this.$router.push({
          name: 'battery-replace',
          query:{
            row:JSON.stringify({batteryNo:String(e.batteryNo)}),
          }
        });
  
      },
  
  
    }
  }
  </script>
  
  <style>
  
  </style>
  