<template>
  <div class="mod-config">
    <el-form :inline="true" label-position="top" label-width="100px" style="margin-top: 0px;">
      <el-form-item :label="'BU:'">
        <el-select v-model="searchData.buNo" placeholder="请选择"  >
          <el-option label="全部" value=""></el-option>
          <el-option
            v-for = "i in buList"
            :key = "i.buNo"
            :label = "i.buDesc"
            :value = "i.buNo">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="'栈板类型:'">
        <el-input v-model="searchData.palletType"  ></el-input>
<!--        <el-select filterable v-model="searchData.palletType" clearable style="width: 140px;">
          <el-option :label="item" :value="item" v-for="(item,index) in palletTypes "
                     :key="index"></el-option>
        </el-select>-->
      </el-form-item>
      <el-form-item :label="' '" >
        <el-button @click="searchTable()" type="primary" style="margin-left: 2px;margin-top: 0px">{{'查询'}}</el-button>
        <el-button @click="addModelOpen()" type="primary" style="margin-left: 2px;margin-top: 0px">{{'新增'}}</el-button>
      </el-form-item>
    </el-form>

    <el-table
      @row-click="changeData"
      highlight-current-row
      :height="height"
      :data="dataList"
      ref="mainTable"
      border
      v-loading="dataListLoading"
      style="width: 100%;">
      <el-table-column
        header-align="center"
        align="center"
        width="100"
        fixed="left"
        label="操作">
        <template slot-scope="scope">
          <a type="text" size="small"  @click="updateModelOpen(scope.row)">编辑</a>
          <a type="text" size="small"  @click="deletePallet(scope.row)">删除</a>
        </template>
      </el-table-column>
      <el-table-column
        v-for="(item,index) in columnList1" :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-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[20, 50, 100, 1000]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>

    <el-dialog title="维护栈板信息" :close-on-click-modal="false" v-drag :visible.sync="addModelFlag" width="500px">
      <el-form  label-position="top"  style="margin-left: 7px;margin-top: -5px;">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item :label="'BU:'">
              <el-select v-model="addModel.buNo" placeholder="请选择" :disabled="addModel.addFlag!==0"  style="width: 100%">
                <el-option
                  v-for = "i in buList"
                  :key = "i.buNo"
                  :label = "i.buDesc"
                  :value = "i.buNo">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item  label="栈板编码">
              <el-input v-model="addModel.palletNo" :disabled="addModel.addFlag!==0"  ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="'栈板类型'" >
              <el-input v-model="addModel.palletType"  ></el-input>
<!--              <el-select filterable v-model="addModel.palletType" clearable style="width:100%;">
                <el-option :label="item" :value="item" v-for="(item,index) in palletTypes "
                           :key="index"></el-option>
              </el-select>-->
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="'应用环境'" >
              <el-select filterable v-model="addModel.applicationArea" clearable style="width: 100%;">
                <el-option label="EU" value="EU"></el-option>
                <el-option label="US" value="US"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item  label="长(M)">
              <el-input v-model="addModel.length"  ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item  label="宽(M)">
              <el-input v-model="addModel.width"  ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item  label="栈板高(M)">
              <el-input v-model="addModel.height"  ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item :label="'备注'" >
              <el-input v-model="addModel.remark"  ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-footer style="height:40px;margin-top: 10px;text-align:center">
        <el-button type="primary" @click="savePallet()">保存</el-button>
        <el-button type="primary" @click="addModelFlag=false">关闭</el-button>
      </el-footer>
    </el-dialog>

    <!--列表的组件-->
    <Chooselist ref="baseList" @getBaseData="getBaseData"></Chooselist>
  </div>
</template>

<script>
  import Chooselist from '@/views/modules/common/Chooselist_eam'
  import {} from "@/api/sysLanguage.js"
  import {
    searchPalletData,
    savePalletData,
    deletePallet,
  }from "@/api/ecss/ecss.js"
  import {getBuList}from  '@/api/factory/site.js'
  export default {
    name: "null",
    components:{
      Chooselist,
    },
    data() {
      return {
        palletTypes: ['木栈板', '塑料栈板', '钢栈板', '复合材料栈板'],
        pageIndex: 1,
        pageSize: 100,
        totalPage: 0,
        height: 200,
        buList:[],
        dataList:[],
        dataList2:[],
        dataListLoading: false,
        searchData: {
          page: 1,
          limit: 100,
          buNo:'',
          palletType:'',
          codeNo:'',
          username:this.$store.state.user.name,
        },
        buttons:{
          search:'查询',
        },
        addModelFlag:false,
        addModel:{
          addFlag:0,
          palletNo:'',
          site:'',
          buNo:'',
          palletType:'',
          length:'',
          width:'',
          height:'',
          applicationArea:'',
          remark:'',
        },
        addDisabledFlag:true,
        activeName:'attribute',
        columnList1: [
          {
            userId: this.$store.state.user.name,
            functionId: 801007,
            serialNumber: '801007Table1BuDesc',
            tableId: "801007Table1",
            tableName: "pallet基础信息",
            columnProp: "buDesc",
            headerAlign: "center",
            align: "center",
            columnLabel: "BU",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 80
          },
          {
            userId: this.$store.state.user.name,
            functionId: 801003,
            serialNumber: '801007Table1PalletNo',
            tableId: "801007Table1",
            tableName: "pallet基础信息",
            columnProp: "palletNo",
            headerAlign: "center",
            align: "center",
            columnLabel: "栈板编码",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 160
          },
          {
            userId: this.$store.state.user.name,
            functionId: 801003,
            serialNumber: '801007Table1PalletType',
            tableId: "801007Table1",
            tableName: "pallet基础信息",
            columnProp: "palletType",
            headerAlign: "center",
            align: "center",
            columnLabel: "栈板类型",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 100
          },
          {
            userId: this.$store.state.user.name,
            functionId: 801003,
            serialNumber: '801007Table1Length',
            tableId: "801007Table1",
            tableName: "pallet基础信息",
            columnProp: "length",
            headerAlign: "center",
            align: "center",
            columnLabel: "长(M)",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 100
          },
          {
            userId: this.$store.state.user.name,
            functionId: 801003,
            serialNumber: '801007Table1Width',
            tableId: "801007Table1",
            tableName: "pallet基础信息",
            columnProp: "width",
            headerAlign: "center",
            align: "center",
            columnLabel: "宽(M)",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 100
          },
          {
            userId: this.$store.state.user.name,
            functionId: 801003,
            serialNumber: '801007Table1Height',
            tableId: "801007Table1",
            tableName: "pallet基础信息",
            columnProp: "height",
            headerAlign: "center",
            align: "center",
            columnLabel: "栈板高(M)",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 100
          },
          {
            userId: this.$store.state.user.name,
            functionId: 801003,
            serialNumber: '801007Table1ApplicationArea',
            tableId: "801007Table1",
            tableName: "pallet基础信息",
            columnProp: "applicationArea",
            headerAlign: "center",
            align: "center",
            columnLabel: "应用环境",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 100
          },
          {
            userId: this.$store.state.user.name,
            functionId: 801003,
            serialNumber: '801007Table1CreateBy',
            tableId: "801007Table1",
            tableName: "pallet基础信息",
            columnProp: "createBy",
            headerAlign: "center",
            align: "center",
            columnLabel: "创建人",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 100
          },
          {
            userId: this.$store.state.user.name,
            functionId: 801003,
            serialNumber: '801007Table1CreateDate',
            tableId: "801007Table1",
            tableName: "pallet基础信息",
            columnProp: "createDate",
            headerAlign: "center",
            align: "center",
            columnLabel: "创建日期",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 140
          },
          {
            userId: this.$store.state.user.name,
            functionId: 801003,
            serialNumber: '801007Table1UpdateBy',
            tableId: "801007Table1",
            tableName: "pallet基础信息",
            columnProp: "updateBy",
            headerAlign: "center",
            align: "left",
            columnLabel: "修改人",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 100
          },
          {
            userId: this.$store.state.user.name,
            functionId: 801003,
            serialNumber: '801007Table1UpdateDate',
            tableId: "801007Table1",
            tableName: "pallet基础信息",
            columnProp: "updateDate",
            headerAlign: "center",
            align: "center",
            columnLabel: "修改日期",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 100
          },
          {
            userId: this.$store.state.user.name,
            functionId: 801003,
            serialNumber: '801007Table1Remark',
            tableId: "801007Table1",
            tableName: "pallet基础信息",
            columnProp: "remark",
            headerAlign: "center",
            align: "left",
            columnLabel: "备注",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 300
          },
        ],
        currentRow:{},
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.height = (window.innerHeight - 240)/2;
      })
    },
    methods: {
      // 获取基础数据列表S
      getBaseList(val, type) {
        this.tagNo = val
        this.$nextTick(() => {
          let strVal = ''
          if (val === 33) {
            strVal = this.addModel.codeNo
          }
          this.$refs.baseList.init(val, strVal)
        })
      },

      /* 列表方法的回调 */
      getBaseData(val) {
        if (this.tagNo === 33) {
          this.addModel.codeNo = val.Code_No
        }
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.searchTable()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.searchTable()
      },
      searchTable(){
        this.searchData.limit = this.pageSize
        this.searchData.page = this.pageIndex
        searchPalletData(this.searchData).then(({data}) => {
          //区分请求成功和失败的状况
          if (data && 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)
            }
          } else {
            this.dataList = [];
          }
        });
      },
      changeData(row){
        this.currentRow = JSON.parse(JSON.stringify(row));
        this.headerData=row;
        this.refreshCurrentTabTable ();
      },
      refreshCurrentTabTable(){
        if(this.currentRow===''||this.currentRow===null){
          this.currentRow={site:'',hsCode:'',buNo:'',recordType:'',codeNo:''}
        }
        if(this.activeName==='detail'){
          // searchEcssCoDelNotifyDetail(this.currentRow).then(({data}) => {
          //   //区分请求成功和失败的状况
          //   if (data && data.code == 0) {
          //     this.dataList2 = data.rows
          //
          //   } else {
          //     this.dataList2 = [];
          //   }
          // });
        }
      },
      tabClick (tab, event) {
        // 刷新列表数据
        this.refreshCurrentTabTable()
      },

      getBu () {
        let tempData = {
          username: this.$store.state.user.name,
        }
        getBuList(tempData).then(({data}) => {
          if (data.code === 0) {
            this.buList = data.row2
          }
        })
      },
      addModelOpen(){
        this.addDisabledFlag=true
        this.addModel={
          addFlag:0,
          palletNo:'',
          site:'',
          buNo:'',
          palletType:'',
          length:'',
          width:'',
          height:'',
          applicationArea:'',
          remark:'',
        }
        this.addModelFlag=true
      },
      updateModelOpen(row){
        this.addDisabledFlag=false
        this.addModel={
          id:row.id,
          addFlag:1,
          palletNo:row.palletNo,
          site:row.site,
          buNo:row.buNo,
          palletType:row.palletType,
          length:row.length,
          width:row.width,
          height:row.height,
          applicationArea:row.applicationArea,
          remark:row.remark,
        }
        this.addModelFlag=true
      },
      deletePallet(row){
        this.$confirm('确认删除？', '提示').then(() => {
          deletePallet(row).then(({data}) => {
            if (data && data.code === 0) {
              this.searchTable()
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {}
              })
            } else {
              this.$alert(data.msg, '错误', {
                confirmButtonText: '确定'
              })
            }
          })
        })
      },
      savePallet(){
        if(this.addModel.buNo==null||this.addModel.buNo===''){
          this.$alert('请选择BU！', '错误', {
            confirmButtonText: '确定'
          })
          return false
        }
        if(this.addModel.palletNo==null||this.addModel.palletNo===''){
          this.$alert('请输入编码！', '错误', {
            confirmButtonText: '确定'
          })
          return false
        }
        if(this.addModel.applicationArea==null||this.addModel.applicationArea===''){
          this.$alert('请输入应用环境！', '错误', {
            confirmButtonText: '确定'
          })
          return false
        }
        if(this.addModel.palletType==null||this.addModel.palletType===''){
          this.$alert('请输入类型！', '错误', {
            confirmButtonText: '确定'
          })
          return false
        }
        if(this.addModel.length==null||this.addModel.length===''){
          this.$alert('请输入长度！', '错误', {
            confirmButtonText: '确定'
          })
          return false
        }
        if(this.addModel.width==null||this.addModel.width===''){
          this.$alert('请输入宽度！', '错误', {
            confirmButtonText: '确定'
          })
        }
        if(this.addModel.height==null||this.addModel.height===''){
          this.$alert('请输入高度！', '错误', {
            confirmButtonText: '确定'
          })
          return false
        }
        savePalletData(this.addModel).then(({data}) => {
          if (data && data.code === 0) {
            this.searchTable()
            this.addModelFlag = false
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {}
            })
          } else {
            this.$alert(data.msg, '错误', {
              confirmButtonText: '确定'
            })
          }
        })
      },
    },
    activated() {
      this.searchTable()
    },
    created() {
      this.getBu ()
    }
  }
</script>

<style scoped>

</style>
