<template>
  <div>
    <div class="d-flex flex-between">
      <div>
        <el-button type="primary" size="medium" @click="addData">
          <span class="d-in-block v-mid el-icon-plus font-size-16"></span>
          <span class="d-in-block v-mid font-size-16">新建报表</span>
        </el-button>
        <el-button type="primary" size="medium" @click="exportAll">
          <span class="d-in-block v-mid font-size-16">导出</span>
        </el-button>
        <el-button
          type="danger"
          size="medium"
          @click="delData"
          :disabled="multipleSelection.length == 0"
        >
          <span class="d-in-block v-mid font-size-16">删除</span>
        </el-button>
      </div>
      <el-form label-width="100px" :inline="true">
        <el-form-item prop="time">
          <el-date-picker
            style="width: 100%"
            v-model="time"
            type="date"
            placeholder="请选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item prop="keyword">
          <el-input
            placeholder="请输入搜索关键字"
            clearable
            @clear="getListData"
            v-model="keyword"
          >
          </el-input>
        </el-form-item>
        <el-button type="primary" class="ml5" @click="getListData"
          >搜索</el-button
        >
      </el-form>
    </div>
    <el-table
      :data="tableData"
      tooltip-effect="dark"
      v-loading="loading"
      ref="multipleTable"
      @selection-change="handleSelectionChange"
      style="width: 100%"
    >
      <el-table-column type="selection" align="center" width="55">
      </el-table-column>
      <el-table-column label="序号" align="center" type="index" width="50">
      </el-table-column>
      <el-table-column
        label="报表名称"
        show-overflow-tooltip
        prop="jjb_name"
        width="auto"
      >
      </el-table-column>
      <el-table-column
        label="创建人"
        width="250"
        prop="jjb_cjr"
      ></el-table-column>
      <el-table-column
        label="创建时间"
        width="300"
        prop="jjb_cjsj"
      ></el-table-column>
      <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <el-button type="text" @click="editData(scope.row)">编辑 </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" width="580px">
      <el-form :model="dialogForm" label-width="80px">
        <el-form-item label="报表名称" prop="bbmc">
          <el-input
            v-model="dialogForm.bbmc"
            placeholder="请输入报表名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="分组依据" prop="fzyj">
          <el-select
            v-model="dialogForm.fzyj"
            style="width: 100%"
            placeholder="请选择分组依据"
          >
            <el-option
              v-for="item in fz_list"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="数据分析" prop="sjfx">
          <el-input v-model="dialogForm.sjfx"></el-input>

          <!-- <el-select
            v-model="dialogForm.sjfx"
            multiple
            style="width: 100%"
            placeholder="请选择数据分析"
          >
            <el-option
              v-for="item in fx_list"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select> -->
        </el-form-item>
        <el-form-item label="权限控制" prop="qxkz">
          <el-radio-group v-model="dialogForm.qxkz">
            <el-radio :label="1">全体所有</el-radio>
            <el-radio :label="2">指定人员/部门/学校</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="dialogForm.qxkz == 2">
          <el-select
            v-model="dialogForm.qxlb"
            multiple
            style="width: 100%"
            placeholder="请选择指定人员/部门/学校"
          >
            <el-option
              v-for="item in lb_list"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="流程类型" prop="lclx">
          <el-select
            v-model="dialogForm.lclx"
            style="width: 100%"
            placeholder="请选择流程类型"
          >
            <el-option
              v-for="item in type_list"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间" prop="cjsj">
          <el-date-picker
            v-model="dialogForm.cjsj"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审核人" prop="shry">
          <el-select
            v-model="dialogForm.shry"
            multiple
            style="width: 100%"
            placeholder="请选择审核人"
          >
            <el-option
              v-for="item in ry_list"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="formCancel">取 消</el-button>
        <el-button type="primary" @click="formSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { useUserStore } from "@/stores/user";
const { real_name, token } = useUserStore();
import { bbtj_list, bbtj_add, bbtj_edit, bbtj_del } from "./api";
export default {
  name: "index",
  data() {
    return {
      time: "",
      keyword: "",
      tableData: [],
      limit: 10,
      total: 12,
      cur_page: 1,
      dialogFormVisible: false,
      dialogForm: {
        bbmc: "",
        fzyj: "",
        sjfx: "",
        qxkz: "",
        qxlb: "",
        lclx: "",
        shr: "",
        cjsj: "",
      },
      type_list: [
        {
          label: "入职流程",
          value: "1",
        },
        {
          label: "流动流程",
          value: "2",
        },
        {
          label: "退休流程",
          value: "3",
        },
        {
          label: "离职流程",
          value: "4",
        },
      ],
      fz_list: [
        {
          label: "创建人",
          value: "1",
        },
        {
          label: "审核人",
          value: "2",
        },
      ],
      fx_list: [
        {
          label: "流程类型",
          value: "1",
        },
        {
          label: "审核人",
          value: "2",
        },
        {
          label: "创建人",
          value: "3",
        },
      ],
      lb_list: [
        {
          label: "办公室",
          value: "1",
        },
        {
          label: "秘书处",
          value: "2",
        },
        {
          label: "工会",
          value: "3",
        },
        {
          label: "教育局",
          value: "4",
        },
        {
          label: "党委办公室",
          value: "5",
        },
      ],
      ry_list: [
        {
          label: "李静",
          value: "1",
        },
        {
          label: "周桂英",
          value: "2",
        },
        {
          label: "吴晗",
          value: "3",
        },
        {
          label: "杨丽梅",
          value: "4",
        },
      ],
      title: "",
      multipleSelection: [],
      id: "",
      loading: false,
      isEdit: false,
    };
  },

  methods: {
    getListData() {
      this.loading = true;
      let data = {
        page: this.cur_page,
        limit: this.limit,
        keyword: this.keyword,
      };

      bbtj_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);
      });
    },
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    addData() {
      this.title = "新建报表";
      this.dialogForm = {
        bbmc: "",
        fzyj: "",
        sjfx: "",
        qxkz: "",
        qxlb: "",
        lclx: "",
        shr: "",
        cjsj: "",
      };
      this.isEdit = false;
      this.dialogFormVisible = true;
    },
    editData(item) {
      this.title = "编辑报表";
      this.isEdit = true;
      this.dialogFormVisible = true;
      this.id = item.jjb_id;
      this.dialogForm.bbmc = item.jjb_name;
      this.dialogForm.fzyj = item.jjb_fzyj;
      this.dialogForm.sjfx = item.jjb_sjfx;
      this.dialogForm.qxkz = item.jjb_qxkz;
      this.dialogForm.cjsj = item.jjb_cjsj;
      this.dialogForm.lclx = item.jjb_lclx;
      this.dialogForm.shr = item.jjb_shr;
    },

    formSubmit() {
      let data = {
        jjb_name: this.dialogForm.bbmc,
        jjb_fzyj: this.dialogForm.fzyj,
        jjb_sjfx: this.dialogForm.sjfx,
        jjb_qxkz: this.dialogForm.qxkz,
        jjb_cjr: real_name,
        jjb_cjsj: this.dialogForm.cjsj,
        jjb_lclx: this.dialogForm.lclx,
        jjb_shr: this.dialogForm.shr,
      };
      if (!this.isEdit) {
        bbtj_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",
            });
          }
        });
      } else {
        data.jjb_id = this.id;
        bbtj_edit(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",
            });
          }
        });
      }
    },

    formCancel() {
      this.dialogFormVisible = false;
    },
    delData() {
      this.$confirm("确认删除这些报表?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let data = {
            jjb_id: this.multipleSelection,
          };

          bbtj_del(data).then((res) => {
            if (res.code == 1) {
              this.$message({
                message: "报表删除成功！",
                type: "success",
              });
              this.getListData();
            } else {
              this.$message({
                message: res.msg,
                type: "error",
              });
            }
          });
        })
        .catch(() => {});
    },

    handleSelectionChange(val) {
      this.multipleSelection = [];
      for (let i in val) {
        this.multipleSelection.push(val[i].jjb_id);
      }
    },
    toDetail(id) {
      this.$router.push({ name: "jxdydj_bbtj_bbxq", params: { id: id } });
    },
    exportAll() {
      let link =
        window.globalVariables.api +
        "/jdbg/jxdddj_bbtj/index?api=xls&page=1&limit=10000&token=" +
        token;
      window.open(link, "_blank");
    },
  },
  mounted() {
    this.getListData();
  },
};
</script>

<style scoped>
</style>