/*
 * @Author: Anmin
 * @Email:changanmin1@crc.com.hk
 * @Date: 2019-03-14 16:40:19
 * @Last Modified by: Anmin
 * @Last Modified time: 2019-03-14 17:31:53
 * @description: 选择成本中心
 */
<template>
  <div>
    <EMSTableNew
      ref="companyTable"
      :action="action"
      :params="searchForm"
      :columnsFn="codeColumns"
      :highlight-row="true"
      :rowClickHandle="rowClick"
      :onSelectionChange="onSelectionChange"
    >
      <Form
        ref="companyForm"
        slot="search-bar"
        :model="searchForm"
        :labelWidth="90"
        style="height: auto !important;"
      >
        <Row>
          <template v-if="moreSearchItem">
            <Col span="8">
            <Form-item
              prop="buCode"
              label="BU"
            >
              <Input
                v-model="searchForm.buCode"
                :disabled="disabledMore"
                placeholder="请输入"
                size="small"
              ></Input>
            </Form-item>
            </Col>
            <Col span="8">
            <Form-item
              prop="businessLineName"
              label="业务线名称"
            >
              <Input
                v-model="searchForm.businessLineName"
                :disabled="disabledMore && !!config.businessLineName"
                placeholder="请输入"
                size="small"
              ></Input>
            </Form-item>
            </Col>
          </template>
          <Col span="8">
          <Form-item
            prop="companyName"
            label="公司名称"
          >
            <Input
              v-model="searchForm.companyName"
              type="text"
              placeholder="请输入"
              size="small"
            ></Input>
          </Form-item>
          </Col>
          <Col span="24">
          <div
            class="search_btns ivu-form-item"
          >
            <Button
              type="warning"
              @click="search"
            >
              查询
            </Button>
            <Button
              @click="reset"
            >
              重置
            </Button>
          </div>
          </Col>
        </Row>
      </Form>
    </EMSTableNew>
  </div>
</template>
<script>
import EMSTableNew from '../emsTableNew/EMSTable.vue';
export default {
  name: 'SelectCostCenter',
  components: {
    EMSTableNew
  },
  directives: {},
  props: {
    title: {
      type: String,
      default() {
        return '请选择公司';
      }
    },
    moreSearchItem: {
      type: Boolean,
      default: false
    },
    disabledMore: {
      type: Boolean,
      default: false
    },
    config: {
      type: Object,
      default: () => {
        return {};
      }
    },
    isMultiple: { // 是否多选
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      action: '/ems-base-application/mdOu/findPage',
      query: 'bu',
      flag: false,
      dataList: [],
      searchForm: {
        companyName: '',
        buCode: '',
        businessLineName: ''
      }
    };
  },

  created: function() {
    this.$emit('SetTitle', this.title);
    // 插入查询条件
    if (this.moreSearchItem) {
      for (const k in this.config) {
        this.searchForm[k] = this.config[k];
      }
    }

    if (this.isMultiple) {
      this.$emit('SetPageActions', [

        {
          text: '确定',
          theme: 'warning',
          action: 'save',
          class: 'ems-btn-medium ems-btn-type1',
          handle: () => {
            this.save();
          }
        }, {
          text: '关闭',
          theme: 'default',
          class: 'ems-btn-medium ems-btn-type3',
          handle: () => {
            this.$emit('Close');
          }
        }
      ]);
    }
  },
  methods: {
    codeColumns() {
      let columns = [
        {
          title: 'BU',
          align: 'left',
          width: 300,
          key: 'buCode',
          resizable: true
        },
        {
          title: '业务线名称',
          align: 'left',
          width: 300,
          key: 'businessLineName',
          resizable: true
        },
        {
          title: '公司名称',
          key: 'ouName',
          align: 'left'
        }
      ]
      this.isMultiple ? columns.unshift({
        type: 'selection',
        width: 50,
        resizable: true,
        align: 'center'}) : columns;
      return columns;
    },
    search(page) {
      this.$refs.companyTable.load(this.searchForm, page);
    },
    reset() {
      this.searchForm.companyName = '';
      this.$refs.companyForm.resetFields();
      this.search(1);
    },
    // 点击数据行
    rowClick(selected) {
      // if (selected.length == 0) {
      //   return;
      // }
      // if (selected.length > 1) {
      //   this.$Message.warning({
      //     content: '只能选择一条数据',
      //     duration: 5
      //   });
      //   return;
      // }
      if (this.isMultiple) return;
      this.$emit('Close', selected);
    },

    // 选中tableList
    onSelectionChange(data) {
      this.tableSelect = [];
      if (data.length > 0) {
        data.map(item => {
          this.tableSelect.push(item);
        });
      }
    },

    // 确定
    save() {
      this.$emit('Close', this.tableSelect);
    }
  }
};
</script>
