<!--
@name: 承运商管理-基础费用
@author: 张泽贤
@time: 2022-08-08
@doc:
-->
<template>
  <p-card :loading="loading">
    <p-transfer
      :data-source="mockData"
      :target-keys="targetKeys"
      :show-search="showSearch"
      :filter-option="(inputValue, item) => item.expenseCode.indexOf(inputValue) !== -1"
      :titles="['待分配费用', '已分配费用']"
      :show-select-all="false"
      @change="onChange"
    >
      <template slot="children" slot-scope="{ props: { direction, filteredItems, selectedKeys, disabled: listDisabled }, on: { itemSelectAll, itemSelect } }">
        <p-table
          :row-selection="getRowSelection({ disabled: listDisabled, selectedKeys, itemSelectAll, itemSelect })"
          :columns="direction === 'left' ? leftColumns : rightColumns"
          row-key="expenseCode"
          :data-source="filteredItems"
          size="small"
          :style="{ pointerEvents: listDisabled ? 'none' : null }"
          :custom-row="
            ({ key, disabled: itemDisabled }) => ({
              on: {
                click: () => {
                  if (itemDisabled || listDisabled) return;
                  itemSelect(key, !selectedKeys.includes(key));
                },
              },
            })
          "
        />
      </template>
    </p-transfer>
  </p-card>
</template>

<script>
import { costList, feeSave } from '../js/api';

const tableColumns = [
  {
    dataIndex: 'expenseCode',
    title: '费用编码',
    ellipsis: true,
  },
  {
    dataIndex: 'expenseName',
    title: '费用名称',
    ellipsis: true,
  },
  {
    dataIndex: 'expenseAccountName',
    title: '费用科目',
    ellipsis: true,
  },
];

export default {
  props: {
    editCode: {
      type: String,
      default: '',
    },
  },
  // watch: {
  //   editCode(nVal, oVal) {
  //     if (nVal) {
  //       this.loading = !this.loading;
  //       this.mockData = [...this.costList];
  //       costList({ code: nVal })
  //         .then((res) => {
  //           this.targetKeys = res.data?.map((item) => item.expenseCode) ?? [];
  //         })
  //         .finally(() => {
  //           this.loading = !this.loading;
  //         });
  //     } else if (!nVal) {
  //       this.mockData = [];
  //       this.targetKeys = [];
  //     }
  //   },
  // },
  data() {
    return {
      loading: false,

      mockData: [],
      targetKeys: [],
      disabled: false,
      showSearch: false,
      leftColumns: tableColumns,
      rightColumns: tableColumns,

      costList: [],
    };
  },
  mounted() {
    costList({ code: '', dataType: 2 }).then((res) => {
      this.costList =
        res.data?.map((item) => {
          item.key = item.expenseCode;
          return item;
        }) ?? [];
    });
  },
  methods: {
    onChange(nextTargetKeys) {
      let request = {
        code: this.editCode,
        list: [],
      };
      this.costList.forEach((item) => {
        if (nextTargetKeys.includes(item.key)) {
          request.list.push({
            expenseAccountCode: item.expenseAccountCode,
            expenseAccountName: item.expenseAccountName,
            expenseCode: item.expenseCode,
            expenseName: item.expenseName,
            dataType: 2,
          });
        }
      });
      feeSave(request).then((res) => {
        if (res.success) {
          this.targetKeys = nextTargetKeys;
        } else {
          this.$exception(res);
        }
      });
    },

    differenceFun(array, array1) {
      let newArray = [];
      array.map((item, index) => {
        if (!array1.includes(item)) {
          newArray.push(item);
        }
      });
      return newArray;
    },
    getRowSelection({ disabled, selectedKeys, itemSelectAll, itemSelect }) {
      let that = this;
      return {
        getCheckboxProps: (item) => ({ props: { disabled: disabled || item.disabled } }),
        onSelectAll(selected, selectedRows) {
          const treeSelectedKeys = selectedRows.filter((item) => !item.disabled).map(({ key }) => key);
          const diffKeys = selected ? that.differenceFun(treeSelectedKeys, selectedKeys) : that.differenceFun(selectedKeys, treeSelectedKeys);
          itemSelectAll(diffKeys, selected);
        },
        onSelect({ key }, selected) {
          itemSelect(key, selected);
        },
        selectedRowKeys: selectedKeys,
      };
    },

    onSearch(nVal) {
      if (nVal) {
        this.loading = !this.loading;
        this.mockData = [...this.costList];
        costList({ code: nVal, dataType: 2 })
          .then((res) => {
            this.targetKeys = res.data?.map((item) => item.expenseCode) ?? [];
          })
          .finally(() => {
            this.loading = !this.loading;
          });
      } else if (!nVal) {
        this.mockData = [];
        this.targetKeys = [];
      }
    },
  },
};
</script>