<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="exportAll">导入模板下载</el-button>
      <el-button
        type="primary"
        @click="exportAll"
        :disabled="tableData.length == 0"
        >导出</el-button
      >
      <el-button type="primary" @click="addData">添加</el-button>
      <el-button
        type="danger"
        @click="delData"
        :disabled="multipleSelection.length == 0"
        >删除</el-button
      >
    </div>
    <el-table
      :data="tableData"
      tooltip-effect="dark"
      ref="multipleTable"
      @selection-change="handleSelectionChange"
      class="mt20"
      style="width: 100%"
    >
      <el-table-column type="selection" align="center" width="55">
      </el-table-column>

      <el-table-column label="徽章类型" prop="xdxh_lxmc" width="">
      </el-table-column>
      <el-table-column label="积分" prop="xdxh_jf" width=""> </el-table-column>
      <el-table-column label="是否启用" prop="xdxh_qyzt" width="">
        <template slot-scope="scope">
          <span v-if="scope.row.xdxh_qyzt_option_k == 2" class="text-red"
            >禁用</span
          >
          <span v-else>启用</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <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="700px">
      <el-form
        :inline="true"
        :model="dialogForm"
        ref="dialogForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item class="mr75" label="徽章类型" prop="hzlx">
          <el-input v-model="dialogForm.hzlx"></el-input>
        </el-form-item>
        <el-form-item class="mr10" label="积分" prop="jf">
          <el-input v-model="dialogForm.jf"></el-input>
        </el-form-item>

        <el-form-item class="mr75" label="启用状态" prop="qyzt">
          <el-radio-group v-model="dialogForm.qyzt">
            <el-radio label="1">启用</el-radio>
            <el-radio label="2">禁用</el-radio>
          </el-radio-group>
        </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 { hzlx_list, hzlx_add, hzlx_edit, hzlx_del, hzlx_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: "",
      dialogForm: {
        hzlx: "1",
        jf: "",
        qyzt: "",
      },
      rules: {
        hzlx: [{ required: true, message: "请输入徽章类型", trigger: "blur" }],
        jf: [{ required: true, message: "请输入积分", trigger: "blur" }],
        qyzt: [{ required: true, message: "请选择状态", trigger: "blur" }],
      },
      tableData: [],
      title: "",
      multipleSelection: [],
      id: "",
      isEdit: false,
      loading: false,
      dialogFormVisible: false,
      // 导入导出
      import_type_url:
        window.globalVariables.api +
        "/xddy/dygl_xsczda_hzlx/import?issubmit=1&token=" +
        token,
      import_type_data: {},
    };
  },
  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,
      };

      hzlx_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 = {
        hzlx: "",
        jf: "",
        qyzt: "",
      };
      this.isEdit = false;
      this.dialogFormVisible = true;
    },
    delData() {
      this.$confirm("确认删除这些类型?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let data = {
            xdxh_id: this.multipleSelection,
          };

          hzlx_del(data).then((res) => {
            if (res.code == 1) {
              this.$message({
                message: "类型删除成功！",
                type: "success",
              });
              this.getListData();
            } else {
              this.$message({
                message: res.msg,
                type: "error",
              });
            }
          });
        })
        .catch(() => {});
    },
    import_success(res) {
      if (res.code == 1) {
        this.$message({
          dangerouslyUseHTMLString: true,
          message: res.msg,
          type: "success",
        });
        this.getListData();
      } else {
        this.$message({
          dangerouslyUseHTMLString: true,
          message: res.msg,
          type: "error",
        });
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = [];
      for (let i in val) {
        this.multipleSelection.push(val[i].xdxh_id);
      }
    },
    editData(item) {
      this.title = "编辑类型";
      this.isEdit = true;
      this.dialogFormVisible = true;
      this.id = item.xdxh_id;
      this.dialogForm.hzlx = item.xdxh_lxmc;
      this.dialogForm.jf = item.xdxh_jf;
      this.dialogForm.qyzt = item.xdxh_qyzt_option_k;
    },

    formSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let data = {
            xdxh_lxmc: this.dialogForm.hzlx,
            xdxh_jf: this.dialogForm.jf,
            xdxh_qyzt: this.dialogForm.qyzt,
          };
          if (!this.isEdit) {
            hzlx_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.xdxh_id = this.id;
            hzlx_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;
    },

    exportAll() {
      let link =
        window.globalVariables.api +
        "/xddy/dygl_xsczda_hzlx/index?api=xls&page=1&limit=10000&token=" +
        token +
        "&xdxh_id=" +
        this.multipleSelection.join(",");
      window.open(link, "_blank");
      console.log(link);
     
    },
  },
  mounted() {
    this.getListData();
  },
};
</script>

<style scoped>
</style>