<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>
      </div>
      <!-- 列表 -->
      <el-table v-loading="loading" :data="list">
        <el-table-column label="编号" align="center" type="index" width="80" >
          <template v-slot="{ $index }">
            <span>{{($index+1)+ (queryParams.pageNo-1)*queryParams.pageSize }}</span>
          </template>
        </el-table-column>
        <el-table-column label="商户名称" align="center" prop="belongBusinessName" />
        <el-table-column label="地块名称" align="center" prop="landName" />
        <el-table-column label="环境类型" align="center" prop="envTypeName" />
        <el-table-column label="环境数据名称" align="center" prop="envName" />
        <el-table-column label="数据值" align="center" prop="value" />
        <el-table-column label="创建时间" align="center" prop="createTime" >
          <template v-slot="{ row }">
            <span>{{parseTime(row.createTime)}}</span>
          </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>

  </div>
</template>

<script>
import { createEnvData, updateEnvData, deleteEnvData, getEnvData, getEnvDataPage, exportEnvDataExcel } from "@/api/iot/envData";
import { getAllEnvType } from "@/api/iot/envType";
import { getBusinessNameList } from '@/api/basic/businessInfo';
import SearchInfo from "@/components/Customer/SearchForm/index.vue"
import EmptyTable from '@/components/EmptyTable'
export default {
  name: "EnvData",
  components: {
    SearchInfo,
    EmptyTable
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 环境数据列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        envTypeId: null,
        envName: null,
        landId: null,
        value: null,
        belongBusinessId: null,
        belongBusinessName: null,
        createTime: [],
        landName: null,
        envTypeId: null,
      },
      // 搜索工作栏参数
      dataSource: [
        {
          key:'belongBusinessId',
          itemType:'select',
          label:'所属商户',
          placeholder:'请选择',
          options: [],
          isNet: true,
        },
        {
          key:'envTypeId',
          itemType:'select',
          label:'类型名称',
          placeholder:'请选择',
          options: [],
        },
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        belongBusinessId: [{ required: true, message: "所属商户id不能为空", trigger: "blur" }],
        belongBusinessName: [{ required: true, message: "所属商户名称不能为空", trigger: "blur" }],
      },
      businessNameList:[],
      envTypeList: [],
    };
  },
  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;

      let { data } = await getAllEnvType();
      this.envTypeList = data;
      this.dataSource[1].options = this.envTypeList.map(item => {
        return {
          label: item.envTypeName,
          value: item.id,
        };
      });
    },

    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getEnvDataPage(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,
        envTypeId: undefined,
        envName: undefined,
        landId: undefined,
        value: undefined,
        belongBusinessId: undefined,
        belongBusinessName: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm","searchForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加环境数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getEnvData(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改环境数据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateEnvData(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createEnvData(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 deleteEnvData(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 exportEnvDataExcel(params);
        }).then(response => {
          this.$download.excel(response, '环境数据.xls');
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
