<template>
  <div class="app-container">
    <el-form v-model="queryParams" :inline="true" label-width="160px">
      <el-form-item :label="$t('mainsystem.MainSystemNumber')">
        <el-input
          v-model="queryParams.mainSystemCode"
          :placeholder="
            $t('share.PleaseEnter') + ' ' + $t('mainsystem.MainSystemNumber')
          "
          suffix-icon="xxxx"
        ></el-input>
      </el-form-item>

      <el-form-item :label="$t('mainsystem.MainSystemName')">
        <el-input
          v-model="queryParams.mainSystemName"
          :placeholder="
            $t('share.PleaseEnter') + ' ' + $t('mainsystem.MainSystemName')
          "
          suffix-icon="xxxx"
        ></el-input>
      </el-form-item>

      <el-form-item
        :label="$t('mainsystem.ManufacturerNumber')"
        prop="ManufacturerNumber"
      >
        <el-input
          v-model="queryParams.manufacturerCode"
          :placeholder="
            $t('share.PleaseEnter') + ' ' + $t('mainsystem.ManufacturerNumber')
          "
          suffix-icon="xxxx"
        ></el-input>
      </el-form-item>

      <el-form-item :label="$t('mainsystem.ManufacturerName')">
        <el-input
          v-model="queryParams.manufacturerName"
          :placeholder="
            $t('share.PleaseEnter') + ' ' + $t('mainsystem.ManufacturerName')
          "
          suffix-icon="xxxx"
        ></el-input>
      </el-form-item>

      <el-form-item :label="$t('mainsystem.StartDateOfUse')">
        <el-date-picker
          v-model="queryParams.createTime"
          type="date"
          value-format="yyyy-MM-dd"
          :placeholder="
            $t('share.PleaseEnter') + ' ' + $t('mainsystem.StartDateOfUse')
          "
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item :label="$t('mainsystem.DeviceMapping')">
        <el-select
          v-model="queryParams.convertFlag"
          style="width: 100%"
          :placeholder="
            $t('share.PleaseChoose') + ' ' + $t('mainsystem.DeviceMapping')
          "
        >
          <el-option label="是" value="1"></el-option>
          <el-option label="否" value="0"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item :label="$t('mainsystem.algorithmMapping')">
        <el-select
          v-model="queryParams.algConvertFlag"
          :placeholder="
            $t('share.PleaseChoose') + ' ' + $t('mainsystem.algorithmMapping')
          "
        >
          <el-option label="是" value="1"></el-option>
          <el-option label="否" value="0"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item :label="$t('mainsystem.SystemType')">
        <el-select
          v-model="queryParams.systemType"
          :placeholder="
            $t('share.PleaseChoose') + ' ' + $t('mainsystem.SystemType')
          "
        >
          <el-option
            :label="item.dictLabel"
            :value="item.dictValue"
            v-for="item in systemTypeData"
            :key="item.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item :label="$t('mainsystem.port')">
        <el-input
          v-model="queryParams.port"
          :placeholder="$t('share.PleaseEnter') + ' ' + $t('mainsystem.port')"
          suffix-icon="xxxx"
        ></el-input>
      </el-form-item>

      <el-form-item :label="$t('mainsystem.user')">
        <el-input
          v-model="queryParams.username"
          :placeholder="$t('share.PleaseEnter') + ' ' + $t('mainsystem.user')"
          suffix-icon="xxxx"
        ></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" :disabled="ids.length <= 0" @click="handleDeleteAll">{{
          $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="id"
        :label="$t('mainsystem.LedgerID')"
        align="center"
      />
      <el-table-column
        prop="mainSystemCode"
        :label="$t('mainsystem.MainSystemNumber')"
        align="center"
      />
      <el-table-column
        prop="mainSystemName"
        :label="$t('mainsystem.MainSystemName')"
        align="center"
      />
      <el-table-column :label="$t('mainsystem.MainSystemType')" align="center">
        <template slot-scope="scope">
          {{ systemTypeFn(scope.row.systemType) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="manufacturerCode"
        :label="$t('mainsystem.ManufacturerNumber')"
        align="center"
      />
      <el-table-column
        prop="manufacturerName"
        :label="$t('mainsystem.ManufacturerName')"
        align="center"
      />
      <el-table-column
        prop="startUseTime"
        :label="$t('mainsystem.StartDateOfUse')"
        align="center"
      />
      <el-table-column
        prop="convertFlag"
        :label="$t('mainsystem.DeviceMapping')"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.convertFlag == 1 ? "是" : "否" }}
        </template>
      </el-table-column>
      <el-table-column prop="algConvertFlag" label="算法映射" align="center">
        <template slot-scope="scope">
          {{ scope.row.algConvertFlag == 1 ? "是" : "否" }}
        </template>
      </el-table-column>
      <el-table-column
        prop="ipAddr"
        :label="$t('mainsystem.ipAddress')"
        align="center"
      />
      <el-table-column
        prop="port"
        :label="$t('mainsystem.port')"
        align="center"
      />
      <el-table-column
        prop="username"
        :label="$t('mainsystem.user')"
        align="center"
      />
      <el-table-column
        prop="password"
        :label="$t('mainsystem.password')"
        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.mainSystemId)"
            >{{ $t("share.edit") }}</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="handleDelete(scope.row.mainSystemId)"
            >{{ $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"
        label-width="170px"
      >
        <el-form-item
          :label="$t('mainsystem.MainSystemNumber')"
          prop="mainSystemCode"
        >
          <el-input
            v-model="dialogForm.mainSystemCode"
            :placeholder="
              $t('share.PleaseEnter') + ' ' + $t('mainsystem.MainSystemNumber')
            "
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('mainsystem.MainSystemName')">
          <el-input
            v-model="dialogForm.mainSystemName"
            :placeholder="
              $t('share.PleaseEnter') + ' ' + $t('mainsystem.MainSystemName')
            "
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('mainsystem.SystemType')" prop="systemType">
          <el-select
            v-model="dialogForm.systemType"
            :placeholder="
              $t('share.PleaseChoose') + ' ' + $t('mainsystem.SystemType')
            "
          >
            <el-option
              :label="item.dictLabel"
              :value="item.dictValue"
              v-for="item in systemTypeData"
              :key="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          :label="$t('mainsystem.ManufacturerNumber')"
          prop="manufacturerCode"
        >
          <el-input
            v-model="dialogForm.manufacturerCode"
            :placeholder="
              $t('share.PleaseEnter') +
              ' ' +
              $t('mainsystem.ManufacturerNumber')
            "
          ></el-input>
        </el-form-item>
        <el-form-item
          :label="$t('mainsystem.ManufacturerName')"
          prop="manufacturerName"
        >
          <el-input
            v-model="dialogForm.manufacturerName"
            :placeholder="
              $t('share.PleaseEnter') + ' ' + $t('mainsystem.ManufacturerName')
            "
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('mainsystem.StartDateOfUse')">
          <el-date-picker
            v-model="dialogForm.startUseTime"
            type="date"
            value-format="yyyy-MM-dd"
            :placeholder="
              $t('share.PleaseEnter') + ' ' + $t('mainsystem.StartDateOfUse')
            "
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item :label="$t('mainsystem.DeviceMapping')">
          <el-select
            v-model="dialogForm.convertFlag"
            style="width: 100%"
            :placeholder="
              $t('share.PleaseChoose') + ' ' + $t('mainsystem.DeviceMapping')
            "
          >
            <el-option label="是" value="1"></el-option>
            <el-option label="否" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="算法映射">
          <el-select
            v-model="dialogForm.algConvertFlag"
            style="width: 100%"
            :placeholder="
              $t('share.PleaseChoose') + ' ' + $t('mainsystem.DeviceMapping')
            "
          >
            <el-option label="是" value="1"></el-option>
            <el-option label="否" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('mainsystem.ipAddress')">
          <el-input
            v-model="dialogForm.ipAddr"
            :placeholder="
              $t('share.PleaseEnter') + ' ' + $t('mainsystem.ipAddress')
            "
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('mainsystem.port')">
          <el-input
            v-model="dialogForm.port"
            :placeholder="$t('share.PleaseEnter') + ' ' + $t('mainsystem.port')"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('mainsystem.user')">
          <el-input
            v-model="dialogForm.username"
            :placeholder="$t('share.PleaseEnter') + ' ' + $t('mainsystem.user')"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('mainsystem.password')">
          <el-input
            v-model="dialogForm.password"
            :placeholder="
              $t('share.PleaseEnter') + ' ' + $t('mainsystem.password')
            "
          ></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, systemType,postAdd,postRemove,getItem,edit } from "@/api/basedata/bdconfig/mainsystem";

export default {
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      tableData: [],
      total: 10,
      dialogVisible: false,
      title: this.$t("mainsystem.AddBasicData-MainSystemConfiguration"),
      dialogForm: {},
      rules: {
        mainSystemCode: [
          {
            required: true,
            message:
              this.$t("share.PleaseEnter") +
              " " +
              this.$t("mainsystem.MainSystemNumber"),
            trigger: "blur",
          },
        ],
        systemType: [
          {
            required: true,
            message:
              this.$t("share.PleaseChoose") +
              " " +
              this.$t("mainsystem.SystemType"),
            trigger: "change",
          },
        ],
        manufacturerCode: [
          {
            required: true,
            message:
              this.$t("share.PleaseEnter") +
              " " +
              this.$t("mainsystem.ManufacturerNumber"),
            trigger: "blur",
          },
        ],
        manufacturerName: [
          {
            required: true,
            message:
              this.$t("share.PleaseEnter") +
              " " +
              this.$t("mainsystem.ManufacturerName"),
            trigger: "blur",
          },
        ],
      },
      systemTypeData: [],
      type: 0,
      ids:[]
    };
  },
  mounted() {
    this.init();
    systemType().then((res) => {
      if (res.code == 200) {
        this.systemTypeData = res.data;
      }
    });
  },
  methods: {
    init() {
      getList(this.queryParams).then((res) => {
        console.log(res);
        if (res.code == 200) {
          this.tableData = res.rows;
        }
      });
    },
    add() {
      this.type = 0;
      this.dialogVisible = true;
      this.title = this.$t("mainsystem.AddBasicData-MainSystemConfiguration");
      this.dialogForm = {};
    },
    handleEdit(id) {
      this.type = 1;
      this.title = this.$t("mainsystem.EditBasicData-MainSystemConfiguration");
      getItem(id).then((res) => {
        if (res.code == 200) {
          this.dialogForm = res.data;
          this.dialogVisible = true;
        }
      });
    },
    // 查询
    handleQuery() {
      this.init();
    },
    // 重置
    resetQuery() {
      this.queryParams = { pageNum: 1, pageSize: 10 };
      this.init();
    },
    // 表格内主系统类型
    systemTypeFn(row) {
      let name = null;
      this.systemTypeData.filter((item) => {
        if (item.dictValue == row) {
          name = item.dictLabel;
        }
      });
      return name;
    },
    handleClose() {
      this.dialogVisible = false;
      this.dialogForm = {};
    },
    submitForm(ruleForm) {
      this.$refs[ruleForm].validate((v) => {
        if (v) {
          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();
              }
            });
          }
        }
      });
    },
    // 导出
    handleExport() {
      this.download('/basedata/bdconfig/mainsystem/export', {
        ...this.queryParams
      }, `mainsystem_${new Date().getTime()}.xlsx`)
    },
    // 删除
    handleDelete(id){
      postRemove(id).then((res) => {
        console.log(res)
        if(res.code == 200){
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.init();
        }
      });
    },
       // 表格多选
       handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.mainSystemId);
    },
    // 多选删除
    handleDeleteAll() {
      let ids = this.ids.toString();
      this.handleDelete(ids);
    },
  },
};
</script>

<style lang="scss" scoped>
// ::v-deep.el-select {
//   display: block;
//   width: 93% !important;
// }
</style>
