<template>
  <el-container>
    <el-header style="width: 100%; height: 80px;font-size: 12px">
      <el-form :inline="true" label-width="55px" label-position="right">
        <el-row>
          <el-col :span="7">
            <el-form-item label="船号:">
              <ProjectSelect ref="projectSelectRef" />
            </el-form-item>
            <el-form-item label="批量:">
              <el-input v-model="queryParam.dcPl" type="search" placeholder="批量" />
            </el-form-item>
          </el-col>
          <el-col :span="15">
            <VxeSelect v-model="plParam.qy" ref="qyRef" clearable :options="qyList" @change="qyChange" style="margin-left: 10px;width: 125px" placeholder="选择区域"/>
            <VxeSelect v-model="plParam.dw" ref="dwRef" clearable :options="dwList" placeholder="选择垛位"/>
            <VxeButton status="primary" content="批量" @click="pl" />
          </el-col>
          <el-col :span="2">
            <el-form-item style="float: right" >
              <el-button type="success" @click="initList">查询</el-button>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="14"></el-col>
          <el-col :span="10" >
            <el-form-item style="float: right" >
              <el-button type="success" @click="toolbarButtonClickEvent('save')">保存</el-button>
              <el-button type="success" @click="toolbarButtonClickEvent('insert')">添加</el-button>
              <el-button type="danger" @click="toolbarButtonClickEvent('delete')">删除</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-header>
    <el-container  style="padding-top: 0px">
      <el-container>
        <el-main>
          <div class="mytable">
            <vxe-grid
              id="id"
              ref="xGrid"
              border
              resizable
              keep-source
              :align="'center'"
              :height="height"
              :auto-resize="true"
              :columns="tableColumn"
              highlight-current-row
              :data="list"
              :custom-config="{ storage: true }"
              :edit-config="{ trigger: 'click', mode: 'cell', showStatus: true}"
              highlight-hover-row
              :scroll-y="{enabled: true}"
              show-overflow
              show-header-overflow
              @checkbox-all="selectAllEvent"
              @checkbox-change="selectChangeEvent"
            />
          </div>
        </el-main>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
import {

  getPgdBase,

} from '@/api/jhzxgl/pgd'
import {
  getTpBase
} from '@/api/fd/base'
import { mapGetters } from 'vuex'
import XEUtils from 'xe-utils'
import {null2str} from "@/utils/rpkj";
import {
  del,
  getBzryDel,
  getBzryInsert,
  getBzryList,
  getBzryUpdate,
  getryBase,
  yppyzcx,
  yppyzUpdate
} from "@/api/sjzx/bzzGL";
import ProjectSelect from "@/components/ProjectSelect/index.vue";
import {getBFangDwxx} from "@/views/zyjhzx/bfang/comps/api";
export default {
  name:'Yppyz',
  components: {ProjectSelect},
  data() {
    return {
      plParam:{
        qy:'',
        dw:'',
      },
      qyList:[],
      dwList:[],
      queryParam: {
        dcCh:'',
        dcPl:'',
        status:'全部',
      },
      inParam:{
        rqfw:'',
        zt:""
      },
      qyData:[],
      list: [],
      clickList: [],
      checkedList:[],
      projectData:[],
      dictData:{},
      height: '500px',
      tableColumn: [
        { type: 'checkbox', width: 50 ,},
        { type: 'seq', width: 40, title: '序号' },
        // {
        //   field: 'dcCh',
        //   title: '船号',editRender: { name: '$select',options: [], },
        //   width: 120,
        //   filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
        //   filterRender: { name: 'FilterCombination' }
        // },
        {
          field: 'ypph',
          title: '虚拟托盘',editRender: { name: 'input' },
          width: 120,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination' }
        },
        // {
        //   field: 'dcPl',
        //   title: '批量',editRender: { name: 'input' },
        //   width: 120,
        //   filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
        //   filterRender: { name: 'FilterCombination' }
        // },
        // {
        //   field: 'dcFd',editRender: { name: 'input' },
        //   title: '分段',
        //   width: 120,
        //   filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
        //   filterRender: { name: 'FilterCombination' }
        // },
        {
          field: 'kw',
          title: '跨位', formatter: ['dictFormat', 'QGKW'],
          editRender: {
            name: '$select', options: [],
            optionProps: {value: 'bm', label: 'bmsm'},
            props: {clearable: true}
            , events: {change: this.selectChange}
          },
          width: 120,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination' }
        },
        {
          field: 'zl',formatter: ['dictFormat', 'ZLLB'],
          title: '组立',editRender: { name: '$select',options: [], props: {clearable: true} },
          width: 120,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination' }
        },

        {
          field: 'tz',
          title: '特征',
          width: 120, editRender: { name: 'input' },
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination' }
        },
        {
          field: 'lx',
          title: '流向',editRender: { name: 'input' },
          width: 120,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination' }
        },
        {
          field: 'xj',
          title: '下级',editRender: { name: 'input' },
          width: 120,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination' }
        },
        {
          field: 'qy',
          title: '区域',
          width: 120,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination' }
        },
        {
          field: 'dwh',
          title: '垛位号',
          width: 120,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination' }
        },
        // {
        //   field: 'status',
        //   title: '状态',
        //   width: 120,
        //   filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
        //   filterRender: { name: 'FilterCombination' }
        // },
      ]
    }
  },
  computed: {
    ...mapGetters(['name', 'bmbm','apbm'])
  },
  created() {
    window.addEventListener('resize', this.getHeight)
    this.getHeight()
    this.initBase()

  },
  methods: {
    pl(){
      const data=this.$refs.xGrid.getCheckboxRecords()
      if(data.length===0){
        this.$message.warning('请选择数据')
        return
      }
      data.forEach(item=>{
        item.qy=this.plParam.qy
        item.dwh=this.plParam.dw
      })
    },
    initBase(){
      getPgdBase({}).then(res=>{
        this.dictData = res.data
        this.projectData=res.data.cbbm
        this.projectData1=res.data.cbbm
        // const select=this.$refs.xGrid.getColumnByField('dcCh')
        // const chList=[]
        // res.data.cbbm.forEach(item=>{
        //   const data={
        //     label:item.cbbm,
        //     value:item.cbbm
        //   }
        //   chList.push(data)
        // })
        // select.editRender.options=chList
        const sldw = this.$refs.xGrid.getColumnByField('kw')
        sldw.editRender.options = this.dictData.qgkw

        const _zlList=JSON.parse(window.localStorage.getItem("ZLLB"))
        const zlList=[]
        _zlList.forEach(item=>{
          zlList.push({label:item.bmsm,value:item.bm})
        })
        const zl = this.$refs.xGrid.getColumnByField('zl')
        console.log(zlList)
        zl.editRender.options = zlList
      })

      getBFangDwxx().then((res)=>{
        this.qyData=res.data
        const qyList=[]
        Object.keys(this.qyData).forEach(key=>{
          const qy={label:key,value:key};
          qyList.push(qy)
        })
        this.qyList=qyList
      })
    },
    tq(){
      this.queryParam.dcCh=this.$refs.projectSelectRef.getDcch()
    },
    initList() {
      this.queryParam.dcCh=this.$refs.projectSelectRef.getDcch()
      yppyzcx(this.queryParam).then((res) => {
        res.data?.forEach(item=>{
          Object.values(this.qyData).forEach(dwList=>{
            dwList.forEach(dwxx=>{
              if (dwxx.id===item.dwxxId){
                item.qy=dwxx.qy
                item.dwh=dwxx.dwh
              }
            })
          })
        })
        this.list = res.data
      })
    },
    qyChange({value}){
      const dwList=[]
      this.qyData[value]?.forEach(item=>{
        const dw={label:item.dwh,value:item.dwh};
        dwList.push(dw)
      })
      this.dwList=dwList
    },
    selectAllEvent() {
      this.checkedList = this.$refs.xGrid.getCheckboxRecords();
    },
    // 单选
    selectChangeEvent() {
      this.checkedList = this.$refs.xGrid.getCheckboxRecords();
    },
    selectChange(row) {
      this.$refs.xGrid.clearActived() // 清除单元格激活状态
      this.$refs.xGrid.setActiveCell(row.row, row.column.property) // 设置单元格为激活状态
      null2str(row.row)
    },
    getHeight() {
      this.height = window.innerHeight - 190
    },
    toolbarButtonClickEvent(code) {
      const {
        insertRecords,
        removeRecords,
        updateRecords
      } = this.$refs.xGrid.getRecordset()
      switch (code) {
        case 'insert':
          this.$refs.xGrid.insertAt({
          }, -1).then(({ row }) => {
            this.$refs.xGrid.setActiveRow(row)
          })
          break
        case 'delete':
          this.$confirm('删除, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then((type) => {
            this.$refs.xGrid.removeCheckboxRow()
          })
          break
        case 'save':
          if (removeRecords.length !== 0) {
            yppyzUpdate({"remove":removeRecords}).then((response) => {
              if (response.success) {
                for (let i = 0; i < this.list.length; i++) {
                  if (removeRecords.indexOf(this.list[i]) !== -1) {
                    this.list.splice(i, 1)
                    i--
                  }
                }
                this.$message({ message: '保存成功！', type: 'success' })
                const xTable = this.$refs.xGrid
                xTable.loadData(this.initList())

              }
            })
          }
          if (updateRecords.length !== 0) {
            updateRecords.forEach(item=>{
              Object.values(this.qyData).forEach(dwList=>{
                dwList.forEach(dwxx=>{
                  if (dwxx.qy===item.qy&&dwxx.dwh===item.dwh){
                    item.dwxxId=dwxx.id
                    item.status='设置垛位'
                  }
                })
              })
            })
            yppyzUpdate({"edit":updateRecords}).then((response) => {
              if (response.success) {
                const xTable = this.$refs.xGrid
                xTable.loadData(this.initList())
                this.$message({message: '保存成功！', type: 'success'})
              }
            })
          }
          if (insertRecords.length !== 0) {
            insertRecords.forEach(item=>{
              Object.values(this.qyData).forEach(dwList=>{
                dwList.forEach(dwxx=>{
                  if (dwxx.qy===item.qy&&dwxx.dwh===item.dwh){
                    item.dwxxId=dwxx.id
                    item.status='设置垛位'
                  }
                })
              })
            })
            yppyzUpdate({"add":insertRecords}).then((response) => {
              if (response.success) {
                this.list = this.list.concat(insertRecords)
                const xTable = this.$refs.xGrid
                xTable.loadData(this.initList())
                this.$message({ message: '保存成功！', type: 'success' })
              }
            })
          }
          this.$message(
            `新增 ${insertRecords.length} 条，删除 ${removeRecords.length} 条，更新 ${updateRecords.length} 条`
          )
          this.clickList = []
          break
      }
    }
  }
}
</script>
<style lang="scss" scoped >

.el-header{margin:0;padding: 5px;height:auto;
  .el-input{
    width: 140px;
  }
  .el-select{
    width: 140px;
  }

}
.el-container{padding: 0;margin:0;}
.el-main{padding: 0;}
.el-col{padding: 0;height: 32px;}
.el-aside{background: #fff;padding: 0}
.el-container{padding: 0}
.el-row {}
.el-row:last-child {margin-bottom: 0; }

.tableStyles{
  background: #0a76a4;
}

.my-dropdown {
  width: 400px;
  height: 400px;
  background-color: #fff;
  border: 1px solid #dcdfe6;
  box-shadow: 0 0 6px 2px rgba(0, 0, 0, 0.1);
  font-size: 12px;

}
.mytable1{

}
.borderClass{

}
.vxe-input {
  display: inline-block !important;
  position: relative !important;
  width: auto;
}
.el-input {
  display: inline-block !important;
  position: relative !important;
  width: auto;
}
.keyword-lighten {
  color: #000;
  background-color: #FFFF00;
}

.vxe-textarea--inner {
  line-height: inherit;
}
.el-form-item__label-wrap {
  margin-left: 0px !important;
  float: left;
}
.fontClass{
  font-size: 12px;
  font-weight: bold;
}


</style>
