<template>
  <base-list>
    <template slot="n_header">
      <el-form v-model="pagerDto" inline />
    </template>
    <template slot="n_option">
      <el-button type="primary" icon="el-icon-upload" @click="dialogSet=true">
        修改规则
      </el-button>
    </template>
    <template>
      <el-table
        v-loading="dataLoading"
        :data="data"
        :highlight-current-row="true"
        :select-on-indeterminate="true"
        width="100%"
      >
        <el-table-column prop="Name" label="公开渠道名" />
        <el-table-column prop="ChannelName" label="服务渠道名" />
        <el-table-column prop="AdjAreaQty" label="调整区域数" />
        <el-table-column label="操作">
          <template slot-scope="r">
            <el-button type="primary" @click="showNewDetails(r.row)" plain>
              <i class="el-icon-tickets" />明细
            </el-button>
            <el-button type="primary" @click="showAdj(r.row)">
              <i class="el-icon-picture-outline-round" />查看明细
            </el-button>
            <el-button type="success" @click="getAdjExcel(r.row)">
              <i class="el-icon-download" />下载文件
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :page-sizes="[10, 20, 40,100, 400]"
        :current-page="pagerDto.PageIndex"
        :page-size.sync="pagerDto.PageSize"
        background
        layout="prev, pager, next ,total ,jumper,sizes,slot "
        :total="pagerDto.total"
        @size-change="changePager"
        @current-change="pagerSerach"
      />
    </template>
    <el-dialog :visible.sync="dialogDetails">
      <el-table :data="details">
        <el-table-column label="配置名" prop="Name" />
        <el-table-column label="开始重量" prop="Start" />
        <el-table-column label="结束重量" prop="End" />
        <el-table-column label="上传重量" prop="Adj" />
        <el-table-column label="调整件数" prop="Qty" />
        <el-table-column label="扣减重量" prop="Deduction" />
      </el-table>
    </el-dialog>
    <!-- 新明细 -->
    <el-dialog :visible.sync="newDialogDetails" custom-class="new-dialogdetails">
      <vxe-table
        ref="orederRef"
        border
        height="600px"
        show-overflow
        :data="newDetailsData"
        :row-config="{isHover: true}"
        :header-row-style="{
          background: '#E6F7FF',
          borderRight: '1px solid #ccc',
        }"
        :column-config="{ resizable: true }"
      >
        <vxe-column field="Start" title="开始重量" :min-width="120" />
        <vxe-column field="End" title="结束重量" :min-width="120" />
        <vxe-column field="Adj" title="调整件数百分比" :min-width="140" />
        <vxe-column field="Qty" title="扣减重量" :min-width="120" />
        <vxe-column field="Name" title="配置名" :min-width="110" />
      </vxe-table>
    </el-dialog>
    <el-dialog :visible.sync="dialogSet">
      <el-form>
        <el-form-item label="调整对象">
          <el-select
            v-model="dto.Target"
            placeholder="调整对象"
            clearable
            filterable
          >
            <el-option
              v-for="item in targets"
              :key="item.Id"
              :label="item.Name"
              :value="item.Id"
            />
          </el-select>
        </el-form-item>
        <el-form-item v-if="dto.Target==2" label="派送渠道">
          <el-select
            v-model="dto.ChannelId"
            placeholder="派送渠道"
            clearable
            filterable
          >
            <el-option
              v-for="item in channels"
              :key="item.Id"
              :label="item.Name"
              :value="item.Id"
            />
          </el-select>
        </el-form-item>
        <el-form-item v-if="dto.Target==1" label="下单渠道">
          <el-select
            v-model="dto.CreateChannelId"
            placeholder="下单渠道"
            clearable
            filterable
          >
            <el-option
              v-for="item in publicChannels"
              :key="item.Id"
              :label="item.Name"
              :value="item.Id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="明细文件">
          <el-upload
            class="upload-demo"
            action="http://wms.xingyunyi.cn/api/Finance/updateLoadFile"
            :on-success="changeFileUrl"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传zip文件</div>
            <div slot="tip" class="el-upload__tip"><el-link @click="downtemp()"> 下载模板</el-link></div>
          </el-upload>
        </el-form-item>

        <el-button @click="start">
          保存
        </el-button>
      </el-form>
    </el-dialog>
  </base-list>
</template>
<script>
import BaseList from '@/components/Xyy/baselist'
import { _getAllPublic, _getDeliverChannel } from '@/api/baseInfo'
import { _getWeightAdjDtos, _getPageResult, _setChanngeWeightAdjs, _getWegihtAdjExcel, _getTemp } from '@/api/channel/weightAdj'
export default {
  name: 'WeightAdj',
  components: { 'base-list': BaseList },
  data() {
    return {
      dialogSet: false,
      publicChannels: [],
      channels: [],
      dto: {},
      data: [], dataLoading: false,
      dialogDetails: false,
      newDialogDetails: false,
      details: [],
      newDetailsData: [],
      pagerDto: { PageIndex: 0, PageSize: 10, total: 0 },
      targets: [
        {
          Name: '下单渠道', Id: 1
        },
        {
          Name: '派送渠道', Id: 2
        }]
    }
  },
  mounted() {
    this.pagerSerach(1)
    _getAllPublic().then(r => {
      this.publicChannels = r
    })
    _getDeliverChannel().then(r => {
      this.channels = r
    })
  },
  methods: {
    changeFileUrl(r) {
      this.dto.FileUrl = r.Data.url
    },
    downtemp() {
      _getTemp()
    },
    start() {
      _setChanngeWeightAdjs(this.dto).then(r => {
        this.$message({ type: 'success', message: '上传成功' })
        this.dialogSet = false
        this.pagerSerach()
      })
    },
    showAdj(dto) {
      _getWeightAdjDtos(dto).then(r => { this.details = r; this.dialogDetails = true })
    },
    // 新明细
    showNewDetails(dto) {
      _getWeightAdjDtos(dto).then(r => { this.newDetailsData = r; this.newDialogDetails = true })
    },
    getAdjExcel(dto) {
      _getWegihtAdjExcel(dto)
    },
    pagerSerach(index) {
      this.dataLoading = true
      if (index) {
        this.pagerDto.PageIndex = index
      }
      _getPageResult(this.pagerDto).then(res => {
        this.data = res.Result
        this.pagerDto.total = res.TotalCount
      }).finally(res => { this.dataLoading = false })
    },
    changePager(v) {
      this.PageSize = v
      this.pagerSerach()
    }
  }
}
</script>
<style>
.new-dialogdetails .row--hover {
  background-color: #E6F7FF !important;
}
</style>
