<template>
  <div class="mod-config">
    <!-- 条件查询 -->
    <el-form :inline="true" label-position="top" :model="searchData" @keyup.enter.native="getDataList()">
      <el-form-item :label="'物料编码:'">
        <el-input v-model="searchData.partNo" clearable style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item :label="'库位编码:'">
        <el-input v-model="searchData.locationID" clearable style="width: 120px"></el-input>
      </el-form-item>

      <el-form-item :label="'料号描述:'">
        <el-input v-model="searchData.partDesc" clearable style="width: 120px"></el-input>
      </el-form-item>

      <el-form-item :label="'出入库单号:'">
        <el-input v-model="searchData.transNo" clearable style="width: 120px"></el-input>
      </el-form-item>


      <el-form-item :label="' '">
        <el-button v-if="!authSearch" type="primary" @click="getDataList()">查询</el-button>
<!--        <download-excel-->
<!--          :fields="fields()"-->
<!--          :data="exportData"-->
<!--          type="xls"-->
<!--          :name="exportName"-->
<!--          :header="exportHeader"-->
<!--          :footer="exportFooter"-->
<!--          :fetch="createExportData"-->
<!--          :before-generate="startDownload"-->
<!--          :before-finish="finishDownload"-->
<!--          worksheet="导出信息"-->
<!--          class="el-button el-button&#45;&#45;primary el-button&#45;&#45;medium">-->
<!--          {{ "导出" }}-->
<!--        </download-excel>-->
      </el-form-item>
    </el-form>

    <!-- 展示列表 -->
    <el-table
      :height="height"
      :data="dataList"
      @row-click="changeData"
      border
      highlight-current-row
      ref="mainTable"
      style="width: 100%;">
      <el-table-column
        v-for="(item,index) in columnList" :key="index"
        :sortable="item.columnSortable"
        :prop="item.columnProp"
        :header-align="item.headerAlign"
        :show-overflow-tooltip="item.showOverflowTooltip"
        :align="item.align"
        :fixed="item.fixed==''?false:item.fixed"
        :width="item.columnWidth"
        :label="item.columnLabel">
        <template slot-scope="scope">
          <span v-if="!item.columnHidden"> {{ scope.row[item.columnProp] }}</span>
          <span v-if="item.columnImage"><img :src="scope.row[item.columnProp]" style="width: 100px; height: 80px"/></span>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[20, 50, 100, 200, 500]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>



    <el-tabs style="font-size: 12px;min-height: 200px" class="customer-tab" v-model="activeName"  type="border-card"  @tab-click="tabClick">
      <el-tab-pane label="卷明细" name="detail">
        <el-table
          :data="dataList2"
          :height="height"
          border
          style="width: 100%; ">
          <el-table-column
            v-for="(item,index) in columnList2" :key="index"
            :sortable="item.columnSortable"
            :prop="item.columnProp"
            :header-align="item.headerAlign"
            :show-overflow-tooltip="item.showOverflowTooltip"
            :align="item.align"
            :fixed="item.fixed==''?false:item.fixed"
            :min-width="item.columnWidth"
            :label="item.columnLabel">
            <template slot-scope="scope">
              <span v-if="!item.columnHidden"> {{ scope.row[item.columnProp] }}</span>
              <span v-if="item.columnImage"><img :src="scope.row[item.columnProp]"
                                                 style="width: 100px; height: 80px"/></span>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>



  </div>
</template>

<script>
import {
  findTrans, // 检验方法查询
  findByRoll //查询卷明细
  // saveLocation, // 检验方法新增
  // updateLocation, // 检验方法修改
  // deleteLocation, // 检验方法删除
  // findByWarehouse, //查询库区
  // inspectionTypeSearch, // 搜索所有检验类型
//   // getSiteAndBuByUserName
} from "@/api/material/seekByTrans.js"
// import {getTableDefaultListLanguage, getTableUserListLanguage} from "@/api/table.js"
// import {userFavoriteList, saveUserFavorite, removeUserFavorite} from '@/api/userFavorite.js'
export default {
  data () {
    return {
      currentRow:'',
      headerData:'',
      wareHouseList:'',
      // 是否收藏
      favorite: false,
      // 导出 start
      exportData: [],
      exportName: "检验方法" + this.dayjs().format('YYYYMMDDHHmmss'),
      exportHeader: ["检验方法"],
      exportFooter: [],
      exportList: [],
      warehouseData: {
        id:'',
        site: '',
      },
      activeName:"detail",
      // 导出 end
      searchData: {
        transNo:'',
        site: '',
        userName: this.$store.state.user.name,
        transDate: '',
        warehouseID: '',
        rollNo: '',
        partNo: '',
        partDesc: '',
        transQty: '',
        batchNo: '',
        locationID: '',
        wdrNo: '',
        manufactureDate: '',
        orderRef1: '',
        receiver: '',
        enterDate: '',
        transType: '',
        page: 1,
        limit: 10,
      },
      pageIndex: 1,
      pageSize: 20,
      totalPage: 0,
      height: 200,
      secondHeight: 200,
      dataList: [],
      dataList2:[],
      dataListLoading: false,
      modalFlag: false,
      modalDisableFlag: false,
      modalData: {
        site: '',
        warehouseId: '',
        locationId: '',
        active: 'Y',
        locationName: '',
        creator: this.$store.state.user.name,
        // updateBy: this.$store.state.user.name,
        locationDescription: '',
        keeper: '',
        remarks: '',
        createTime: '',
      },
      // 标头展示
      columnList: [
        {
          userId: this.$store.state.user.name,
          functionId: 200003,
          serialNumber: '200003TableSite',
          tableId: "200003Table",
          tableName: "工厂编码",
          columnWidth: 100,
          columnProp: 'site',
          headerAlign: 'center',
          align: "center",
          columnLabel: '工厂编码',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
        },
        {
          userId: this.$store.state.user.name,
          functionId: 200003,
          serialNumber: '200003TableTransType',
          tableId: "200003Table",
          tableName: "出入库类型",
          columnWidth: 105,
          columnProp: 'transType',
          headerAlign: 'left',
          align: "left",
          columnLabel: '出入库类型',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
        },
        {
          userId: this.$store.state.user.name,
          functionId: 200003,
          serialNumber: '200003TableTransNo',
          tableId: "200003Table",
          tableName: "出入库单号",
          columnWidth: 100,
          columnProp: 'transNo',
          headerAlign: 'left',
          align: "left",
          columnLabel: '出入库单号',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
        },
        {
          userId: this.$store.state.user.name,
          functionId: 200003,
          serialNumber: '200003TableTransDate',
          tableId: "200003Table",
          tableName: "出入库日期",
          columnWidth: 105,
          columnProp: 'transDate',
          headerAlign: 'center',
          align: "center",
          columnLabel: '出入库日期',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
        },
        {
          userId: this.$store.state.user.name,
          functionId: 200003,
          serialNumber: '200003TableWarehouseId',
          tableId: "200003Table",
          tableName: "仓库编码",
          columnWidth: 105,
          columnProp: 'warehouseID',
          headerAlign: 'left',
          align: "left",
          columnLabel: '仓位编码',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
        },
        {
          userId: this.$store.state.user.name,
          functionId: 200003,
          serialNumber: '200003TablePartNo',
          tableId: "200003Table",
          tableName: "物料编码",
          columnWidth: 105,
          columnProp: 'partNo',
          headerAlign: 'left',
          align: "left",
          columnLabel: '物料编码',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
        },
        {
          userId: this.$store.state.user.name,
          functionId: 200003,
          serialNumber: '200003TablePartDesc',
          tableId: "200003Table",
          tableName: "料号描述",
          columnWidth: 105,
          columnProp: 'partDesc',
          headerAlign: 'left',
          align: "left",
          columnLabel: '料号描述',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
        },
        {
          userId: this.$store.state.user.name,
          functionId: 200003,
          serialNumber: '200003TableNum',
          tableId: "200003Table",
          tableName: "卷数量",
          columnWidth: 105,
          columnProp: 'num',
          headerAlign: 'right',
          align: "right",
          columnLabel: '卷数量',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
        },
        {
          userId: this.$store.state.user.name,
          functionId: 200003,
          serialNumber: '200003TableTransQty',
          tableId: "200003Table",
          tableName: "出入库数量",
          columnWidth: 105,
          columnProp: 'transQty',
          headerAlign: 'right',
          align: "right",
          columnLabel: '出入库数量',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
        },
        {
          userId: this.$store.state.user.name,
          functionId: 200003,
          serialNumber: '200003TableBatchNo',
          tableId: "200003Table",
          tableName: "批号",
          columnWidth: 105,
          columnProp: 'batchNo',
          headerAlign: 'left',
          align: "left",
          columnLabel: '批号',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
        },
        {
          userId: this.$store.state.user.name,
          functionId: 200003,
          serialNumber: '200003TableLocationID',
          tableId: "200003Table",
          tableName: "库位",
          columnWidth: 105,
          columnProp: 'locationID',
          headerAlign: 'left',
          align: "left",
          columnLabel: '库位',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
        },
        {
          userId: this.$store.state.user.name,
          functionId: 200003,
          serialNumber: '200003TableWdrNo',
          tableId: "200003Table",
          tableName: "W/D/R",
          columnWidth: 105,
          columnProp: 'wdrNo',
          headerAlign: 'left',
          align: "left",
          columnLabel: 'W/D/R',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
        },
        {
          userId: this.$store.state.user.name,
          functionId: 200003,
          serialNumber: '200003TableManufactureDate',
          tableId: "200003Table",
          tableName: "生产日期",
          columnWidth: 105,
          columnProp: 'manufactureDate',
          headerAlign: 'center',
          align: "center",
          columnLabel: '生产日期',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
        },
        {
          userId: this.$store.state.user.name,
          functionId: 200003,
          serialNumber: '200003TableOrderRef1',
          tableId: "200003Table",
          tableName: "关联单号",
          columnWidth: 105,
          columnProp: 'orderRef1',
          headerAlign: 'left',
          align: "left",
          columnLabel: '关联单号',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
        },
        {
          userId: this.$store.state.user.name,
          functionId: 200003,
          serialNumber: '200003TableReceiver',
          tableId: "200003Table",
          tableName: "经手人",
          columnWidth: 100,
          columnProp: 'receiver',
          headerAlign: 'left',
          align: "left",
          columnLabel: '经手人',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
        },
        {
          userId: this.$store.state.user.name,
          functionId: 200003,
          serialNumber: '200003TableEnterDate',
          tableId: "200003Table",
          tableName: "录入时间",
          columnWidth: 130,
          columnProp: 'enterDate',
          headerAlign: 'center',
          align: "center",
          columnLabel: '录入时间',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
        },
        {
          userId: this.$store.state.user.name,
          functionId: 200003,
          serialNumber: '200003TableUserName',
          tableId: "200003Table",
          tableName: "录入人",
          columnWidth: 105,
          columnProp: 'userName',
          headerAlign: 'left',
          align: "left",
          columnLabel: '录入人',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
        },

      ],
      columnList2:[
        {
          userId: this.$store.state.user.name,
          functionId: 200003,
          serialNumber: '200003TableTransNo',
          tableId: "200003Table",
          tableName: "出入库单号",
          columnWidth: 200,
          columnProp: 'transNo',
          headerAlign: 'left',
          align: "left",
          columnLabel: '出入库单号',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
        },
        {
          userId: this.$store.state.user.name,
          functionId: 200003,
          serialNumber: '200003TableRollNo',
          tableId: "200003Table",
          tableName: "卷号",
          columnWidth: 200,
          columnProp: 'rollNo',
          headerAlign: 'left',
          align: "left",
          columnLabel: '卷号',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
        },
        {
          userId: this.$store.state.user.name,
          functionId: 200003,
          serialNumber: '200003TableSum',
          tableId: "200003Table",
          tableName: "数量",
          columnWidth: 200,
          columnProp: 'sum',
          headerAlign: 'right',
          align: "right",
          columnLabel: '数量',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
        },
        {
          userId: this.$store.state.user.name,
          functionId: 200003,
          serialNumber: '200003TableManufactureDate',
          tableId: "200003Table",
          tableName: "生产日期",
          columnWidth: 200,
          columnProp: 'manufactureDate',
          headerAlign: 'center',
          align: "center",
          columnLabel: '生产日期',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
        },
        {
          userId: this.$store.state.user.name,
          functionId: 200003,
          serialNumber: '200003TableEnterDate',
          tableId: "200003Table",
          tableName: "录入时间",
          columnWidth: 200,
          columnProp: 'enterDate',
          headerAlign: 'center',
          align: "center",
          columnLabel: '录入时间',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
        },
      ],
      rules: {
        warehouseId:[
          {
            required: true,
            message: ' ',
            trigger: ['blur','change']
          }
        ],
        locationId:[
          {
            required: true,
            message: ' ',
            trigger: ['blur','change']
          }
        ],
        site:[
          {
            required: true,
            message: ' ',
            trigger: ['blur','change']
          }
        ],
        locationName:[
          {
            required: true,
            message: ' ',
            trigger: ['blur','change']
          }
        ]
      },
      options: [],
      userBuList: [],
      authSearch: false,
      authSave: false,
      authUpdate: false,
      authDelete: false,
      menuId: this.$route.meta.menuId,
    }
  },



  mounted () {
    this.$nextTick(() => {
      this.height = (window.innerHeight- 260) / 2
    })
  },

  //页面加载前初始化着两个方法
  created () {
    this.getDataList()
    //this.getWareHouseList()
  },

  methods: {
    // 获取用户的bu
    getSiteAndBuByUserName () {
      let tempData = {
        username: this.$store.state.user.name,
      }
      getSiteAndBuByUserName(tempData).then(({data}) => {
        if (data.code === 0) {
          this.userBuList = data.rows
        }
      })
    },

    tabClick (tab, event) {
      // 刷新列表数据
      this.refreshCurrentTabTable()
    },
    changeData(row){
      this.currentRow = JSON.parse(JSON.stringify(row));
      this.headerData=row;
      this.refreshCurrentTabTable ();
    },
    refreshCurrentTabTable(){
      if(this.currentRow===''||this.currentRow===null){
        this.currentRow={site:'',transNo:''}
      }
        findByRoll(this.currentRow).then(({data}) => {
          //区分请求成功和失败的状况
          if (data && data.code == 0) {
            this.dataList2 = data.rows
          } else {
            this.dataList2 = [];
          }
        });
    },

    // 查询获取数据列表
    getDataList (){
      this.searchData.limit = this.pageSize
      this.searchData.page = this.pageIndex
      findTrans(this.searchData).then(({data}) => {
        if (data.code === 0) {
          this.dataList = data.page.list
          this.pageIndex = data.page.currPage
          this.pageSize = data.page.pageSize
          this.totalPage = data.page.totalCount
          // if(this.dataList.length>0){
          //   this.$refs.mainTable.setCurrentRow(this.dataList[0]);
          //   this.changeData(this.dataList[0])
          // }else {
          //   this.changeData(null)
          // }
        }
      })
    },

    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },

    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },


    //导出excel
    async createExportData() {
      this.searchData.limit = -1
      this.searchData.page = 1
      await findTrans(this.searchData).then(({data}) => {
        this.exportList= data.rows
      })
      return this.exportList
    },

    startDownload() {},

    finishDownload() {},

    fields () {
      let json = "{"
      this.columnList.forEach((item, index) => {
        if (index == this.columnList.length - 1) {
          json += "\"" + item.columnLabel + "\"" + ":" + "\"" + item.columnProp + "\""
        } else {
          json += "\"" + item.columnLabel + "\"" + ":" + "\"" + item.columnProp + "\"" + ","
        }
      })
      json += "}"
      let s = eval("(" + json + ")")
      return s
    },

    // 动态列开始 获取 用户保存的 格式列
    async getTableUserColumn (tableId, columnId) {
      let queryTableUser = {
        userId: this.$store.state.user.name,
        functionId: this.$route.meta.menuId,
        tableId: tableId,
        status: true,
        languageCode: this.$i18n.locale
      }
      await getTableUserListLanguage(queryTableUser).then(({data}) => {
        if (data.rows.length > 0) {
          //this.columnList1 = []
          switch (columnId) {
            case 1:
              this.columnList = data.rows
              break;
            // case 2:
            //   this.columnDetailList = data.rows
            //   break;
            // case 3:
            //   this.columnList2 = data.rows
            //   break;
            // case 4:
            //   this.columnList3 = data.rows
            //   break;
          }
        } else {
          this.getColumnList(tableId, columnId)
        }
      })
    },

    // 获取 tableDefault 列
    async getColumnList (tableId, columnId) {
      let queryTable = {
        functionId: this.$route.meta.menuId,
        tableId: tableId,
        languageCode: this.$i18n.locale
      }
      await getTableDefaultListLanguage(queryTable).then(({data}) => {
        if (!data.rows.length == 0) {
          switch (columnId) {
            case 1:
              this.columnList = data.rows
              break;
            // case 2:
            //   this.columnDetailList = data.rows
            //   break;
            // case 3:
            //   this.columnList2 = data.rows
            //   break;
            // case 4:
            //   this.columnList3 = data.rows
            //   break;
          }
        } else {
          // this.showDefault = true.
        }
      })
    },

    //获取按钮的权限数据
    getButtonAuthData () {
      let searchFlag = this.isAuth(this.menuId+":search")
      let saveFlag = this.isAuth(this.menuId+":save")
      let updateFlag = this.isAuth(this.menuId+":update")
      let deleteFlag = this.isAuth(this.menuId+":delete")
      //处理页面的权限数据
      this.authSearch = !searchFlag
      this.authSave = !saveFlag
      this.authUpdate = !updateFlag
      this.authDelete = !deleteFlag
    },
  }
}
</script>
