<template>
  <div>
    <div>
      <el-upload
        class="upload-demo"
        :action="import_type_url"
        :show-file-list="false"
        :data="import_type_data"
        :on-success="import_success"
        style="display: inline-block; margin: 0 10px"
      >
        <el-button type="primary">导入</el-button>
      </el-upload>
      <el-button type="primary" @click="exportType('one')">导入模板下载</el-button>
      <el-button type="primary" @click="exportType('mul')">导出</el-button>
      <el-button type="primary" @click="addType">添加</el-button>
      <el-button
        type="danger"
        :disabled="multipleSelection.length == 0"
        @click="delType"
        >删除</el-button
      >
    </div>
    <el-table
      :data="firstTable"
      v-loading="loading"
      tooltip-effect="dark"
      class="mt20"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" align="center" width="55">
      </el-table-column>
      <el-table-column
        label="分类编码"
        align="left"
        prop="xdbh_code"
        width=""
      ></el-table-column>
      <el-table-column
        label="分类名称"
        width=""
        prop="xdbh_name"
      ></el-table-column>
      <el-table-column
        label="审核人"
        width=""
        prop="xdbh_shr_user_realname"
      ></el-table-column>
      <el-table-column label="是否启用" width="">
        <template slot-scope="scope">
          <span v-if="scope.row.xdbh_sfqy_option_k == 1">启用</span>
          <span class="text-red" v-if="scope.row.xdbh_sfqy_option_k == 2"
            >禁用</span
          >
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button type="text" @click="editType(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="typeDialogShow" width="500px">
      <el-form
        :model="typeForm"
        ref="typeForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="分类代码：" prop="xdbh_code">
          <el-input
            v-model="typeForm.xdbh_code"
            placeholder="输入分类代码"
          ></el-input>
        </el-form-item>
        <el-form-item label="分类名称：" prop="xdbh_name">
          <el-input
            v-model="typeForm.xdbh_name"
            placeholder="输入分类名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="审核人：" prop="xdbh_shr">
          <el-select
            v-model="typeForm.xdbh_shr"
            filterable
            placeholder="请选择审核人"
            style="width: 360px"
          >
            <el-option
              v-for="item in shr_list"
              :key="item.id"
              :label="item.label"
              :value="{ value: item.id, label: item.label }"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="启用状态：" prop="xdbh_sfqy">
          <el-radio v-model="typeForm.xdbh_sfqy" label="1">启用</el-radio>
          <el-radio v-model="typeForm.xdbh_sfqy" label="2">禁用</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="typeDialogShow = false">取 消</el-button>
        <el-button type="primary" @click="typeFormSubmit('typeForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { hdlb_list, hdlb_add, hdlb_edit, hdlb_del } from "./api";
import { useUserStore } from "@/stores/user";
const { token } = useUserStore();
export default {
  name: "index",
  data() {
    return {
      firstTable: [],
      limit: 10,
      total: 0,
      loading: false,
      cur_page: 1,
      gradeList: [
        {
          label: "六年级一班",
          value: 1,
        },
        {
          label: "五年级一班",
          value: 2,
        },
        {
          label: "四年级一班",
          value: 3,
        },
        {
          label: "三年级一班",
          value: 4,
        },
      ],
      typeList: [],

      multipleSelection: [],
      title: "",
      typeDialogShow: false,
      shr_list: [
        {
          id: "1",
          label: "管理员",
        },
        {
          id: "2",
          label: "田静",
        },
        {
          id: "3",
          label: "王语嫣",
        },
        {
          id: "4",
          label: "杨军",
        },
      ],
      typeForm: {
        xdbh_id: "",
        xdbh_name: "",
        xdbh_shr: "",
        xdbh_sfqy: "",
        xdbh_code: "",
      },
      rules: {
        xdbh_code: [
          { required: true, message: "请输入分类代码", trigger: "blur" },
        ],
        xdbh_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
        ],
        xdbh_shr: [
          {
            type: "object",
            required: true,
            message: "请选择审核人",
            trigger: "change",
          },
        ],
        xdbh_sfqy: [
          { required: true, message: "请选择启用状态", trigger: "change" },
        ],
      },
      isEdit: false,
      // 导入导出
      import_type_url:
        window.globalVariables.api +
        "/xddy/dygl_bjhd_hdlbgl/import?issubmit=1&token=" +
        token,
      import_type_data: {},
    };
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getActivityType();
    },
    getActivityType() {
      this.loading = true;
      let data = {
        page: this.page,
        limit: this.limit,
      };
      hdlb_list(data).then((res) => {
        if (res.code == 1) {
          this.loading = false;
          this.firstTable = res.data.page_data;
          this.cur_page = Number(res.data.page_now);
          this.total = Number(res.data.total_rows);
        }
      });
    },
    addType() {
      this.title = "添加活动类别";
      this.typeForm = {
        xdbh_id: "",
        xdbh_name: "",
        xdbh_shr: "",
        xdbh_sfqy: "",
        xdbh_code: "",
      };
      this.isEdit = false;
      this.typeDialogShow = true;
    },
    editType(item) {
      this.title = "编辑活动类别";
      this.isEdit = true;
      this.typeForm = {
        xdbh_id: item.xdbh_id,
        xdbh_name: item.xdbh_name,
        xdbh_shr: {
          value: item.xdbh_shr_user_id,
          label: item.xdbh_shr_user_realname,
        },
        xdbh_sfqy: item.xdbh_sfqy_option_k,
        xdbh_code: item.xdbh_code,
      };
      this.typeDialogShow = true;
    },
    typeFormSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let data = {
            xdbh_name: this.typeForm.xdbh_name,
            xdbh_shr_user_id: this.typeForm.xdbh_shr.value,
            xdbh_shr_user_realname: this.typeForm.xdbh_shr.label,
            xdbh_sfqy: this.typeForm.xdbh_sfqy,
            xdbh_code: this.typeForm.xdbh_code,
          };
          if (!this.isEdit) {
            hdlb_add(data).then((res) => {
              if (res.code == 1) {
                this.$message({
                  message: "活动类别新建成功！",
                  type: "success",
                });
                this.typeDialogShow = false;
                this.getActivityType();
              } else {
                this.$message({
                  message: res.msg,
                  type: "error",
                });
              }
            });
          } else {
            data.xdbh_id = this.typeForm.xdbh_id;
            hdlb_edit(data).then((res) => {
              if (res.code == 1) {
                this.$message({
                  message: "活动类别编辑成功！",
                  type: "success",
                });
                this.typeDialogShow = false;
                this.getActivityType();
              } else {
                this.$message({
                  message: res.msg,
                  type: "error",
                });
              }
            });
          }
        }
      });
    },
    delType() {
      let del_arr = [];
      for (let i in this.multipleSelection) {
        del_arr.push(this.multipleSelection[i].xdbh_id);
      }
      this.$confirm("确定要删除这些活动类别?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let data = {
            xdbh_id: del_arr,
          };
          hdlb_del(data).then((res) => {
            if (res.code == 1) {
              this.$message({
                message: "活动类别删除成功！",
                type: "success",
              });
              this.getActivityType();
            } else {
              this.$message({
                message: res.msg,
                type: "error",
              });
            }
          });
        })
        .catch(() => {});
    },
    exportType(type) {
      let link;
      if (type == 'one') {
         link =
          window.globalVariables.api +
          "/xddy/dygl_bjhd_hdlbgl/index?api=xls&page=1&limit=1&token=" +
          token;
      } else {
        let exp_arr = [];
        for (let i in this.multipleSelection) {
          exp_arr.push(this.multipleSelection[i].xdbh_id);
        }
         link =
          window.globalVariables.api +
          "/xddy/dygl_bjhd_hdlbgl/index?api=xls&page=1&limit=10000&token=" +
          token + '&xdbh_id=' + exp_arr.join(',');
      }
      window.open(link, "_self");
    },
    import_success(res) {
      if (res.code == "1") {
        this.$message({
          dangerouslyUseHTMLString: true,
          message: res.msg,
          type: "success",
        });
        this.getActivityType();
      } else {
        this.$message({
          dangerouslyUseHTMLString: true,
          message: res.msg,
          type: "error",
        });
      }
    },
  },
  mounted() {
    this.getActivityType();
  },
};
</script>

<style scoped>
</style>