<template>
  <div>
    <!--搜索区-->
    <el-button
      type="primary"
      icon="el-icon-plus"
      class="margin-left"
      @click="addOrUpdate"
    >添加设备类型
    </el-button
    >

    <template>
      <el-select
        filterable
        clearable
        v-model="page.data.faceDeviceTypeId"
        placeholder="请选择设备类型名称"
        class="margin-left"
      >
        <el-option
          v-for="item in machine_list"
          :key="item.faceDeviceTypeId"
          :label="item.name"
          :value="item.faceDeviceTypeId"
        >
        </el-option>
      </el-select>
      <el-button
        type="primary"
        icon="el-icon-search"
        class="margin-left"
        @click="getList"
      >搜索
      </el-button
      >
    </template>
    <!-- 表格 -->
    <el-table
      class="table"
      ref="table_form"
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="序号" align="center" width="80px" type="index"/>
      <el-table-column label="设备类型名称" align="center">
        <template slot-scope="scope">
          {{ scope.row.name | matchNull }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.createTime | matchNull }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作区" width="200">
        <template slot-scope="scope">
          <el-button
            type="danger"
            size="small"
            @click="deleteUser(scope.row.faceDeviceTypeId)"
          >删除
          </el-button
          >
          <el-button type="primary" size="small" @click="addOrUpdate(scope.row)"
          >修改
          </el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      :total="total"
      :page.sync="page.pageNum"
      :limit.sync="page.pageSize"
      @pagination="getList"
    />
    <!-- 弹出框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="800px">
      <el-form
        :model="form"
        :rules="rules"
        ref="form"
        label-width="120px"
        label-position="left"
      >
        <el-form-item label="设备类型名称" prop="name">
          <el-input show-word-limit
                    v-model.trim="form.name"/>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false"> 取消</el-button>
        <el-button
          type="primary"
          @click="dialogTitle === '添加' ? add() : doUpdate()"
        >
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import Pagination from "@/components/Pagination/index";
  import {addOrUpdateMachineType, deleteMachineType, getMachineTypeList, getMachineTypeListSearch} from "@/api/old_api_pengwenbing";

  export default {
    name: "machine_type",
    filters: {
      matchNull(str) {
        if (!str) {
          return "暂无";
        } else {
          return str;
        }
      },
    },
    components: {
      Pagination
    },
    data() {
      return {
        // 加载状态
        listLoading: true,
        // 表格
        list: [],
        dialogVisible: false,
        // 弹出框标题
        dialogTitle: "",
        // 添加的参数
        form: {
          name: null,
        },
        formCopy: {},
        // 分页参数
        page: {
          data: {
            faceDeviceTypeId: null,
          },
          pageNum: 1,
          pageSize: 10,
        },
        total: 0,
        // 验证规则
        rules: {
          name: [{required: true, message: "请输入设备的品牌名称", trigger: "blur"}],
        },
        // 第三方设备列表
        machine_list: []
      };
    },
    methods: {
      // 列表数据
      async getList() {
        this.listLoading = true;
        const a = await getMachineTypeList(this.page);
        const {list, total} = a.data;
        this.list = list;
        this.total = total;
        this.listLoading = false;
      },
      // 账号列表
      getMachineTypeListSearch() {
        getMachineTypeListSearch().then(res => {
          this.machine_list = res.data
        })
      },
      // 删除
      deleteUser(id) {
        this.$confirm("确定要删除此类设备吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          center: true,
        }).then(() => {
          deleteMachineType(id).then((res) => {
            this.getList();
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          });
        });
      },
      // 添加或修改
      addOrUpdate(row) {
        this.dialogVisible = true;
        if (row.faceDeviceTypeId) {
          this.dialogTitle = "修改";
          Object.assign(this.form, row)
        } else {
          this.dialogTitle = "添加";
          this.form = JSON.parse(JSON.stringify(this.formCopy));
        }
        this.$nextTick(() => {
          this.$refs["form"].clearValidate();
        });
      },
      // 确认新增
      add() {
        this.$refs["form"].validate((valid) => {
          if (valid) {
            addOrUpdateMachineType(this.form).then((res) => {
              this.dialogVisible = false;
              this.getList();
              this.$message({
                type: "success",
                message: "添加成功!",
              });
            });
          }
        });
      },
      // 确认修改
      doUpdate() {
        this.$refs["form"].validate((valid) => {
          if (valid) {
            addOrUpdateMachineType(this.form).then((res) => {
              this.dialogVisible = false;
              this.getList();
              this.$message({
                type: "success",
                message: "编辑成功!",
              });
            });
          }
        });
      },
    },
    created() {
      this.getList();
      this.getMachineTypeListSearch()
      this.formCopy = JSON.parse(JSON.stringify(this.form))
    },
  }
</script>

<style scoped>
  .table {
    margin-top: 15px;
  }

  .flex {
    display: flex;
    justify-content: flex-start;
    align-items: center;
  }

  .margin-left {
    margin: 10px 0 0 10px;
  }

  .input {
    width: 200px;
  }
</style>


