<template>
  <div class="mod-transport">
    <!-- 搜索栏 -->
    <div class="search-bar">
      <el-form :inline="true" class="search-form" ref="searchForm" :model="searchForm" label-width="auto" size="small">
        <div class="input-row">
          <el-form-item label="打印渠道" class="search-form-item" v-if="shopId == 1">
            <el-select
              v-model="searchForm.printChannel"
              clearable
              placeholder="请选择打印渠道"
              size="small">
<!--              <el-option key="KuaiYin" label="快印" value="KuaiYin"></el-option>-->
<!--              <el-option key="WoNiu" label="蜗牛" value="WoNiu"></el-option>-->
              <el-option key="Lightning" label="闪电" value="Lightning"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="结算名称" class="search-form-item">
            <el-input type="text" v-model="searchForm.settleName" placeholder="请输入结算名称"></el-input>
          </el-form-item>
          <el-form-item label="结算状态" class="search-form-item">
            <el-select
              v-model="searchForm.status"
              clearable
              placeholder="请选择结算状态"
              size="small">
              <el-option key="0" label="待结算" value="0"></el-option>
              <el-option key="1" label="已结算" value="1"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="结算时段" class="search-form-item">
            <el-date-picker
              :default-time="['00:00:00', '23:59:59']"
              v-model="dateRange"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              value-format="yyyy-MM-dd HH:mm:ss">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <div class="default-btn primary-btn" @click="searchChange(true)">{{$t('crud.searchBtn')}}</div>
            <div class="default-btn" @click="resetSearchForm('searchForm')">{{$t('shop.resetMap')}}</div>
          </el-form-item>
        </div>
      </el-form>
    </div>

    <div class="main-container">
      <!-- 操作栏 -->
      <div class="operation-bar">
        <div
          v-if="shopId == 1"
          class="default-btn primary-btn"
          @click="addOrUpdateHandle()">
          新增结算
        </div>
      </div>
      <div style="margin-top: 10px; width: 1000px;">
        <el-row :gutter="12">
          <el-col :span="6">
            <el-card :body-style="{padding:'10px'}"shadow="never">
              <div>
                <span style="font-size: 16px">待结算总金额</span>
                <div style="margin-top: 5px;">
                  <span v-if="stat.settleAmount" style="color: green; font-weight: bold;font-size: 20px">+ ¥{{parseFloat(stat.settleAmount).toFixed(2)}}</span>
                  <span v-else style="color: green; font-weight: bold;font-size: 20px">+ ¥0</span>
                </div>
              </div>
            </el-card>
          </el-col>
          <el-col :span="6">
            <el-card :body-style="{padding:'10px'}"shadow="never">
              <div>
                <span style="font-size: 16px">待结算材质金额</span>
                <div style="margin-top: 5px">
                  <span v-if="stat.settleSkuAmount" style="color: green; font-weight: bold;font-size: 20px;">+ ¥{{parseFloat(stat.settleSkuAmount).toFixed(2)}}</span>
                  <span v-else style="color: green; font-weight: bold;font-size: 20px;">+ ¥0</span>
                </div>
              </div>
            </el-card>
          </el-col>
          <el-col :span="6">
            <el-card :body-style="{padding:'10px'}"shadow="never">
              <div>
                <span style="font-size: 16px">待结算打包快递金额</span>
                <div style="margin-top: 5px">
                  <span v-if="stat.settleLaborAmount" style="color: green; font-weight: bold;font-size: 20px;">+ ¥{{parseFloat(stat.settleLaborAmount).toFixed(2)}}</span>
                  <span v-else style="color: green; font-weight: bold;font-size: 20px;">+ ¥0</span>
                </div>
              </div>
            </el-card>
          </el-col>
          <el-col :span="6">
            <el-card :body-style="{padding:'10px'}"shadow="never">
              <div>
                <span style="font-size: 16px">售后结算金额</span>
                <div style="margin-top: 5px">
                  <span v-if="stat.settleAfterSalesCostAmount" style="color: red; font-weight: bold;font-size: 20px">- ¥{{parseFloat(stat.settleAfterSalesCostAmount).toFixed(2)}}</span>
                  <span v-else style="color: red; font-weight: bold;font-size: 20px">- ¥0</span>
                </div>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>

      <!-- 表格 -->
      <div class="table-con transport-table">
        <el-table
          ref="waveTable"
          :data="dataList"
          border
          header-cell-class-name="table-header"
          row-class-name="table-row-low"
          style="width: 100%">
          <el-table-column
            v-if="shopId == 1"
            align="center"
            prop="settleName"
            label="打印渠道">
            <template slot-scope="scope">
              <span class="table-cell-text">{{ scope.row.printChannel }}</span>
            </template>
          </el-table-column>
          <el-table-column
            width="160px"
            align="center"
            prop="settleName"
            label="结算名称">
            <template slot-scope="scope">
              <span class="table-cell-text">{{ scope.row.settleName }}</span>
            </template>
          </el-table-column>
          <el-table-column
            width="160px"
            label="结算时段"
            align="center"
            prop="startTime">
            <template slot-scope="scope">
              <span>{{scope.row.startTime }}</span>
              <br/>
              <span>{{scope.row.endTime }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="结算总金额"
            align="center"
            prop="settleAmount">
            <template slot-scope="scope">
              <span style="color: green; font-weight: bold;font-size: 16px">{{scope.row.settleAmount }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="settleSkuAmount"
            label="材料结算金额">
            <template slot-scope="scope">
              <span style="color: green; font-weight: bold;font-size: 16px">+{{scope.row.settleSkuAmount }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            width="110px"
            label="打包快递结算金额(按件2.8元)"
            prop="settleLaborAmount">
            <template slot-scope="scope">
              <span style="color: green; font-weight: bold;font-size: 16px">+{{scope.row.settleLaborAmount }}</span>
            </template>
          </el-table-column>
          <el-table-column
            v-if="shopId == 1"
            align="center"
            width="110px"
            label="快递结算金额(已发货)"
            prop="settleDeliveryAmount">
            <template slot-scope="scope">
              <span style="color: green; font-weight: bold;font-size: 16px">+{{scope.row.settleDeliveryAmount }}</span>
            </template>
          </el-table-column>
          <el-table-column
            width="110px"
            align="center"
            label="售后结算金额(按成本)"
            prop="settleAfterSalesAmount">
            <template slot-scope="scope">
              <span style="color: red; font-weight: bold;font-size: 16px">-{{scope.row.settleAfterSalesCostAmount }}</span>
            </template>
          </el-table-column>
          <el-table-column
            v-if="shopId === 1"
            align="center"
            label="售后结算金额"
            prop="settleAfterSalesAmount">
            <template slot-scope="scope">
              <span style="color: red; font-weight: bold;font-size: 16px">-{{scope.row.settleAfterSalesAmount }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="settleOrderNums"
            label="订单数">
            <template slot-scope="scope">
              <span>{{scope.row.settleOrderNums }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="settleOrderDeliveryNums"
            label="已发货数">
            <template slot-scope="scope">
              <span>{{scope.row.settleOrderDeliveryNums }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="settleOrderSheets"
            label="材料总张数">
            <template slot-scope="scope">
              <span>{{scope.row.settleOrderSheets }}</span>
            </template>
          </el-table-column>
<!--          <el-table-column-->
<!--            align="center"-->
<!--            prop="settleDeliveryNums"-->
<!--            label="快递总数">-->
<!--            <template slot-scope="scope">-->
<!--              <span>{{scope.row.settleDeliveryNums }}</span>-->
<!--            </template>-->
<!--          </el-table-column>-->
          <el-table-column
            align="center"
            prop="status"
            label="结算状态">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.status === 0" type="danger" effect="dark">待结算</el-tag>
              <el-tag v-if="scope.row.status ===1" type="success" effect="dark">已结算</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            :label="$t('crud.menu')"
            width="160">
            <template slot-scope="scope">
              <div style="display: inline-block;float: left;">
              </div>
              <div
                class="default-btn text-btn"
                @click="viewSettlementDetail(scope.row)">小计</div>
              <div
                v-if="shopId == 1"
                class="default-btn text-btn"
                @click="addOrUpdateHandle(scope.row.thirdSettleId)">
                编辑
              </div>
              <div
                v-if="shopId == 1"
                class="default-btn text-btn"
                @click="syncSettlement(scope.row)">更新</div>
              <br/>
              <div
                v-if="shopId == 1"
                style="margin: 0 0"
                class="default-btn text-btn"
                @click="openModifySettleDeliveryDialog(scope.row.thirdSettleId)">
                修改运费
              </div>
              <br/>
              <div
                style="margin: 0 0"
                class="default-btn text-btn"
                :loading="downloadLoading"
                @click="downloadSettlementSku(scope.row)">
                下载明细单
              </div>
              <br/>
              <div
                style="margin: 0 0"
                class="default-btn text-btn"
                :loading="downloadLoading"
                @click="downloadAfterSales(scope.row)">
                下载售后单
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 分页 -->
      <el-pagination
        v-if="dataList.length"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>

    <!-- 修改波次 -->
    <el-dialog
      title="修改快递运费结算"
      :visible.sync="settleDeliveryVisible"
      v-if="settleDeliveryVisible"
      width="40%"
      center
      :close-on-click-modal="false"
      :close-on-press-escape="false">
      <el-form :model="settlement" label-position="left">
        <el-form-item label="结算账单" label-width="100px">
          <span autocomplete="off">{{settlement.settleName}}</span>
        </el-form-item>
        <el-form-item label="当前快递结算" label-width="100px">
          <span autocomplete="off">{{settlement.settleDeliveryAmount}}</span>
        </el-form-item>
        <el-form-item label="最新快递结算" label-width="100px">
          <el-input v-model="settlement.modifySettleDeliveryAmount" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="updateSettleDeliveryAmount(settlement)">确 认</el-button>
      </div>
    </el-dialog>

    <PrintSettlementAddOrUpdate v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="refreshChange"></PrintSettlementAddOrUpdate>
    <PrintSettlementDetail v-if="settlementDetailVisible" ref="settlementDetail"></PrintSettlementDetail>


  </div>
</template>

<script>
  import {downloadXls} from "@/utils/excel";
  import {downloadZip} from "@/utils/zip";
  import PrintSettlementAddOrUpdate from './print-settlement-add-or-update';
  import PrintSettlementDetail from './third-print-settlement-detail';
  export default {
    components: {PrintSettlementAddOrUpdate, PrintSettlementDetail},
    data () {
      return {
        theData: null, // 保存上次点击查询的请求条件
        addOrUpdateVisible: false,
        settlementDetailVisible: false,
        settleDeliveryVisible: false,
        downloadLoading: false,
        stat:{},
        dataForm: {
          transName: ''
        },
        dataList: [],
        dataListLoading: false,
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 10 // 每页显示多少条
        },
        searchForm: {},
        dateRange: [],
        selectAll: false,
        settlement: {
          settleDeliveryAmount: 0,
          modifySettleDeliveryAmount:null
        }

      }
    },
    created () {
      this.getDataList();
      this.getStat();
    },
    computed: {
      shopId: {
        get () { return this.$store.state.user.shopId }
      },
    },
    methods: {
      getStat(){
        this.$http({
          url: this.$http.adornUrl('/printSettlement/thirdPrintSettlement/stat'),
          method: 'GET',
          params: this.$http.adornParams(this.searchForm)
        }).then(({data}) => {
          this.stat = data;
        }).catch((error) => {
        })
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      viewSettlementDetail(settlement){
        this.settlementDetailVisible = true;
        this.$nextTick(() => {
          this.$refs.settlementDetail.init(settlement)
        })
      },
      openModifySettleDeliveryDialog(thirdSettleId){
        this.getSettlement(thirdSettleId);
        this.settleDeliveryVisible = true;
      },
      refreshChange () {
        this.getDataList(this.page);
      },
      // 获取数据列表
      getDataList (page, newData = false) {
        this.dataListLoading = true
        if (newData || !this.theData) {
          this.searchForm.startTime = this.dateRange === null ? null : this.dateRange[0], // 开始时间
          this.searchForm.endTime = this.dateRange === null ? null : this.dateRange[1], // 结束时间
          this.theData = JSON.parse(JSON.stringify(this.searchForm))
        }
        this.$http({
          url: this.$http.adornUrl('/printSettlement/thirdPrintSettlement/page'),
          method: 'get',
          params: this.$http.adornParams(
            Object.assign(
              {
                current: page == null ? this.page.currentPage : page.currentPage,
                size: page == null ? this.page.pageSize : page.pageSize
              },
              this.theData
            )
          )
        }).then(({data}) => {
          this.dataList = data.records
          this.page.total = data.total
          this.dataListLoading = false
        })
      },
      // 获取详情
      getSettlement(thirdSettleId){
        this.$http({
          url: this.$http.adornUrl('/printSettlement/thirdPrintSettlement/info/' + thirdSettleId),
          method: 'GET',
          params: this.$http.adornParams(),
        }).then(({data}) => {
          if(data){
            this.settlement = data;
          }
        }).catch((error) => {
        })
      },
      updateSettleDeliveryAmount(){
        this.$http({
          url: this.$http.adornUrl('/printSettlement/thirdPrintSettlement/updateDeliveryAmount'),
          method: 'POST',
          data: this.$http.adornData(this.settlement),
        }).then(({data}) => {
          this.settleDeliveryVisible = false;
          this.getDataList(this.page, false);
        }).catch((error) => {
        })
      },
      downloadSettlementSku(settlement){
        this.downloadLoading = true;
        this.$http({
          url: this.$http.adornUrl('/printSettlement/thirdPrintSettlement/downloadSettlementSku'),
          method: 'GET',
          params: this.$http.adornParams(settlement),
          responseType: 'blob'
        }).then(({data}) => {
          let fileName = settlement.settleName + "_结算账单明细表";
          downloadXls(data, fileName + ".xlsx");
          this.downloadLoading = false;
        }).catch((error) => {
        })
      },
      downloadAfterSales(settlement){
        this.downloadLoading = true;
        this.$http({
          url: this.$http.adornUrl('/printSettlement/thirdPrintSettlement/downloadAfterSales'),
          method: 'GET',
          params: this.$http.adornParams(settlement),
          responseType: 'blob'
        }).then(({data}) => {
          let fileName = settlement.settleName + "_售后明细表";
          downloadXls(data, fileName + ".xlsx");
          this.downloadLoading = false;
        }).catch((error) => {
        })
      },
      syncSettlement(settlement){
        this.$http({
          url: this.$http.adornUrl('/printSettlement/thirdPrintSettlement/syncSettlement'),
          method: 'POST',
          data: this.$http.adornData(settlement),
        }).then(({data})=> {
          if(data){
            this.getDataList(this.page, false);
            this.$message({
              message: "更新成功",
              type: 'success'
            })
          }else{
            this.$message({
              message: "暂无更新",
              type: 'warning'
            })
          }
        }).catch((error) => {
        })
      },

      // 条件查询
      searchChange (newData = false) {
        this.getDataList(this.page, newData);
        this.getStat();
      },
      /**
       * 重置表单
       * @param {String} formName 表单名称
       */
      resetSearchForm (formName) {
        this.$refs[formName].resetFields()
        this.searchForm = {};
        this.stat = {settleAmount:0, settleSkuAmount:0, settleAfterCostAmount:0, settleLaborAmount:0}
      },
      handleSizeChange (val) {
        this.page.pageSize = val
        this.getDataList()
      },
      handleCurrentChange (val) {
        this.page.currentPage = val
        this.getDataList(this.page, false)
      }
    }
  }
</script>

<style lang="scss" scoped>

</style>
