<template>
  <div class="mytable">
    <el-row style="width: 100%;text-align: right;margin-bottom: 3px">
    <el-button size="small"  type="primary" @click="getList" >查询</el-button>
    <el-button size="small"  type="success" @click="add" >新增</el-button>
    <el-button size="small"  type="danger" @click="remove" >删除</el-button>
    <el-button size="small"  type="success" @click="saveSbcn" >保存</el-button>
    </el-row>
    <el-row>
    <vxe-grid
      id="sbcnId"
      ref="xGridSbcn"
      border
      resizable
      keep-source
      :align="'center'"
      :height="height"
      :auto-resize="true"
      :columns="sbcnColumn"
      highlight-current-row
      :data="sbcnList"
      :custom-config="{storage: true }"
      :scroll-x="{enabled: true}"
      :scroll-y="{enabled: true}"
      :edit-config="{
                trigger: 'click',
                mode: 'row',
                showStatus: true,
              }"
      highlight-hover-row
      show-overflow
      show-header-overflow
    >
    </vxe-grid>
    </el-row>
  </div>
</template>

<script>
import {getSbcnAll, saveSbcn, removeSbcn, getGxDict} from '@/api/jhzx/pc'
export default {
  name:'Sbcn',
  props:{
    height:{
      type:Number,
      default:700,
    }
  },
  data(){
    return {
      sbcnList:[],
      gxList:[],
      sbcnColumn:[
        { type: 'checkbox', width: 50 },
        {type: 'seq', width: 60, title: '序号', },
        { field: '跨位', title: '所属跨位', width: 100,
          editRender: {  name: '$select', options:[
              {label:'1垮',value:'1垮'},
              {label:'2垮',value:'2垮'},
              {label:'3垮',value:'3垮'},
              {label:'4垮',value:'4垮'},
              {label:'5垮',value:'5垮'},
              {label:'6垮',value:'6垮'},
              {label:'光电间',value:'光电间'},
            ], props: {clearable: true}},
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination', },
        },
        { field: '_工序', title: '工序', width: 100,
          editRender: {  name: '$select', options:[], props: {clearable: true}},
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination', },
        },
        { field: 'code', title: '编码', width: 150,
          editRender: { name: "input" },
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination', },
        },
        { field: '设备名称', title: '设备名称', width: 150,
          editRender: { name: "input" },
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination', },
        },
        { field: '套料图编码', title: '套料图编码', width: 150,
          editRender: { name: "input" },
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination', },
        },
        /*{ field: '单台效率', title: '单台效率', width: 100,
          editRender: { name: "input" },
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination', },
        },
        { field: '单台产能', title: '单台产能', width: 100,
          editRender: { name: "input" },
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination', },
        },*/
        { field: '白班设备产能', title: '白班设备产能', width: 150,
          editRender: { name: "input" },
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination', },
        },
        { field: '二班设备产能', title: '二班设备产能', width: 150,
          editRender: { name: "input" },
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination', },
        },
        { field: '双班合计产能', title: '双班合计产能', width: 150,
          editRender: { name: "input" },
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination', },
        },
        { field: '禁用', title: '禁用',
          editRender: {  name: '$select', options:[{ label: '是', value:true}, { label: '否', value:false}], props: {clearable: true}},
          formatter:this.formatterJy,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination', },
        },
      ],
    }
  },
  created() {
    getGxDict().then((res)=>{
      res.data.forEach(item=>{
        // this.gxList.push({label:item.name,value:item.code})
        this.gxList.push({label:item.name,value:item.name})
      })
      const xGridSbcn = this.$refs.xGridSbcn
      const gx = xGridSbcn.getColumnByField('_工序')
      gx.editRender.options = this.gxList
    })
    this.getList()
  },
  methods:{
    getList(){
      getSbcnAll().then((res) => {
        const sbcnList=[]
        this.gxList.forEach(gx=>{
          res.data.forEach(item=>{
            if (gx.value===item._工序){
              sbcnList.push(item)
            }
          })
        })
        this.sbcnList=sbcnList.sort((a,b)=>{
          if (a.跨位 < b.跨位) {
            return -1;
          }
          if (a.跨位 > b.跨位) {
            return 1;
          }
          return 0;
        })
      })
    },
    add(){
      this.$refs.xGridSbcn.insertAt({},-1)
    },
    remove(){
      this.$refs.xGridSbcn.removeCheckboxRow()
    },
    async saveSbcn(){
      const {insertRecords, removeRecords, updateRecords} = this.$refs.xGridSbcn.getRecordset()
      if (insertRecords.length !== 0 ) {
        await saveSbcn(insertRecords).then(res => {
          this.getList()
        })
      }
      if (updateRecords.length !== 0) {
        await saveSbcn(updateRecords).then(res => {
          this.getList()
        })
      }
      if (removeRecords.length !== 0) {
        await removeSbcn(removeRecords).then(res => {
          this.getList()
        })
      }
    },
    //转码
    formatterJy({cellValue}){
      if(cellValue){
        return '是'
      }
      if(!cellValue){
        return '否'
      }
      return cellValue
    },

  }
}
</script>
