<template>
  <div class="app-container">
    <el-form v-model="queryParams" :inline="true">
      <el-form-item :label="$t('alg.AlgorithmName')">
        <el-input
          v-model="queryParams.algName"
          :placeholder="$t('share.PleaseEnter') + ' ' + $t('alg.AlgorithmName')"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          @click="handleQuery"
          size="mini"
          >{{ $t("share.search") }}</el-button
        >
        <el-button icon="el-icon-refresh" @click="resetQuery" size="mini">{{
          $t("share.reset")
        }}</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          plain
          size="mini"
          @click="add"
          >{{ $t("share.new") }}</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          plain
          size="mini"
          @click="handleDeleteAll"
          :disabled="ids.length <= 0"
          >{{ $t("share.delete") }}</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" plain size="mini" @click="handleExport">{{
          $t("share.export")
        }}</el-button>
      </el-col>
    </el-row>
    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        prop="algId"
        :label="$t('alg.AlgorithmID')"
        width="150"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="algName"
        :label="$t('alg.AlgorithmName')"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="address"
        :label="$t('share.operate')"
        width="180"
        align="center"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="handleEdit(scope.row.algId)"
            >{{ $t("share.edit") }}</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="handleDelete(scope.row.algId)"
            >{{ $t("share.delete") }}</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="init"
    />
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form v-model="dialogForm">
        <el-form-item :label="$t('alg.AlgorithmName')" label-width="120px">
          <el-input
            v-model="dialogForm.algName"
            :placeholder="
              $t('share.PleaseEnter') + ' ' + $t('alg.AlgorithmName')
            "
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">{{ $t("share.cancel") }}</el-button>
        <el-button type="primary" @click="submitForm">{{
          $t("share.confirm")
        }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getList,
  postRemove,
  postAdd,
  getItem,
  edit,
  Export,
} from "@/api/basedata/alg/alg";
export default {
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      tableData: [],
      total: 0,
      title: this.$t("alg.AddAlgorithm"),
      dialogVisible: false,
      dialogForm: {},
      ids: [],
      type: 0,
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    // 初始化
    init() {
      getList(this.queryParams).then((res) => {
        if (res.code == 200) {
          this.tableData = res.rows;
          this.total = res.total;
          this.$message({
            message: `查询成功`,
            type: "success",
          });
        }
      });
    },
    // 查询
    handleQuery() {
      this.init();
    },
    // 重置
    resetQuery() {
      this.queryParams = { pageNum: 1, pageSize: 10 };
      this.init();
    },
    // 删除
    handleDelete(id) {
      this.$confirm(`确定要删除Id为${id}的数据吗？`, "删除", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          postRemove(id).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.init();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.algId);
    },
    // 多选删除
    handleDeleteAll() {
      let ids = this.ids.toString();
      this.handleDelete(ids);
    },
    add() {
      this.dialogForm = {};
      this.type = 0;
      this.dialogVisible = true;
      this.title = this.$t("alg.AddAlgorithm");
    },
    handleEdit(id) {
      this.type = 1;
      this.dialogVisible = true;
      this.title = this.$t("alg.EditAlgorithm");
      getItem(id).then((res) => {
        if (res.code == 200) {
          this.dialogForm = res.data;
        }
      });
    },
    // 弹框确定
    submitForm() {
      if (this.type == 0) {
        postAdd(this.dialogForm).then((res) => {
          if (res.code == 200) {
            this.$message({
              type: "success",
              message: "添加成功!",
            });
            this.dialogVisible = false;
            this.init();
          }
        });
      } else {
        edit(this.dialogForm).then((res) => {
          if (res.code == 200) {
            this.$message({
              type: "success",
              message: "编辑成功!",
            });
            this.dialogVisible = false;
            this.init();
          }
        });
      }
    },
    // 取消
    cancel() {
      this.dialogVisible = false;
    },
     handleExport(){
       this.download(
        "/basedata/alg/export",
        {
          ...this.queryParams,
        },
        `alg_${new Date().getTime()}.xlsx`
      );
    },
    handleClose(){
        this.dialogVisible = false;
    }
  },
};
</script>

<style scoped></style>
