/*
 * @Author: Anmin
 * @Email:changanmin1@crc.com.hk
 * @Date: 2019-03-14 10:52:45
 * @Last Modified by: Anmin
 * @Last Modified time: 2019-05-15 21:24:35
 * @description:  组件:会计科目
 */

<template>
  <div>
    <EMSTableNew
      ref="configTable"
      highlightRow
      :action="action"
      :columnsFn="propsColumns"
      :params="searchForm"
      :rowClickHandle="rowClick"
    >
      <Form
        ref="searchForm"
        slot="search-bar"
        :model="searchForm"
        :label-width="120"
      >
      <Row>
        <Col span="8">
          <FormItem
            prop="accountItemCode"
            label="会计科目编码"
          >
            <Input
              v-model="searchForm.accountItemCode"
              placeholder="请输入"
              type="text"
              clearable
            />
          </FormItem>
        </Col>
        <Col span="8">
          <FormItem
          prop="accountItemName"
          label="会计科目名称"
        >
          <Input
            v-model="searchForm.accountItemName"
            placeholder="请输入"
            type="text"
            clearable
          />
        </FormItem> 
        </Col>
        <Col span="8">
          <FormItem
            prop="childAccountItemCode"
            label="子目编码"
          >
            <Input
              v-model="searchForm.childAccountItemCode"
              placeholder="请输入"
              type="text"
              clearable
            />
          </FormItem>
        </Col>
        </Row>
        <Row>
          <Col span="8">
            <FormItem
              prop="childAccountItemName"
              label="子目名称"
            >
              <Input
                v-model="searchForm.childAccountItemName"
                placeholder="请输入"
                type="text"
                clearable
              />
            </FormItem>
            </Col>
            <Col span="8">
            <FormItem
              prop="buCode"
              label="所属BU"
            >
              <DataSelect
                v-model="searchForm.buCode"
                placeholder="请选择"
                clearable
                transfer
                code="BU"
                :disabled="disableBU"
                @loadFinish="data => DataSetBu = data.valueMap"
              ></DataSelect>
            </FormItem>
            </Col>
        </Row>
      </Form>
      <div slot="handle-bar">
        <Button
          type="warning"
          @click="search()"
        >
          查询
        </Button>
        <Button
          type="default"
          @click.native="reset"
          style="margin-right:0;"
        >
          重置
        </Button>
      </div>
    </EMSTableNew>
  </div>
</template>
<script>
export default {
  name: 'SelectAccounting',
  props: {
    valid: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: '请选择会计科目'
    },
    buCode: {
      type: String,
      default: ''
    },
    disableBU: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // action: '/ems-base-data/account/findPage',
      action: '/ems-base-application/account/findPage',
      DataSetBu: null,
      // 搜索表单的属性
      searchForm: {
        accountItemCode: '',
        accountItemName: '',
        childAccountItemCode: '',
        childAccountItemName: '',
        buCode: '',
        emsIsValidity: "Y",
        valid: "Y"
        // securityEnabled: "1"
      }
    };
  },
  created() {
    this.$emit('SetTitle', this.title);
    this.searchForm.buCode = this.buCode;
    this.valid && (this.searchForm.valid = 'Y');
  },
  mounted() {},
  methods: {
    propsColumns() {
      const self = this;
      return [
        {
          title: '序号',
          type: 'index',
          width: 60,
          resizable: true,
          align: 'left'
        },
        {
          title: '会计科目编码',
          align: 'left',
          key: 'accountItemCode',
          width: 140,
          resizable: true,
          render: (h, params) => {
            const val = params.row.accountItemCode || '';
            return h(
              'div',
              {
                attrs: {
                  class: 'ivu-table-cell ivu-table-cell-ellipsis',
                  title: val
                }
              },
              val
            );
          }
        },
        {
          title: '会计科目名称',
          align: 'left',
          key: 'accountItemName',
          width: 300,
          resizable: true,
          render: (h, params) => {
            const val = params.row.accountItemName || '';
            return h(
              'div',
              {
                attrs: {
                  class: 'ivu-table-cell ivu-table-cell-ellipsis',
                  title: val
                }
              },
              val
            );
          }
        },
        {
          title: '子目编码',
          align: 'left',
          key: 'childAccountItemCode',
          width: 100,
          resizable: true,
          render: (h, params) => {
            const val = params.row.childAccountItemCode || '';
            return h(
              'div',
              {
                attrs: {
                  class: 'ivu-table-cell ivu-table-cell-ellipsis',
                  title: val
                }
              },
              val
            );
          }
        },
        {
          title: '子目名称',
          align: 'left',
          key: 'childAccountItemName',
          width: 160,
          resizable: true,
          render: (h, params) => {
            const val = params.row.childAccountItemName || '';
            return h(
              'div',
              {
                attrs: {
                  class: 'ivu-table-cell ivu-table-cell-ellipsis',
                  title: val
                }
              },
              val
            );
          }
        },
        {
          title: '所属BU',
          align: 'left',
          key: 'buCode',
          minWidth: 150,
          render: (h, params) => {
            const val = params.row.buCode && self.DataSetBu ? self.DataSetBu[params.row.buCode] : '';
            return h(
              'div',
              {
                attrs: {
                  class: 'ivu-table-cell ivu-table-cell-ellipsis',
                  title: val
                }
              },
              val
            );
          }
        }
      ];
    },
    // 点击表格行 触发该事件
    onSelectRow(selected = []) {
      if (selected.length == 0) {
        return;
      }
      if (selected.length > 1) {
        this.$Message.warning({
          content: '只能选择一条数据',
          duration: 5
        });
        return;
      }
      this.$emit('Close', selected[0]);
    },
    search(page = 1) {
      // 搜索数据
      this.$refs.configTable.load(this.searchForm, page);
    },
    reset() {
      this.$refs.searchForm.resetFields();
      this.search(1);
    },
    rowClick(selection) {
      if (selection) {
        this.$emit('Close', selection);
      }
    }
  }
};
</script>
<style lang="less" scoped>
.emstable-wrap-new /deep/ .search-form form {
  height: auto !important;
}
.emstable-wrap-new /deep/  .handle-bar-solt {
  top: 57px;
  right: 0;
}
</style>

