<template>
  <div>
    <div style="display: flex; align-items: flex-end">
      <img
        src="@/assets/setting/user.png"
        alt
        style="width: 26px; height: 26px"
      >
      <span
        style="
          font-size: 18px;
          font-weight: bold;
          color: #333333;
          line-height: 26px;
          margin-left: 5px;
        "
      >{{ $t('controlorder.header') }}</span>
    </div>
    <div style="display: flex; align-items: flex-end; margin-top: 5px">
      <div style="width: 75px; height: 4px; background: #396fff" />
      <div
        style="
          width: 75px;
          height: 1px;
          background: #fff;
          border: 1px solid #acacac;
        "
      />
    </div>
    <!-- 搜索工作栏 -->
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" size="small" :inline="true" style="margin-top: 20px;">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item :label="$t('controlorder.transport')" prop="transportId">
            <dict-selector v-model="queryParams.transportId" :type="DICT_TYPE.ECW_TRANSPORT_TYPE" formatter="number" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('controlorder.No')" prop="searchKey" style="margin-left: 28px;">
            <el-input v-model="queryParams.searchKey" style="width: 250px;" :placeholder="$t('controlorder.NoRules')" clearable />
          </el-form-item>
        </el-col></el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item :label="$t('controlorder.controlStatus')" prop="cargoControlStatus">
            <dict-selector v-model="queryParams.cargoControlStatus" type="control_goods_status" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('controlorder.marks')" prop="marks" style="margin-left: 28px;">
            <el-input v-model="queryParams.marks" style="width: 250px;" :placeholder="$t('controlorder.marksRules')" clearable />
          </el-form-item>
        </el-col></el-row>
      <el-form-item :label="$t('controlorder.date')">
        <dict-selector v-model="dateFilterType" :type="DICT_TYPE.ECW_BEGINTIME_TYPE_ENDTIME" style="width: 150px; margin-right: 5px" />
        <el-date-picker v-model="dateFilter" type="datetimerange" range-separator="-" :start-placeholder="$t('controlorder.start')" :end-placeholder="$t('controlorder.end')" value-format="yyyy-MM-dd HH:mm:ss" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">{{ $t('common.search') }}</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">{{ $t('common.reload') }}</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <!-- 列表 -->
    <el-table
      :data="orderList"
      style="width: 100%"
    >
      <el-table-column
        type="index"
        width="50"
        align="center"
        header-align="center"
      />
      <el-table-column
        :label="$t('controlorder.orderNo')"
        width="120"
        align="center"
        header-align="center"
      >
        <template slot-scope="scope">
          <router-link :to="{path: '/jiedao/jd-orderdetail',query: {orderId: scope.row.orderId}}" class="link-type">
            <span>{{ scope.row.orderNo }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column
        prop="tidanNo"
        :label="$t('提单号')"
        width="100"
        align="center"
        header-align="center"
      />
      <el-table-column
        :label="$t('controlorder.transport')"
        width="100"
        align="center"
        header-align="center"
      >
        <template slot-scope="{row}">
          <dict-tag :type="DICT_TYPE.ECW_TRANSPORT_TYPE" :value="row.transportId" />
        </template>
      </el-table-column>
      <el-table-column
        prop="marks"
        :label="$t('controlorder.marks')"
        width="100"
        align="center"
        header-align="center"
      />
      <el-table-column
        :label="$t('controlorder.status')"
        prop="statusMsg"
        align="center"
        header-align="center"
      >
        <template slot-scope="scope">
          {{ getStatusName(scope.row) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="sumNum"
        :label="$t('入仓箱数')"
        width="100"
        align="center"
        header-align="center"
      />
      <el-table-column
        prop="releaseNum"
        :label="$t('已放箱数')"
        width="100"
        align="center"
        header-align="center"
      />
      <el-table-column :label="$t('controlorder.cargoControlStatus')" align="center" prop="transportId">
        <template slot-scope="{row}">
          <dict-tag :type="DICT_TYPE.CONTROL_GOODS_STATUS" :value="row.cargoControlStatus" />
        </template>
      </el-table-column>
      <el-table-column
        prop="consigneeName"
        :label="$t('收货人')"
        width="100"
        align="center"
        header-align="center"
      />
      <el-table-column
        prop="consigneePhone"
        :label="$t('收货人电话')"
        width="100"
        align="center"
        header-align="center"
      />
      <el-table-column
        prop="createTime"
        :label="$t('controlorder.createTime')"
        align="center"
        header-align="center"
      />
      <el-table-column :label="$t('common.action')" align="center" class-name="small-padding fixed-width" width="120" fixed="right">
        <template slot-scope="scope">
          <div v-if="[0,2].indexOf(scope.row.cargoControlStatus) > -1 && scope.row.sumNum > 0">
            <el-button type="text" size="mini" @click="showReleaseOrderId=scope.row.orderId">{{ $t('controlorder.release') }}</el-button>
            <el-button type="text" size="mini" @click="showTransferOrder = scope.row">{{ $t('controlorder.transfer') }}</el-button>
          </div>
          <el-button type="text" size="mini" @click="detailId = scope.row.orderId;detailFlag=true">{{ $t('controlorder.view') }}</el-button>
          <el-button type="text" size="mini" @click="logId = scope.row.orderId;logFlag=true">{{ $t('控货日志') }}</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"
    />
    <transfer v-if="showTransferOrder" v-bind="showTransferOrder" @close="showTransferOrder=null;getList();" />
    <release v-if="showReleaseOrderId" :order-id="showReleaseOrderId" @close="showReleaseOrderId=null;getList();" @success="handleReleaseSuccess" />
    <release-detail v-if="detailFlag" :order-id="detailId" @close="detailFlag=false;" />
    <release-log v-if="logFlag" :order-id="logId" @close="logFlag=false;" />
  </div>
</template>

<script>
import { getControl } from '@/api/order'
import { parseTime } from '@/utils'
import RightToolbar from '@/components/RightToolbar'
import Pagination from '@/components/Pagination'
import DictTag from '@/components/DictTag'
import DictSelector from '@/components/DictSelector'
import Transfer from './components/Transfer'
import Release from './components/Release'
import ReleaseDetail from './components/ReleaseDetail'
import ReleaseLog from './components/ReleaseLog'
export default {
  components: {
    RightToolbar,
    Pagination,
    DictTag,
    DictSelector,
    Transfer,
    Release,
    ReleaseDetail,
    ReleaseLog
  },
  data() {
    return {
      parseTime,
      detailFlag: false,
      orderList: [],
      // 显示搜索条件
      showSearch: true,
      dateFilterType: '', // 日期筛选类别
      dateFilter: [], // 筛选日期
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        page: 1,
        rows: 10,
        transportId: null,
        cargoControlStatus: null,
        searchKey: null,
        marks: null
      },
      showTransferOrder: null, // 控制权转移操作订单
      showReleaseOrderId: null, // 放货ID
      detailId: null,
      logFlag: false,
      logId: null
    }
  },
  computed: {
    // expoerCityList() {
    //   return this.tradeCityList.filter(item => item.type == 2)
    // },
    // importCityList() {
    //   return this.tradeCityList.filter(item => item.type == 1)
    // },
    combinedQueryParams() {
      const timeParams = {}
      if (this.dateFilterType && this.dateFilter) {
        timeParams['begin' + this.dateFilterType] = this.dateFilter[0]
        timeParams['end' + this.dateFilterType] = this.dateFilter[1]
      }
      return Object.assign({}, this.queryParams, timeParams)
    },
    isChinese() {
      return this.$i18n.locale === 'zh_CN'
    }
  },
  watch: {
    isChinese() {
      this.getList()
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getStatusName: function (item) {
      const orderStatus = this.getRealStatus(item);
      const special = this.getDictDatas(this.DICT_TYPE.APP_ORDER_STATUS);
      const statusMsg=special.filter(e => {
        return e.value.split(',').map(Number).includes(orderStatus)
      });
      return this.isChinese ? statusMsg[0].label : statusMsg[0].labelEn;
    },
    getList() {
      getControl(this.combinedQueryParams).then((r) => {
        this.orderList = r.data.list
        this.total = r.data.total
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs.queryForm.resetFields()
      this.getList()
    },
    specialRendering(val) {
      if (val !== undefined) {
        const i = val.split(',')
        const special = this.getDictDatas(this.DICT_TYPE.ORDER_SPECIAL_NEEDS).filter(e => {
          return i.indexOf(e.value) > -1
        })
        return special
      }
    },
    // 放货成功
    handleReleaseSuccess() {
      this.showReleaseOrderId = null
      this.getList()
    },
    getRealStatus(item){
      let status = 0;
      if (item.status === 12 && item.shipmentState === 325){
        status = 12325
      }else if (item.status === 5 && item.airShipment === 1 && item.abnormalState === 0 && item.auditType=== 0 && item.transportId === 3){
        status = 10501
      }else if (item.status === 5 && (item.airShipment === 2 || item.airShipment === 3) && item.abnormalState === 0 && item.auditType=== 0 && item.transportId === 3){
        status = 10502
      }else if (item.status === 5 && item.airShipment === 4 && item.transportId === 3) {
        status = 10503;
      } else if (item.status === 5 && item.abnormalState === 0 && item.auditType === 0 &&
        ((item.transportId === 3 && (item.airShipment === 2 || item.airShipment === 3 || item.airShipment === 4)) ||
          (item.transportId === 1 && (item.airShipment === 0 || item.airShipment === 2 || item.airShipment === 3 || item.airShipment === 4)))) {
        status = 10504;
      } else if (item.status === 32 && item.shipmentState === 411 && item.transportId === 3) {
        status = 132411;
      } else if (item.status === 32 && item.shipmentState === 412 && item.transportId === 3) {
        status = 132412;
      } else if (item.status === 32 && item.shipmentState === 409 && item.transportId === 3) {
        status = 132409;
      } else if (item.status === 32 && item.shipmentState === 428 && item.transportId === 3) {
        status = 118428;
      }else {
        status = item.status;
      }
      return status
    }
  }
}
</script>

<style scoped>
::v-deep .el-table__header .el-table__cell{
  background-color: #27417C;
  font-size: 13px;
  height: 48px;
  text-align: center;
  font-size: 13px;
  font-weight: 500;
  color: #FFFFFF;
}
</style>
