<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="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-row>
        <el-row>
          <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
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="30px" label="" prop="recAddress">
              <el-input v-model="queryParams.recAddress" clearable />
            </el-form-item>
          </el-col>
        </el-row>
        <el-collapse-transition>
          <div v-show="isActive">
            <el-row>
              <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-row>
          </div>
        </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 style="float: right">
        <el-dropdown :hide-on-click="false" style="width: 120px">
          <span class="el-dropdown-link kuang">
            {{ '筛选字段'
            }}<i class="el-icon-arrow-down el-icon--right" />
          </span>
          <el-dropdown-menu slot="dropdown" style="">
            <el-dropdown-item v-for="item in dataList" :key="item.id">
              <el-checkbox
                v-model="item.checked"
                @change="getMenu($event, item.id)"
              >{{ item.text }}</el-checkbox></el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>
    <div class="main">
      <div class="maintable">
        <el-table
          ref="table"
          v-loading="loading"
          :data="tableData"
          style="width: 100%"
          height="660px"
          :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[1].checked == true"
            align="center"
            min-width="200px"
            prop="orderCode"
            :label="'订单编号'"
          />
          <el-table-column
            v-if="dataList[2].checked == true"
            align="center"
            prop="orderTypeVal"
            :label="'订单类型'"
          />
          <el-table-column
            v-if="dataList[3].checked == true"
            align="center"
            min-width="120px"
            prop="memberCode"
            :label="'会员编号'"
          />
          <el-table-column
            v-if="dataList[4].checked == true"
            align="center"
            prop="memberName"
            :label="'会员姓名'"
          />

          <el-table-column
            v-if="dataList[5].checked == true"
            align="center"
            min-width="120px"
            prop="phone"
            :label="'联系方式'"
          />
          <el-table-column
            v-if="dataList[6].checked == true"
            align="center"
            prop="recName"
            :label="'收货人'"
          />
          <el-table-column
            v-if="dataList[7].checked == true"
            align="center"
            min-width="120px"
            prop="recPhone"
            :label="'收货电话'"
          />
          <el-table-column
            v-if="dataList[8].checked == true"
            align="center"
            prop="recProvinceName"
            :label="'省'"
          />
          <el-table-column
            v-if="dataList[9].checked == true"
            align="center"
            prop="recCityName"
            :label="'市'"
          />
          <el-table-column
            v-if="dataList[10].checked == true"
            align="center"
            prop="recCountyName"
            :label="'区/县'"
          />
          <el-table-column
            v-if="dataList[11].checked == true"
            align="center"
            min-width="200px"
            prop="recAddress"
            :label="'详细地址'"
          />
          <el-table-column
            v-if="dataList[12].checked == true"
            align="center"
            min-width="100px"
            prop="orderAmount"
            :label="`${'订单金额'}(${isLocalSymbol()})`"
          />
          <el-table-column
            v-if="dataList[13].checked == true"
            align="center"
            min-width="100px"
            prop="postage"
            :label="'订单邮费'"
          />
          <el-table-column
            v-if="dataList[14].checked == true"
            align="center"
            min-width="100px"
            prop="payMoney"
            :label="$t('实付金额')"
          />
          <el-table-column
            v-if="dataList[15].checked == true"
            align="center"
            min-width="100px"
            prop="orderAchieve"
            :label="'订单业绩' + '(¥)'"
          />
          <el-table-column
            v-if="dataList[16].checked == true"
            align="center"
            prop="deliveryWayVal"
            :label="'发货方式'"
            min-width="100px"
          />
          <el-table-column
            v-if="dataList[17].checked == true"
            min-width="100px"
            align="center"
            prop="payTypeVal"
            :label="'支付方式'"
          />
          <el-table-column
            v-if="dataList[18].checked == true"
            min-width="100px"
            align="center"
            prop="remark"
            :label="'备注'"
          />
          <el-table-column
            v-if="dataList[19].checked == true"
            min-width="100px"
            align="center"
            prop="remarks"
            :label="'开票备注'"
          />
          <el-table-column
            v-if="dataList[20].checked == true"
            min-width="150px"
            align="center"
            prop="creationTime"
            :label="'创建日期'"
          />
          <el-table-column
            v-if="dataList[21].checked == true"
            min-width="150px"
            align="center"
            prop="payTime"
            :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="['CommodityAddRemarks']"
                  type="text"
                  size="small"
                  @click.native.prevent="addRemarks(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"
    />
    <el-dialog
      :title="'开票备注'"
      :close-on-click-modal="false"
      :visible.sync="addReShow"
      width="30%"
      append-to-body
    >
      <div>
        <el-form ref="addreForm" :model="addreForm" label-width="100px">
          <el-row>
            <el-col :span="24">
              <el-form-item :label="'开票备注'" prop="remarks">
                <el-input
                  v-model="addreForm.remarks"
                  :rows="2"
                  type="textarea"
                  autosize
                />
              </el-form-item> </el-col></el-row>
        </el-form>
        <div class="footer" style="text-align: center; margin-top: 20px">
          <el-button @click="addReShow = false">
            {{ '取消' }}</el-button>
          <el-button type="primary" @click="sureAddRemarks('addreForm')">{{
            '确定'
          }}</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import topBar from '@/components/topBar'
import * as log from '@/api/logistics'
import * as api from '@/api/site.js'
import { getRoleMenu } from '@/api/notice'
export default {
  name: 'SpInvoicing',
  components: {
    topBar
  },
  data() {
    return {
      moren: 'spInvoicing',
      allTxt: {},
      dataList: [
        {
          id: 0,
          text: '全选',
          checked: false
        },
        {
          id: 1,
          prop: 'orderCode',
          checked: true,
          text: '订单编号'
        },
        {
          id: 2,
          prop: 'orderTypeVal',
          checked: true,
          text: '订单类型'
        },
        {
          id: 3,
          prop: 'memberCode',
          checked: true,
          text: '会员编号'
        },
        {
          id: 4,
          prop: 'memberName',
          checked: true,
          text: '会员姓名'
        },
        {
          id: 5,
          prop: 'phone',
          checked: true,
          text: '联系方式'
        },
        {
          id: 6,
          prop: 'recName',
          checked: true,
          text: '收货人'
        },
        {
          id: 7,
          prop: 'recPhone',
          checked: true,
          text: '收货电话'
        },
        {
          id: 8,
          prop: 'recProvinceName',
          checked: true,
          text: '省'
        },
        {
          id: 9,
          prop: 'recCityName',
          checked: true,
          text: '市'
        },
        {
          id: 10,
          prop: 'recCountyName',
          checked: true,
          text: '区/县'
        },
        {
          id: 11,
          prop: 'recAddress',
          checked: true,
          text: '详细地址'
        },
        {
          id: 12,
          prop: 'orderAmount',
          checked: true,
          text: '订单金额'
        },
        {
          id: 13,
          prop: 'postage',
          checked: true,
          text: '订单邮费'
        },
        {
          id: 14,
          prop: 'payMoney',
          checked: true,
          text: this.$t('实付金额')
        },
        {
          id: 15,
          prop: 'orderAchieve',
          checked: true,
          text: '订单业绩'
        },
        {
          id: 16,
          prop: 'deliveryWayVal',
          checked: true,
          text: '发货方式'
        },
        {
          id: 17,
          prop: 'payTypeVal',
          checked: true,
          text: '支付方式'
        },
        {
          id: 18,
          prop: 'remark',
          checked: true,
          text: '备注'
        },
        {
          id: 19,
          prop: 'remarks',
          checked: true,
          text: '开票备注'
        },
        {
          id: 20,
          prop: 'creationTime',
          checked: true,
          text: '创建日期'
        },
        {
          id: 21,
          prop: 'payTime',
          checked: true,
          text: '支付日期'
        }
      ],
      topList: [
        {
          name: '订单开票',
          path: 'spInvoicing'
        }
      ],

      multipleSelection: [],
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        shoppingAddress: [],
        orderCode: '', // 订单编号
        memberCode: '', // 会员编号
        memberName: '', // 会员名称
        phone: '', // 电话
        orderType: '', // 订单类型
        deliveryWay: '', // 发货方式
        payType: '', // 支付方式
        recName: '', // 收货人
        recPhone: '', // 收货电话
        recProvince: '',
        recCity: '',
        recCountry: '',
        recAddress: '', // 详细地址
        startCreateDate: '',
        endCreateDate: '',
        startPayDate: '',
        endPayDate: ''
      },
      loading: false,
      tableData: [],
      // 折叠搜索框
      isActive: false,
      areaData: [],
      regionParams: {
        label: 'name',
        value: 'id',
        children: 'children',
        expandTrigger: 'hover'
      },
      typeList: [],
      fhwayList: [],
      paywayList: [],
      total: 0,
      flag: false,
      pkOrder: '',
      addReShow: false,
      addreForm: {
        remarks: ''
      }
    }
  },
  created() {
    this.getData()
    this.getSearch()
    this.getUserRole()
  },
  methods: {
    getMenu(e, id) {
      if (e && id == 0) {
        this.dataList.forEach((item) => {
          this.$set(item, 'checked', true)
        })
      } else if (!e && id == 0) {
        this.dataList.forEach((item) => {
          this.$set(item, 'checked', false)
        })
      }
    },
    // 开票备注
    addRemarks(row, data) {
      this.pkOrder = row.pkOrder
      this.addReShow = true
    },
    sureAddRemarks(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          log
            .updateOrderRemarks({
              pkOrder: this.pkOrder,
              remarks: this.addreForm.remarks
            })
            .then((res) => {
              if (res.code == 200) {
                this.$message({
                  message: res.msg,
                  type: 'success'
                })
                this.addReShow = false
                this.getSearch()
              }
            })
        }
      })
    },
    getUserRole() {
      getRoleMenu('Commodity').then((res) => {
        const obj = {}
        res.data.forEach((item) => {
          obj[item] = 1
        })
        this.allTxt = obj
        console.log(this.allTxt, 'all')
        // this.$nextTick(() => {
        //   this.$refs.table.doLayout();
        // });
      })
    },
    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
      })
    },
    // 展开折叠
    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.getOrderLists(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>
.kuang {
  padding: 5px 20px;
  background: rgba(255, 255, 255, 0);
  border-radius: 4px;
  border: 1px solid #cccccc;
}
.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;
}
.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: 8px;
    justify-content: flex-end;
    align-items: center;
    margin-right: 10px;
    .mainbtn {
      display: flex;
      // justify-content: space-between;
      align-content: center;
      width: 100%;
      .thebtn1 {
        color: #ffffff;
        background: #3181e5;
      }
      .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>
