<template>
  <base-list>
    <template slot="n_header">
      <el-form v-model="pagerDto" inline>
        <el-row>
          <el-col :span="20">
            <el-form-item label="补充类型">
              <xyy-a-enum :url.sync="enumUrl" :model.sync="pagerDto._Eq_SupplementType" title="补充类型" enum-name="SupplementType" />
            </el-form-item>
            <el-form-item v-if="pagerDto._Eq_SupplementType==2" label="下单渠道">
              <xyy-select-dc :model.sync="pagerDto._Eq_SupplementTarge" />
            </el-form-item>
            <el-form-item v-if="pagerDto._Eq_SupplementType==3" label="服务渠道">
              <xyy-select-pc :model.sync="pagerDto._Eq_SupplementTarge" />
            </el-form-item>
            <el-form-item label="补充节点">
              <xyy-a-enum :url.sync="enumUrl" :model.sync="pagerDto._Eq_JudgmentNode" title="补充节点" enum-name="JudgmentNode" />
            </el-form-item>
            <el-form-item label="补充位置">
              <xyy-a-enum :url.sync="enumUrl" :model.sync="pagerDto._Eq_TrackSupplementLocation" title="补充位置" enum-name="TrackSupplementLocation" />
            </el-form-item>
            <el-form-item label="是否启用">
              <el-select v-model="pagerDto._Eq_IsEnable">
                <el-option>全部</el-option>
                <el-option value="1" label="启用" />
                <el-option value="0" label="停用" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-button type="primary" @click="pagerSerach(1)"> 搜索</el-button>
          </el-col>
        </el-row>
      </el-form>
    </template>
    <template slot="n_option">
      <el-button type="primary" size="small" @click="addConfig()">
        添加配置
      </el-button>
      <el-dropdown style="margin-left:10px" trigger="click" @command="batchOperation">
        <el-button size="small">批量操作<i class="el-icon-arrow-down el-icon--right" /></el-button>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item command="启用">启用</el-dropdown-item>
          <el-dropdown-item command="禁用">禁用</el-dropdown-item>
          <el-dropdown-item command="删除">删除</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
      <el-tag type="info" class="el-icon-info" style="margin-left:10px">
        <b> 请注意，配置优先级为：1.服务渠道 2.下单渠道 3.全部。</b>
      </el-tag>
    </template>
    <template>
      <el-table
        v-loading="dataLoading"
        border=""
        :data="data"
        :highlight-current-row="true"
        :select-on-indeterminate="true"
        width="100%"
        @selection-change="changeSelection"
      >
        <el-table-column type="selection" label="选择" />
        <el-table-column label="补充类型" prop="StrSupplementType" width="140" />
        <el-table-column label="补充对象" prop="SupplementTarge" width="140">
          <template slot-scope="r">
            {{ getTarget(r.row) }}
          </template>
        </el-table-column>
        <el-table-column label="补充节点" prop="StrJudgmentNode" width="140" />
        <el-table-column label="补充位置" prop="StrTrackSupplementLocation" width="140" />
        <el-table-column label="未更新小时数" prop="NoUpdateHour" width="140">
          <template slot-scope="r">
            {{ r.row.NoUpdateHour }}H
          </template>
        </el-table-column>
        <el-table-column label="补充小时数" prop="TrackSupplementHour" width="140">
          <template slot-scope="r">
            +{{ r.row.TrackSupplementHour }}H
          </template>
        </el-table-column>
        <el-table-column label="补充内容" prop="TrackSupplementText" />
        <el-table-column label="是否启用" prop="IsEnable" width="80">
          <template slot-scope="r">
            <el-tag v-if="r.row.IsEnable==0" type="danger">禁用</el-tag>
            <el-tag v-if="r.row.IsEnable==1" type="success">启用</el-tag>
          </template>
        </el-table-column>
        <el-table-column width="80" label="编辑">
          <template slot-scope="r">
            <el-button size="mini" @click="editConfig(r.row)">编辑</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="showConfigEdit" :title="(dto.Id>0?'修改':'添加')+'配置'">
      <el-tag type="danger" class="el-icon-info" style="margin-left:10px;margin-bottom:10px">
        <b> 请注意，配置优先级为：1.服务渠道 2.下单渠道 3.全部。</b>
      </el-tag>
      <el-form label-width="120px" label-position="left">
        <el-form-item label="补充类型">
          <xyy-a-enum :url.sync="enumUrl" :model.sync="dto.SupplementType" title="补充类型" enum-name="SupplementType" />
        </el-form-item>
        <el-form-item v-if="dto.SupplementType==2" label="下单渠道">
          <xyy-select-dc :model.sync="dto.SupplementTarge" />
        </el-form-item>
        <el-form-item v-if="dto.SupplementType==3" label="服务渠道">
          <xyy-select-pc :model.sync="dto.SupplementTarge" />
        </el-form-item>
        <el-form-item label="补充节点">
          <xyy-a-enum :url.sync="enumUrl" :model.sync="dto.JudgmentNode" title="补充节点" enum-name="JudgmentNode" />
        </el-form-item>
        <el-form-item label="补充位置">
          <xyy-a-enum :url.sync="enumUrl" :model.sync="dto.TrackSupplementLocation" title="补充位置" enum-name="TrackSupplementLocation" />
        </el-form-item>
        <el-form-item label="未更新小时数">
          <el-input-number v-model="dto.NoUpdateHour" />
        </el-form-item>
        <el-form-item label="补充小时数">
          <el-input-number v-model="dto.TrackSupplementHour" />
        </el-form-item>
        <el-form-item label="是否启用">
          <el-switch v-model="dto.IsEnable" />
        </el-form-item>
        <el-form-item label="补充内容">
          <el-input v-model="dto.TrackSupplementText" type="textarea" :rows="2" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button>取消</el-button>
        <el-button type="primary" size="small" @click="save()">保存</el-button>
      </div>
    </el-dialog>
  </base-list>
</template>
<script>
import ajaxEnum from '@/components/Xyy/Enums/ajaxEnums'
import deliveryChannel from '@/components/Xyy/Selects/deliveryChannel'
import publicChannel from '@/components/Xyy/Selects/publicChannel'
import BaseList from '@/components/Xyy/baselist'
import { _getDeliverChannel, _getChannel } from '@/api/baseInfo'
import { _query, _add, _update, _disable, _enable, _delete } from '@/api/tracking/traceSupplementConfigApi'
export default {
  name: 'SupplementConfig',
  components: { 'base-list': BaseList, 'xyy-a-enum': ajaxEnum, 'xyy-select-dc': deliveryChannel, 'xyy-select-pc': publicChannel },
  data() {
    return {
      channels: [],
      data: [], dataLoading: false,
      showConfigEdit: false,
      enumUrl: process.env.VUE_APP_Tracking_API + '/Enum',
      pagerDto: { PageIndex: 0, PageSize: 10, total: 0, _Eq_IsDelete: 0 },
      dto: {},
      tableSelection: [],
      delvierChannels: []
    }
  },
  mounted() {
    this.pagerSerach(1)
    _getDeliverChannel().then(r => {
      this.delvierChannels = r
    })
    _getChannel().then((res) => {
      this.channels = res
    })
  },
  methods: {
    getTarget(data) {
      if (data.SupplementType == 1) {
        return '全部'
      } else if (data.SupplementType == 2) {
        for (var i = 0; i < this.channels.length; i++) {
          if (this.channels[i].Id == data.SupplementTarge) {
            return this.channels[i].Name
          }
        }
      } else if (data.SupplementType == 3) {
        for (var j = 0; j < this.delvierChannels.length; j++) {
          if (this.delvierChannels[j].Id == data.SupplementTarge) {
            return this.delvierChannels[j].Name
          }
        }
      }
    },
    changeSelection(selection) {
      this.tableSelection = []
      for (var i = 0; i < selection.length; i++) {
        var x = selection[i]
        this.tableSelection.push(x.Id)
      }
    },
    batchOperation(command) {
      if (this.tableSelection.length == 0) {
        this.$message({ type: 'error', message: '需勾选相应操作的行'
        })
        return
      }
      if (command == '禁用') {
        _disable(this.tableSelection).then(r => {
          this.$message('禁用成功')
          this.pagerSerach(1)
        })
      } else if (command == '启用') {
        _enable(this.tableSelection).then(r => {
          this.$message('启用成功')
          this.pagerSerach(1)
        })
      } else if (command == '删除') {
        _delete(this.tableSelection).then(r => {
          this.$message('删除成功')
          this.pagerSerach(1)
        })
      }
    },
    editConfig(row) {
      this.dto = row
      this.showConfigEdit = true
    },
    save() {
      if (this.dto.Id > 0) {
        _update(this.dto).then(r => {
          this.$message('编辑成功')
          this.pagerSerach(1)
        })
      } else {
        _add(this.dto).then(r => {
          this.$message('添加成功')
          this.pagerSerach(1)
        })
      }
    },

    addConfig() {
      this.dto = { IsEnable: true }
      this.showConfigEdit = true
    },
    pagerSerach(index) {
      this.dataLoading = true
      if (index) {
        this.pagerDto.PageIndex = index
      }
      _query(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>
