<template>
  <div class="app-container">
    <el-form v-model="queryParams" :inline="true">
      <el-form-item :label="$t('algconvert.mainSystem')">
        <el-select
          v-model="queryParams.mainSystemCode"
          :placeholder="
            $t('share.PleaseChoose') + ' ' + $t('algconvert.mainSystem')
          "
        >
          <el-option
            :label="item.mainSystemName || item.mainSystemCode"
            :value="item.mainSystemCode"
            v-for="item in mainSystemData"
            :key="item.mainSystemId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('algconvert.algorithm')">
        <el-select
          :placeholder="
            $t('share.PleaseChoose') + ' ' + $t('algconvert.algorithm')
          "
          v-model="queryParams.algId"
        >
          <el-option
            :label="item.algSubtypeName"
            :value="item.algSubtypeId"
            v-for="item in subtypeData"
            :key="item.algSubtypeId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="原算法编号">
        <el-input
          v-model="queryParams.originalAlgCode"
          :placeholder="
            $t('share.PleaseChoose') +
            ' ' +
            $t('algconvert.OriginalAlgorithmNumber')
          "
        ></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="success" icon="el-icon-edit" plain size="mini"  @click="handleEdit()">{{
          $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"
      style="width: 100%; margin-bottom: 20px"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        prop="algConvertId"
        :label="$t('algconvert.LedgerID')"
        align="center"
      />
      <el-table-column
        prop="mainSystemCode"
        :label="$t('algconvert.MainSystemNumber')"
        align="center"
      />
      <el-table-column
        prop="mainSystemName"
        :label="$t('algconvert.MainSystemName')"
        align="center"
      />
      <el-table-column
        prop="manufacturerCode"
        :label="$t('algconvert.ManufacturerNumber')"
        align="center"
      />
      <el-table-column
        prop="manufacturerName"
        :label="$t('algconvert.ManufacturerName')"
        align="center"
      />
      <el-table-column
        prop="algName"
        :label="$t('algconvert.algorithm')"
        align="center"
      />
      <el-table-column
        prop="algCode"
        :label="$t('algconvert.AlgorithmNumber')"
        align="center"
      />
      <el-table-column
        prop="originalAlgCode"
        :label="$t('algconvert.OriginalAlgorithmNumber')"
        align="center"
      />
      <el-table-column :label="$t('share.operate')" align="center">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="handleEdit(scope.row.algConvertId)"
            >{{ $t("share.edit") }}</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="handleDelete(scope.row.algConvertId)"
            >{{ $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 :model="dialogForm" :rules="rules" ref="ruleForm">
        <el-form-item
          :label="$t('algconvert.mainSystem')"
          prop="mainSystemName"
        >
          <el-select
            v-model="dialogForm.mainSystemName"
            style="width: 100%"
            :placeholder="
              $t('share.PleaseChoose') + ' ' + $t('algconvert.mainSystem')
            "
            value-key="mainSystemId"
            @change="mainSystemFn"
          >
            <el-option
              :label="item.mainSystemName || item.mainSystemCode"
              :value="item.mainSystemCode"
              v-for="item in mainSystemData"
              :key="item.mainSystemId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('algconvert.algorithm')" prop="algId">
          <el-select
            style="width: 100%"
            :placeholder="
              $t('share.PleaseChoose') + ' ' + $t('algconvert.algorithm')
            "
            v-model="dialogForm.algId"
            multiple
          >
            <el-option
              :label="item.algSubtypeName"
              :value="item.algSubtypeId"
              v-for="item in subtypeData"
              :key="item.algSubtypeId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          :label="$t('algconvert.OriginalAlgorithmNumber')"
          prop="originalAlgCode"
        >
          <el-input
            v-model="dialogForm.originalAlgCode"
            :placeholder="
              $t('share.PleaseEnter') +
              ' ' +
              $t('algconvert.OriginalAlgorithmNumber')
            "
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">{{ $t("share.cancel") }}</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">{{
          $t("share.confirm")
        }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getList,
  mainsystemList,
  subtypeList,
  postAdd,
  getItem,
  edit,
  postRemove,
} from "@/api/basedata/bdconfig/algconvert";
export default {
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      tableData: [],
      total: 10,
      dialogVisible: false,
      dialogForm: {},
      title: this.$t("algconvert.AddBasicData-DeviceCorrespondence"),
      rules: {
        mainSystemName: [
          {
            required: true,
            message:
              this.$t("share.PleaseChoose") +
              " " +
              this.$t("algconvert.mainSystem"),
            trigger: "change",
          },
        ],
        algId: [
          {
            required: true,
            message:
              this.$t("share.PleaseChoose") +
              " " +
              this.$t("algconvert.algorithm"),
            trigger: "change",
          },
        ],
        originalAlgCode: [
          {
            required: true,
            message:
              this.$t("share.PleaseEnter") +
              " " +
              this.$t("algconvert.OriginalAlgorithmNumber"),
            trigger: "blur",
          },
        ],
      },
      mainSystemData: [],
      subtypeData: [],
      type: 0,
      ids: [],
    };
  },
  computed: {},
  created() {},
  mounted() {
    this.init();
    mainsystemList().then((res) => {
      if (res.code == 200) {
        this.mainSystemData = res.rows;
      }
    });
    subtypeList().then((res) => {
      if (res.code == 200) {
        this.subtypeData = res.rows;
      }
    });
  },
  methods: {
    // 初始化
    init() {
      getList(this.queryParams).then((res) => {
        if (res.code == 200) {
          this.tableData = res.rows;
          this.total = res.total;
        }
      });
    },
    add() {
      this.dialogForm = {};
      this.dialogVisible = true;
      this.title = this.$t("algconvert.AddBasicData-DeviceCorrespondence");
    },
    handleEdit(id) {
      this.dialogVisible = true;
      this.title = this.$t("algconvert.EditBasicData-DeviceCorrespondence");
      this.type = 1;
      getItem(id).then((res) => {
        console.log(res);
        this.dialogForm = res.data;
        let arr = this.dialogForm.algId.split(",") || [];
        this.dialogForm.algId = arr.map(Number);
        console.log(this.dialogForm.algId);
      });
    },
    handleClose() {
      this.dialogVisible = false;
    },
    // 弹框保存
    submitForm(ruleForm) {
      this.$refs[ruleForm].validate((v) => {
        let data = JSON.parse(JSON.stringify(this.dialogForm));
        data.algId = this.dialogForm.algId.toString();
        // return
        if (v) {
          if (this.type == 0) {
            postAdd(data).then((res) => {
              if (res.code == 200) {
                this.init();
                this.dialogVisible = false;
              }
            });
          } else {
            edit(data).then((res) => {
              if (res.code == 200) {
                this.init();
                this.dialogVisible = false;
              }
            });
          }
        }
      });
    },
    mainSystemFn(val) {
      const selectedSystem = this.mainSystemData.find(
        (item) => item.mainSystemCode === val
      );
      console.log(selectedSystem, "点击");
      this.dialogForm.mainSystemName = selectedSystem.mainSystemName;
      this.dialogForm.mainSystemCode = selectedSystem.mainSystemCode;
      this.dialogForm.manufacturerCode = selectedSystem.manufacturerCode;
      this.dialogForm.manufacturerName = selectedSystem.manufacturerName;
    },
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
      };
      this.init();
    },
    // 查询
    handleQuery() {
      console.log(this.queryParams);
      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.algConvertId);
    },
    // 多选删除
    handleDeleteAll() {
      let ids = this.ids.toString();
      this.handleDelete(ids);
    },
    // 导出
    handleExport() {
      this.download(
        "/basedata/bdconfig/algconvert/export",
        {
          ...this.queryParams,
        },
        `algconvert_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style lang="scss" scoped></style>
