<template>
  <div>
      <div style="height: 280px;overflow-y: auto;overflow-x: hidden">
        <PageTable ref="dataTable"  :columns="columns" :resource="resource" :isPage="false">
        </PageTable>
      </div>
  </div>
</template>
<script>
export default{
  name: 'AssignListDimension',
  components:{

  },
  props:{
    objectId:{
      type: String,
      default(){
        return '';
      }
    },
    dimensionWdCode:{
      type: String,
      default(){
        return '';
      }
    }
  },
  data(){
    return{
      selectedIds: [],
      resource: [],
      ids: [],
      formSearch: {
        objectId: this.objectId, 
        dimensionWdCode: this.dimensionWdCode,
      },
      columns: [
        {
          type: "selection",
          width: 55,
          fixed: 'left',
          align: "center"
        },
        {
          title: this.$t("module.dimension.name"),
          key: "objId",
          // sortable: true,
          // width: 500,
          render: (h, params) => {
            return h('div', {
              attrs: {
                class: 'ivu-table-cell ivu-table-cell-ellipsis',
                title: params.row.objValue
              }
            }, params.row.objValue);
          }
        },
      ],
    }
  },
  computed:{

  },
  created:function(){
    this.init();
  },
  beforeMount:function(){

  },
  mounted:function(){
    this.search();
  },
  beforeDestroy:function(){

  },
  destroyed:function(){

  },
  methods:{
    search(page) {
      this.$refs["dataTable"].load(this.formSearch, page);
    },
    init(){
      $store.dispatch("QueryOrgDimensionInfo", this.formSearch).then((response) => {
        if(response){
          this.resource = response.datas.recordList;
          this.$refs['dataTable'].refresh();
  
          this.selectedIds = response.defaultValue;
          for (const keyId in this.selectedIds) {
            
            for(const keyObj in response.datas.recordList){
              if(this.selectedIds[keyId] == response.datas.recordList[keyObj].objId){
                this.$set(response.datas.recordList[keyObj], '_checked', true)
              };
            }
          }
        }else{
          this.$Message.warning({
            content: this.$t("module.dimension.warning.getDataFailed"),
            duration: 3
          })
        }
      })  
    },
    onOk() {
      let sectedIds = this.getId();
      if(sectedIds.length != this.selectedIds.length){
        $store.dispatch("UpdateOrgDimension", {dimensionWdCode:this.dimensionWdCode, objectId: this.objectId, dimensionValueList: sectedIds}).then(res => {
              if (res.statusCode == "0") {
                this.$Message.success({
                  content: this.$t("module.dimension.warning.createSuccessfully"),
                  duration: 3
                });
              } else {
                this.$Message.warning({
                  content: res.msg,
                  duration: 3
                });
                return;
              }
            });
      }else{
        return ;
      }
    },
    getId() {
      let list = this.$refs["dataTable"].getSelectioned() || [];
      console.log(list)
      for (let item of list) {
        this.ids.push(item.objId);
      }
      return this.ids;
    }
  },
  watch:{

  },
  directives:{

  }
}
</script>
