/*
 * @Author: Anmin
 * @Email:changanmin1@crc.com.hk
 * @Date: 2019-03-14 11:35:45
 * @Last Modified by: Anmin
 * @Last Modified time: 2019-06-27 17:31:17
 * @description:  选择交易参考
 */

<template>
  <EMSTable
    ref="searchForm"
    :server="this"
    action="findTransInfoByAccount"
    :params="searchForm"
    :columnsFn="columns"
    :highlight-row="true"
    :onSelectionChange="onSelectionChange"
  >
    <Form
      ref="searchForm"
      slot="search-bar"
      :model="searchForm"
    >
      <FormItem
        prop="lookupCode"
        label="coa编码"
      >
        <Input
          v-model="searchForm.lookupCode"
          placeholder="请输入"
          type="text"
        ></Input>
      </FormItem>
      <FormItem
        prop="meaning"
        label="coa名称"
      >
        <Input
          v-model="searchForm.meaning"
          placeholder="请输入"
          type="text"
        ></Input>
      </FormItem>
    </Form>
    <div slot="handle-bar">
      <Button
        type="warning"
        icon="search"
        @click.native="search"
      >
        搜索
      </Button>
      <Button
        type="info"
        icon="loop"
        @click.native="reset"
      >
        重置
      </Button>
    </div>
  </EMSTable>
</template>
<script>
// // import { EMSTable } from "ems-plugin";
export default {
  name: 'SelectTransactionReference',
  directives: {},
  // components: { EMSTable },
  props: {
    title: {
      type: String,
      default() {
        return '请选择交易参考值';
      }
    },
    coaCodeType: {
      type: String,
      default() {
        return '';
      }
    },
    ouCode: {
      type: String,
      default: ''
    },
    buCode: {
      type: String,
      default: ''
    },
    accountItemCode: {
      type: String,
      default: ''
    },
    credentialEntryDate: {
      type: Date,
      default: ''
    }
  },
  data() {
    return {
      searchForm: {
        lookupCode: '',
        meaning: '',
        ouCode: this.ouCode,
        buCode: this.buCode,
        enabledFlag: 'Y'
      }
    };
  },
  computed: {},
  watch: {},
  created: function() {
    if (!this.accountItemCode) {
      this.$Message.warning({
        content: '请选择会计科目后再选择交易参考',
        duration: 5
      });
      this.$emit('Close');
      return;
    }
    this.$emit('SetTitle', this.title);
    this.$emit('SetPageSize', window.innerWidth * 0.85);
  },
  beforeMount: function() {},
  mounted: function() {},
  beforeDestroy: function() {},
  destroyed: function() {},
  methods: {
    columns() {
      return [
        {
          type: 'selection',
          width: 40,
          align: 'center'
        },
        {
          title: '参考代码',
          align: 'center',
          key: 'lookupCode',
          minWidth: 150,
          render: (h, params) => {
            return h(
              'div',
              {
                attrs: {
                  class: 'ivu-table-cell ivu-table-cell-ellipsis',
                  title: params.row.lookupCode
                }
              },
              params.row.lookupCode
            );
          }
        },
        {
          title: '参考值',
          align: 'center',
          key: 'meaning',
          minWidth: 150,
          render: (h, params) => {
            return h(
              'div',
              {
                attrs: {
                  class: 'ivu-table-cell ivu-table-cell-ellipsis',
                  title: params.row.meaning
                }
              },
              params.row.meaning
            );
          }
        },
        {
          title: '有效性',
          align: 'center',
          key: 'enabledFlag',
          width: 90,
          render: (h, params) => {
            return h(
              'Tag',
              {
                props: {
                  type: 'dot',
                  color: params.row.enabledFlag == 'Y' ? 'blue' : 'red'
                }
              },
              params.row.enabledFlag == 'Y' ? '有效' : '无效'
            );
          }
        }
      ];
    },
    search(page = 1) {
      this.$refs.searchForm.load(this.searchForm, page);
    },
    reset() {
      this.searchForm.lookupCode = '';
      this.searchForm.meaning = '';
      this.search(1);
    },

    // 点击数据行
    onSelectionChange(selected) {
      if (selected.length == 0) {
        return;
      }
      if (selected.length > 1) {
        this.$Message.warning({
          content: '只能选择一条数据',
          duration: 5
        });
        return;
      }
      this.$emit('Close', selected[0]);
    },
    // 交易参考       /coa/findTransInfoByAccount

    /**
     * 交易参考字段
     * http://10.0.63.12:8088/browse/EMSHQ-793
     */
    findTransInfoByAccount(param = {}) {
      param.params.accountCode = this.accountItemCode;
      return new Promise(resolve => {
        $axios
          .post(`/ems-base-application/coa/findTransInfoByAccount`, param)
          .then(response => {
            resolve(response);
          })
          .catch(error => {
            const responseData = {
              dataResult: [],
              extra: '',
              msg: error.response.data.message || '服务异常，请联系管理员',
              statusCode: '-1'
            };
            resolve(responseData);
          });
      });
    }
  }
};
</script>
