<template>
  <div class="app-container">
    <el-form v-model="queryParams" :inline="true">
      <el-form-item :label="$t('channel.ChannelCoding')">
        <el-input
          v-model="queryParams.channelCode"
          :placeholder="
            $t('share.PleaseEnter') + ' ' + $t('channel.ChannelCoding')
          "
        ></el-input>
      </el-form-item>
      <el-form-item :label="$t('channel.ChannelName')">
        <el-input
          v-model="queryParams.channelName"
          :placeholder="
            $t('share.PleaseEnter') + ' ' + $t('channel.ChannelName')
          "
        ></el-input>
      </el-form-item>
      <el-form-item :label="$t('channel.device')">
        <el-select
          v-model="queryParams.eqpbookId"
          :placeholder="$t('share.PleaseChoose') + ' ' + $t('channel.device')"
        >
          <el-option
            :label="item.patroldeviceName"
            :value="item.eqpbookId"
            v-for="item in eqpbookListData"
            :key="item.eqpbookId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('channel.ChannelType')">
        <el-select
          v-model="queryParams.channelType"
          :placeholder="
            $t('share.PleaseChoose') + ' ' + $t('channel.ChannelType')
          "
        >
          <el-option label="红外" value="ir"></el-option>
          <el-option label="可见光" value="vl"></el-option>
        </el-select>
      </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"
          >{{ $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="channelId" :label="$t('channel.ChannelID')">
      </el-table-column>

      <el-table-column
        prop="channelCode"
        :label="$t('channel.ChannelCoding')"
        align="center"
      >
      </el-table-column>

      <el-table-column
        prop="channelName"
        :label="$t('channel.ChannelName')"
        align="center"
      >
      </el-table-column>

      <el-table-column
        prop="eqpbookName"
        :label="$t('channel.device')"
        align="center"
      >
      </el-table-column>

      <el-table-column
        prop="patroldeviceCode"
        :label="$t('channel.EquipmentNumber')"
        align="center"
      >
      </el-table-column>

      <el-table-column
        prop="username"
        :label="$t('channel.user')"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="password"
        :label="$t('channel.password')"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="channelType" label="通道类型" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.channelType == 'vl'">可见光</el-tag>
          <el-tag v-if="scope.row.channelType == 'ir'">红外</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        prop="onlineState"
        :label="$t('channel.state')"
        align="center"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.onlineState == '1'">在线</el-tag>
          <el-tag v-if="scope.row.onlineState == '2'">离线</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        prop="channelConten"
        :label="$t('channel.SpecificContentOfTheChannel')"
        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.channelId)"
            >{{ $t("share.edit") }}</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="handleDelete(scope.row.channelId)"
            >{{ $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"
        ref="ruleForm"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item :label="$t('channel.ChannelCoding')">
          <el-input
            v-model="dialogForm.channelCode"
            :placeholder="
              $t('share.PleaseEnter') + ' ' + $t('channel.ChannelCoding')
            "
          ></el-input>
        </el-form-item>

        <el-form-item :label="$t('channel.ChannelName')" prop="channelName">
          <el-input
            v-model="dialogForm.channelName"
            :placeholder="
              $t('share.PleaseEnter') + ' ' + $t('channel.ChannelName')
            "
          ></el-input>
        </el-form-item>

        <el-form-item :label="$t('channel.ChannelType')" prop="channelType">
          <el-select
            v-model="dialogForm.channelType"
            :placeholder="
              $t('share.PleaseChoose') + ' ' + $t('channel.ChannelType')
            "
          >
            <el-option label="红外" value="ir"></el-option>
            <el-option label="可见光" value="vl"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item :label="$t('channel.device')" prop="eqpbookId">
          <el-select
            v-model="dialogForm.eqpbookId"
            :placeholder="$t('share.PleaseChoose') + ' ' + $t('channel.device')"
          >
            <el-option
              :label="item.patroldeviceName"
              :value="item.eqpbookId"
              v-for="item in eqpbookListData"
              :key="item.eqpbookId"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item :label="$t('channel.state')">
          <el-select
            v-model="dialogForm.onlineState"
            :placeholder="$t('share.PleaseChoose') + ' ' + $t('channel.state')"
          >
            <el-option label="在线" value="1"></el-option>
            <el-option label="离线" value="2"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item :label="$t('channel.SpecificContentOfTheChannel')">
          <el-input
            v-model="dialogForm.channelConten"
            :placeholder="
              $t('share.PleaseEnter') +
              ' ' +
              $t('channel.SpecificContentOfTheChannel')
            "
            type="textarea"
          ></el-input>
        </el-form-item>
        <!--  -->
        <el-form-item :label="$t('channel.HOSTIP')">
          <el-input
            v-model="dialogForm.host"
            :placeholder="$t('share.PleaseEnter') + ' ' + $t('channel.HOSTIP')"
          ></el-input>
        </el-form-item>

        <el-form-item :label="$t('channel.port')">
          <el-input
            v-model="dialogForm.port"
            :placeholder="$t('share.PleaseEnter') + ' ' + $t('channel.port')"
          ></el-input>
        </el-form-item>

        <el-form-item :label="$t('channel.EquipmentNumber')">
          <el-input
            v-model="dialogForm.patroldeviceCode"
            :placeholder="
              $t('share.PleaseEnter') + ' ' + $t('channel.EquipmentNumber')
            "
          ></el-input>
        </el-form-item>

        <el-form-item :label="$t('channel.user')">
          <el-input
            v-model="dialogForm.username"
            :placeholder="$t('share.PleaseEnter') + ' ' + $t('channel.user')"
          ></el-input>
        </el-form-item>

        <el-form-item :label="$t('channel.password')">
          <el-input
            v-model="dialogForm.password"
            :placeholder="
              $t('share.PleaseEnter') + ' ' + $t('channel.password')
            "
          ></el-input>
        </el-form-item>

        <el-form-item label="设备来源">
          <el-input
            v-model="dialogForm.deviceSource"
            placeholder="请输入设备来源"
          ></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 {
  eqpbookList,
  getList,
  postRemove,
  postAdd,
  getItem,
  edit,
  getAlgTypeList,
} from "@/api/basedata/eqpbook/channel";
export default {
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      tableData: [],
      total: 10,
      dialogVisible: false,
      title: this.$t("channel.AddDeviceChannel"),
      dialogForm: {
        channelName: "",
        channelType: "",
        eqpbookId: "",
      }, // 只保留一次定义
      rules: {
        channelName: [
          {
            required: true,
            message:
              this.$t("share.PleaseEnter") +
              " " +
              this.$t("channel.ChannelName"),
            trigger: "blur",
          },
        ],
        channelType: [
          {
            required: true,
            message:
              this.$t("share.PleaseChoose") +
              " " +
              this.$t("channel.ChannelType"),
            trigger: "change",
          },
        ],
        eqpbookId: [
          {
            required: true,
            message:
              this.$t("share.PleaseChoose") + " " + this.$t("channel.device"),
            trigger: "change",
          },
        ],
      },
      type: 0,
      ids: [],
      eqpbookListData: [],
    };
  },
  mounted() {
    this.init();
    eqpbookList().then((res) => {
      console.log(res, 111111);
      if (res.code == 200) {
        this.eqpbookListData = res.rows;
      }
    });
  },
  methods: {
    // 初始化
    init() {
      getList(this.queryParams).then((res) => {
        if (res.code == 200) {
          this.tableData = res.rows;
          this.total = res.total;
        }
      });
    },

    handleQuery() {
      this.init();
    },
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
      };
      this.init();
    },
    handleClose() {
      this.dialogVisible = false;
    },
    add() {
      this.type = 0;
      this.dialogForm = {
        channelName: "",
        channelType: "",
        eqpbookId: "",
      };
      this.dialogVisible = true;
      this.title = this.$t("channel.AddDeviceChannel");
    },
    handleEdit(id) {
      this.type = 1;
      this.dialogVisible = true;
      this.title = this.$t("channel.EditDeviceChannel");
      getItem(id).then((res) => {
        if (res.code == 200) {
          this.dialogForm = res.data;
        }
      });
    },
    // 删除
    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);
    },
    // 表格多选
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.channelId);
    },
    // 弹框确定
    submitForm(ruleForm) {
      console.log(this.dialogForm);
      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/eqpbook/channel/export",
        {
          ...this.queryParams,
        },
        `channel_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style scoped></style>
