<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <!--<el-form-item label="起始卡号" prop="startno">-->
        <!--<el-input-->
          <!--v-model="queryParams.startno"-->
          <!--placeholder="请输入起始卡号"-->
          <!--clearable-->
          <!--size="small"-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="截至卡号" prop="endno">-->
        <!--<el-input-->
          <!--v-model="queryParams.endno"-->
          <!--placeholder="请输入截至卡号"-->
          <!--clearable-->
          <!--size="small"-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->

      <!--<el-form-item label="代理商code" prop="sender">-->
        <!--<el-input-->
          <!--v-model="queryParams.sender"-->
          <!--placeholder="请输入代理商code"-->
          <!--clearable-->
          <!--size="small"-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="分配时间时间" prop="createTime">-->
        <!--<el-date-picker clearable size="small"-->
          <!--v-model="queryParams.createTime"-->
          <!--type="date"-->
          <!--value-format="yyyy-MM-dd HH:mm:ss"-->
          <!--placeholder="选择分配时间时间">-->
        <!--</el-date-picker>-->
      <!--</el-form-item>-->
      <el-form-item label="代理商" prop="sender">
        <el-select v-model="queryParams.sender" placeholder="代理商" filterable clearable size="small">
          <el-option :value="''">所有</el-option>
          <el-option
            v-for="item in busiAgentOptions"
            :key="item.agentId"
            :label="item.name"
            :value="item.agentId"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="学校" prop="schoolId">
        <el-select v-model="queryParams.schoolId" placeholder="学校" filterable clearable size="small">
          <el-option
            v-for="item in busiSchoolsOptions"
            :key="item.id"
            :label="
            item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="卡类型" prop="cardType">
        <el-select v-model="queryParams.cardType" placeholder="请选择卡类型" clearable>
          <el-option
            v-for="dict in dict.type.card_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="重新开卡" prop="isReopen">
        <el-select v-model="queryParams.isReopen" placeholder="请选择重新开卡" clearable>
          <el-option
            v-for="dict in dict.type.bool_flag"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </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"-->
          <!--plain-->
          <!--icon="el-icon-plus"-->
          <!--size="mini"-->
          <!--@click="handleAdd"-->
          <!--v-hasPermi="['back:bBusiCardsOpen:add']"-->
        <!--&gt;新增</el-button>-->
      <!--</el-col>-->
      <!--<el-col :span="1.5">-->
        <!--<el-button-->
          <!--type="success"-->
          <!--plain-->
          <!--icon="el-icon-edit"-->
          <!--size="mini"-->
          <!--:disabled="single"-->
          <!--@click="handleUpdate"-->
          <!--v-hasPermi="['back:bBusiCardsOpen:edit']"-->
        <!--&gt;修改</el-button>-->
      <!--</el-col>-->
      <!--<el-col :span="1.5">-->
        <!--<el-button-->
          <!--type="danger"-->
          <!--plain-->
          <!--icon="el-icon-delete"-->
          <!--size="mini"-->
          <!--:disabled="multiple"-->
          <!--@click="handleDelete"-->
          <!--v-hasPermi="['back:bBusiCardsOpen:remove']"-->
        <!--&gt;删除</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="['back:bBusiCardsOpen:export']"-->
        <!--&gt;导出</el-button>-->
      <!--</el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bBusiCardsOpenList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="学校" align="center" prop="schoolName" />
      <el-table-column label="开始卡号" align="center" prop="startNo" />
      <el-table-column label="截止卡号" align="center" prop="endNo" />
      <el-table-column label="卡数量" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <router-link :to="'/back/bBusiCardsOpenDetail/' + scope.row.id" class="link-type">
            <span>{{scope.row.count}}</span>
          </router-link>
        </template>
      </el-table-column>
      <!--<el-table-column label="卡类型" align="center" prop="cardtype" :formatter="typeFormat" />-->
      <el-table-column label="卡类型" align="center" prop="cardType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.card_type" :value="scope.row.cardType"/>
        </template>
      </el-table-column>
      <el-table-column label="代理商" align="center" prop="agent.name" />
      <el-table-column label="重新开卡" align="center" prop="isReopen">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bool_flag" :value="scope.row.isReopen"/>
        </template>
      </el-table-column>
      <el-table-column label="分配时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>

      <!--<el-table-column label="年级" align="center" prop="greadid" />-->
      <!--<el-table-column label="班级/入学年份" align="center" prop="year" />-->
      <!--<el-table-column label="状态(0:无效,1:审核结束,2:审核中)" align="center" prop="status" />-->
      <!--<el-table-column label="操作" 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="['back:bBusiCardsOpen:edit']"-->
          <!--&gt;修改</el-button>-->
          <!--<el-button-->
            <!--size="mini"-->
            <!--type="text"-->
            <!--icon="el-icon-delete"-->
            <!--@click="handleDelete(scope.row)"-->
            <!--v-hasPermi="['back:bBusiCardsOpen:remove']"-->
          <!--&gt;删除</el-button>-->
        <!--</template>-->
      <!--</el-table-column>-->
    </el-table>

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

    <!-- 添加或修改卡分配记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学校" prop="schoolid">
          <el-input v-model="form.schoolid" placeholder="请输入学校" />
        </el-form-item>
        <el-form-item label="起始卡号" prop="startno">
          <el-input v-model="form.startno" placeholder="请输入起始卡号" />
        </el-form-item>
        <el-form-item label="截至卡号" prop="endno">
          <el-input v-model="form.endno" placeholder="请输入截至卡号" />
        </el-form-item>
        <el-form-item label="结束日期" prop="enddate">
          <el-date-picker clearable size="small"
            v-model="form.enddate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="代理商code" prop="sender">
          <el-input v-model="form.sender" placeholder="请输入代理商code" />
        </el-form-item>
        <el-form-item label="处理结果(0:未通过,1:已开卡)" prop="res">
          <el-input v-model="form.res" placeholder="请输入处理结果(0:未通过,1:已开卡)" />
        </el-form-item>
        <el-form-item label="卡类型" prop="cardtype">
          <el-select v-model="form.cardtype" placeholder="请选择卡类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="年级" prop="greadid">
          <el-input v-model="form.greadid" placeholder="请输入年级" />
        </el-form-item>
        <el-form-item label="班级/入学年份" prop="year">
          <el-input v-model="form.year" placeholder="请输入班级/入学年份" />
        </el-form-item>
        <el-form-item label="状态(0:无效,1:审核结束,2:审核中)">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBBusiCardsOpen, getBBusiCardsOpen, delBBusiCardsOpen, addBBusiCardsOpen, updateBBusiCardsOpen, exportBBusiCardsOpen } from "@/api/back/bBusiCardsOpen";
import { listBusiAgents} from "@/api/voluntary/busiAgents";
import { getAllBusiSchools} from "@/api/voluntary/busiSchools";

export default {
  name: "BusiCardsOpen",
  dicts: ['card_status', 'bool_flag', 'card_type', 'bool_flag'],
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 卡分配记录表格数据
      bBusiCardsOpenList: [],
      busiAgentOptions: [],
      busiSchoolsOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      resOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        schoolId: null,
        startNo: null,
        endNo: null,
        endDate: null,
        sender: null,
        createTime: null,
        isReopen: null,
        cardType: null,
        gradeId: null,
        year: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        createTime: [
          { required: true, message: "分配时间时间不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态(0:无效,1:审核结束,2:审核中)不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("bool_flag").then(response => {
      this.resOptions = response.data;
    });
    listBusiAgents({ pageNum: 1, pageSize: 10000}).then(response => {
      this.busiAgentOptions = response.rows;
    });
    getAllBusiSchools().then(response => {
      this.busiSchoolsOptions = response.data;
    });
  },
  methods: {
    /** 查询卡分配记录列表 */
    getList() {
      this.loading = true;
      listBBusiCardsOpen(this.queryParams).then(response => {
        this.bBusiCardsOpenList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    resFormat(row, column) {
      return this.selectDictLabel(this.resOptions, row.res);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        schoolid: null,
        startno: null,
        endno: null,
        enddate: null,
        sender: null,
        createTime: null,
        res: null,
        cardtype: null,
        greadid: null,
        year: null,
        status: 0
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加卡分配记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBBusiCardsOpen(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改卡分配记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBBusiCardsOpen(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBBusiCardsOpen(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除卡分配记录编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delBBusiCardsOpen(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有卡分配记录数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportBBusiCardsOpen(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        })
    }
  }
};
</script>
