<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 ref="queryParams" :model="jmdListQuery" :inline="true" class="tab-header">
              <el-form-item label="日期">
                <el-date-picker v-model="jmdListQuery.date" type="date" clearable value-format="yyyy-MM-dd"
                  placeholder="选择日期" />
              </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>
        <el-row>
          <el-col>
            <el-tabs v-model="tabActiveName" @tab-click="handleOrder">
              <el-tab-pane label="入库记录表" name="uplog">
                <div>
                  <el-table v-loading="tableLoading1" :data="logList" border style="width: 100%">
                    <el-table-column fixed width="100" type="index" label="序号" :index="indexMethod" align="center" />
                    <el-table-column prop="contractNumber" label="合同编号" align="center" />
                    <el-table-column prop="contractSigningDate" label="合同日期" width="150" align="center" />
                    <el-table-column prop="warehousingDate" label="入库日期" width="150" align="center" />
                    <el-table-column prop="tankNumber" label="油罐编号" width="150" align="center" />
                      <el-table-column prop="crudeName" label="原油类型" width="150" align="center" />
                    <el-table-column prop="inventory" label="油罐库存" width="150" align="center" />
                    <el-table-column prop="weight" label="入库净重" width="150" align="center" />
                    <el-table-column prop="value" label="价值" width="150" align="center" />
                    <el-table-column prop="supplierSid" label="供货商" width="150" align="center" />
                    <el-table-column prop="licensePlateNumber" label="发货车牌号" width="150" align="center" />
                  </el-table>
                </div>
                <div class="pages">
                  <!-- 翻页 -->
                  <pagination v-show="logListTotal > 0" :total="logListTotal" :page.sync="logListQuery.current"
                    :limit.sync="logListQuery.size" class="pagination" @pagination="loadLogList" />
                </div>
              </el-tab-pane>
              <el-tab-pane label="出库记录表" name="k112">
                <div>
                  <el-table v-loading="tableLoading2" :data="in112List" border style="width: 100%">
                    <el-table-column fixed width="100" type="index" label="序号" :index="indexMethod2" align="center" />
                    <el-table-column prop="salesContractNumber" label="合同编号" align="center" />
                    <el-table-column prop="contractSigningDate" label="合同日期" width="150" align="center" />
                    <el-table-column prop="deliveryDate" label="出库日期" width="150" align="center" />
                    <el-table-column prop="tankNumber" label="油罐编号" width="150" align="center" />
                    <el-table-column prop="crudeName" label="原油类型" width="150" align="center" />
                    <el-table-column prop="inventory" label="油罐库存" width="150" align="center" />
                    <el-table-column prop="weight" label="出库净重" width="150" align="center" />
                    <el-table-column prop="value" label="价值" width="150" align="center" />
                  </el-table>
                </div>
                <div class="pages">
                  <!-- 翻页 -->
                  <pagination v-show="in112ListTotal > 0" :total="in112ListTotal" :page.sync="in112ListQuery.current"
                    :limit.sync="in112ListQuery.size" class="pagination" @pagination="loadIn112List" />
                </div>
              </el-tab-pane>
            </el-tabs>
          </el-col>
        </el-row>
      </div>

    </div>
    <inBoundAdd v-show="viewState ==2" ref="inBoundAdd" @doback="resetState" @reloadlist="init" />
    <outBoundAdd v-show="viewState ==3" ref="ouBoundAdd" @doback="resetState" @reloadlist="init" />
  </div>
</template>

<script>
  import req from '@/api/inAndOutBound/inAndOutBound'
  import Pagination from '@/components/pagination'
  import {
    parseTime
  } from '@/utils/index'
  import ButtonBar from '@/components/ButtonBar'
  import inBoundAdd from './inBoundAdd.vue'
  import outBoundAdd from './outBoundAdd.vue'
  export default {
    components: {
      ButtonBar,
      Pagination,
      inBoundAdd,
      outBoundAdd
    },
    data() {
      return {
        viewState: 1,
        tabActiveName: 'uplog',
        index: "0",
        isSearchShow: false,
        searchxianshitit: '显示查询条件',
        btndisabled: false,
        tableLoading1: false,
        tableLoading2: false,
        btnList: [ // 右上角按钮
          // {
          //   type: 'success',
          //   size: 'small',
          //   icon: 'export',
          //   btnKey: 'build',
          //   btnLabel: '导出'
          // },
          {
            type: 'primary',
            size: 'small',
            icon: 'plus',
            btnKey: 'toAdd1',
            btnLabel: '新增入库记录'
          },
          {
            type: 'primary',
            size: 'small',
            icon: 'plus',
            btnKey: 'toAdd2',
            btnLabel: '新增出库记录'
          },
          {
            type: 'info',
            size: 'small',
            icon: 'cross',
            btnKey: 'doClose',
            btnLabel: '关闭'
          }
        ],
        logList: [],
        logListQuery: {
          current: 1,
          size: 10
        },
        logListTotal: 1,
        in112List: [],
        in112ListQuery: {
          current: 1,
          size: 10
        },
        in112ListTotal: 1,

        jmdListQuery: {
          date: ""
        }
      }
    },
    mounted() {
      // 初始化按钮
      this.$refs['btnbar'].setButtonList(this.btnList)
    },
    created() {
      // 初始化变量
      this.init()
    },
    methods: {
      // 搜索条件效果
      clicksearchShow() {
        this.isSearchShow = !this.isSearchShow
        if (this.isSearchShow) {
          this.searchxianshitit = '隐藏查询条件'
        } else {
          this.searchxianshitit = '显示查询条件'
        }
      },
      dosearch() {
        this.jmdListQuery = {
          date: parseTime(new Date(), '{y}-{m}-{d}')
        }
        if (this.index == "0") {
          this.loadLogList()
        } else if (this.index == "1") {
          this.loadInallList()
        }

      },
      resetQuery() {
        this.jmdListQuery = {
          date: parseTime(new Date(), '{y}-{m}-{d}')
        }
      },
      // 右上角点击事件
      btnHandle(btnKey) {
        switch (btnKey) {
          case 'toAdd1': // 新增入库
            this.doAdd1()
            break
          case 'toAdd2': // 新增出库
            this.doAdd2()
            break
          case 'build': // 导出
            this.doBuild()
            break
          case 'doClose': // 关闭
            this.doClose()
            break
          default:
            break
        }
      },
      init() {
        this.loadLogList()
        this.loadIn112List()
      },
      handleOrder(tab, event) {
        console.log("tab", tab)
        // this.listQuery.status = tab.name;
        // this.getList();
        // this.orderDatas(tab.name);

        this.index = tab.index

        switch (tab.index) {
          // 入库
          case "0":
            if (this.logList.length == 0)
              this.loadLogList()
            break;
            // 出库
          case "1":
            if (this.in112List.length == 0)
              this.loadIn112List()
            break;
          default:
            break;
        }

      },
      loadLogList() {
        req.logPagerList({
          current: this.logListQuery.current,
          size: this.logListQuery.size,
          params: {
            date: ''
          }
        }).then(response => {
          if (response.code === '200' && response.data && response.data.total > 0) {
            this.logList = response.data.records
            this.logListTotal = response.data.total
            this.logListQuery.current = response.data.current
            this.logListQuery.size = response.data.size
          } else {
            this.logList = []
            this.logListTotal = 0
          }
        })
      },
      // 序号
      indexMethod(index) {
        var pagestart = (this.logListQuery.current - 1) * this.logListQuery.size
        var pageindex = index + 1 + pagestart
        return pageindex
      },
      loadIn112List() {
        req.in112PagerList({
          current: this.in112ListQuery.current,
          size: this.in112ListQuery.size,
          params: {
            date: ''
          }
        }).then(response => {
          if (response.code === '200' && response.data && response.data.total > 0) {
            this.in112List = response.data.records
            this.in112ListTotal = response.data.total
            this.in112ListQuery.current = response.data.current
            this.in112ListQuery.size = response.data.size
          } else {
            this.in112List = []
            this.in112ListTotal = 0
          }
        })
      },
      // 序号
      indexMethod2(index) {
        var pagestart = (this.in112ListQuery.current - 1) * this.in112ListQuery.size
        var pageindex = index + 1 + pagestart
        return pageindex
      },
      doAdd1() {
         this.viewState = 2
      },
      doAdd2() {
         this.viewState = 3
      },


      // 导出文件
      doBuild() {
        const loading = this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })

        exportExcel(this.jmdListQuery).then((resp) => {
          loading.close()
          const blob = new Blob([resp], {
            type: 'application/vnd.ms-excel;charset=UTF-8'
          })
          const fileName = '库存明细列表' + '.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()
        })

      },

      resetState() {
        this.viewState = 1
      },
      doClose() {
        this.$store.dispatch('tagsView/delView', this.$route)
        this.$router.go(-1)
      }
    }
  }
</script>
<style scoped>
  .wenjiantit {
    font-size: 16px;
    font-weight: bold;
    margin: 25px 0 10px 0;
  }

  .forminfo {
    margin: 0;
    padding: 0;
  }

  .listcon {
    height: calc(100vh - 250px);
    overflow-y: auto;
    overflow-x: hidden;
  }
</style>
