<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="盲票组名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入盲票组名称"
          clearable
          size="small"
          style="width: 240px"
          @change="pageParams.pageNum = 1;handleQuery()"

        /><!--          @keyup.enter.native="handleQuery"-->
      </el-form-item>
      <el-form-item label="盲票组类型" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择盲票组类型"
          clearable
          size="small"
          @change="pageParams.pageNum = 1;handleQuery()"
        >
          <el-option label="全部" value="" />
          <el-option label="线上票" value="online" />
          <el-option label="线下票" value="offline" />
        </el-select>
      </el-form-item>
      <el-form-item label="盲票组状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择盲票组状态"
          clearable
          size="small"
          @change="pageParams.pageNum = 1;handleQuery()"
        >
          <el-option label="全部" value="" />
          <el-option label="待出票" value="wait" />
          <el-option label="出票中" value="doing" />
          <el-option label="待上架" value="done" />
          <el-option label="已上架" value="on" />
          <el-option label="已下架" value="off" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="pageParams.pageNum = 1;handleQuery()"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:ticket:add']"
          >添加盲票组</el-button
        >
      </el-col>
      <!--<el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['business:ticket:import']"
          >导入</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['business:ticket:export']"
          >导出</el-button
        >
      </el-col>-->

      <el-col :span="1.5">
        <el-button
          type="infor"
          plain
          icon="el-icon-printer"
          size="mini"
          :loading="exportLoading"
          @click="handleExportDraw"
          v-hasPermi="['business:ticket:export']"
          >导出印刷</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList()"
      ></right-toolbar>
    </el-row>

    <el-table
      ref="table"
      v-loading="loading"
      :data="list"
      @select="handleSelect"
      class="ticket-table"  @sort-change="sortChannelId"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
        fixed="left"
      />
      <el-table-column label="盲票组ID" prop="boxId" min-width="100" sortable="custom" />
      <el-table-column label="盲票图片" prop="picUrl" min-width="120">
        <template slot-scope="scope">
          <div>
            <el-image
              style="width: 70px; height: 80px"
              :src="scope.row.picUrl"
              :preview-src-list="[scope.row.picUrl]"
            />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="盲票组名称" prop="title" min-width="85" />
      <el-table-column label="盲票组面值" prop="facePrice" min-width="85" sortable="custom" width="110px">
        <template slot-scope="scope">
          <div>￥{{ $numberFormat(scope.row.facePrice) }}</div>
        </template>
      </el-table-column>
      <el-table-column label="盲票总数" prop="quantity" width="100px" sortable="custom">
        <template slot-scope="scope">
          <div>{{ scope.row.quantity }}张</div>
        </template>
      </el-table-column>
      <el-table-column label="已售张数" prop="saleQty" min-width="100" sortable="custom">
        <template slot-scope="scope">
          <div>{{ scope.row.saleQty }}张</div>
        </template>
      </el-table-column>
      <el-table-column label="盲票类型" prop="type" min-width="80">
        <template slot-scope="scope">
          {{ scope.row.type.desc }}
        </template>
      </el-table-column>
      <el-table-column label="采购单价" prop="pkgSalePrice" min-width="100" sortable="custom">
        <template slot-scope="scope">
          <div>￥{{ $numberFormat(scope.row.pkgSalePrice) }}/包</div>
        </template>
      </el-table-column>
      <el-table-column label="预付售票价" prop="pkgPrePrice" min-width="100">
        <template slot-scope="scope">
          <div>￥{{ $numberFormat(scope.row.pkgPrePrice) }}/包</div>
        </template>
      </el-table-column>
      <el-table-column label="已采购数量" prop="salePkgQty" min-width="110" sortable="custom">
        <template slot-scope="scope">
          <div>{{ scope.row.salePkgQty }}包</div>
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="status" min-width="80">
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.status.value === 'on' ? 'success' : 'info'"
            >{{ scope.row.status.desc }}</el-tag
          >
        </template>
      </el-table-column>

      <el-table-column label="排序" prop="sortWeight"  sortable="custom" align="center" width="140px">
        <template slot-scope="{row, $index}">
          <el-input-number v-model="row.sortWeight" @change="handleSortChange($index,row.sortWeight)" controls-position="right" size="small"></el-input-number>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        show-overflow-tooltip
        class-name="small-padding  fixed-width"
        min-width="150"
        fixed="right"
      >
        <template slot-scope="scope">
          <div>
            <el-button
              v-hasPermi="['business:ticket:query']"
              type="text"
              @click="getDetail(scope.row)"
              >查看</el-button
            >
            <el-button
              v-hasPermi="['business:ticket:edit']"
              type="text"
              @click="setDetail(scope.row)"
            >编辑</el-button
            >
            <el-button
              v-show="
                scope.row.status.value === 'off' ||
                scope.row.status.value === 'done'
              "
              v-hasPermi="['business:ticket:on']"
              type="text"
              @click="setStatus(scope.row, 'on')"
              >上架</el-button
            >
            <el-button
              v-show="scope.row.status.value === 'on'"
              v-hasPermi="['business:ticket:on']"
              type="text"
              @click="setStatus(scope.row, 'off')"
              >下架</el-button
            >
            <el-button
              v-show="scope.row.status.value === 'done'"
              v-hasPermi="['business:ticket:remove']"
              type="text"
              class="del"
              @click="del(scope.row)"
              >删除</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="pageParams.pageNum"
      :limit.sync="pageParams.pageSize"
      @pagination="getList"
    />

    <!-- 导入 -->
    <ticket-import
      v-if="importShow"
      :dialog-visible="importShow"
      @close="hideDialog"
    />
  </div>
</template>
<script>
import {
  getTicketList,
  exportTicket,
  exportDraw,
  ticketBoxPut,
  ticketBoxRemove, setListData
} from "@/api/business/ticket";
import { publicFileGetUrl } from "@/api/common";
import TicketImport from "./import";
export default {
  name: "Ticket",
  components: {
    TicketImport,
  },
  data() {
    return {
      sortNum:0,
      loading: false,
      exportLoading: false,
      showSearch: true,
      queryParams: {
        title: "",
        type: "",
        status: "",
      },
      pageParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: '',
        isAsc: 'desc',
      },
      // 总条数
      total: 0,
      list: [],
      ids: [],
      importShow: false,
    };
  },
  watch: {
    $route: {
      handler: function (val, oldVal) {
        if (val.name == "Ticket") {
          this.getList();
        }
      },
    },
  },
  created() {
    this.getList();
  },
  methods: {
    //切换排序
    sortChannelId(row){
      if (row){
        let prop =  row.prop == 'channelId' || row.prop == 'commRate' ? ('t1.'+ row.prop) : row.prop;
        this.pageParams.orderByColumn = prop
        this.pageParams.isAsc = row.order=='ascending'?"asc":"desc";
        this.getList()
      }
    },

    //步进器修改
    handleSortChange(index,sortWeight){
      this.list[index].sortWeight = sortWeight
      let data = {
        boxId: this.list[index].boxId,
        sortWeight: this.list[index].sortWeight
      }
      setListData(data)
    },

    // 盲票组列表
    getList() {
      this.loading = true;
      getTicketList( 'pageNum=' + this.pageParams.pageNum + '&pageSize=' + this.pageParams.pageSize + '&orderByColumn='+ this.pageParams.orderByColumn +'&isAsc='+ this.pageParams.isAsc +'&',
        this.queryParams
      )
        .then((res) => {
          this.loading = false;
          if (res.code == 0) {
            res.rows.forEach((item) => {
              let picUrlArr = item.picUrl.split(",");
              item.picUrl = publicFileGetUrl + picUrlArr[0];
              item.status = JSON.parse(item.status);
              item.type = JSON.parse(item.type);
            });
            this.total = res.total;
            this.list = res.rows;
          }
        })
        .catch(() => {
          this.loading = false;
        });
    },

    setStatus(item, status) {
      this.$confirm(
        `确认${status === "on" ? "上架" : "下架"}盲票 “${item.title}” 吗？`,
        `${status === "on" ? "上架" : "下架"}盲票`,
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        ticketBoxPut({
          boxId: item.boxId,
          status,
        }).then((res) => {
          if (res.code === 0) {
            this.$message.success("操作已完成！");
            this.getList();
          }
        });
      });
    },

    del(item) {
      this.$confirm(`确认删除盲票 “${item.title}” 吗？`, "删除盲票", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        ticketBoxRemove({ boxId: item.boxId }).then((res) => {
          if (res.code === 0) {
            this.$message.success("操作已完成！");
            this.getList();
          }
        });
      });
    },

    // 搜索
    handleQuery() {
      this.getList();
    },

    // 重置
    resetQuery() {
      this.resetForm("queryForm");
      this.getList();
    },

    // 添加盲票组
    handleAdd() {
      this.$router.push({ name: "TicketCreate" });
    },

    handleSelect(selection, row) {
      const isSelect = selection.find((item) => item.boxId === row.boxId);
      this.$refs.table.clearSelection();
      if (isSelect) {
        this.$refs.table.toggleRowSelection(row, true);
        this.ids = row.boxId;
      } else {
        this.ids = "";
      }
    },

    // 查看详情
    getDetail(row) {
      this.$router.push({ name: "TicketDetail", query: { id: row.boxId } });
    },

    // 编辑信息
    setDetail(row) {
      this.$router.push({ name: "TicketEdit", query: { id: row.boxId } });
    },

    handleDetail() {},

    handleImport() {
      this.importShow = true;
    },

    hideDialog() {
      this.importShow = false;
    },

    /** 导出按钮操作 */
    handleExport() {
      const boxIds = this.ids || [];

      if (boxIds.length == 0) {
        this.$alert("请选择你要导出的盲票组！", "提示", { type: "warning" });
      } else {
        this.$confirm("是否确认导出所选盲票数据项?", "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.vloading = this.$loading({
              lock: true,
              text: "正在导出.....",
              background: "rgba(0, 0, 0, 0.7)",
            });
            return exportTicket({ ids: boxIds });
          })
          .then((response) => {
            this.vloading.close();
            this.download(response.msg);
            this.exportLoading = false;
          })
          .catch(() => {
            this.vloading.close();
            this.exportLoading = false;
          });
      }
    },

    /** 导出按钮操作 */
    handleExportDraw() {
      const boxIds = this.ids || [];

      if (boxIds.length == 0) {
        this.$alert("请选择你要导出的盲票组！", "提示", { type: "warning" });
      } else {
        this.$confirm("是否确认导出所选盲票数据项?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.vloading = this.$loading({
              lock: true,
              text: "正在导出印刷.....",
              background: "rgba(0, 0, 0, 0.7)",
            });
            return exportDraw({ ids: boxIds });
          })
          .then((response) => {
            this.vloading.close();
            this.download(response.msg);
            this.exportLoading = false;
          })
          .catch(() => {
            this.vloading.close();
            this.exportLoading = false;
          });
      }
    },
  },
  mounted() {},
  destroyed() {},
};
</script>
<style lang="scss">
.ticket-table {
  thead {
    .el-checkbox {
      display: none !important;
    }
  }
}
</style>
