<!--
@name: 仓库管理
@author: 张泽贤
@time: 2022-07-12
@doc: PROLOG_UPCloudSolutionForTMS__v1.2.1_基础数据_1.2.1.doc
-->
<template>
  <div class="pages">
    <p-spin :spinning="spinning" v-if="!editType" v-show="!addressAnalysisShow">
      <p-card style="margin-bottom: 10px">
        <p-form-model ref="formTarget" :model="form" :label-col="{ span: 8 }" :wrapper-col="{ span: 16 }" class="plg-grid-from">
          <a-row>
            <a-col :span="6">
              <p-form-model-item label="仓库编码">
                <p-input v-model="form.code" />
              </p-form-model-item>
            </a-col>
            <a-col :span="6">
              <p-form-model-item label="仓库名称">
                <p-input v-model="form.name" />
              </p-form-model-item>
            </a-col>
            <a-col :span="12">
              <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>
                  <!-- <p-button type="primary" @click="onSeniorSearch">高级查询</p-button> -->
                </a-space>
              </p-form-model-item>
            </a-col>
          </a-row>
        </p-form-model>
      </p-card>
      <p-card title="仓库信息" style="margin-bottom: 10px">
        <a-space slot="extra">
          <p-button type="primary" @click="onEdit({}, 'add')">新增</p-button>
          <p-button type="primary" @click="onEnOrDis(10)" :disabled="!selectedRowKeys.length">启用</p-button>
          <p-button type="danger" @click="onEnOrDis(0)" :disabled="!selectedRowKeys.length">停用</p-button>
          <p-button type="danger" @click="onDel" :disabled="!selectedRowKeys.length">删除</p-button>
          <p-button type="primary" @click="onAddressAnalysis" :disabled="!selectedRowKeys.length">地址解析</p-button>
          <import-Button :importUrl="importUrl" :tmpUrl="downUrl" fileName="仓库模板" />
        </a-space>
        <p-table
          ref="masterTable"
          dragle
          bordered
          :extendedObjectCode="'6'"
          row-key="id"
          :activeKey="selectedId"
          :id="$route.meta.menuId + '_wharehouse'"
          :scroll="{ x: true, y: 300 }"
          :data-source.sync="dataSource"
          :remote-url="listRemoteUrl"
          :remoteData="listParams"
          :rowSelectType="2"
          :selectedRowKeys.sync="selectedRowKeys"
          @clickRow="masterClickRow"
          :sign="true"
        >
          <p-table-column data-index="code" title="仓库编码" :width="120" />
          <p-table-column data-index="name" title="仓库名称" :width="120" />
          <p-table-column data-index="shortName" title="仓库简称" :width="120" />
          <p-table-column data-index="departmentName" title="部门" :width="120" />
          <p-table-column data-index="statusName" title="状态" :width="120" />
          <p-table-column data-index="contact" title="联系人" :width="120" />
          <p-table-column data-index="phoneNumber" title="手机号" :width="120" />
          <p-table-column data-index="landlineNumber" title="座机号" :width="120" />
          <p-table-column data-index="email" title="邮箱" :width="120" />
          <p-table-column data-index="postCode" title="邮编" :width="120" />
          <p-table-column data-index="provinceName" title="省" :width="120" />
          <p-table-column data-index="cityName" title="市" :width="120" />
          <p-table-column data-index="districtName" title="区" :width="120" />
          <p-table-column data-index="address" title="详细地址" :width="120" />
          <p-table-column data-index="longitude" title="经度" :width="120" />
          <p-table-column data-index="latitude" title="纬度" :width="120" />
          <p-table-column data-index="stationCode" title="站点编码" :width="120" />
          <p-table-column data-index="stationName" title="站点名称" :width="120" />
          <p-table-column data-index="stationDeliveryOrder" title="配送顺序" :width="120" />
          <p-table-column data-index="stationAreas" title="区域" :width="120" />
          <p-table-column data-index="memo" title="备注" :width="120" />
          <p-table-column data-index="startTime" title="每日作业开始时间" :width="120" />
          <p-table-column data-index="endTime" title="每日作业结束时间" :width="120" />
          <p-table-column data-index="workingTime" title="作业时长" :width="120" />
          <p-table-column data-index="loadInterval" title="装车间隔" :width="120" />
          <p-table-column data-index="pickUpSignName" title="提货签到" :width="120" />
          <p-table-column data-index="pickUpPictureName" title="提货图片" :width="120" />
          <p-table-column data-index="defaultPickUpRail" title="默认提货围栏" :width="120" />
          <p-table-column data-index="deliverySignName" title="送达签到" :width="120" />
          <p-table-column data-index="deliveryPictureName" title="送达图片" :width="120" />
          <p-table-column data-index="defaultDeliveryRail" title="默认送达围栏" :width="120" />
          <p-table-column v-for="item in mextendedField_list" :key="item.fieldName" :data-index="item.fieldName" :title="item.displayName" :width="120" />
          <p-table-column data-index="createperson" title="创建人" :width="120" />
          <p-table-column data-index="createtime" title="创建时间" :width="120" />
          <p-table-column data-index="updateperson" title="修改人" :width="120" />
          <p-table-column data-index="updatetime" title="修改时间" :width="120" />
          <p-table-column key="action" title="操作" :width="180" fixed="right">
            <template slot-scope="text, record">
              <a-space>
                <a @click.stop="onEdit(record, 'view')">查看</a>
                <a @click.stop="onEdit(record, 'copy')">复制</a>
                <a @click.stop="onEdit(record, 'edit')">修改</a>
                <p-switch @change="toEnOrDis(record)" :checked="record.status === 10 ? true : false" size="small" />
                <a @click.stop="toDel(record)">删除</a>
              </a-space>
            </template>
          </p-table-column>
        </p-table>
      </p-card>
      <p-card title="车位信息" v-action:parking>
        <a-space slot="extra">
          <p-button type="primary" @click="onEdit({}, 'parkingAdd')" :disabled="!selectedId">新增</p-button>
          <p-button type="primary" @click="onParkingEnOrDis(10)" :disabled="!parkingSelectedRowKeys.length">启用</p-button>
          <p-button type="danger" @click="onParkingEnOrDis(0)" :disabled="!parkingSelectedRowKeys.length">停用</p-button>
          <p-button type="danger" @click="onParkingDel" :disabled="!parkingSelectedRowKeys.length">删除</p-button>
          <import-Button :importUrl="parkingImportUrl" :tmpUrl="parkingDownUrl" fileName="车位模板" />
        </a-space>
        <p-table
          ref="parkingTable"
          dragle
          bordered
          :extendedObjectCode="'7'"
          row-key="id"
          :id="$route.meta.menuId + '_parkingTable'"
          :scroll="{ x: true, y: 300 }"
          :data-source.sync="parkingDataSource"
          :remote-url="parkingListRemoteUrl"
          :remoteData="parkingListParams"
          :rowSelectType="2"
          :selectedRowKeys.sync="parkingSelectedRowKeys"
          :autoLoad="false"
          :pagination="false"
        >
          <p-table-column data-index="code" title="车位编码" :width="120" />
          <p-table-column data-index="name" title="车位名称" :width="120" />
          <p-table-column data-index="description" title="描述" :width="120" />
          <p-table-column data-index="statusName" title="状态" :width="120" />
          <p-table-column data-index="typeName" title="类型" :width="120" />
          <p-table-column data-index="carTypeName" title="适用车型" :width="120" />
          <p-table-column v-for="item in parkingMextendedField_list" :key="item.fieldName" :data-index="item.fieldName" :title="item.displayName" :width="120" />
          <p-table-column key="action" title="操作" :width="180" fixed="right">
            <template slot-scope="text, record">
              <a-space>
                <a @click.stop="onEdit(record, 'parkingView')">查看</a>
                <a @click.stop="onEdit(record, 'parkingCopy')">复制</a>
                <a @click.stop="onEdit(record, 'parkingEdit')">修改</a>
                <p-switch @change="toParkingEnOrDis(record)" :checked="record.status === 10 ? true : false" size="small" />
                <a @click.stop="toParkingDel(record)">删除</a>
              </a-space>
            </template>
          </p-table-column>
        </p-table>
      </p-card>
    </p-spin>

    <!-- 编辑 -->
    <edit
      ref="edit"
      v-if="warehouse.includes(editType)"
      v-show="!addressAnalysisShow"
      :type="editType"
      :editId="editId"
      :options="options"
      :mextendedField_list="mextendedField_list"
      @back="editType = ''"
      @upadteAddress="upadteAddress"
    ></edit>

    <!-- 车位 -->
    <parkingEdit
      v-if="parking.includes(editType)"
      :type="editType"
      :editId="editId"
      :branchid="selectedId"
      :options="options"
      :mextendedField_list="parkingMextendedField_list"
      @back="parkingBack"
    ></parkingEdit>

    <!-- 地址解析 -->
    <addressAnalysis :showType="addressAnalysisShow" :type="'branch'" :searchParams="addressSearchData" :addressData="addressData" @back="addressBack" @returnData="onMapReturnData" />
  </div>
</template>

<script>
import { initDictionary, extendedFieldList } from '@/blocks/utils';
import {
  listRemoteUrl,
  parkingListRemoteUrl,
  activeBranch,
  deleteBranch,
  batch_delete,
  activeCarport,
  deleteCarport,
  batch_delete_carport,
  importUrl,
  downUrl,
  parkingImportUrl,
  parkingDownUrl,
} from './js/api';

import edit from './modules/edit';
import parkingEdit from './modules/parkingEdit';
import addressAnalysis from '@/blocks/addressAnalysis/index.addressAnalysis.vue'; // 地址解析
import importButton from '@/blocks/components/importButton/index.importButton.vue';

export default {
  components: { edit, parkingEdit, addressAnalysis, importButton },
  computed: {
    // 页面权限
    operations() {
      const { currentOperations, currentPageElements } = this.$store.getters;
      const operations = currentOperations.concat(currentPageElements);
      return operations;
    },
  },
  data() {
    return {
      spinning: false,
      // 空-列表页 view-查看 add-新增 edit-修改 copy-复制 address-地址解析 parkingView-车位查看 parkingAdd-车位新增 parkingEdit-车位修改 parkingCopy-车位复制
      editType: '',
      editId: '',
      warehouse: ['view', 'add', 'edit', 'copy'],
      parking: ['parkingView', 'parkingAdd', 'parkingEdit', 'parkingCopy'],
      importUrl,
      downUrl,
      parkingImportUrl,
      parkingDownUrl,

      form: {
        code: '',
        name: '',
      },
      options: {
        MAINDATA_STATUS: [],
        CARPORT_TYPE: [],
        WHETHER: [],
        AVAILABLE_TIME: [],
        AVAILABLE_UNIT: [],
        APPOINTMENT_TYPE: [],
        CALL_HANDLE_TYPE: [],
        OVER_NUM_HANDLE_TYPE: [],
        VEHICLE_TYPE_CATEGORY: [],
      },

      // 主表格
      dataSource: [],
      selectedRowKeys: [],
      listRemoteUrl,
      listParams: {},
      selectedId: '',

      // 车位表格
      parkingDataSource: [],
      parkingSelectedRowKeys: [],
      parkingListRemoteUrl,
      parkingListParams: {},

      mextendedField_list: [],
      parkingMextendedField_list: [],

      addressAnalysisShow: 0,
      addressData: {},
      addressSearchData: {},
    };
  },
  mounted() {
    initDictionary([
      { objectcode: 'SIGN_TIME_UNIT' },
      { objectcode: 'MAINDATA_STATUS' },
      { objectcode: 'CARPORT_TYPE' },
      { objectcode: 'WHETHER' },
      { objectcode: 'AVAILABLE_TIME' },
      { objectcode: 'AVAILABLE_UNIT' },
      { objectcode: 'APPOINTMENT_TYPE' },
      { objectcode: 'CALL_HANDLE_TYPE' },
      { objectcode: 'OVER_NUM_HANDLE_TYPE' },
      { objectcode: 'PICK_UP_SIGN_TIME' },
      { objectcode: 'VEHICLE_TYPE_CATEGORY' },
    ]).then((res) => {
      for (let key in res) {
        res[key].forEach((item) => {
          if (!isNaN(item.value)) {
            item.value = +item.value;
          }
        });
      }
      this.options = { ...this.options, ...res };
    });
    extendedFieldList({ extendedObjectCode: '6' }).then((res) => (this.mextendedField_list = res));
    extendedFieldList({ extendedObjectCode: '7' }).then((res) => (this.parkingMextendedField_list = res));
  },
  methods: {
    // 查询
    onSearch() {
      this.listParams = { ...this.form };
      this.parkingDataSource = [];
    },
    // 重置
    onReset() {
      this.form = {
        code: '',
        name: '',
      };
    },

    // 新增、修改、复制、查看
    onEdit(record = { id: '' }, type) {
      this.editId = record.id;
      this.editType = type;
    },

    // 启用、停用
    onEnOrDis(status) {
      this.WhetherToEnable(status === 10 ? activeBranch : deleteBranch, this.selectedRowKeys, status === 10 ? '启用' : '停用');
    },
    toEnOrDis(record) {
      this.WhetherToEnable(record.status === 10 ? deleteBranch : activeBranch, [record.id], record.status === 10 ? '停用' : '启用');
    },
    // 删除
    onDel() {
      this.WhetherToEnable(batch_delete, this.selectedRowKeys, '删除');
    },
    toDel(record) {
      this.WhetherToEnable(batch_delete, [record.id], '删除');
    },
    WhetherToEnable(method, idList, text) {
      let self = this;
      this.$confirm({
        title: '是否' + text + '?',
        onOk() {
          return new Promise((resolve, reject) => {
            method(idList)
              .then((res) => {
                if (res.success) {
                  self.$message.success(res.message);
                  self.onSearch();
                } else {
                  self.$exception(res);
                }
                resolve();
              })
              .catch((res) => {
                self.$exception(res);
                resolve();
              });
          });
        },
      });
    },

    // 地址解析
    onAddressAnalysis() {
      this.addressData = {};
      this.addressSearchData = { ids: [...this.selectedRowKeys] };
      this.addressAnalysisShow = 2;
    },
    addressBack() {
      this.addressAnalysisShow = 0;
      this.parkingDataSource = [];
      this.selectedId = '';
      if (!this.editType) {
        this.$refs.masterTable.reload();
      }
    },
    upadteAddress(addressData) {
      this.addressData = addressData;
      this.addressAnalysisShow = 1;
    },
    onMapReturnData(value) {
      this.$set(this.$refs.edit.form, 'address', value.address);
      this.$set(this.$refs.edit.form, 'longitude', value.longitude);
      this.$set(this.$refs.edit.form, 'latitude', value.latitude);
      this.addressAnalysisShow = 0;
    },

    // 行点击
    masterClickRow(event, activeID, rowRecord) {
      this.selectedId = rowRecord.id;
      if (this.operations.includes('parking')) {
        this.parkingListParams = { branchid: rowRecord.id };
      }
    },

    // 车站启用、停用
    onParkingEnOrDis(status) {
      this.PackingWhetherToEnable(status === 10 ? activeCarport : deleteCarport, this.parkingSelectedRowKeys, status === 10 ? '启用' : '停用');
    },
    toParkingEnOrDis(record) {
      this.PackingWhetherToEnable(record.status === 10 ? deleteCarport : activeCarport, [record.id], record.status === 10 ? '停用' : '启用');
    },
    // 车站删除
    onParkingDel() {
      this.PackingWhetherToEnable(batch_delete_carport, this.parkingSelectedRowKeys, '删除');
    },
    toParkingDel(record) {
      this.PackingWhetherToEnable(batch_delete_carport, [record.id], '删除');
    },
    PackingWhetherToEnable(method, idList, text) {
      let self = this;
      this.$confirm({
        title: '是否' + text + '?',
        onOk() {
          return new Promise((resolve, reject) => {
            method(idList)
              .then((res) => {
                if (res.success) {
                  self.$message.success(res.message);
                  self.$refs.parkingTable.reload();
                } else {
                  self.$exception(res);
                }
                resolve();
              })
              .catch((res) => {
                self.$exception(res);
                resolve();
              });
          });
        },
      });
    },

    parkingBack(rowRecord) {
      this.editType = '';
      this.$nextTick(() => {
        this.masterClickRow('', '', rowRecord);
        this.selectedId = rowRecord.id;
      });
    },
  },
};
</script>