<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="入库单号">
                <el-input v-model="queryParams.params.inStotageBillNo" placeholder="" clearable />
              </el-form-item>
              <el-form-item label="外部单号">
                <el-input v-model="queryParams.params.sourceBillNo" placeholder="" clearable />
              </el-form-item>
              <el-form-item label="预约单号">
                <el-input v-model="queryParams.params.preBillNo" placeholder="" clearable />
              </el-form-item>
              <el-form-item label="入库库位">
                <el-input v-model="queryParams.params.warehouseRackName" placeholder="" clearable />
              </el-form-item>
              <el-form-item label="入库时间">
                <el-date-picker v-model="queryParams.params.inStotageStartTime" 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.inStotageEndTime" type="date" placeholder="选择日期"
                  format="yyyy-MM-dd" value-format="yyyy-MM-dd" style="width: 200px;">
                </el-date-picker>
              </el-form-item>
              <el-form-item label="承运商">
                <el-input v-model="queryParams.params.carrierName" placeholder="" clearable />
              </el-form-item>
              <el-form-item label="运单号">
                <el-input v-model="queryParams.params.waybillNumber" placeholder="" clearable />
              </el-form-item>
              <el-form-item label="商品">
                <el-select v-model="queryParams.params.goodsSkuSid" placeholder="请选择" style="width: 200px;">
                  <el-option v-for="(item,i) in goodsList" :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.supplierSid" placeholder="请选择" style="width: 200px;">
                  <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>
            <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>
          <pagination v-show="dataList.length > 0" :total="queryParams.total" :page.sync="queryParams.current"
            :limit.sync="queryParams.size" class="pagination" @pagination="loadList" />
        </div>
        <!-- End 项目列表头部 -->
        <!-- Start 项目列表 -->
        <div class="">
          <el-table v-loading="tableLoading" :data="dataList" border style="width: 100%"
            @selection-change="selectionLineChangeHandle" @row-click="rowItemClick">
            <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 label="操作" align="center" width="80">
              <template slot-scope="scope">
                <el-button type="primary" size="mini" @click="toRelevancy(scope.row)">查看</el-button>
              </template>
            </el-table-column>
            <el-table-column prop="billStateValue" width="100" label="状态" align="center" />
            <el-table-column prop="billNo" width="150" label="入库单号" align="center" />
            <el-table-column prop="preBillNo" width="150" label="预约单号" align="center" />
            <el-table-column prop="goodsTotal" width="100" label="商品总额" align="center" />
            <el-table-column prop="goodsWeight" width="150" label="商品总重量(kg)" align="center" />
            <el-table-column prop="goodsVolume" width="150" label="商品总体积(m³)" align="center" />
            <el-table-column prop="goodsCount" width="100" label="商品总数" align="center" />
            <el-table-column prop="warehouseName" width="180" label="供应商" align="center" />
            <el-table-column prop="carrierName" width="180" label="承运商" align="center" />
            <el-table-column prop="waybillNumber" width="120" label="运单号" align="center" />
            <el-table-column prop="sourceBillNo" width="150" label="外部单号" align="center" />
            <el-table-column prop="remarks" min-width="180" label="单据备注" align="center" />
            <el-table-column prop="inStotageTime" width="120" label="入库时间" align="center" />
            <el-table-column prop="operationName" width="120" label="操作员" align="center" />
            <el-table-column prop="operationTime" width="120" label="操作时间" align="center" />

          </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> -->

        <el-menu :default-active="activeIndex" class="el-menu-demo" mode="horizontal" @select="handleSelect"
          text-color="#666" active-text-color="white">

          <el-menu-item index="1">入库明细</el-menu-item>
          <el-menu-item index="2">单据备注</el-menu-item>
          <el-menu-item index="3">操作记录</el-menu-item>
        </el-menu>

        <div v-show="activeIndex=='1'" style="margin-bottom: 50px;">
          <el-table :data="goodsDetailList" border style="width: 100%">
            <el-table-column width="60" label="序号" type="index" :index="indexMethod" align="center" />
            <el-table-column prop="goodsName" label="商品名称" align="center" />
            <el-table-column prop="goodsCode" label="商品编码" align="center" />
            <el-table-column prop="goodsBarCode" label="商品条码" align="center" />
            <el-table-column prop="goodsSpc" label="规格值" align="center" />
            <el-table-column prop="boxBarCode" label="箱条码" align="center" />
            <el-table-column prop="boxSpc" label="箱规格" align="center" />
            <el-table-column prop="unit" label="单位" align="center" />
            <el-table-column prop="costPrice" label="成本单价" align="center" />
            <el-table-column prop="totalPrice" label="总价" align="center" />
            <el-table-column prop="count" label="数量" align="center" />
            <el-table-column prop="auxiliaryUnit" label="辅助单位" align="center" />
            <el-table-column prop="serialNumber" label="序列号" align="center" />
            <el-table-column prop="batchNumber" label="生产批次号" align="center" />
            <el-table-column prop="warehouserack" label="入库库位" align="center" />
            <el-table-column prop="remarks" label="明细备注" align="center" />
          </el-table>
        </div>

        <div v-show="activeIndex=='2'" style="margin-bottom: 50px;">

          <el-form ref="dataForm" class="formaddcopy02" :model="billRemarks">
            <el-row class="first_row">
              <el-col :span="8">
                <div class="span-sty">买家留言</div>
                <el-form-item> <span class="addinputInfo">{{ billRemarks.buyerMessage }}</span></el-form-item>
              </el-col>
              <el-col :span="8">
                <div class="span-sty">备注</div>
                <el-form-item> <span class="addinputInfo">{{ billRemarks.reamrks }}</span></el-form-item>
              </el-col>
              <el-col :span="8">
                <div class="span-sty">系统备注</div>
                <el-form-item> <span class="addinputInfo">{{ billRemarks.systemRemarks }}</span></el-form-item>
              </el-col>
            </el-row>
          </el-form>

        </div>
        <div v-show="activeIndex=='3'" style="margin-bottom: 50px;">
          <el-table :data="operationList" border style="width: 100%">
            <el-table-column width="60" label="序号" type="index" :index="indexMethod" align="center" />
            <el-table-column prop="times" label="操作时间" width="250" align="center" />
            <el-table-column prop="name" label="操作账号" width="180" align="center" />
            <el-table-column prop="centent" label="操作内容" align="center" />
          </el-table>
        </div>

      </div>
    </div>
    <!-- End 查询和其列表部分 -->
    <!-- 新增修改部分组件 -->
    <divAdd v-show="viewState == 2 || viewState == 3" ref="divadd" @doback="resetState" @reloadlist="loadList" />
    <divInfo v-show="viewState == 4" ref="divinfo" @doback="resetState" @reloadlist="loadList" />
    <divAddupShelfRecord v-show="viewState == 5" ref="divAddupShelfRecord" @doback="resetState"
      @reloadlist="loadList" />

    <el-dialog title="选择预约单" :visible.sync="dialogVisible" :before-close="handleClose"
      style="width:150%;margin-left: -20%;">
      <el-form :inline="true" style="margin-top: -30px;">
        <el-form-item label="外部单号">
          <el-input v-model="queryParams.params.sourceBillNo" placeholder="" clearable />
        </el-form-item>
        <el-form-item label="入库状态">
          <el-select v-model="queryParams.params.billState" placeholder="请选择" style="width: 200px;">
            <el-option v-for="(item,i) in billStateList" :key="item.sid" :label="item.name" :value="item.sid">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="供应商">
          <el-select v-model="queryParams.params.supplierSid" placeholder="请选择" style="width: 200px;">
            <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-date-picker v-model="queryParams.params.inStotageStartTime" 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.inStotageEndTime" type="date" placeholder="选择日期"
            format="yyyy-MM-dd" value-format="yyyy-MM-dd" style="width: 200px;">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="商品">
          <el-select v-model="queryParams.params.goodsSkuSid" placeholder="请选择" style="width: 200px;">
            <el-option v-for="(item,i) in goodsList" :key="i" :label="item.name" :value="item.sid">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item style="margin-left: 50px;">
          <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>
        </el-form-item>
        <!-- <div class="btn" style="text-align: center;margin-bottom: 30px;">
          <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> -->

      </el-form>

      <el-table :data="billList" border style="width: 100%" @selection-change="selectionLineChangeHandle"
        @row-click="rowItemClick">
        <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 type="expand">
          <template slot-scope="scope">
            <el-table :data="scope.row.goodsLsit" border stripe size="small" :ref="'sub' + scope.$index"
              @select="subSelect" @select-all="subSelectAll">
              <el-table-column type="selection" width="100" align="center" />
              <el-table-column prop="goodsCode" label="商品编码" align="center" show-overflow-tooltip />
              <el-table-column prop="goodsBarCode" label="商品条码" align="center" show-overflow-tooltip />
              <el-table-column prop="goodsName" label="商品名称" align="center" show-overflow-tooltip />
              <el-table-column prop="goodsSpcCode" label="规格编码" align="center" show-overflow-tooltip />
              <el-table-column prop="goodsSpc" label="规格值" align="center" show-overflow-tooltip />
              <el-table-column prop="unit" label="单位" align="center" show-overflow-tooltip />
              <el-table-column prop="reservationNum" label="预约数量" align="center" show-overflow-tooltip />
              <el-table-column prop="unInStorageNum" label="待入库数量" align="center" show-overflow-tooltip />
              <el-table-column prop="actualInStorageNum" label="实际入库数量" align="center" show-overflow-tooltip />
            </el-table>
          </template>
        </el-table-column>

        <el-table-column prop="stae" label="状态" align="center" />
        <el-table-column prop="createTime" label="申请时间" align="center" />
        <el-table-column prop="billNo" label="单据编码" align="center" />
        <el-table-column prop="warehouseName" label="仓库" align="center" />
        <el-table-column prop="supplierName" label="供应商" align="center" />
        <el-table-column prop="warehouseRackName" label="入库库位" align="center" />
        <el-table-column prop="sourceBillNo" label="外部单号" align="center" />
        <el-table-column prop="remarks" label="单据备注" align="center" />
        <el-table-column prop="createByName" label="申请人" align="center" />
        <el-table-column prop="contacts" label="联系电话" align="center" />
        <el-table-column prop="contactsNumber" label="联系人" align="center" />
      </el-table>

      <div style="display: flex;flex-direction: row;width: 100%;justify-content: center;align-items: center;">
        <el-button type="primary" style="margin-top: 20px;" @click="saveData">确 定</el-button>
      </div>

    </el-dialog>


  </div>
</template>

<script>
  import req from '@/api/inStorage/receivingGoods.js'
  // import req2 from '@/api/baseinfo/goodsShelves/goodsShelves.js'
  import req3 from '@/api/goods/goods.js'
  import ButtonBar from '@/components/ButtonBar'
  import Pagination from '@/components/pagination'
  import pageye from '@/components/pagination/pageye'
  import divAdd from './receivingGoodsAdd.vue'
  import divInfo from './receivingGoodsInfo.vue'
  import divAddupShelfRecord from './upShelfAddRecord.vue'
  import {
    // getTypeValueList,
    choiceSupplierInfo
  } from '@/api/Common/dictcommons'
  export default {
    components: {
      ButtonBar,
      Pagination,
      pageye,
      divAdd,
      divInfo,
      divAddupShelfRecord
    },
    data() {
      return {
        activeIndex: "1",
        dialogVisible: false,
        btndisabled: false,
        viewState: 1, // 1、列表 2、添加 3、修改 4、查看
        isSearchShow: false,
        searchxianshitit: '显示查询条件',
        tableLoading: false,
        dataList: [],
        btnList: [{
            type: 'primary',
            size: 'small',
            icon: 'plus',
            btnKey: 'toAdd',
            btnLabel: '选择预约单'
          },
          {
            type: 'success',
            size: 'small',
            icon: 'export',
            btnKey: 'toPrintBill',
            btnLabel: '打印采购单'
          },
          {
            type: 'success',
            size: 'small',
            icon: 'export',
            btnKey: 'toPrintCode',
            btnLabel: '打印条码'
          },
          {
            type: 'success',
            size: 'small',
            icon: 'export',
            btnKey: 'toBuild',
            btnLabel: '导出'
          },
          {
            type: 'info',
            size: 'small',
            icon: 'cross',
            btnKey: 'doClose',
            btnLabel: '关闭'
          }
        ],
        queryParams: {
          current: 1,
          size: 10,
          total: 0,
          params: {
            name: ''
          }
        },
        sids: [],
        selectionList: [],
        goodsList: [],
        supplierList: [],
        goodsDetailList: [],
        billRemarks: {},
        operationList: [],
        billStateList: [{
            name: "在途",
            sid: "0"
          },
          {
            name: "部分收货",
            sid: "1"
          },
          {
            name: "收货完成",
            sid: "2"
          },
          {
            name: "关闭",
            sid: "3"
          }
        ],
        billList: []
      }
    },
    mounted() {
      this.$refs['btnbar'].setButtonList(this.btnList)
    },
    created() {
      this.init()
      this.loadList()
    },
    methods: {

      init() {
        var query = {
          orgPath: window.sessionStorage.getItem('orgSidPath')
        }
        //   req2.warehouserackListAll(query).then((res) => {
        //     if (res.success) {
        //       this.warehouseRackList = res.data

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

        //     }
        //   })
        req3.getGoodsListAllByOrg(query).then((res) => {
          if (res.success) {
            this.goodsList = res.data
          }
        })

        // 供应商
        choiceSupplierInfo({
          createOrgSid: window.sessionStorage.getItem('orgSidPath')
        }).then((resp) => {
          if (resp.success) {
            this.supplierList = resp.data
          }
        })
      },


      rowItemClick(row) {
        console.log("row", row);
        // this.rowItemSid = row.sid
        this.goodsDetailList = []
        this.billRemarks = {}
        this.operationList = []
        req.selectByBillSid(row.sid).then((resp) => {
          this.goodsDetailList = resp.data.goodsList
          this.billRemarks = resp.data.billRemarks
          this.operationList = resp.data.operationList
        }).catch(() => {})

        // this.handleSelect(this.activeIndex)
      },

      handleSelect(val) {
        console.log("val", val);
        this.activeIndex = val

        // if (val == '1') { // 商品明细
        //   req.selectByBillSid(this.rowItemSid).then((resp) => {
        //     this.goodsDetailList = resp.data
        //   }).catch(() => {})
        // }

        // if (val == '2') { // 发货人信息
        //   req.fetchDetailsBySid(this.rowItemSid).then((resp) => {
        //     this.formobj = resp.data
        //   }).catch(() => {})
        // }

        // if (val == '3') { // 操作记录
        //   var parpams = {
        //     sid: this.rowItemSid
        //   }
        //   req.operationrecordDetails(parpams).then((resp) => {
        //     this.operateList = resp.data
        //   }).catch(() => {})
        // }

      },

      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 'toAdd':
            this.toAdd()
            break
          case 'toPrintBill':
            this.toPrintBill()
            break
          case 'toPrintCode':
            this.toPrintCode()
            break
          case 'toBuild':
            this.toBuild()
            break
          case 'doClose':
            this.doClose()
            break
          default:
            break
        }
      },
      loadList() {
        this.tableLoading = true
        this.queryParams.params.userSid = window.sessionStorage.getItem('userSid')
        this.queryParams.params.orgPath = window.sessionStorage.getItem('orgSidPath')
        this.queryParams.params.menuUrl = this.$route.path
        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
            this.rowItemClick(this.dataList[0])
          } 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: {
            name: ''
          }
        }
        this.loadList()
      },
      toAdd() {

        req.getReservationBill(window.sessionStorage.getItem('orgSidPath')).then((resp) => {
          if (resp.success) {
            this.billList = resp.data
            this.dialogVisible = true
          } else {}
        }).catch(() => {})


      },
      toPrintBill() {
        if (this.selectionList.length != 1) {
          this.$message({
            type: 'warning',
            message: "请选择一条数据。",
            showClose: true
          })
          return
        }

        if (this.selectionList[0].billState == '新建') {
          this.$message({
            type: 'warning',
            message: "此记录未完成,不可操作。",
            showClose: true
          })
          return
        }

        this.viewState = 5
        this.$refs['divAddupShelfRecord'].showEdit(this.selectionList[0])
      },
      toPrintCode(row) {
        this.viewState = 3
        this.$refs['divadd'].showEdit(row)

      },
      toBuild(row) {
        this.viewState = 4
        this.$refs['divinfo'].showEdit(row)
      },
      doClose() {
        this.$store.dispatch('tagsView/delView', this.$route)
        this.$router.go(-1)
      },


      resetState() {
        this.viewState = 1
      },

    }
  }
</script>
<style scoped>
  /deep/ .el-form-item__error {
    margin-left: 120px !important;
  }

  .must {
    color: #f00;
  }

  .span-sty {
    width: 130px !important;
  }

  .addinputInfo {
    margin-left: 120px !important;
  }

  .formaddcopy02 .el-row .el-col /deep/ .el-form-item .addinputw {
    margin-left: 120px !important;
    width: calc(100% - 115px);
  }

  .first_row {
    border-top: 1px solid #E0E3EB;
  }

  .titleOne {
    padding: 7px;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
  }

  .el-menu-item.is-active {
    background-color: #3370ff !important;
    color: #fff;

    span {
      color: #fff;
    }
  }

  .el-submenu__title.is-active {
    background: #1c88cf !important;
    color: #fff !important;
  }
</style>
