<template>
  <div class="app-container">
    <div class="app-container-search main-bgcolor main-radius">
      <h1 class="">控制器类型</h1>
      <br />
      <!-- 搜索工作栏 -->
      <SearchInfo
          ref="searchForm"
          :searchFormParam="{queryParams , dataSource}"
          :showSearch.sync="showSearch"
          @handleQuery="handleQuery"
          @resetQuery="resetQuery"
      ></SearchInfo>
    </div>

    <div class="app-container-list main-bgcolor main-radius shopping">
      <div class="shopping-top">
        <div class="shopping-title">控制器类型列表</div>
        <el-button class="shopping-add-btn" type="primary" plain @click="handleAdd"
          v-hasPermi="['product:controller-type:create']">添加</el-button>
      </div>
      <!-- 列表 -->
      <el-table v-loading="loading" :data="list">
        <el-table-column label="序号" align="center" width="80px">
          <template slot-scope="{ $index }">
            {{ ($index+1) }}
          </template>
        </el-table-column>
        <el-table-column label="控制器类型名称" align="center" prop="controllerTypeName" />
        <el-table-column label="控制器类型字段" align="center" prop="conFieldName" />
        <el-table-column label="商户名称" align="center" prop="belongBusinessName" />
        <el-table-column label="创建时间" align="center" prop="createTime" >
          <template v-slot="{ row }">
            <span>{{parseTime(row.createTime)}}</span>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remarks" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template v-slot="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                       v-hasPermi="['product:controller-type:update']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                       v-hasPermi="['product:controller-type:delete']">删除</el-button>
          </template>
        </el-table-column>
        <div slot="empty">
            <EmptyTable />
        </div>
      </el-table>
      <!-- 分页组件 -->
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                  @pagination="getList"/>
    </div>

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="500px" v-dialogDrag append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" label-position="top">
        <el-form-item label="所属商户" prop="belongBusinessId">
          <el-select class="w100" placeholder="请选择" :disabled="isDisable" v-model="form.belongBusinessId" @change="changeBusiness">
            <el-option
            v-for="(item,index) in businessNameList"
            :key="index"
            :label="item.businessName"
            :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="控制器类型名称" prop="controllerTypeName">
          <el-input v-model="form.controllerTypeName" placeholder="请输入控制器类型名称" />
        </el-form-item>
        <el-form-item label="控制器类型字段，为emqx推送数据所使用的字段" prop="conFieldName">
          <el-input v-model="form.conFieldName" placeholder="请输入控制器类型字段，为emqx推送数据所使用的字段" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { createControllerType, updateControllerType, deleteControllerType, getControllerType, getControllerTypePage, exportControllerTypeExcel } from "@/api/iot/controllerType";
import { getBusinessNameList } from '@/api/basic/businessInfo';
import SearchInfo from "@/components/Customer/SearchForm/index.vue"
import EmptyTable from '@/components/EmptyTable'
export default {
  name: "ControllerType",
  components: {
    SearchInfo,
    EmptyTable
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 控制器类型列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      isDisable: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        controllerTypeName: null,
        conFieldName: null,
        remarks: null,
        belongBusinessId: null,
        belongBusinessName: null,
        createTime: [],
      },
      // 搜索工作栏参数
      dataSource: [
        {
          key:'belongBusinessId',
          itemType:'select',
          label:'所属商户',
          placeholder:'请选择',
          options: [],
        },
        {
          key:'controllerTypeName',
          itemType:'input',
          label:'类型名称',
          placeholder:'请输入',
          onEnterKeyUp:this.handleQuery
        },
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        controllerTypeName: [{ required: true, message: "控制器类型名称不能为空", trigger: "blur" }],
        conFieldName: [{ required: true, message: "控制器类型字段不能为空", trigger: "blur" }],
        belongBusinessId: [{ required: true, message: "所属商户id不能为空", trigger: "blur" }],
        belongBusinessName: [{ required: true, message: "所属商户名称不能为空", trigger: "blur" }],
      },

      businessNameList:[],
    };
  },
  created() {
    this.getList();
    this.initDate();
  },
  methods: {
    // 初始化数据
    async initDate(){
      let res = await getBusinessNameList();
      res.data.map(item=>{
        item.label = item.businessName;
        item.value = item.id;
      });
      this.dataSource[0].options = res.data;
      this.businessNameList = res.data;
    },

    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getControllerTypePage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        controllerTypeName: undefined,
        conFieldName: undefined,
        remarks: undefined,
        belongBusinessId: undefined,
        belongBusinessName: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.isDisable = false;
      this.title = "添加控制器类型";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getControllerType(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.isDisable = true;
        this.title = "修改控制器类型";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateControllerType(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createControllerType(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除控制器类型编号为"' + id + '"的数据项?').then(function() {
          return deleteControllerType(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal.confirm('是否确认导出所有控制器类型数据项?').then(() => {
          this.exportLoading = true;
          return exportControllerTypeExcel(params);
        }).then(response => {
          this.$download.excel(response, '控制器类型.xls');
          this.exportLoading = false;
        }).catch(() => {});
    },
    changeBusiness(){
      this.$nextTick(async () => {
        let label = this.$refs.businessName.selected.label;
        this.form.belongBusinessName = label;
      });
    },
  }
};
</script>
