<template>
  <div>
    <div class="d-flex flex-between pt7">
      <div>
        <el-button type="primary" @click="exportAll">导出</el-button>
        <el-button type="primary" @click="addData">添加</el-button>
      </div>
      <el-form :inline="true">
        <el-form-item class="mr10" label="">
          <el-date-picker
            v-model="firstForm.time"
            @change="getTime"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-button type="primary" class="ml5" @click="getListData"
          >搜索</el-button
        >
        <!-- <el-button type="primary" class="ml5" @click="clearData">清空</el-button> -->
      </el-form>
    </div>

    <el-table
      :data="tableData"
      tooltip-effect="dark"
      v-loading="loading"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" align="center" width="55">
      </el-table-column>
      <el-table-column label="报表名" width="auto">
        <template slot-scope="scope">
          <h3
            class="font-size-14 color-2 hand"
            @click="toDetail(scope.row.xdxb_id)"
          >
            {{ scope.row.xdxb_title }}
          </h3>
        </template>
      </el-table-column>
      <el-table-column label="日期范围" prop="xdxb_qdzdysj">
        <template slot-scope="scope">
          {{ scope.row.xdxb_qdzdysj.replace(",", " - ") }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="xdxb_cjsj"> </el-table-column>
      <el-table-column label="操作" width="300">
        <template slot-scope="scope">
          <el-button type="text" @click="exportOne(scope.row.xdxb_id)"
            >下载
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <footer
      class="flex-item-none"
      style="display: flex; justify-content: flex-end; margin-top: 30px"
    >
      <el-pagination
        background
        layout="total,prev, pager, next"
        :page-size="limit"
        :total="total"
        :current-page.sync="cur_page"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </footer>
    <el-dialog
      :title="title"
      :visible.sync="dialogFormVisible"
      append-to-body
      width="680px"
    >
      <el-form
        :inline="true"
        :model="dialogForm"
        ref="dialogForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item class="mr75" label="报表名" prop="bbm">
          <el-input
            placeholder="请输入报表名"
            v-model="dialogForm.bbm"
          ></el-input>
        </el-form-item>
        <el-form-item class="mr75" label="显示列" prop="col_ids">
          <el-select
            v-model="dialogForm.col_ids"
            value-key="col_id"
            placeholder="请选择要显示的列"
            clearable
            filterable
            multiple
            @change="handleColChange"
          >
            <el-option
              v-for="item in showList"
              :label="item.col_name"
              :value="item"
              :key="item.col_id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="mr75" label="排序" prop="rankCol_id">
          <el-select
            v-model="dialogForm.rankCol_id"
            placeholder="请选择要排序的列"
            clearable
            filterable
          >
            <el-option
              v-show="!item.isShow"
              v-for="item in showList"
              :label="item.col_name"
              :value="item.col_id"
              :key="item.col_id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="mr75" label="" prop="rankCol_id">
          <el-select
            v-model="dialogForm.rank_id"
            placeholder="请选择排序方式"
            clearable
            filterable
          >
            <el-option
              v-for="item in rankList"
              :label="item.rank_name"
              :value="item.rank_id"
              :key="item.rank_id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="日期范围" prop="sj">
          <el-date-picker
            v-model="dialogForm.sj"
            @change="handleDateChange"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="text-center">
        <el-button @click="formCancel">取 消</el-button>
        <el-button type="primary" @click="formSubmit('dialogForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { bbgl_list, bbgl_add, bbgl_export } from "./api";
import { useUserStore } from "@/stores/user";
const { token } = useUserStore();
export default {
  name: "index",
  data() {
    return {
      limit: 10,
      total: 12,
      cur_page: 1,
      keyword: "",
      title: "",
      xdxb_cjsj: "",
      tableData: [],
      loading: false,
      id: "",
      dialogForm: {
        bbm: "",
        sj: "",
        lx: this.id,
        col_ids: [],
        rankCol_id: "",
        rank_id: "desc",
      },
      firstForm: {
        time: [],
      },
      rules: {
        col_ids: [
          { required: true, message: "请选择要显示的列", trigger: "blur" },
        ],
        bbm: [{ required: true, message: "请输入报表", trigger: "blur" }],
        sj: [{ required: true, message: "请选择时间", trigger: "blur" }],
      },
      dialogFormVisible: false,
      multipleSelection: [],
      showList: [
        {
          col_name: "全部",
          col_id: "",
          isShow: true,
        },
        {
          col_name: "学生姓名",
          col_id: "xdx_xsxm",
        },
        {
          col_name: "学籍号",
          col_id: "xdx_xsxh",
        },
        {
          col_name: "出生日期",
          col_id: "xdx_csrq",
        },
        {
          col_name: "证件类型",
          col_id: "xdx_zjlx",
        },
        {
          col_name: "曾用名",
          col_id: "xdx_zym",
        },
        {
          col_name: "证件号码",
          col_id: "xdx_zjhm",
        },
        {
          col_name: "籍贯",
          col_id: "xdx_jg",
        },
        {
          col_name: "性别",
          col_id: "xdx_xb",
        },
        {
          col_name: "民族",
          col_id: "xdx_mz",
        },
        {
          col_name: "政治面貌",
          col_id: "xdx_zzmm",
        },
        {
          col_name: "文化程度",
          col_id: "xdx_whcd",
        },
        {
          col_name: "入党时间",
          col_id: "xdx_rdsj",
        },
        {
          col_name: "所在支部",
          col_id: "xdx_szzb",
        },
        {
          col_name: "所在党小组",
          col_id: "xdx_szdxz",
        },
        {
          col_name: "家庭住址",
          col_id: "xdx_jtzz",
        },
        {
          col_name: "详细地址",
          col_id: "xdx_xxdz",
        },
        {
          col_name: "上传照片",
          col_id: "xdx_sczp",
        },
        {
          col_name: "联系方式",
          col_id: "xdx_lxfs",
        },
        {
          col_name: "爱好特长",
          col_id: "xdx_ahtc",
        },
        {
          col_name: "异动备注",
          col_id: "xdx_ydbz",
        },
        {
          col_name: "异动当前状态",
          col_id: "xdx_yddqzt",
        },
        {
          col_name: "年级名称",
          col_id: "grade_name",
        },
        {
          col_name: "班级名称",
          col_id: "class_name",
        },
        {
          col_name: "备注",
          col_id: "xdx_bz",
        },
        {
          col_name: "家庭成员",
          col_id: "xdx_jtcy",
        },
        {
          col_name: "获奖荣誉",
          col_id: "xdx_hjry",
        },
        {
          col_name: "年龄",
          col_id: "xdx_nl",
        },
        {
          col_name: "血型",
          col_id: "xdx_xx",
        },
      ],
      rankList: [
        {
          rank_name: "升序",
          rank_id: "asc",
        },
        {
          rank_name: "倒序",
          rank_id: "desc",
        },
      ],
      colNameList: [],
      colIdList: [],
    };
  },
  methods: {
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    getListData() {
      this.loading = true;
      let data = {
        page: this.cur_page,
        limit: this.limit,
        keyword: this.keyword,
        xdxb_cjsj: this.xdxb_cjsj,
        xdxb_blx: this.id,
      };
      if (this.firstForm.time.length > 0) {
        data.xdxb_cjsj =
          this.firstForm.time[0] + " - " + this.firstForm.time[1];
      } else {
        data.xdxb_cjsj = "";
      }
      bbgl_list(data).then((res) => {
        this.loading = false;
        this.tableData = res.data.page_data;
        this.cur_page = Number(res.data.page_now);
        this.total = Number(res.data.total_rows);
      });
    },
    addData() {
      this.title = "添加";
      this.dialogForm = {
        bbm: "",
        sj: "",
        lx: this.id,
        xdxb_custom_json: [],
      };
      this.dialogFormVisible = true;
    },
    formSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (
            this.dialogForm.rankCol_id == "" &&
            this.dialogForm.rank_id != ""
          ) {
            return this.$message({
              message: "请先选择要排序的列",
              type: "error",
            });
          }
          this.dialogForm.col_ids.map((item) => {
            if (!item.col_id) {
              this.dialogForm.col_ids = [];
            }
          });

          let data = {
            xdxb_title: this.dialogForm.bbm,
            xdxb_qdzdysj: this.dialogForm.sj,
            xdxb_blx: this.id,
            xdxb_custom_json: JSON.stringify([
              {
                col_ids: this.dialogForm.col_ids,
                rankCol_id: this.dialogForm.rankCol_id,
                rank_id: this.dialogForm.rank_id,
              },
            ]),
          };
          console.log(data, "data");
          bbgl_add(data).then((res) => {
            if (res.code == 1) {
              this.$message({
                message: "添加成功！",
                type: "success",
              });
              this.dialogFormVisible = false;
              this.getListData();
            } else {
              this.$message({
                message: res.msg,
                type: "error",
              });
            }
          });
        }
      });
    },
    handleColChange(e) {
      this.colNameList = [];
      this.colIdList = [];
      e.map((item) => {
        this.colIdList.push(item.col_id);
        this.colNameList.push(item.col_name);
      });
    },
    formCancel() {
      this.dialogFormVisible = false;
    },
    getTime() {
      if (!this.firstForm.time) {
        this.firstForm.time = [];
      }
      this.cur_page = 1;
      this.getListData();
    },
    handleDateChange(val) {
      // if (val) {
      //   this.dialogForm.sj = val[0] + " - " + val[1];
      // }
      // console.log(this.dialogForm.sj, " this.dialogForm.sj ");
      // this.getListData();
    },
    handleSelectionChange(val) {
      this.multipleSelection = [];
      for (let i in val) {
        this.multipleSelection.push(val[i].xdxb_id);
      }
    },
    exportOne(xdxb_id) {
      let link =
        window.globalVariables.api +
        "/xddy/dygl_xsxjk/index?api=xls&page=1&limit=10000&token=" +
        token +
        "&xdxb_id=" +
        xdxb_id;
      window.open(link, "_blank");
      console.log(link, "_blank");
    },
    exportAll() {
      let time = "";
      if (this.firstForm.time.length > 0) {
        time = this.firstForm.time[0] + " - " + this.firstForm.time[1];
      } else {
        time = "";
      }
      let link =
        window.globalVariables.api +
        "/xddy/dygl_xsxjk_bbgl/index?api=xls&page=1&limit=10000&token=" +
        token +
        "&xdxb_id=" +
        this.multipleSelection.join(",");
    },
    toDetail(id) {
      this.$router.push({
        name: "xsxjk_bbgl_bbxq_xqlb",
        params: { id: id },
      });
    },
    clearData() {
      this.firstForm.time = "";
    },
  },
  mounted() {
    if (this.$route.params.id != "") {
      this.id = this.$route.params.id;
      this.getListData();
    }
  },
};
</script>

<style scoped>
::v-deep .el-date-editor .el-range-separator {
  width: 8%;
}
</style>