<template>
  <div class="app-container">
    <div v-show="viewState == 1">
      <button-bar ref="btnbar" view-title="出入库查询" :btndisabled="btndisabled" @btnhandle="btnHandle" />
      <div class="main-content">
        <div class="searchcon">
          <el-button size="small" class="searchbtn" @click="clicksearchShow">
            {{ searchxianshitit }}
          </el-button>
          <div v-show="isSearchShow" class="search">
            <el-form :inline="true" class="tab-header">
              <el-form-item label="商品ID">
                <el-input v-model="queryParams.params.goodsID" placeholder="" clearable />
              </el-form-item>
              <el-form-item label="商品名称">
                <el-input v-model="queryParams.params.goodsSpuName" placeholder="" clearable />
              </el-form-item>
              <el-form-item label="商品编码">
                <el-input v-model="queryParams.params.goodsSkuCode" placeholder="" clearable />
              </el-form-item>
              <el-form-item label="仓库">
                <el-select v-model="queryParams.params.warehouseName" placeholder="请选择" @change='selectWarehouseName'>
                  <el-option v-for="(item,i) in warehouseList" :key="i" :label="item.warehouseName" :value="item.sid">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="库位">
                <el-select v-model="queryParams.params.warehouseRackCode" placeholder="请选择"
                  @change='selectWarehouseAreaCode'>
                  <el-option v-for="(item,i) in warehouseRackList" :key="i" :label="item.areaCode" :value="item.sid">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="供应商">
                <el-select v-model="queryParams.params.billObjName" placeholder="请选择" @change='supplierNameSelect'>
                  <el-option v-for="(item,i) in supplierList" :key="i" :label="item.supplierName" :value="item.sid">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="批次号">
                <el-input v-model="queryParams.params.batchNumber" placeholder="" clearable />
              </el-form-item>
              <el-form-item label="来源单号">
                <el-input v-model="queryParams.params.billNo" placeholder="" clearable />
              </el-form-item>
              <el-form-item label="单据类型">
                <el-select v-model="queryParams.params.billType" placeholder="请选择">
                  <el-option v-for="(item,i) in billTypeList" :key="i" :label="item.name" :value="item.sid">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="业务类型">
                <el-select v-model="queryParams.params.busTypeValue" placeholder="请选择">
                  <el-option v-for="(item,i) in busTypeList" :key="i" :label="item.name" :value="item.sid">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="发生时间">
                <el-date-picker v-model="queryParams.params.createTimeStart" type="date" placeholder="选择日期"
                  format="yyyy-MM-dd" value-format="yyyy-MM-dd" style="width: 200px;">
                </el-date-picker>至
                <el-date-picker v-model="queryParams.params.createTimeEnd" type="date" placeholder="选择日期"
                  format="yyyy-MM-dd" value-format="yyyy-MM-dd" style="width: 200px;">
                </el-date-picker>
              </el-form-item>
            </el-form>
            <div class="btn" style="text-align: center;">
              <el-button type="primary" size="small" icon="el-icon-search" @click="dosearch">查询</el-button>
              <el-button type="primary" size="small" icon="el-icon-refresh" @click="resetQuery">重置</el-button>
            </div>
          </div>
        </div>
        <!-- Start 项目列表头部 -->
        <div class="listtop">
          <div class="tit">商品出入库列表</div>
        </div>
        <!-- End 项目列表头部 -->
        <!-- Start 项目列表 -->
        <div class="">
          <el-table v-loading="tableLoading" :data="dataList" border style="width: 100%"
            @selection-change="selectionLineChangeHandle">
            <el-table-column fixed width="50" type="selection" align="center" />
            <el-table-column fixed width="60" label="序号" type="index" :index="indexMethod" align="center" />
            <el-table-column fixed prop="goodsID" label="商品ID" align="center" width="120"/>
            <el-table-column  fixed prop="goodsSpuName" label="商品名称" align="center" width="120"/>
            <el-table-column prop="goodsSkuCode" label="商品编码" align="center" width="120"/>
            <el-table-column prop="goodsSkuOwnSpec" label="规格" align="center" width="120"/>
            <el-table-column prop="unit" label="单位" align="center"width="80"/>
            <el-table-column prop="warehouseName" label="仓库" align="center"width="100"/>
            <el-table-column prop="warehouseRackCode" label="库位" align="center" width="100"/>
            <el-table-column prop="billNo" label="来源单号" align="center" width="120"/>
            <el-table-column prop="billType" label="单据类型" align="center"width="120"/>
            <el-table-column prop="createTime" label="发生时间" align="center" width="120"/>
            <el-table-column prop="busTypeValue" label="业务类型" align="center" width="120"/>
            <el-table-column prop="billObjName" label="客户/供应商" align="center" width="150"/>
            <el-table-column prop="batchNumber" label="批次号" align="center" width="100"/>
            <el-table-column prop="count" label="数量" align="center" width="80"/>
            <el-table-column prop="currentCount" label="出入库后的库存" align="center" width="150"/>
          </el-table>
        </div>
        <!-- End 项目列表 -->
        <div class="pages">
          <div class="tit" />
          <!--  翻页  -->
          <pagination v-show="dataList.length > 0" :total="queryParams.total" :page.sync="queryParams.current"
            :limit.sync="queryParams.size" class="pagination" @pagination="loadList" />
        </div>
      </div>
    </div>
    <!-- End 查询和其列表部分 -->
    <!-- 新增修改部分组件 -->
    <!-- <divAdd v-show="viewState == 2 || viewState == 3" ref="divadd" @doback="resetState" @reloadlist="loadList" /> -->
  </div>
</template>

<script>
  import req from '@/api/storage/inOutStorage.js'
  import ButtonBar from '@/components/ButtonBar'
  import Pagination from '@/components/pagination'
  import pageye from '@/components/pagination/pageye'
  // import divAdd from './brandsAdd.vue'
  export default {
    components: {
      ButtonBar,
      Pagination,
      pageye,
      // divAdd
    },
    data() {
      return {
        btndisabled: false,
        viewState: 1, // 1、列表 2、添加 3、修改 4、查看
        isSearchShow: false,
        searchxianshitit: '显示查询条件',
        tableLoading: false,
        dataList: [],
        btnList: [{
            type: 'success',
            size: 'small',
            icon: 'export',
            btnKey: 'build',
            btnLabel: '导出'
          },
          {
            type: 'info',
            size: 'small',
            icon: 'cross',
            btnKey: 'doClose',
            btnLabel: '关闭'
          }
        ],
        queryParams: {
          current: 1,
          size: 10,
          total: 0,
          params: {
            "goodsID": "",
            "billNo": "",
            "billType": "",
            "busTypeValue": "",
            "billObjName": "",
            "batchNumber": "",
            "goodsSpuName": "",
            "goodsSkuCode": "",
            "warehouseName": "",
            "warehouseRackCode": "",
            "createTimeStart": "",
            "createTimeEnd": ""
          }
        },
        billTypeList: [{
            name: "单据类型1",
            sid: "0"
          },
          {
            name: "单据类型2",
            sid: "1"
          }
        ],
        busTypeList: [{
            name: "业务类型1",
            sid: "1"
          },
          {
            name: "业务类型2",
            sid: "2"
          }
        ],
        warehouseList: [],
        warehouseRackList: [],
        supplierList: [],
        sids: [],
        selectionList: []
      }
    },
    mounted() {
      this.$refs['btnbar'].setButtonList(this.btnList)
    },
    created() {
      this.loadList()
      this.getsupplierLust()
      this.getWarehouseList()
    },
    methods: {
      supplierNameSelect(val) {

        console.log("supplierNameSelect", val);
        const choose = this.supplierList.filter((item) => item.sid == val)
        console.log('>>>>>>>>>supplierNameSelect', choose)
        this.queryParams.params.billObjName = choose[0].supplierName
        this.queryParams.params.billObjSid = choose[0].sid

      },
      getsupplierLust() {
        var params = {
          createOrgSid: ""
        }
        req.choiceSupplier(params).then((res) => {
          if (res.success) {
            this.supplierList = res.data

            console.log("aaaaaa", this.supplierList);

          }
        })
      },
      getWarehouseList() {
        req.getWarehouses().then((res) => {
          if (res.success) {
            this.warehouseList = res.data

            console.log("aaaaaa", this.warehouseList);

          }
        })
      },
      getWarehouseAreaList(sid) {
        var query = {
          ckSid: sid
        }
        req.getWarehouseareas(query).then((res) => {
          if (res.success) {
            this.warehouseRackList = res.data

            console.log("aaaaaa", this.warehouseRackList);

          }
        })
      },
      selectWarehouseName(val) {
        console.log("selectWarehouseName", val);
        const choose = this.warehouseList.filter((item) => item.sid == val)
        console.log('>>>>>>>>>selectWarehouseName', choose)
        this.queryParams.params.warehouseName = choose[0].warehouseName
        this.queryParams.params.warehouseSid = choose[0].sid

        this.getWarehouseAreaList(choose[0].sid)

      },
      selectWarehouseAreaCode(val) {
        console.log("selectWarehouseAreaCode", val);

        const choose = this.warehouseRackList.filter((item) => item.sid == val)
        console.log('>>>>>>>>>selectWarehouseAreaCode', choose)
        this.queryParams.params.warehouseRackName = choose[0].areaCode
        this.queryParams.params.warehouseRackSid = choose[0].sid

      },

      selectionLineChangeHandle(val) {
        console.log("val", val);

        this.selectionList = val
        const aa = []
        val.forEach(element => {
          aa.push(element.sid)
        })
        this.sids = aa


      },

      // 搜索条件效果
      clicksearchShow() {
        this.isSearchShow = !this.isSearchShow
        if (this.isSearchShow) {
          this.searchxianshitit = '隐藏查询条件'
        } else {
          this.searchxianshitit = '显示查询条件'
        }
      },
      btnHandle(btnKey) {
        switch (btnKey) {
          case 'build':
            // this.toBuild()
            break
          case 'doClose':
            this.doClose()
            break
          default:
            break
        }
      },
      loadList() {
        this.tableLoading = true
        req.listPage(this.queryParams).then((resp) => {
          this.tableLoading = false
          if (resp.success) {
            const data = resp.data
            this.queryParams.total = data.total
            this.dataList = data.records
          } else {
            // 根据resp.code进行异常情况处理
            this.dataList = []
            this.queryParams.total = 0
          }
        }).catch(() => {
          this.tableLoading = false
        })
      },

      // 序号
      indexMethod(index) {
        var pagestart = (this.queryParams.current - 1) * this.queryParams.size
        var pageindex = index + 1 + pagestart
        return pageindex
      },

      dosearch() {
        this.queryParams.current = 1
        this.loadList()
      },
      resetQuery() {
        this.queryParams = {
          current: 1,
          size: 10,
          total: 0,
          params: {
            "goodsID": "",
            "billNo": "",
            "billType": "",
            "busTypeValue": "",
            "billObjName": "",
            "batchNumber": "",
            "goodsSpuName": "",
            "goodsSkuCode": "",
            "warehouseName": "",
            "warehouseRackCode": "",
            "createTimeStart": "",
            "createTimeEnd": ""
          }
        }
        this.loadList()
      },

      toBuild() {
        const loading = this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })


        req.exportExcel(this.queryParams.params).then((resp) => {
          loading.close()
          const blob = new Blob([resp], {
            type: 'application/vnd.ms-excel'
          })
          const fileName = '出入库明细_' + this.queryParams.params.createDateStart + '.xls'
          const elink = document.createElement('a')
          elink.download = fileName
          elink.style.display = 'nonde'
          elink.href = URL.createObjectURL(blob)
          document.body.appendChild(elink)
          elink.click()
          URL.revokeObjectURL(elink.href)
          document.body.removeChild(elink)
        }).catch(() => {
          loading.close()
        })
      },
      doClose() {
        this.$store.dispatch('tagsView/delView', this.$route)
        this.$router.go(-1)
      },

      resetState() {
        this.viewState = 1
      },

    }
  }
</script>
<style scoped>
</style>
