<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="公司名" prop="companyId">
        <el-select filterable v-model="queryParams.companyId" placeholder="请选择公司名" @change="companyChange" clearable size="small">
          <el-option
            v-for="item in companys"
            :key="item.companyId"
            :label="item.companyName"
            :value="item.companyId"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <treeselect style="width: 220px" :clearable="false" v-model="queryParams.deptId" :options="deptOptions" :show-count="true" placeholder="请选择归属部门" />
      </el-form-item>

      <el-form-item label="订单单号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入订单单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销售名称" prop="companyUserNickName">
        <el-input
          v-model="queryParams.companyUserNickName"
          placeholder="请输入销售名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="userPhone">
        <el-input
          v-model="queryParams.userPhone"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="订单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择订单状态" clearable size="small">
          <el-option
            v-for="item in orderStatusOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="parseInt(item.dictValue)"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="物流单号" prop="deliveryId">
        <el-input
          v-model="queryParams.deliveryId"
          placeholder="请输入物流单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="银行交易流水" prop="bankTransactionId" label-width="100">
        <el-input
          v-model="queryParams.bankTransactionId"
          placeholder="请输入银行交易流水"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="处理状态" prop="deliveryExceptionStatus">
        <el-select v-model="queryParams.deliveryExceptionStatus" placeholder="请选择处理状态" clearable size="small">
          <el-option label="待处理" :value="1" />
          <el-option label="已处理" :value="2" />
        </el-select>
      </el-form-item>

      <el-form-item label="异常状态" prop="deliveryType">
        <el-select v-model="queryParams.deliveryType" placeholder="请选择异常状态" clearable size="small" style="width: 220px">
          <el-option
            v-for="item in deliveryTypeOptions"
            :key="item.dictValue"
            :label="item.dictValue + ' ' + item.dictLabel"
            :value="item.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          style="width:205.4px"
          clearable
          size="small"
          v-model="dateRange"
          type="daterange"
          value-format="yyyy-MM-dd"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['store:storeDeliveryAbnormalOrder:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table height="500" border v-loading="loading" :data="storeOrderList" :row-class-name="tableRowClassName">
      <el-table-column label="ID" align="center" prop="id" width="80" />
      <el-table-column label="所属公司" align="center" prop="companyName" />
      <el-table-column label="所属员工" align="center" prop="companyUserNickName" />
      <el-table-column label="订单单号" align="center" prop="orderCode" width="180" />
      <el-table-column label="支付单号" align="center" prop="payCode" />
      <el-table-column label="会员手机号" align="center" prop="userPhone" />
      <el-table-column label="订单状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-for="item in orderStatusOptions" :key="item.dictValue" v-if="scope.row.status == item.dictValue">{{ item.dictLabel }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="物流状态" align="center" prop="deliveryStatus">
        <template slot-scope="scope">
          <el-tag v-for="item in deliveryStatusOptions" :key="item.dictValue" v-if="scope.row.deliveryStatus == item.dictValue">{{ item.dictLabel }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="物流单号" align="center" prop="deliveryId" />
      <el-table-column label="异常状态" align="center" prop="deliveryType" min-width="140">
        <template slot-scope="scope">
          <span>{{ formatDeliveryType(scope.row.deliveryType) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="处理状态" align="center" prop="deliveryExceptionStatus" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.deliveryExceptionStatus === 1" type="danger" size="mini">待处理</el-tag>
          <el-tag v-else-if="scope.row.deliveryExceptionStatus === 2" type="success" size="mini">已处理</el-tag>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="异常备注" align="center" prop="deliveryExceptionRemark" min-width="160" show-overflow-tooltip />
      <el-table-column label="创建时间" align="center" prop="createTime" width="160" />
      <el-table-column label="操作" align="center" fixed="right" width="120px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleShow(scope.row)"
            v-hasPermi="['store:storeDeliveryAbnormalOrder:query']"
          >查看/处理</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-drawer size="75%" :title="title" :visible.sync="open" @close="handleDrawerClose">
      <product-order ref="productOrder" delivery-abnormal-mode @refresh="getList" />
    </el-drawer>
  </div>
</template>

<script>
import { listDeliveryAbnormalOrder, exportDeliveryAbnormalOrder } from '@/api/hisStore/storeDeliveryAbnormalOrder'
import productOrder from '../components/productOrder'
import { getCompanyList } from '@/api/company/company'
import { treeselect } from '@/api/company/companyDept'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  watch: {
    'queryParams.deptId': 'currDeptChange'
  },
  components: { productOrder, Treeselect },
  name: 'StoreDeliveryAbnormalOrderList',
  data() {
    return {
      companyId: undefined,
      orderStatusOptions: [],
      deliveryStatusOptions: [],
      deliveryTypeOptions: [],
      companys: [],
      deptOptions: [],
      loading: true,
      showSearch: true,
      total: 0,
      storeOrderList: [],
      title: '物流异常订单',
      open: false,
      dateRange: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderCode: null,
        companyId: null,
        deptId: null,
        companyUserNickName: null,
        userPhone: null,
        productName: null,
        status: null,
        deliveryStatus: 4,
        deliveryId: null,
        bankTransactionId: null,
        deliveryExceptionStatus: null,
        deliveryType: null
      }
    }
  },
  created() {
    getCompanyList().then(response => {
      this.companys = response.data
      if (this.companys != null && this.companys.length > 0) {
        this.companyId = this.companys[0].companyId
        this.getTreeselect()
      }
    })
    this.getDicts('store_order_status').then(response => {
      this.orderStatusOptions = response.data
    })
    this.getDicts('store_order_delivery_status').then(response => {
      this.deliveryStatusOptions = response.data
    })
    this.getDicts('store_order_delivery_type').then(response => {
      this.deliveryTypeOptions = response.data
    })
    this.getList()
  },
  methods: {
    buildQueryParams() {
      const params = { ...this.queryParams }
      if (this.dateRange != null && this.dateRange.length === 2) {
        params.createTimeRange = this.dateRange[0] + '--' + this.dateRange[1]
      } else {
        params.createTimeRange = null
      }
      return params
    },
    formatDeliveryType(value) {
      if (!value) {
        return '-'
      }
      const item = this.deliveryTypeOptions.find(d => d.dictValue === value || d.dictValue === String(value))
      return item ? `${item.dictValue} ${item.dictLabel}` : value
    },
    handleShow(row) {
      this.title = '物流异常订单 - ' + row.orderCode
      this.open = true
      this.$nextTick(() => {
        if (this.$refs.productOrder) {
          this.$refs.productOrder.getOrder(row.id)
        }
      })
    },
    handleDrawerClose() {
      this.getList()
    },
    getList() {
      this.loading = true
      listDeliveryAbnormalOrder(this.buildQueryParams()).then(response => {
        this.storeOrderList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    tableRowClassName({ row }) {
      if (row.deliveryExceptionStatus === 2) {
        return 'delivery-exception-handled-row'
      }
      return 'delivery-exception-row'
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.queryParams.deliveryStatus = 4
      this.queryParams.deliveryExceptionStatus = null
      this.handleQuery()
    },
    handleExport() {
      const queryParams = this.buildQueryParams()
      this.$confirm('是否确认导出物流异常订单数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return exportDeliveryAbnormalOrder(queryParams)
      }).then(response => {
        this.download(response.msg)
      }).catch(() => {})
    },
    getTreeselect() {
      treeselect({ companyId: this.companyId }).then(response => {
        this.deptOptions = response.data
      })
    },
    companyChange(val) {
      this.companyId = val
      this.getTreeselect()
    },
    currDeptChange(val) {
      this.queryParams.deptId = val
      this.getList()
    }
  }
}
</script>

<style scoped>
::v-deep .el-table .delivery-exception-row > td {
  background-color: #fde2e2 !important;
}
::v-deep .el-table .delivery-exception-handled-row > td {
  background-color: #e1f3d8 !important;
}
</style>
