<template>
  <div class="page">
    <topBar v-if="topList.length > 0" :top-list="topList" :moren="moren" />
    <div class="thetopbox">
      <el-form ref="form" :model="queryParams" label-width="100px">
        <el-row>
          <el-col :span="4">
            <el-form-item :label="'订单编号'" prop="orderCode">
              <el-input v-model="queryParams.orderCode" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'会员编号'" prop="memberCode">
              <el-input v-model="queryParams.memberCode" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'会员姓名'" prop="memberName">
              <el-input v-model="queryParams.memberName" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'隶属体系'" prop="vertexId">
              <el-select
                v-model="queryParams.vertexId"
                clearable
              >
                <el-option
                  v-for="item in vertexList"
                  :key="item.pkId"
                  :label="item.vertexName"
                  :value="item.pkId"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'联系方式'" prop="phone">
              <el-input v-model="queryParams.phone" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'订单类型'" prop="orderType">
              <el-select
                v-model="queryParams.orderType"
                clearable
                :placeholder="'请选择'"
              >
                <el-option
                  v-for="(item, index) in typeList"
                  :key="index"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="4">
            <el-form-item :label="'发货方式'" prop="deliveryWay">
              <el-select
                v-model="queryParams.deliveryWay"
                clearable
                :placeholder="'请选择'"
              >
                <el-option
                  v-for="(item, index) in fhwayList"
                  :key="index"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="4">
            <el-form-item :label="'收货人'" prop="recName">
              <el-input v-model="queryParams.recName" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'收货电话'" prop="recPhone">
              <el-input v-model="queryParams.recPhone" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item class="dizhi" :label="'详细地址'" prop="teamCode">
              <el-cascader
                v-model="queryParams.shoppingAddress"
                :options="areaData"
                :props="regionParams"
                clearable
                @change="handleChange1"
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label-width="30px" label="" prop="recAddress">
              <el-input v-model="queryParams.recAddress" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item
              :label="`${'购买人' + '编号'}`"
              prop="buyMemberCode"
            >
              <el-input
                v-model="queryParams.buyMemberCode"
                clearable
              />
            </el-form-item>
          </el-col>

        </el-row>
        <el-collapse-transition>
          <el-row v-show="isActive">
            <el-col :span="4">
              <el-form-item
                :label="`${'购买人' + '姓名'}`"
                prop="buyMemberName"
              >
                <el-input
                  v-model="queryParams.buyMemberName"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item :label="'支付方式'" prop="payType">
                <el-select
                  v-model="queryParams.payType"
                  clearable
                  :placeholder="'请选择'"
                >
                  <el-option
                    v-for="(item, index) in paywayList"
                    :key="index"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item :label="'创建日期'" prop="teamCode">
                <el-date-picker
                  v-model="queryParams.cjsj"
                  type="daterange"
                  range-separator="——"
                  :start-placeholder="'开始日期'"
                  :end-placeholder="'结束日期'"
                  value-format="yyyy-MM-dd"
                  :unlink-panels="true"
                  @change="changeCjsj"
                />
              </el-form-item>
            </el-col>

            <el-col :span="8">
              <el-form-item :label="'支付日期'" prop="teamCode">
                <el-date-picker
                  v-model="queryParams.zfsj"
                  type="daterange"
                  range-separator="——"
                  :start-placeholder="'开始日期'"
                  :end-placeholder="'结束日期'"
                  value-format="yyyy-MM-dd"
                  :unlink-panels="true"
                  @change="changeZfsj"
                />
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="订单状态" prop="orderStatus">
                <el-select
                  v-model="queryParams.orderStatus"
                  clearable
                  placeholder="请选择订单状态"
                >
                  <el-option
                    v-for="(item, index) in orderStatusList"
                    :key="index"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-collapse-transition>
        <el-row style="margin-left: 20px">
          <el-col :span="4">
            <div class="searchbox">
              <el-button class="searchbtn" @click="getSearch">
                {{ '搜索' }}</el-button>
              <el-button @click="reChongzhi"> {{ '重置' }}</el-button>
            </div>
          </el-col>
          <div class="openClose" @click="changeActive">
            <i :class="isActive ? 'el-icon-arrow-up' : 'el-icon-arrow-down'" />
            <div>{{ isActive ? '点击收起' : '点击展开' }}</div>
          </div>
        </el-row>
      </el-form>
    </div>
    <div class="maintop">
      <div class="mainbtn">
        <el-button
          v-hasButtons="['CommodityExport']"
          size="small"
          class="thebtn2"
          @click="handleExport"
        >
          {{ '导出' }}</el-button>
      </div>
    </div>
    <div class="main">
      <div class="maintable">
        <el-table
          v-if="flag"
          v-loading="loading"
          :data="tableData"
          style="width: 100%"
          height="600px"
          :header-cell-style="{ background: '#EEEEEE' }"
          :row-class-name="tableRowClassName"
          :span-method="objectSpanMethod"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" />

          <el-table-column
            v-if="dataList[0].key"
            align="center"
            min-width="200px"
            prop="orderCode"
            :label="'订单编号'"
          />
          <el-table-column
            v-if="dataList[1].key"
            align="center"
            prop="orderTypeStr"
            :label="'订单类型'"
            min-width="120px"
          />
          <el-table-column
            v-if="dataList[2].key"
            align="center"
            min-width="120px"
            prop="memberCode"
            :label="'会员编号'"
          />
          <el-table-column
            align="center"
            prop="vertexName"
            :label="'隶属体系'"
            min-width="120"
          />
          <el-table-column
            v-if="dataList[3].key"
            align="center"
            prop="memberName"
            :label="'会员姓名'"
          />

          <el-table-column
            v-if="dataList[4].key"
            align="center"
            min-width="120px"
            prop="phone"
            :label="'联系方式'"
          />
          <el-table-column
            v-if="dataList[5].key"
            align="center"
            prop="recName"
            :label="'收货人'"
          />
          <el-table-column
            v-if="dataList[6].key"
            align="center"
            min-width="120px"
            prop="recPhone"
            :label="'收货电话'"
          />
          <el-table-column
            v-if="dataList[19].key"
            align="center"
            min-width="120px"
            prop="buyMemberCode"
            :label="`${'购买人' + '编号'}`"
          />
          <el-table-column
            v-if="dataList[20].key"
            align="center"
            min-width="120px"
            prop="buyMemberName"
            :label="`${'购买人' + '姓名'}`"
          />
          <el-table-column
            v-if="dataList[7].key"
            align="center"
            prop="recProvinceName"
            :label="'省'"
          />
          <el-table-column
            v-if="dataList[8].key"
            align="center"
            prop="recCityName"
            :label="'市'"
          />
          <el-table-column
            v-if="dataList[9].key"
            align="center"
            prop="recCountryName"
            :label="'区/县'"
          />
          <el-table-column
            v-if="dataList[10].key"
            align="center"
            min-width="200px"
            prop="recAddress"
            :label="'详细地址'"
          />
          <el-table-column
            v-if="dataList[11].key"
            align="center"
            min-width="100px"
            prop="orderAmount"
            :label="`订单金额(${isLocalSymbol()})`"
          />
          <el-table-column
            v-if="dataList[13].key"
            align="center"
            min-width="120px"
            prop="orderAchieve"
            :label="`订单业绩(${isLocalSymbol()})`"
          />
          <el-table-column
            v-if="dataList[21].key"
            align="center"
            min-width="100px"
            prop="orderAssAchieve"
            :label="'商品订单' + 'BV'"
          />
          <el-table-column
            v-if="dataList[12].key"
            align="center"
            min-width="100px"
            prop="postage"
            :label="'订单邮费'"
          />
          <el-table-column
            v-if="dataList[14].key"
            align="center"
            prop="deliveryWayStr"
            :label="'发货方式'"
            min-width="100px"
          />
          <el-table-column
            v-if="dataList[15].key"
            min-width="100px"
            align="center"
            prop="payTypeStr"
            :label="'支付方式'"
          />
          <el-table-column
            v-if="dataList[16].key"
            min-width="100px"
            align="center"
            prop="remark"
            :label="'备注'"
          />

          <el-table-column
            v-if="dataList[17].key"
            min-width="160px"
            align="center"
            prop="creationTime"
            :label="'创建日期'"
          />
          <el-table-column
            v-if="dataList[18].key"
            min-width="160px"
            align="center"
            prop="payTime"
            label="支付日期"
          />
          <el-table-column
            min-width="100px"
            align="center"
            prop="boxNum"
            label="订单套数"
          />
          <el-table-column
            align="center"
            prop="time"
            :label="'操作'"
            fixed="right"
            min-width="120px"
            width="auto"
          >
            <template slot-scope="scope">
              <div>
                <el-button
                  v-hasButtons="['CommodityOpen']"
                  type="text"
                  size="small"
                  @click.native.prevent="findDetail(scope.row, tableData)"
                >
                  {{ '查看' }}
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getSearch"
    />
  </div>
</template>

<script>
import topBar from '@/components/topBar'
import * as log from '@/api/logistics'
import * as api from '@/api/site.js'
import { getVartexList } from '@/api/affiliatedteam'
import { getRoleMenu, roleDetails, getStatus } from '@/api/notice'
export default {
  name: 'Spdd',
  components: {
    topBar
  },
  data() {
    return {
      moren: 'spdd',
      vertexList: [],
      dataList: [
        {
          name: 'orderCode',
          key: ''
        },
        {
          name: 'orderTypeStr',
          key: ''
        },
        {
          name: 'memberCode',
          key: ''
        },
        {
          name: 'memberName',
          key: ''
        },
        {
          name: 'phone',
          key: ''
        },
        {
          name: 'recName',
          key: ''
        },
        {
          name: 'recPhone',
          key: ''
        },
        {
          name: 'recProvinceName',
          key: ''
        },
        {
          name: 'recCityName',
          key: ''
        },
        {
          name: 'recCountryName',
          key: ''
        },
        {
          name: 'recAddress',
          key: ''
        },
        {
          name: 'orderAmount',
          key: ''
        },
        {
          name: 'postage',
          key: ''
        },
        {
          name: 'orderAchieve',
          key: ''
        },
        {
          name: 'deliveryWayStr',
          key: ''
        },
        {
          name: 'payTypeStr',
          key: ''
        },
        {
          name: 'remark',
          key: ''
        },

        {
          name: 'creationTime',
          key: ''
        },
        {
          name: 'payTime',
          key: ''
        },
        {
          name: 'buyMemberCode',
          key: ''
        },
        {
          name: 'buyMemberName',
          key: ''
        },
        {
          name: 'orderAssAchieve',
          key: ''
        }
      ],
      topList: [
        {
          name: '商品订单',
          path: 'spdd'
        },
        {
          name: '订单详情',
          path: 'ckspdd'
        }
      ],

      multipleSelection: [],
      queryParams: {
        vertexId: '',
        pageNum: 1,
        pageSize: 50,
        shoppingAddress: [],
        orderCode: '', // 订单编号
        memberCode: '', // 会员编号
        memberName: '', // 会员名称
        phone: '', // 电话
        orderType: '', // 订单类型
        deliveryWay: '', // 发货方式
        payType: '', // 支付方式
        recName: '', // 收货人
        recPhone: '', // 收货电话
        recProvince: '',
        recCity: '',
        recCountry: '',
        recAddress: '', // 详细地址
        startCreateDate: '',
        endCreateDate: '',
        startPayDate: '',
        endPayDate: '',
        orderStatus: ''
      },
      loading: false,
      tableData: [],
      // 折叠搜索框
      isActive: false,
      areaData: [],
      regionParams: {
        label: 'name',
        value: 'id',
        children: 'children',
        expandTrigger: 'hover'
      },
      typeList: [],
      fhwayList: [],
      paywayList: [],
      orderStatusList: [],
      total: 0,
      flag: false
    }
  },
  created() {
    this.getData()
    this.getSearch()
    this.getUserRole()
    this.getOrderStatus()
  },
  methods: {
    getOrderStatus() {
      getStatus().then((res) => {
        this.orderStatusList = res.data
      })
    },

    getUserRole() {
      getRoleMenu('Commodity').then((res) => {
        this.dataList.forEach((item) => {
          res.data.forEach((items) => {
            if (item.name == items) {
              item.key = items
            }
            this.flag = true
          })
        })
      })
    },
    // 导出
    handleExport() {
      this.$confirm('是否确认导出所有数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then((_) => {
        this.download(
          '/sale/manage/order/get-order-list-export',
          { ...this.queryParams },
          `${'商品订单'}${new Date().getTime()}.xlsx`
        )
      })
    },
    handleChange1() {},
    findDetail(row, data) {
      this.$router.push({
        path: '/logistics/commodity/ckspdd',
        query: { orderCode: row.orderCode }
      })
    },
    getData() {
      api.areaList().then((res) => {
        this.areaData = res.data
      })
      log.getOrderType().then((res) => {
        this.typeList = res.data
      })
      log.getDeliveryList().then((res) => {
        this.fhwayList = res.data
      })
      log.getOrderPayType().then((res) => {
        this.paywayList = res.data
      })
      getVartexList().then((res) => {
        this.vertexList = res.data
      })
    },
    // 展开折叠
    changeActive() {
      this.isActive = !this.isActive
    },
    changeCjsj(val) {
      this.queryParams.startCreateDate = val[0]
      this.queryParams.endCreateDate = val[1]
    },
    changeZfsj(val) {
      this.queryParams.startPayDate = val[0]
      this.queryParams.endPayDate = val[1]
    },
    getSearch() {
      this.loading = true
      this.queryParams.recProvince = this.queryParams.shoppingAddress[0]
      this.queryParams.recCity = this.queryParams.shoppingAddress[1]
      this.queryParams.recCountry = this.queryParams.shoppingAddress[2]
      log.getOrderList(this.queryParams).then((res) => {
        this.tableData = res.rows
        this.total = res.total
        this.loading = false
      })
    },
    // 重置
    reChongzhi() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 50,
        shoppingAddress: []
      }
      this.getSearch()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      // if (columnIndex === 0) {
      //   if (rowIndex % 2 === 0) {
      //     return {
      //       rowspan: 2,
      //       colspan: 1,
      //     };
      //   } else {
      //     return {
      //       rowspan: 0,
      //       colspan: 0,
      //     };
      //   }
      // }
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 == 1) {
        return 'warning-row'
      } else if (rowIndex % 2 == 0) {
        return 'success-row'
      }
      return ''
    }
  }
}
</script>

<style lang="scss" scoped>
.el-range-editor.el-input__inner {
  width: 100%;
}
.el-date-editor--datetimerange.el-input,
.el-date-editor--datetimerange.el-input__inner {
  width: 100%;
}
::v-deep .dizhi {
  width: 100%;
}
::v-deep .el-table .warning-row {
  background: #f9f9f9;
}

::v-deep .el-table .success-row {
  background: #ffffff;
}
::v-deep .el-table thead {
  color: #000000;
}
.openClose {
  margin-top: 10px;
  margin-right: 10px;
  color: #666666;
  text-align: center;
  height: fit-content;
}
.page {
  padding: 10px 20px;
  background: #f9f9f9;
  font-size: 14px;
  .thetopbox {
    padding: 20px 0;
    background: #ffffff;
    border-radius: 8px;
    .searchbox {
      display: flex;
      align-items: center;
      .searchtitle {
        margin-right: 10px;
      }
      .searchbtn {
        background: #c8161d;
        color: #ffffff;
      }
    }
  }
  .maintop {
    display: flex;
    padding: 10px 20px;
    justify-content: space-between;
    align-items: center;
    .mainbtn {
      .thebtn2 {
        color: #ffffff;
        background: #ffad41;
      }
    }
  }
  .main {
    // margin-top: 20px;
    background: #ffffff;
    border-radius: 8px;
    box-shadow: 0px 2px 20px 0px rgba(238, 238, 238, 0.5);
  }
}
</style>
