<template>
  <div class="app-container">
    <el-form v-model="queryParams" :inline="true">
      <el-form-item :label="$t('deviceconvert.mainSystem')">
        <el-select
          v-model="queryParams.mainSystemCode"
          style="width: 100%"
          :placeholder="
            $t('share.PleaseChoose') + ' ' + $t('deviceconvert.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('deviceconvert.device')">
        <el-select
          v-model="queryParams.deviceId"
          style="width: 100%"
          :placeholder="
            $t('share.PleaseChoose') + ' ' + $t('deviceconvert.device')
          "
        >
          <el-option
            :label="item.deviceName"
            :value="item.deviceId"
            v-for="item in deviceData"
            :key="item.deviceId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('deviceconvert.OriginalEquipmentNumber')">
        <el-input
          v-model="queryParams.originalDeviceCode"
          :placeholder="
            $t('share.PleaseChoose') +
            ' ' +
            $t('deviceconvert.OriginalEquipmentNumber')
          "
        ></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="deviceConvertId"
        :label="$t('deviceconvert.LedgerID')"
        align="center"
      />
      <el-table-column
        prop="mainSystemCode"
        :label="$t('deviceconvert.MainSystemNumber')"
        align="center"
      />
      <el-table-column
        prop="mainSystemName"
        :label="$t('deviceconvert.MainSystemName')"
        align="center"
      />
      <el-table-column
        prop="manufacturerCode"
        :label="$t('deviceconvert.ManufacturerNumber')"
        align="center"
      />
      <el-table-column
        prop="manufacturerName"
        :label="$t('deviceconvert.ManufacturerName')"
        align="center"
      />
      <el-table-column
        prop="deviceName"
        :label="$t('deviceconvert.device')"
        align="center"
      />
      <el-table-column
        prop="deviceCode"
        :label="$t('deviceconvert.DeviceNumber')"
        align="center"
      />
      <el-table-column
        prop="originalDeviceCode"
        :label="$t('deviceconvert.OriginalEquipmentNumber')"
        align="center"
      />
      <el-table-column
        prop="deviceLevel"
        :label="$t('deviceconvert.EquipmentHierarchy')"
        align="center"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.deviceLevel == 1">间隔</el-tag>
          <el-tag v-if="scope.row.deviceLevel == 2">主设备</el-tag>
          <el-tag v-if="scope.row.deviceLevel == 3">设备点位</el-tag>
          <el-tag v-if="scope.row.deviceLevel == 4">部件</el-tag>
        </template>
      </el-table-column>
      <el-table-column :label="$t('share.operate')" align="center">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="handleEdit(scope.row.deviceConvertId)"
            >{{ $t("share.edit") }}</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="handleDelete(scope.row.deviceConvertId)"
            >{{ $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 ref="dialogForm" :model="dialogForm" :rules="rules">
        <el-form-item
          :label="$t('deviceconvert.mainSystem')"
          prop="mainSystemCode"
        >
          <el-select
            v-model="dialogForm.mainSystemCode"
            style="width: 100%"
            :placeholder="
              $t('share.PleaseChoose') + ' ' + $t('deviceconvert.mainSystem')
            "
            @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('deviceconvert.device')" prop="deviceId">
          <el-select
            v-model="dialogForm.deviceId"
            style="width: 100%"
            :placeholder="
              $t('share.PleaseChoose') + ' ' + $t('deviceconvert.device')
            "
            @change="deviceFn"
          >
            <el-option
              :label="item.deviceName"
              :value="item.deviceId"
              v-for="item in deviceData"
              :key="item.deviceId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          :label="$t('deviceconvert.OriginalEquipmentNumber')"
          prop="originalDeviceCode"
        >
          <el-input
            v-model="dialogForm.originalDeviceCode"
            :placeholder="
              $t('share.PleaseChoose') +
              ' ' +
              $t('deviceconvert.OriginalEquipmentNumber')
            "
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('deviceconvert.EquipmentHierarchy')">
          <el-select
            v-model="dialogForm.deviceLevel"
            style="width: 100%"
            :placeholder="
              $t('share.PleaseChoose') +
              ' ' +
              $t('deviceconvert.EquipmentHierarchy')
            "
          >
            <el-option label="间隔" value="1"></el-option>
            <el-option label="主设备" value="2"></el-option>
            <el-option label="设备点位" value="3"></el-option>
            <el-option label="部件" value="4"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">{{
          $t("share.cancel")
        }}</el-button>
        <el-button type="primary" @click="submitForm">{{
          $t("share.confirm")
        }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>
  
  <script>
import {
  getList,
  mainsystemList,
  getProDeviceTreeSelect,
  postAdd,
  getItem,
  edit,
  postRemove,
} from "@/api/basedata/bdconfig/deviceconvert";
export default {
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      tableData: [],
      total: 10,
      dialogVisible: false,
      rules: {
        mainSystemCode: [
          {
            required: true,
            message:
              this.$t("share.PleaseChoose") +
              " " +
              this.$t("deviceconvert.mainSystem"),
            trigger: "change",
          },
        ],
        deviceId: [
          {
            required: true,
            message:
              this.$t("share.PleaseChoose") +
              " " +
              this.$t("deviceconvert.device"),
            trigger: "change",
          },
        ],
        originalDeviceCode: [
          {
            required: true,
            message:
              this.$t("share.PleaseEnter") +
              " " +
              this.$t("deviceconvert.OriginalEquipmentNumber"),
            trigger: "blur",
          },
        ],
      },
      dialogForm: {},
      title: this.$t("deviceconvert.AddBasicData-DeviceCorrespondence"),
      mainSystemData: [],
      deviceData: [],
      type: 0,
      ids: [],
    };
  },
  computed: {},
  created() {},
  mounted() {
    this.init();
    mainsystemList().then((res) => {
      if (res.code == 200) {
        this.mainSystemData = res.rows;
      }
    });
    getProDeviceTreeSelect().then((res) => {
      if (res.code == 200) {
        this.deviceData = res.data;
      }
    });
  },
  methods: {
    // 初始化
    init() {
      getList(this.queryParams).then((res) => {
        this.tableData = res.rows;
        this.total = res.total;
      });
    },
    add() {
      this.type = 0;
      this.dialogVisible = true;
      this.title = this.$t("deviceconvert.AddBasicData-DeviceCorrespondence");
    },

    submitForm() {
      this.$refs["dialogForm"].validate((valid) => {
        if (valid) {
          if (this.type == 0) {
            postAdd(this.dialogForm).then((res) => {
              console.log(res, 111111);
              if (res.code == 200) {
                this.dialogVisible = false;
                this.$message({
                  message: `新增成功`,
                  type: "success",
                });
                this.init();
              }
            });
          } else {
            edit(this.dialogForm).then((res) => {
              if (res.code == 200) {
                this.dialogVisible = false;
                this.$message({
                  message: `修改成功`,
                  type: "success",
                });
                this.init();
              }
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    mainSystemFn(val) {
      const selectedSystem = this.mainSystemData.find(
        (item) => item.mainSystemCode === val
      );
      this.dialogForm.mainSystemName = selectedSystem.mainSystemName;
      this.dialogForm.mainSystemCode = selectedSystem.mainSystemCode;
      this.dialogForm.manufacturerCode = selectedSystem.manufacturerCode;
      this.dialogForm.manufacturerName = selectedSystem.manufacturerName;
    },
    deviceFn(val) {
      const deviceData = this.deviceData.find((item) => item.deviceId === val);
      this.dialogForm.deviceCode = deviceData.deviceCode;
      this.dialogForm.deviceName = deviceData.deviceName;
    },
    handleEdit(id) {
      this.dialogVisible = true;
      this.type = 1;
      this.title = this.$t("deviceconvert.EditBasicData-DeviceCorrespondence");
      getItem(id).then((res) => {
        if (res.code == 200) {
          this.dialogForm = res.data;
        }
      });
    },
    // 删除
    handleDelete(id) {
      postRemove(id).then((res) => {
        if (res.code == 200) {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.init();
        }
      });
    },
    // 表格多选
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.deviceConvertId);
    },
    // 多选删除
    handleDeleteAll() {
      let ids = this.ids.toString();
      this.handleDelete(ids);
    },
    // 查询
    handleQuery() {
      this.init()
    },
    // 重置
    resetQuery(){
      this.queryParams = { pageNum: 1, pageSize: 10 };
      this.init()
    },
    // 导出
    handleExport() {
      this.download(
        "/basedata/bdconfig/deviceconvert/export",
        {
          ...this.queryParams,
        },
        `deviceconvert_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
  
  <style lang="scss" scoped></style>
  