<!--
@name: 承运物流价格-甲方弹窗
@author: 张泽贤
@time: 2023-02-24
@doc:
-->
<template>
  <div>
    <p-input v-model="inputValue" readOnly @click="onClick" :disabled="disabled" />

    <p-modal title="甲方" width="60%" :visible="visible" :confirm-loading="confirmLoading" @ok="handleOk" @cancel="visible = false">
      <p-tabs :animated="false" :activeKey="activeKey" @tabClick="activeKey = $event">
        <p-tab-pane key="1" tab="货主" :forceRender="true">
          <p-form-model :model="searchForm">
            <a-row>
              <a-col :span="8">
                <p-form-model-item label="货主编码" :label-col="{ span: 8 }" :wrapper-col="{ span: 16 }">
                  <p-input v-model="searchForm.cargoOwnerCode" />
                </p-form-model-item>
              </a-col>
              <a-col :span="8">
                <p-form-model-item label="货主名称" :label-col="{ span: 8 }" :wrapper-col="{ span: 16 }">
                  <p-input v-model="searchForm.cargoOwnerName" />
                </p-form-model-item>
              </a-col>
              <a-col :span="8">
                <p-form-model-item :wrapper-col="{ span: 24 }" style="text-align: right">
                  <a-space>
                    <p-button type="primary" @clickSync="onSearch" icon="search">查询</p-button>
                    <p-button @click="onReset">重置</p-button>
                  </a-space>
                </p-form-model-item>
              </a-col>
            </a-row>
          </p-form-model>
          <t-table
            dragle
            bordered
            row-key="cargoOwnerId"
            :data-source.sync="dataSource"
            :remote-url="listRemoteUrl"
            :remoteData="listParams"
            :rowSelectType="1"
            :selectedRowKeys.sync="selectedRowKeys"
          >
            <t-table-column data-index="cargoOwnerCode" title="货主编号" />
            <t-table-column data-index="cargoOwnerName" title="货主名称" />
            <t-table-column data-index="cargoOwnerShortName" title="货主简称" />
          </t-table>
        </p-tab-pane>
        <!-- <p-tab-pane key="2" tab="部门" :forceRender="true">
          <p-tree v-model="departmentValue" checkable :tree-data="treeData" :replaceFields="{ key: 'code', title: 'name', value: 'id' }" defaultExpandAll @check="departmenTreeChange" />
        </p-tab-pane> -->
      </p-tabs>
    </p-modal>
  </div>
</template>

<script>
import { consigner_listRemoteUrl, organization_all } from '../js/api.js';

export default {
  props: {
    partyA: {
      type: Object,
      default: () => ({}),
    },
    disabled: {
      type: Boolean,
      default: false,
    },
  },
  watch: {
    partyA(val) {
      this.inputValue = val.PartyName.join(',');
      if (val.PartyType === 'C') {
        this.activeKey = '1';
        this.selectedRowKeys = val.PartyId;
        this.treeDefaultExpandedKeys = [];
      } else if (val.PartyType === 'D') {
        this.activeKey = '2';
        this.selectedRowKeys = [];
        this.treeDefaultExpandedKeys = val.PartyCode;
      } else {
        this.activeKey = '1';
        this.selectedRowKeys = [];
        this.treeDefaultExpandedKeys = [];
      }
    },
  },
  data() {
    return {
      visible: false,
      confirmLoading: false,
      activeKey: '1',

      inputValue: '',

      // 货主
      searchForm: {
        cargoOwnerName: '',
        cargoOwnerCode: '',
      },
      dataSource: [],
      listRemoteUrl: consigner_listRemoteUrl,
      listParams: {},
      selectedRowKeys: [],

      // 部门
      treeData: [],
      treeDefaultExpandedKeys: [],
      departmentValue: [],
      DEPARTMENT: [],
    };
  },
  created() {
    this.organization_all();
  },
  methods: {
    onClick() {
      this.visible = true;
    },

    // 货主
    onSearch() {
      this.listParams = { ...this.searchForm };
    },
    onReset() {
      this.searchForm = {
        cargoOwnerName: '',
        cargoOwnerCode: '',
      };
    },

    // 获取部门
    organization_all() {
      organization_all().then((res) => {
        if (res.success) {
          this.DEPARTMENT = res.data;
          this.treeData = this.resultTreeNodes(this.DEPARTMENT);
          let item = this.treeData.find((item) => item.parentId == '0');
          this.treeDefaultExpandedKeys = item?[item.id]:[];
          this.departmentValue = this.departmentId
            ? this.departmentId.split(',').map((item) => {
                return { value: item };
              })
            : [];
        } else {
          this.$exception(res);
        }
      });
    },
    resultTreeNodes(data, map = {}) {
      if (!data) return [];
      data.forEach((item) => {
        delete item.children;
        item.scopedSlots = { icon: 'custom' };
        item.isLeaf = !!item.isLeaf;

        map[item.id] = item;
      });
      let value = [];

      data.forEach((item) => {
        var p = map[item.parentId];
        if (p) {
          if (!p.children) {
            p.children = [];
          }
          //item.isLeaf = true
          p.children.push(item);
        } else {
          value.push(item);
        }
      });
      return value;
    },
    departmenTreeChange(value, label, extra) {
      this.departmentValue = value;
      if (!value || value.length === 0) {
      } else {
        label = [];
        let _value = value.map((item) => {
          label.push(item.label);
          return item.value;
        });
        let departmentId = _value.join(),
          departmentCode = [],
          departmentName = label.join();
        this.DEPARTMENT.forEach((item) => {
          if (departmentId.includes(item.id)) {
            departmentCode.push(item.code);
          }
        });
        departmentCode = departmentCode.join();
      }
    },

    handleOk() {
      let request = {
        PartyId: [],
        PartyCode: [],
        PartyName: [],
        PartyType: '',
      };
      if (this.activeKey === '1') {
        request.PartyType = 'C';
        this.dataSource.forEach((item) => {
          if (this.selectedRowKeys.includes(item.cargoOwnerId)) {
            request.PartyId.push(item.cargoOwnerId);
            request.PartyCode.push(item.cargoOwnerCode);
            request.PartyName.push(item.cargoOwnerName);
          }
        });
      } else {
        request.PartyType = 'D';
        this.DEPARTMENT.forEach((item) => {
          if (this.departmentValue.includes(item.code)) {
            request.PartyId.push(item.id);
            request.PartyCode.push(item.code);
            request.PartyName.push(item.name);
          }
        });
      }
      this.inputValue = request.PartyName.join(',');
      this.$emit('getPartyA', request);
      this.visible = false;
    },
  },
};
</script>