<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">

      <el-form-item :label="$t('运输方式')" prop="transportType">
        <dict-selector :type="DICT_TYPE.ECW_TRANSPORT_TYPE" v-model="queryParams.transportType" formatter="number"
                       clearable/>
      </el-form-item>
      <el-form-item>
        <el-form-item :label="$t('始发仓')" prop="startWarehouseId">
          <el-select multiple v-model="queryParams.startWarehouseIdList" :placeholder="$t('请选择始发仓')" clearable>
            <el-option v-for="item in exportWarehouseList" :label="$l(item, 'title')" :value="item.id"
                       :key="item.id"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item :label="$t('出货渠道')" prop="shippingChannel">
          <el-select v-model="queryParams.channelId" :placeholder="$t('请选择出货渠道')" clearable>
            <el-option v-for="item in channelList" :label="item.nameZh" :value="item.channelId"
                       :key="item.channelId"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item :label="$t('目的国')" prop="destCountryId">
        <el-select
          v-model="destCountryId"
          multiple
          :label="destCountryId"
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="item in countryList"
            :key="item.id"
            :label="$l(item, 'title')"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('目的地')" prop="destCityId">
        <el-select v-model="destCityId"
        multiple
        clearable
        @change="handleQuery">
          <el-option
            v-for="item in AddressCity"
            :key="item.shi"
            :label="item.shiName"
            :value="item.shi"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item :label="$t('目的仓')" prop="destWarehouseId">
        <el-select v-model="destWarehouseId"
          clearable
          multiple
          @change="handleQuery">
          <el-option
            v-for="item in AddressTown"
            :key="item.id"
            :label="item.titleZh"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

        <el-button type="primary" icon="el-icon-search" @click="handleQuery">{{ $t('搜索') }}</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column :label="$t('序号')" align="center" type="index"/>
      <el-table-column :label="$t('始发仓')" align="center">
        <template slot-scope="{row}">
          {{ $l(row, 'startWarehouseTitle') }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('目的仓')" align="center">
        <template slot-scope="{row}">
          {{ $l(row, 'destWarehouseTitle') }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('运输方式')" align="center">
        <template slot-scope="{row}">
          <dict-tag :type="DICT_TYPE.ECW_TRANSPORT_TYPE" :value="row.transportType"></dict-tag>
        </template>
      </el-table-column>
      <el-table-column :label="$t('目的仓国家')" align="center">
        <template slot-scope="{row}">
      {{$l(row, 'destCountryName')}}
        </template>
      </el-table-column>
      <el-table-column :label="$t('出货渠道')" align="center">
        <template slot-scope="{row}">
          {{ row.channelIds ? $l(row, 'channelNames') : '/' }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center">
        <template slot-scope="{row}">
          <el-button v-if="row.transportType === 3" type="text" @click="$router.push({
          path:'/offer/channel-route?id='+ row.id,
          query:{
            startWarehouseTitle:$l(row,'startWarehouseTitle'),
            destWarehouseTitle: $l(row, 'destWarehouseTitle'),
            destCountryName:$l(row,'destCountryName')
          }
          })">空运渠道包装清关费
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.rows"
                @pagination="getList"/>
  </div>
</template>

<script>
import {
  createDeptTarget,
  updateDeptTarget,
  deleteDeptTarget,
  getDeptTarget,
  getDeptTargetPage,
  exportDeptTargetExcel,
  getCreateInitData,
  getPersonTargetPage,
  myAchievementByPage
} from "@/api/ecw/deptTarget";
import { getRegionList } from "@/api/ecw/order";
import {listSimpleDepts} from "@/api/system/dept";
import {getChannelList} from '@/api/ecw/channel';
import { getTradeCityList, getListTree } from "@/api/ecw/region";
import { getCityList } from "@/api/ecw/region";
import dayjs from "dayjs";
import {listServiceUser} from "@/api/system/user";
import {getWarehouseList} from '@/api/ecw/warehouse'
import {DICT_TYPE} from "@/utils/dict";
import {warehouseLinePage} from "@/api/ecw/customerContacts";
import {getTradeCountryList} from "@/api/ecw/region";

export default {
  name: "EcwOfferAll-routes-list",
  components: {},
  data() {
    return {
      AddressCity: [], //目的城市
      AddressProvince: [], //省份
      AddressTown: [], //目的仓


      countryList: [], //目的国
      warehouseList: [], //目前仓

      destCountryId: [],
      destCityId: null,
      destWarehouseId: "",
      objectiveId: [],

      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      list: [],
      // 是否显示弹出层
      open: false,
      tableOpen: false,
      channelList: [],
      // 查询参数
      queryParams: {
        page: 1,
        rows: 10,
        destCityId: null, //目的城市
        destCountryId: null, //目的国
        destWarehouseId: null, //目的仓
        objectiveId: null,
      },

      // 表单校验
      myAchievementData: {},
      warehouseList: []
    };
  },

  watch: {
    destCountryId: {
      //监听当前地区值的变化，于与上方地区值进行了双向绑定
      deep: true, //深度监听
      handler() {
        //每当值省份值改变时其下地区值进行清空
        this.AddressCity = [];
        this.AddressTown = [];
        this.destWarehouseId = "";
        this.destCityId = "";
        this.findByprovinceCode();
        if (this.destCountryId == "") {
          //1 是所有区域，2 国家，3是市，
          getRegionList(4, 4)
            .then(({ data }) => {
              this.AddressCity = data;
            })
            .catch((error) => {
              console.log(error);
            });

          getRegionList(5, 5)
            .then(({ data }) => {
              this.AddressTown = data;
            })
            .catch((error) => {
              console.log(error);
            });
        } else if (
          this.destCountryId != "" &&
          this.objectiveId == "" &&
          this.destWarehouseId == ""
        ) {
          getRegionList(2, this.destCountryId)
            .then(({ data }) => {
              this.AddressCity = data;
            })
            .catch((error) => {
              console.log(error);
            });
          getRegionList(5, 5)
            .then(({ data }) => {
              this.AddressTown = data;
            })
            .catch((error) => {
              console.log(error);
            });
        }
      },
    },
    destCityId: {
      deep: true, //深度监听 目的仓
      handler() {
        this.AddressTown = [];
        this.destWarehouseId = "";
        this.findBycityCode();
        if (
          this.destCityId != "" &&
          this.destCountryId != "" &&
          this.destWarehouseId == ""
        ) {
          //获取当前城市值id，获取该城市下区域
          getRegionList(3, this.destCityId)
            .then(({ data }) => {
              this.AddressTown = data;
            })
            .catch((error) => {
              console.log(error);
            });
        } else if (
          this.destCountryId == "" &&
          this.destCityId == "" &&
          this.destWarehouseId == ""
        ) {
          getRegionList(5, 5)
            .then(({ data }) => {
              this.AddressTown = data;
            })
            .catch((error) => {
              console.log(error);
            });
        } else if (
          this.destCountryId != "" &&
          this.destCityId == "" &&
          this.destWarehouseId == ""
        ) {
          getRegionList(2, this.destCountryId)
            .then(({ data }) => {
              this.AddressCity = data;
            })
            .catch((error) => {
              console.log(error);
            });
          getRegionList(5, 5)
            .then(({ data }) => {
              this.AddressTown = data;
            })
            .catch((error) => {
              console.log(error);
            });
        } else if (
          this.destCountryId == "" &&
          this.destCityId != "" &&
          this.destWarehouseId == ""
        ) {
          //获取当前城市值id，获取该城市下区域
          getRegionList(3, this.destCityId)
            .then(({ data }) => {
              this.AddressTown = data;
            })
            .catch((error) => {
              console.log(error);
            });
        }
      },
    },
    destWarehouseId: {
      deep: true, //深度监听
      handler() {
        if (
          this.destCountryId != "" &&
          this.destCityId != "" &&
          this.destWarehouseId == ""
        ) {
          //获取当前城市值id，获取该城市下区域
          getRegionList(3, this.destCityId)
            .then(({ data }) => {
              this.AddressTown = data;
            })
            .catch((error) => {
              console.log(error);
            });
        } else if (
          this.destCountryId != "" &&
          this.destCityId == "" &&
          this.destWarehouseId == ""
        ) {
          getRegionList(5, 5)
            .then(({ data }) => {
              this.AddressTown = data;
            })
            .catch((error) => {
              console.log(error);
            });
        } else if (
          this.destCountryId == "" &&
          this.destCityId != "" &&
          this.destWarehouseId == ""
        ) {
          getRegionList(3, this.destCityId)
            .then(({ data }) => {
              this.AddressTown = data;
            })
            .catch((error) => {
              console.log(error);
            });
        }
      },
    },

  },
  computed: {
    DICT_TYPE() {
      return DICT_TYPE
    },
    exportWarehouseList() {
      /* tradeType 1 进口，2出口，3进出口 */
      return this.warehouseList.filter(item => item.tradeType == 2 || item.tradeType == 3)
    },
    importWarehouseList() {
      return this.warehouseList.filter(item => item.tradeType == 1 || item.tradeType == 3)
    },
  },
  activated() {
    this.getChannelList();
    this.getList();
  },
  created() {
    getTradeCountryList().then(r => {
      this.countryList = r.data
    })
    this.getChannelList();
    this.getList();

    this.getData();
    //获取国家列表
    this.getCountryList();

    listServiceUser().then(r => {
      this.customerServiceList = r.data;
    })
    getWarehouseList().then(res => {
      this.warehouseList = res.data
    })
  },
  methods: {
    findByprovinceCode() {
      //获取当前省份值id，获取该省份下城市 destCountryId provinceCode
      getRegionList(2, this.destCountryId)
        .then(({ data }) => {
          this.AddressCity = data;
        })
        .catch((error) => {
          console.log(error);
        });
    },

    findBycityCode() {
      //获取当前城市值id，获取该城市下区域
      getRegionList(3, this.objectiveId)
        .then(({ data }) => {
          this.AddressTown = data;
        })
        .catch((error) => {
          console.log(error);
        });
    },

    getData() {
      //加载时发起请求获取所有省份值
      getRegionList(1, 1)
        .then(({ data }) => {
          this.AddressProvince = data;
        })
        .catch((error) => {
          console.log(error);
        });
      //目的城市
      getRegionList(4, 4)
        .then(({ data }) => {
          this.AddressCity = data;
        })
        .catch((error) => {
          console.log(error);
        });
      //目的仓
      getRegionList(5, 5)
        .then(({ data }) => {
          this.AddressTown = data;
        })
        .catch((error) => {
          console.log(error);
        });
    },

    /* 国家 */
    getCountryList() {
      getListTree({ treeType: 1 }).then((response) => {
        this.countryList = response.data;
      });
    },

    getChannelList() {
      getChannelList().then(res => this.channelList = res.data)
    },
    customerServiceFn(val) {
      if (this.customerServiceList.length > 0) {
        let index = this.customerServiceList.findIndex(item => item.id === val.userId);
        return index !== -1 ? this.customerServiceList[index]?.nickname : ''
      } else {
        return ''
      }
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      const params = {...this.queryParams}
      // 执行查询
      warehouseLinePage(params).then(r => {
        this.loading = false;
        this.list = r.data.list;
        this.total = r.data.total
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;

      //目的城市
      this.queryParams.destCityId = this.destCityId;
      //目的国
      this.queryParams.destCountryId = this.destCountryId;
      //目的仓
      this.queryParams.destWarehouseId = this.destWarehouseId;
      this.getList();
    },
  }
};
</script>


<style scoped>
.total_num {
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 20px 0;
  font-size: 15px;
}

.total_num span {
  margin: 0 8px;
}
</style>

