<template>
  <div>
    <PageTable ref="dimensionTable" action="QueryDimensionList" :columnsFn="columns">
      <Form :model="formSearch" ref="dimensionFormSearch" :rules="rule" slot="search-bar" label-position="top" class="ivu-form-no-margin-bottom" inline>
        <Form-item prop="wdCode" :label="$t('module.dimension.dimensionCode')">
          <Input type="text" maxlength="30" v-model="formSearch.wdCode" :placeholder="$t('module.dimension.warning.enterCode')" size="small"></Input>
        </Form-item>
        <Form-item prop="wdName" :label="$t('module.dimension.dimensionName')">
          <Input type="text" maxlength="30" v-model="formSearch.wdName" :placeholder="$t('module.dimension.warning.enterName')" size="small"></Input>
        </Form-item>
      </Form>
      <div slot="handle-bar">
        <Button size="small" @click.native="search" type="warning" icon="ios-search">{{$t('common.search')}}</Button>
        <Button size="small" @click.native="reset" type="info" icon="md-sync">{{$t('common.reset')}}</Button>
      </div>
      <div slot="table-bar">
        <OptionRight >
            <Button type="ghost" icon="plus-round" size="small" @click="addOrUpdateDimension">{{$t('common.create')}}</Button>
        </OptionRight>
        <OptionRight >
            <Button type="ghost" icon="trash-a" size="small" @click="deleteDimension">{{$t('common.delete')}}</Button>
        </OptionRight>
      </div>
    </PageTable>
    

    <!-- 维度编辑窗口 -->
    <!-- <Modal v-model="editModal" width="650">
      <p slot="header">
        <span>编辑维度</span>
      </p>
      <div class="run-modal-style">
        <Row :gutter="20" class="code-row-bg">
          <Col span="24" style="border:none;">
          <Form :model="formInfo" ref="formDimension" class="ivu-form-no-margin-bottom" :rules="rule" :label-width="90">
            <Form-item prop="wdCode" label="维度编码">
              <Input placeholder="" size="small" v-model="formInfo.wdCode" maxlength="30" style="width:90%" disabled></Input>
            </Form-item>
            <Form-item prop="wdName" label="维度名称">
              <Input placeholder="" size="small" v-model="formInfo.wdName" maxlength="50" style="width:90%" disabled></Input>
            </Form-item>
            <Form-item prop="wdDataUrl" label="数据配置">
              <Input placeholder="" size="small" v-model="formInfo.wdDataUrl" maxlength="256" style="width:90%"></Input>
            </Form-item>
            <Form-item label="描述">
              <Input placeholder="" type="textarea" :rows="4" size="small" v-model="formInfo.wdDesc" maxlength="300" style="width:90%"></Input>
            </Form-item>
          </Form>
          </Col>
        </Row>
      </div>
      <div slot="footer" class="run-modal-ftbButton">
        <Button type="primary" :loading="saveLoading" @click="save()">确定</Button>
      </div>
    </Modal> -->
  </div>
</template>
<script>
import AddOrUpdateDimension from './AddOrUpdateDimension.vue'
  export default{
    components:{

    },
    data(){
      return{
        editModal: false,
        formInfo: {
          dimensionId: "",
          wdCode: "",
          wdName: "",
          wdDataUrl: "",
          wdDesc: ""
        },
       
        formSearch: {
          wdCode: "",
          wdName: ""
        },
        rule: {
          wdDataUrl: [{ required: true, message: this.$t('module.dimension.warning.enterConfig'), trigger: "blur" }]
        },
      }
    },
    computed:{

    },
    created:function(){

    },
    beforeMount:function(){

    },
    mounted:function(){

    },
    beforeDestroy:function(){

    },
    destroyed:function(){

    },
    methods:{
      columns() {
         return [
          {
            type: "selection",
            width: 55,
            fixed: 'left',
            align: "center"
          },
          {
            title: this.$t('common.operating'),
            align: "center",
            width: 65,
            render: (h, params) => {
              return h("RowTools", {
                props: {
                  data: [
                    {
                      type: "button",
                      text: this.$t('common.edit'),
                      handle: () => {
                        this.addOrUpdateDimension(params.row);
                        this.modal = true;
                      }
                    }
                  ]
                }
              });
            }
          },
          {
            title: this.$t('module.dimension.dimensionCode'),
            key: "wdCode",
            width: 170,
            align: "center",
            render: (h, params) => {
              return h(
                "span",
                {
                  attrs: {
                    title: params.row.wdCode
                  }
                },
                params.row.wdCode
              );
            }
          },
          {
            title: this.$t('module.dimension.dimensionName'),
            key: "wdName",
            align: "center",
            width: 170,
            render: (h, params) => {
              return h(
                "span",
                {
                  attrs: {
                    title: params.row.wdName
                  }
                },
                params.row.wdName
              );
            }
          },
          {
            title: this.$t('module.dimension.dataConfig'),
            key: "wdDataUrl",
            align: "center",
            width: 400,
            render: (h, params) => {
              return h(
                "span",
                {
                  attrs: {
                    title: params.row.wdDataUrl
                  }
                },
                params.row.wdDataUrl
              );
            }
          },
          {
            title: this.$t('module.dimension.desc'),
            key: "wdDesc",
            align: "center",
            ellipsis: true,
            render: (h, params) => {
              return h(
                "span",
                {
                  attrs: {
                    title: params.row.wdDesc
                  }
                },
                params.row.wdDesc
              );
            }
          }
        ]
      },
      edit(dimensionId) {
        this.editModal = true;
        this.$store.dispatch("QueryDimensionDetail", { dimensionId }).then(response => {
          this.formInfo = {
            dimensionId: response.dimensionId,
            wdCode: response.wdCode,
            wdName: response.wdName,
            wdDataUrl: response.wdDataUrl,
            wdDesc: response.wdDesc
          };
        });
      },
      save(){
        let param = {
          dimensionId: this.formInfo.dimensionId,
          wdCode: this.formInfo.wdCode,
          wdName: this.formInfo.wdName,
          wdDataUrl: this.formInfo.wdDataUrl,
          wdDesc: this.formInfo.wdDesc
        };
        this.saveLoading = true;
        this.$store.dispatch("UpdateDimension", param).then(response => {
          this.saveLoading = false;
          if (response.statusCode == "0") {
            this.editModal = false;
            this.$Message.success({
              content: this.$t('module.dimension.warning.updateSuccessfully'),
              duration: 3
            });
            this.search();
          } else {
            this.$Message.warning({
              content: response.msg,
              duration: 3
            });
          }
        });
      },
      search(page) {
        this.$refs["dimensionTable"].load(this.formSearch, page);
      },
      reset(page) {
        this.$refs["dimensionFormSearch"].resetFields();
        this.search(1);
      },
      addOrUpdateDimension(obj){
        new this.$pageModal(
          AddOrUpdateDimension,
          {
            props:{
              dimensionId: obj.dimensionId
            }
          },(data) => {
            this.search();
          }
        )
      },
      deleteDimension(){
        let deleteStr = this.getIdAndVersion();
          if (deleteStr == "") {
              this.$Message.warning({
              content: this.$t('module.dimension.warning.selectDimension'),
              duration: 3
              });
              return;
          }
          new $confirm(`${this.$t('common.confirmDeletion')}`, this).then(() => {
              this.$store.dispatch("DeleteDimension", this.getIdAndVersion()).then(res => {
              if (res.statusCode == "0") {
                  this.$Message.success({
                      content: this.$t('module.dimension.warning.deleteSuccessfully'),
                      duration: 3
                  })
                  this.search();
              }else{
                  this.$Message.warning({
                  content: res.msg,
                  duration: 3
                  });
                  return;
                  }
              });
          });
      },
      getIdAndVersion(){
        let list = this.$refs["dimensionTable"].getSelectioned() || [];
          let ids = [];
          for (let dimension of list) {
              ids.push({"dimensionId": dimension.dimensionId, "wdCode": dimension.wdCode, "version": dimension.version });
          }
          return ids;
      }
    },
    watch:{

    },
    directives:{

    }
  }
</script>
