<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item :label="$t('名称')" prop="nameZh">
        <el-input v-model="queryParams.nameZh" :placeholder="$t('请输入名称')" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <!-- <el-form-item label="名称-英文" prop="nameEn">
        <el-input v-model="queryParams.nameEn" placeholder="请输入名称-英文" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item> -->
      <el-form-item :label="$t('内部名称')" prop="internalNameZh">
        <el-input v-model="queryParams.internalNameZh" :placeholder="$t('请输入内部名称')" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <!-- <el-form-item label="内部名称-英文" prop="internalNameEn">
        <el-input v-model="queryParams.internalNameEn" placeholder="请输入内部名称-英文" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item> -->
      <el-form-item :label="$t('简码')" prop="typeNumber">
        <el-input v-model="queryParams.code" :placeholder="$t('请输入简码')" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <!-- <el-form-item label="仓库ID字符串" prop="warehouseIds">
        <el-input v-model="queryParams.warehouseIds" placeholder="请输入仓库ID字符串" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item> -->
      <!-- <el-form-item :label="$t('排序')" prop="sort">
        <el-input v-model="queryParams.sort" :placeholder="$t('请输入排序')" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item> -->
      <el-form-item :label="$t('所属国家')" prop="countryId">
        <el-select filterable clearable v-model="queryParams.countryId" :placeholder="$t('请选择国家')">
          <el-option v-for="dict in countryList"
                     :key="dict.id" :label="$l(dict,'title')" :value="parseInt(dict.id)"/>
        </el-select>
        <!--        <el-input v-model="queryParams.countryId" :placeholder="$t('请输入所属国家')" clearable @keyup.enter.native="handleQuery"/>-->
      </el-form-item>
      <!-- <el-form-item label="备注-英文" prop="remarksEn">
        <el-input v-model="queryParams.remarksEn" placeholder="请输入备注-英文" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item> -->
      <!-- <el-form-item label="状态(0:禁用 1:启用)" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态(0:禁用 1:启用)" clearable size="small">
          <el-option :label="$t('请选择字典生成')" value="" />
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item :label="$t('创建时间')">
        <el-date-picker v-model="dateRangeCreateTime" style="width: 240px" value-format="yyyy-MM-dd"
                        type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['ecw:channel:create']">{{ $t('新增') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                   :loading="exportLoading"
                   v-hasPermi="['ecw:channel:export']">{{ $t('导出') }}
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column :label="$t('渠道ID')" align="center" prop="channelId"/>
      <el-table-column :label="$t('名称')" align="center" prop="nameZh">
        <template slot-scope="{row}">{{ $l(row, 'name') }}</template>
      </el-table-column>
      <!-- <el-table-column label="名称-英文" align="center" prop="nameEn" /> -->
      <el-table-column :label="$t('内部名称')" align="center" prop="internalNameZh">
        <template slot-scope="{row}">{{ $l(row, 'internalName') }}</template>
      </el-table-column>

      <!-- <el-table-column label="内部名称-英文" align="center" prop="internalNameEn" />
      <el-table-column :label="$t('类型编码')" align="center" prop="typeNumber" />-->
      <!-- <el-table-column label="仓库id字符串" align="center" prop="warehouseIds" /> -->
      <!--      <el-table-column :label="$t('仓库名')" align="center" prop="warehouseNameList" width="180">-->
      <!--       <template slot-scope="scope">-->
      <!--       <el-scrollbar style="margin-right: 6px;">-->
      <!--           <div class="left">-->
      <!--          <el-tag v-for="warehouseName in scope.row.warehouseNameList"-->
      <!--             :key="warehouseName.index">-->
      <!--             <span>{{warehouseName}}</span>-->
      <!--           </el-tag>-->
      <!--          </div>-->
      <!--       </el-scrollbar>-->
      <!--       </template>-->
      <!--      </el-table-column>-->
      <el-table-column :label="$t('简码')" align="center" prop="code"/>
      <el-table-column :label="$t('排序')" align="center" prop="sort"/>
      <el-table-column :label="$t('快递公司')" align="center" prop="companyName"/>
      <el-table-column :label="$t('预计时间(天)')" align="center" prop="etaTime"/>
      <el-table-column :label="$t('所属国家')">
        <template slot-scope="{row}">
          {{ $l(row, 'countryTitle') }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('备注')" align="center" prop="remarksZh"/>
      <!-- <el-table-column label="备注-英文" align="center" prop="remarksEn" /> -->
      <!-- <el-table-column :label="$t('状态')" align="center" prop="status" /> -->
      <el-table-column :label="$t('创建时间')" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('创建人')" prop="creatorName"></el-table-column>
      <el-table-column :label="$t('更新时间')" prop="">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('更新人')" prop="updaterName">
      </el-table-column>
      <el-table-column :label="$t('状态')" prop="updaterName">
        <template v-slot="{row}">
          <el-switch @change="(e)=>{changeSwitch(e,row)}" :validate-event="false" :active-value="true"
                     v-model="row.status"></el-switch>
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['ecw:channel:update']">{{ $t('修改') }}
          </el-button>
            <el-button size="mini" type="text" icon="el-icon-view" @click="handleUpdate(scope.row,2)"
                     v-hasPermi="['ecw:channel:view']">{{ $t('查看') }}
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['ecw:channel:delete']">{{ $t('删除') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

  </div>
</template>

<script>
import {
  createChannel,
  updateChannel,
  deleteChannel,
  getChannel,
  getChannelPage,
  exportChannelExcel, channelSwitch
} from "@/api/ecw/channel";
import {getCountryListAll} from "@/api/ecw/country";
import {getTradeCountryList} from "@/api/ecw/region";

export default {
  name: "EcwChannelIndex",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: false,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 渠道管理列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dateRangeEtaTime: [],
      dateRangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      countryList: [],
    };
  },
  created() {
    getTradeCountryList().then(r => {
      this.countryList = r.data
    })
    this.getList();
  },
  activated() {
    this.getList();
  },
  methods: {
    changeSwitch(val, row) {
      let title = '是否要' + (val ? '打开' : '关闭') + "《" + this.$l(row, 'name') + '》'
      this.$confirm(title, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        channelSwitch(row.channelId).then(r => {
          this.$message.success('修改成功')
          console.log(r)
        })
      }).catch(() => {
        row.status = !val
      });

    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 处理查询参数
      let params = {...this.queryParams};
      // 执行查询
      getChannelPage(params).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNo: 1,
        pageSize: 10
      }
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      return this.$router.push('create_channel')
    },
    /** 修改按钮操作 */
    handleUpdate(row, type = 1) {
      if (type === 1) {
        return this.$router.push('edit_channel?id=' + row.channelId)
      } else {
        return this.$router.push('view_channel?id=' + row.channelId)
      }
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const channelId = row.channelId;
      this.$modal.confirm(this.$t('是否确认删除渠道管理编号为{channelId}的数据项?', {channelId})).then(function () {
        return deleteChannel(channelId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.addBeginAndEndTime(params, this.dateRangeEtaTime, 'etaTime');
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime');
      // 执行导出
      this.$modal.confirm('是否确认导出所有渠道管理数据项?').then(() => {
        this.exportLoading = true;
        return exportChannelExcel(params);
      }).then(response => {
        this.$download.excel(response, '渠道列表.xls');
        this.exportLoading = false;
      }).catch(() => {
      });
    }
  }
};
</script>

<style lang="scss">

.left {

  .el-tag {
    background-color: #e6f6fd;
    border: 1px solid #ccecfb;
    display: inline-block;
    height: auto;
    padding: 0 10px;
    line-height: 30px;
    font-size: 12px;
    color: #02a1e9;
    border-radius: 4px;
    box-sizing: border-box;
    white-space: nowrap;

  }

}

</style>
