<template>
  <div class="app-container">
    <el-form v-model="queryParams" :inline="true">
      <el-form-item :label="$t('subtype.AlgorithmMethodTypeName')">
        <el-input
          v-model="queryParams.algSubtypeName"
          :placeholder="
            $t('share.PleaseEnter') +
            ' ' +
            $t('subtype.AlgorithmMethodTypeName')
          "
        ></el-input>
      </el-form-item>
      <el-form-item :label="$t('share.CreationTime')">
        <!-- :placeholder="
            $t('share.PleaseChoose') + ' ' + $t('subtype.CreationTime')
          " -->
        <el-date-picker
          v-model="queryParams.createTime"
          type="date"
          placeholder="请选择创建时间"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </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="success" icon="el-icon-edit" plain size="mini">{{
          $t("share.edit")
        }}</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"
      @selection-change="handleSelectionChange"
      style="width: 100%; margin-bottom: 20px"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        prop="algSubtypeId"
        :label="$t('subtype.AlgorithmMethodTypeID')"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="algName"
        :label="$t('subtype.AlgorithmTypeName')"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="algSubtypeCode"
        :label="$t('subtype.AlgorithmMethodTypeEncoding')"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="algSubtypeName"
        :label="$t('subtype.AlgorithmMethodTypeName')"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="lowerValue"
        :label="$t('subtype.lowerLimitsOfNormal')"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="upperValue"
        :label="$t('subtype.UpperLimitOfNormalRange')"
        align="center"
      >
      </el-table-column>
      <el-table-column :label="$t('share.operate')" width="180" align="center">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="handleEdit(scope.row.algSubtypeId)"
            >{{ $t("share.edit") }}</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="handleDelete(scope.row.algSubtypeId)"
            >{{ $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('subtype.MainTypeOfAlgorithm')">
          <el-select
            style="width: 100%"
            v-model="dialogForm.algId"
            :placeholder="
              $t('share.PleaseChoose') + ' ' + $t('subtype.MainTypeOfAlgorithm')
            "
          >
            <!-- <el-option label="item.label" value="item.value"> </el-option> -->
            <el-option
              :label="item.algName"
              :value="item.algId"
              v-for="item in algTypeList"
              :key="item.algId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('subtype.AlgorithmMethodTypeName')">
          <el-input
            v-model="dialogForm.algSubtypeName"
            :placeholder="
              $t('share.PleaseEnter') +
              ' ' +
              $t('subtype.AlgorithmMethodTypeName')
            "
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('subtype.AlgorithmMethodTypeEncoding')">
          <el-input
            v-model="dialogForm.algSubtypeCode"
            :placeholder="
              $t('share.PleaseEnter') +
              ' ' +
              $t('subtype.AlgorithmMethodTypeEncoding')
            "
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('subtype.lowerLimitsOfNormal')">
          <el-input
            v-model="dialogForm.lowerValue"
            :placeholder="
              $t('share.PleaseEnter') + ' ' + $t('subtype.lowerLimitsOfNormal')
            "
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('subtype.UpperLimitOfNormalRange')">
          <el-input
            v-model="dialogForm.upperValue"
            :placeholder="
              $t('share.PleaseEnter') +
              ' ' +
              $t('subtype.UpperLimitOfNormalRange')
            "
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('subtype.UpperLimitLabel')">
          <el-switch
            v-model="dialogForm.lowerFlag"
            active-color="#13ce66"
            inactive-color="#eee"
             active-value="1"
            inactive-value="0"
          >
          </el-switch>
        </el-form-item>
        <el-form-item :label="$t('subtype.LowerLimitIdentification')">
          <el-switch
            v-model="dialogForm.upperFlag"
            active-color="#13ce66"
            inactive-color="#eee"
            active-value="1"
            inactive-value="0"
          >
          </el-switch>
        </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,
  getAlgTypeList,
} from "@/api/basedata/alg/subtype";
export default {
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      tableData: [],
      total: 0,
      title: this.$t("subtype.AddAlgorithmAppendix"),
      dialogVisible: false,
      ids: [],
      type: 0,
      dialogForm: {},
      algTypeList: [],
    };
  },
  mounted() {
    this.init();
    this.algTypeListInit();
  },
  methods: {
    init() {
      getList(this.queryParams).then((res) => {
        if (res.code == 200) {
          this.tableData = res.rows;
          this.total = res.total;
          this.$message({
            message: `查询成功`,
            type: "success",
          });
        }
      });
    },
    handleClose() {
      this.dialogVisible = false;
    },
    algTypeListInit() {
      getAlgTypeList().then((res) => {
        if (res.code == 200) {
          this.algTypeList = res.data;
        }
      });
    },
    // 搜索
    handleQuery() {
      console.log(this.queryParams);
      this.init();
    },
    // 重置
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
      };
      this.init();
    },
    add() {
      this.type = 0;
      this.dialogVisible = true;
      this.title = this.$t("subtype.AddAlgorithmAppendix");
    },
    handleEdit(id) {
      this.type = 1;
      this.dialogVisible = true;
      this.title = this.$t("subtype.EditAlgorithmAppendix");
      getItem(id).then((res) => {
        if (res.code == 200) {
          this.dialogForm = res.data;
        }
      });
    },
    // 表格多选
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.algSubtypeId);
    },
    // 删除
    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: "已取消删除",
          });
        });
    },
    // 多选删除
    handleDeleteAll() {
      let ids = this.ids.toString();
      this.handleDelete(ids);
    },
    // 弹框确定
    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.handleClose()
    },
    handleExport(){
       this.download(
        "/basedata/subtype/export",
        {
          ...this.queryParams,
        },
        `subtype_${new Date().getTime()}.xlsx`
      );
    }
  },
};
</script>

<style scoped></style>
