<template>
  <div class="app-container">
    <div>
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item>
          <el-select
            v-model="pagerDto._Eq_Id"
            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>
          <el-select v-model="pagerDto._Eq_IsShow" placeholder="是否显示" clearable>
            <el-option v-for="item in [{name:'全部'},{name:'是',value:'true'},{name:'否',value:'false'}]" :key="item.value" :label="item.name" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            icon="el-icon-search"
            size="small"
            type="primary"
            :loading="loading"
            @click="pagerSerach"
          >搜索</el-button>
          <el-button icon="el-icon-plus" size="small" plain type="primary">
            <router-link
              :to="{ name: 'PublicChannelEdit' }"
            >添加渠道</router-link></el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="sData">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left">
            <el-form-item label="包裹限重">
              <span>{{ props.row.MaxWeight }}</span>
            </el-form-item>
            <el-form-item label="货件尺寸">
              <span>{{ props.row.DimensionDescription }}</span>
            </el-form-item>
            <el-form-item label="计费方式">
              <span>{{ props.row.BillingDescription }}</span>
            </el-form-item>
            <el-form-item label="走货属性">
              <span>{{ props.row.GoodsLimit }}</span>
            </el-form-item>
            <el-form-item label="申报要求">
              <span>{{ props.row.DeclareDemand }}</span>
            </el-form-item>
            <el-form-item label="派送范围">
              <span>{{ props.row.DeliveryRange }}</span>
            </el-form-item>
            <el-form-item label="包装要求">
              <span>{{ props.row.PackingDemand }}</span>
            </el-form-item>
            <el-form-item label="宣传图片">
              <el-image
                style="width: 100px; height: 100px"
                :src="props.row.ImageThumbnail"
                :preview-src-list="[props.row.Image]"
              />
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="名称" prop="Name" />
      <el-table-column label="代码" prop="Code" />

      <el-table-column label="渠道类型" prop="ChannelTypeName" />
      <el-table-column label="是否启用" prop="Enable">
        <template scope="s">
          <el-tag v-if="s.row.Enable" type="success">是</el-tag>
          <el-tag v-if="!s.row.Enable" type="danger">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" plain>
            <router-link
              :to="{ name: 'PublicChannelEdit', query: { id: scope.row.Id } }"
            >编辑</router-link>
          </el-button>
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-picture"
            @click="openUpload(scope.row.Id)"
          >上传图片</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :page-sizes="[100, 200, 300, 400]"
      :current-page="pagerDto.PageIndex"
      :page-size="pagerDto.PageSize"
      background
      layout="prev, pager, next ,total ,jumper,sizes,slot "
      :total="pagerDto.total"
      @current-change="pagerSerach"
    />

    <el-dialog
      title="上传图片"
      :visible.sync="openUploadPannel"
      width="30%"
      :before-close="handleClose"
      style="text-align: center"
    >
      <span>
        <el-upload
          ref="uploader"
          drag
          action="http://wmx.xingyunyi.cn/api/PublicChannel/UploadImage"
          :data="uploadParam"
          :on-success="uploadSuccess"
          :on-error="uploadFailed"
        >
          <i class="el-icon-upload" />
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          <div slot="tip" class="el-upload__tip">
            只能上传jpg/png文件，且不超过500kb
          </div>
        </el-upload>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="openUploadPannel = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { _queryPager, setEnabled } from '@/api/publicChannel'
import { _getCustomers, _getAllPublic } from '@/api/baseInfo'
export default {
  name: 'PublicChannel',
  data() {
    return {
      uploadParam: {},
      openUploadPannel: false,
      loading: false,
      sData: [],
      channels: [],
      pagerDto: {
        PageIndex: 1,
        PageSize: 100,
        total: 0,
        _In_TrackingNumber: '',
        QuerySort: [{ Field: 'Id', IsDesc: true }],
        _Eq_Id: '',
        _Eq_Enable: 'True'
      }
    }
  },
  created() {
    this.pagerSerach()
    _getAllPublic().then((response) => {
      this.channels = response
    })
  },
  methods: {
    openUpload(id) {
      this.uploadParam = { id }
      this.openUploadPannel = true
      this.$nextTick(() => {
        this.$refs.uploader.clearFiles()
      })
    },
    uploadSuccess(response, file, fileList) {
      this.openUploadPannel = false
      this.$message({
        message: '上传成功',
        type: 'success'
      })
    },
    uploadFailed() {
      this.$message.error('上传失败')
    },
    handleClose() {},
    pagerSerach() {
      this.ladding = true
      _queryPager(this.pagerDto)
        .then((response) => {
          this.sData = response.Result
          this.loading = false
          this.pagerDto.total = response.TotalCount
        })
        .catch((response) => {
          this.loading = false
        })
    }
  }
}
</script>
