<template>
  <div class="app-container">
    <div v-show="viewState == 1">
      <button-bar view-title="入库车辆管理" ref="btnbar" :btndisabled="btndisabled" @btnhandle="btnHandle"/>
      <div class="main-content">
        <div class="switch_btn">
          <el-button :class="{btn_style:viewState === 1}">待入库</el-button>
          <el-button class="" @click="handleInvoiceList">已入库</el-button>
        </div>
        <div class="searchcon">
          <el-button size="small" class="searchbtn" @click="isSearchShow = !isSearchShow">
            {{ isSearchShow ? '隐藏查询条件' : '显示查询条件' }}
          </el-button>
          <div v-show="isSearchShow" class="search">
            <el-form :inline="true" class="tab-header">
              <el-form-item label="车架号">
                <el-input v-model="listQuery.params.vin" placeholder="请输入车架号" clearable/>
              </el-form-item>
              <el-form-item label="车型">
                <el-select v-model="listQuery.params.model" placeholder="请选择" filterable clearable
                           class="addinputw addinputw_chexing">
                  <el-option v-for="item in modelName_list" :key="item.sid" :label="item.modelName" :value="item.sid"/>
                </el-select>
                <!--                <el-input v-model="listQuery.params.warehouseAttribute" placeholder="请输入存放地点属性" clearable/>-->
              </el-form-item>
              <el-form-item label="订单类型">
                <el-select v-model="listQuery.params.purchaseType" placeholder="请选择" clearable class="addinputw">
                  <el-option v-for="item in purchaseType_list" :key="item.dictKey" :label="item.dictValue"
                             :value="item.dictKey"/>
                </el-select>
                <!--                <el-input v-model="listQuery.params.warehouseContract" placeholder="请输入负责人" clearable/>-->
              </el-form-item>
            </el-form>
            <div class="btn" style="text-align: center;">
              <el-button type="primary" icon="el-icon-search" size="small"  @click="getList">查询</el-button>
              <el-button type="primary" icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
            </div>
          </div>
        </div>
        <!--Start 项目列表头部-->
        <div class="listtop">
          <div class="tit">车辆列表</div>
          <pageye v-show="dataList.length > 0" :total="listQuery.total" :page.sync="listQuery.current"
                  :limit.sync="listQuery.size" class="pagination" @pagination="loadList"/>
        </div>
        <!--End 项目列表头部-->
        <!--Start 项目列表-->
        <div class=""> <!--@selection-change="handleSelectionChange"-->
          <el-table v-loading="tableLoading" :data="dataList" border style="width: 100%">
            <!--            <el-table-column width="50px" type="selection" align="center"/>-->
            <el-table-column width="80px" label="序号" type="index" :index="indexMethod" align="center"/>
            <el-table-column label="操作" align="center" width="90px" class-name="small-padding fixed-width">
              <template slot-scope="{row}"><!--v-show="!row.result"-->
                <el-button size="mini" v-show="row.showWarehousingBtn" type="primary" @click="handleRuku(row)">入库</el-button>
              </template>
            </el-table-column>
            <el-table-column width="180px" prop="vin" label="车架号" align="center"/>
            <el-table-column prop="model" label="车型" align="center"/>
            <el-table-column width="160px" prop="purchaseType" label="订单类型" align="center"/>
            <el-table-column width="140px" prop="dispatchedDate" label="发车日期" align="center"/>
            <el-table-column width="140px" prop="inspectedDate" label="验车日期" align="center"/>
            <el-table-column width="140px" prop="inspectedName" label="验车人" align="center"/>
<!--            <el-table-column width="140px" prop="address" label="存放地点" align="center"/>-->
<!--            <el-table-column width="140px" prop="name" label="仓库管理员" align="center"/>-->
          </el-table>
        </div>
        <!--End 项目列表-->
        <div class="pages">
          <div class="tit"/>
          <!-- 翻页 -->
          <pagination
            v-show="dataList.length > 0"
            :total="listQuery.total"
            :page.sync="listQuery.current"
            :limit.sync="listQuery.size"
            class="pagination"
            @pagination="loadList"
          />
        </div>
      </div>
    </div>
    <rukuguanli-add v-show="viewState == 2 || viewState == 3" ref="divadd" @doback="resetState" @reloadlist="getList"/>
    <rukuguanli v-show="viewState == 5" ref="divalready" @doback="resetState"/>
  </div>
</template>

<script>
import ButtonBar from '@/components/ButtonBar'
import Pagination from '@/components/pagination'
import pageye from '@/components/pagination/pageye'
import { selectCarWarehousingList } from '@/api/supplychain/rukuguanli'
import rukuguanliAdd from '@/views/supplychain/rukuguanli/rukuguanliAdd'
import rukuguanli from '@/views/supplychain/rukuguanli/rukuguanliAlready'
import { getUseOrgByUserSid, selectModelName } from '@/api/cheliang/basevehicle'
import { typeValues } from '@/api/supplychain/yancheguanli'

export default {
  name: 'rukuguanliawait',
  components: {
    ButtonBar,
    Pagination,
    pageye,
    rukuguanliAdd,
    rukuguanli
  },
  data() {
    return {
      btndisabled: false,
      viewState: 1, // 1、列表 2、添加 3、修改 4、查看
      isSearchShow: false,
      tableLoading: false,
      dataList: [],
      useOrg_list: [],
      modelName_list: [],
      slowMachine_list: [],
      purchaseType_list: [],
      useOrg: '', // 使用组织
      useOrgSid: '', // 使用组织sid
      listQuery: {
        current: 1,
        size: 10,
        total: 0,
        params: {
          vin: '',
          model: '',
          purchaseType: '',
          type: '1',
          userSid: window.sessionStorage.getItem('userSid')
        }
      },
      btnList: [
        {
          type: 'info',
          size: 'small',
          icon: 'cross',
          btnKey: 'doClose',
          btnLabel: '关闭'
        }
      ],
      multipleSelection: []
    }
  },
  created() {
    this.getList()
    // this.getUseOrg()
    this.getModelName()
    this.getType()
  },
  mounted() {
    this.$refs['btnbar'].setButtonList(this.btnList)
  },
  methods: {
    getType() {
      typeValues({
        type: 'purchaseOrderType '
      }).then((response) => {
        if (response.code === '200' && response.data) {
          this.purchaseType_list = response.data
        }
      })
    },
    resetState() {
      this.viewState = 1
    },
    btnHandle(btnKey) {
      console.log('XXXXXXXXXXXXXXX    ' + btnKey)
      switch (btnKey) {
        case 'doClose':
          this.doClose()
          break
        default:
          break
      }
    },
    handleRuku(row) {
      this.viewState = 2
      this.$refs['divadd'].showAdd(row)
    },

    handleCheck(row) {
      this.$refs['divinfo'].showInfo(row)
      console.log('详情回显', row)
      this.viewState = 4
    },
    handleInvoiceList() {
      this.viewState = 5
      this.$refs['divalready'].getList()
    },
    getList() {
      this.listQuery.current = 1
      this.loadList()
    },
    resetQuery() {
      this.listQuery = {
        current: 1,
        size: 10,
        total: 0,
        params: {
          vin: '',
          model: '',
          purchaseType: '',
          type: '1',
          userSid: window.sessionStorage.getItem('userSid')
        }
      }
      this.getList()
    },
    loadList() {
      const _this = this
      this.tableLoading = true
      selectCarWarehousingList(this.listQuery)
        .then(resp => {
          _this.tableLoading = false
          const data = resp.data
          _this.listQuery.total = data.total
          _this.dataList = data.records
        })
        .catch(() => {
          _this.tableLoading = false
        })
    },
    // handleSelectionChange(val) {
    //   this.multipleSelection = val
    //   console.log('点击数据', this.multipleSelection)
    // },
    // 车型名称下拉框
    getModelName() {
      selectModelName({ name: '' }).then((response) => {
        if (response.code === '200') {
          this.modelName_list = response.data
        }
      })
    },
    // 根据个人sid查询个人分公司
    getUseOrg() {
      const userSid = window.sessionStorage.getItem('userSid')
      getUseOrgByUserSid({ userSid: userSid }).then((res) => {
        if (res.success) {
          this.useOrg = res.data.name // 使用组织
          this.useOrgSid = res.data.sid // 使用组织sid
          this.listQuery.params.useOrgSid = res.data.sid
          this.loadList()
        }
      })
    },
    // 序号
    indexMethod(index) {
      var pagestart = (this.listQuery.current - 1) * this.listQuery.size
      var pageindex = index + 1 + pagestart
      return pageindex
    },
    doClose() {
      this.$store.dispatch('tagsView/delView', this.$route)
      this.$router.go(-1)
    }
  }
}
</script>

<style scoped>
.switch_btn {
  padding: 15px 0 10px 0;
}

.btn_style {
  background-color: #018ad2;
  color: white;
  font-weight: 600;
}
.addinputw_chexing {
  width: 500px;
}
</style>
