<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="盲票组名称">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入盲票组名称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="盲票组类型">
        <el-select
          v-model="queryParams.ticketType"
          placeholder="请选择盲票组类型"
          clearable
          size="small"
          @change="handleQuery"
        >
          <el-option
            v-for="item in ticketTypeArr"
            :key="item.areaId"
            :label="item.areaName"
            :value="item.areaId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="盲票组状态">
        <el-select
          v-model="queryParams.ticketState"
          placeholder="请选择盲票组状态"
          clearable
          size="small"
          @change="handleQuery"
        >
          <el-option
            v-for="item in ticketStateArr"
            :key="item.areaId"
            :label="item.areaName"
            :value="item.areaId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="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>
      <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"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
        fixed="left"
      />
      <el-table-column label="盲票组ID" prop="boxId" min-width="80" />
      <el-table-column label="盲票图片" prop="picUrl" min-width="100">
        <template slot-scope="scope">
          <div>
            <el-image
              style="width: 100px; height: 100px"
              :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">
        <template slot-scope="scope">
          <div>￥{{ $numberFormat(scope.row.facePrice) }}</div>
        </template>
      </el-table-column>
      <el-table-column label="盲票总数" prop="quantity" width="90">
        <template slot-scope="scope">
          <div>{{ scope.row.quantity }}张</div>
        </template>
      </el-table-column>
      <el-table-column label="已售张数" prop="saleQty" min-width="90">
        <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="90">
        <template slot-scope="scope">
          <div>￥{{ $numberFormat(scope.row.pkgSalePrice) }}/包</div>
        </template>
      </el-table-column>
      <el-table-column label="已采购数量" prop="salePkgQty" min-width="90">
        <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">
          {{ scope.row.status.desc }}
        </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>
          <div>
            <el-button
              size="mini"
              type="text"
              disabled
              @click="handleDetail(scope.row)"
              v-hasPermi="['']"
              >查看</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} from "@/api/business/ticket";
import { publicFileGetUrl } from "@/api/common";
import TicketImport from './import'
export default {
  name: "Ticket",
  components: {
   TicketImport
  },
  data() {
    return {
      loading: false,
      exportLoading: false,
      showSearch: true,
      queryParams: {
        title: "",
        ticketType: "",
        ticketState: "",
      },
      pageParams: {
        pageNum: 1,
        pageSize: 10,
      },
      ticketTypeArr: [],
      ticketStateArr: [],
      // 总条数
      total: 10,
      list: [],
      ids: [],
      importShow:false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 盲票组列表
    getList() {
      this.loading = true;
      getTicketList(
        "pageNum=" +
          this.pageParams.pageNum +
          "&pageSize=" +
          this.pageParams.pageSize +
          "&",
        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;
        });
    },
    // 搜索
    handleQuery() {
      this.getList()
    },
    // 重置
    resetQuery() {},
    // 添加盲票组
    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 = ''
      }
    },

    handleDetail() {},

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

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

    /** 导出按钮操作 */
    handleExport() {
     const boxIds = this.ids;
      this.$confirm('是否确认导出所选盲票数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportTicket({ids:boxIds});
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
  },
  mounted() {},
  destroyed() {},
};
</script>
<style lang="scss">
.ticket-table {
  thead {
    .el-checkbox {
      display: none !important;
    }
  }
  
}
</style>
