<template>
  <div class="manage-container">
    <div class="manage-input">
      <el-form ref="queryForm" :model="queryForm" label-width="140px">
        <vab-query-form>
          <el-form-item width="100" prop="name" :label="$t('project.xmmc')">
            <el-input
              v-model="queryForm.name"
              size="small"
              :placeholder="$t('project.qsrxmcm')"
            ></el-input>
          </el-form-item>
          <el-form-item width="100" prop="leader" :label="$t('project.rzr')">
            <el-input
              v-model="queryForm.leader"
              size="small"
              :placeholder="$t('project.qsrfzr')"
            ></el-input>
          </el-form-item>
          <el-form-item width="100" prop="contact" :label="$t('project.lxfs')">
            <el-input
              v-model="queryForm.contact"
              size="small"
              :placeholder="$t('project.qsrxmcm')"
            ></el-input>
          </el-form-item>
        </vab-query-form>
      </el-form>
    </div>
    <div class="manage-input">
      <el-form ref="queryForm" :model="queryForm" label-width="140px">
        <vab-query-form>
          <el-form-item width="100" prop="regionCode" :label="$t('project.dq')">
            <el-cascader
              v-model="queryForm.regionCode"
              size="small"
              :placeholder="$t('project.dq')"
              :options="city"
              :props="{
                value: 'areaCode',
                label: 'areaName',
                children: 'children',
              }"
              filterable
              @change="changeCity"
            ></el-cascader>
          </el-form-item>
        </vab-query-form>
      </el-form>
    </div>
    <div class="manage-button">
      <el-button size="small" @click="resetForm()">
        {{ $t('defalult.chongz') }}
      </el-button>
      <el-button type="primary" size="small" @click="search">
        {{ $t('defalult.cx') }}
      </el-button>
      <el-button type="primary" size="small" @click="handleAdd">
        {{ $t('project.tjxm') }}
      </el-button>
      <el-button type="primary" size="small" @click="remove">
        {{ $t('defalult.plsc') }}
      </el-button>
      <el-button type="primary" size="small" @click="exportData">
        {{ $t('defalult.dc') }}
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="projectData"
      border
      stripe
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="40"></el-table-column>
      <el-table-column
        prop="id"
        label="ID"
        width="60"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="name"
        :label="$t('project.xmmc')"
        width="250"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="longitude"
        :label="$t('project.jd')"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="latitude"
        :label="$t('project.wd')"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="provinceCode"
        :label="$t('project.sbm')"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="provinceName"
        :label="$t('project.smc')"
        width="150"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="cityCode"
        :label="$t('project.sbma')"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="cityName"
        :label="$t('project.smcheng')"
        width="150"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="areaCode"
        :label="$t('project.qxbm')"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="areaName"
        :label="$t('project.qxmc')"
        width="150"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="leader"
        :label="$t('project.rzr')"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="contact"
        :label="$t('project.lxfs')"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="approvalTime"
        :label="$t('project.lxsj')"
        width="180"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="projectGroup.name"
        :label="$t('project.xmzmc')"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="createTime"
        :label="$t('grouping.cjsj')"
        width="180"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="updateTime"
        :label="$t('grouping.gxsj')"
        width="180"
        align="center"
      ></el-table-column>
      <el-table-column
        fixed="right"
        :label="$t('defalult.cz')"
        width="220"
        align="center"
      >
        <template #default="{ row }">
          <el-button type="text" @click="device(row)">
            {{ $t('project.zd') }}
          </el-button>
          <el-button type="text" @click="alarmUser(row)">
            {{ $t('project.bjyh') }}
          </el-button>
          <el-button type="text" @click="handleEdit(row)">
            {{ $t('defalult.bj') }}
          </el-button>
          <el-button type="text" @click="handleDelete(row)">
            {{ $t('defalult.sc') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :background="background"
      :current-page="queryForm.page"
      :layout="layout"
      :page-size="queryForm.size"
      :total="total"
      style="text-align: right"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
    <project-edit ref="edit"></project-edit>
    <group ref="group"></group>
    <alarm ref="alarm"></alarm>
  </div>
</template>
<script>
import { doDelete, doEdit, getList, exportProject } from '@/api/project';
import { getCity } from '@/api/city';
import ajax from '@/api/download';
import projectEdit from './components/projectEdit';
import Group from './components/group';
import alarm from '@/views/iot/project/components/alarm';
export default {
  name: 'Index',
  components: {
    Group,
    projectEdit,
    alarm,
  },
  data() {
    return {
      projectData: [],
      lazy: true,
      activeName: 'first',
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      input: '',
      textarea: '',
      list: [],
      background: true,
      listLoading: true,
      elementLoadingText: '正在加载...',
      ids: [],
      city: [],
      groupId: '',
      queryForm: {
        page: 1,
        size: 20,
        regionCode: '',
        name: '',
        leader: '',
        contact: '',
        groupId: '',
      },
    };
  },
  computed: {
    height() {
      return 500;
    },
  },
  created() {
    this.groupId = this.$route.query.groupId;
    this.queryForm.groupId = this.$route.query.groupId;
    this.fetchData();
    this.getCity();
  },
  methods: {
    async getCity() {
      const { data } = await getCity();
      this.city = this.getTreeData(data);
    },
    getTreeData(data) {
      for (let i = 0; i < data.length; i++) {
        if (data[i].children.length < 1) {
          // 最后一级没有数据将children变成undefined
          data[i].children = undefined;
        } else {
          // children不为空时继续调用该方法
          this.getTreeData(data[i].children);
        }
      }
      return data;
    },
    // 报警用户
    alarmUser(row) {
      this.$refs['alarm'].showEdit(row.id);
    },
    changeCity() {
      const city = this.queryForm.regionCode;
      const code = city[city.length - 1];
      this.queryForm.regionCode = code;
    },
    //导出
    async exportData() {
      const url = await exportProject(this.queryForm);
      ajax.downloadFile(url, { fileName: '项目.xls' });
    },
    resetForm() {
      this.$refs.queryForm.resetFields();
    },
    async soreStatus(status, id) {
      const { code, msg } = await doEdit({ status: status, id: id });
      if (code == 0) {
        this.$notify({
          title: msg,
          type: 'success',
        });
      } else {
        this.$notify.error({
          title: msg,
        });
      }
      this.fetchData();
    },
    async fetchData() {
      this.listLoading = true;
      const { data } = await getList(this.queryForm);
      this.projectData = data.items;
      this.total = data.total;
      setTimeout(() => {
        this.listLoading = false;
      }, 500);
    },
    device(row) {
      this.$router.push({
        path: '/device',
        query: { projectId: row.id },
      });
    },
    search() {
      this.fetchData();
    },
    handleAdd() {
      this.$refs['edit'].showEdit({ groupId: this.queryForm.groupId }, 1);
    },
    handleEdit(row) {
      this.$refs['edit'].showEdit(row, 2);
    },
    groupShow(row) {
      this.$refs['group'].showGroup(row);
    },
    handleQuery() {
      this.queryForm.page = 1;
      this.fetchData();
    },
    handleSizeChange(val) {
      this.queryForm.size = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.queryForm.page = val;
      this.fetchData();
    },
    remove() {
      const ids = this.ids;
      this.deletes(ids);
    },
    deletes(dis) {
      const that = this;
      this.$confirm('你确定要删除当前项吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          const { msg } = doDelete(dis);
          this.$message({
            type: 'success',
            message: msg == undefined ? '删除成功' : msg,
          });
          setTimeout(function () {
            that.fetchData();
          }, 1000);
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          });
        });
    },
    handleSelectionChange(val) {
      const ids = [];
      val.forEach(row => {
        ids.push(row.id);
      });
      this.ids = ids;
    },
    handleDelete(row) {
      const that = this;
      that.deletes([row.id]);
    },
  },
};
</script>

<style lang="scss" scoped>
.el-input {
  width: 200px !important;
}
.el-select {
  width: 200px !important;
}
.manage-button {
  padding-left: 12px;
  margin-bottom: 30px;
}
</style>

<style lang="scss">
.el-submenu__title:hover {
  background-color: rgba(#1890ff, 0.085) !important;
  color: hsla(208, 100%, 55%, 0.95) !important;
}
.el-dialog {
  .el-dialog__header {
    background-color: #1890ff !important;
    padding: 15px 20px;
    text-align: left !important;
    .el-dialog__title {
      color: #e8f4ff !important;
    }
    .el-dialog__headerbtn .el-dialog__close {
      color: #e8f4ff !important;
    }
  }
  .el-dialog__body {
    padding: 20px !important;
    .el-form-item {
      margin-bottom: 10px !important;
      .el-input {
        width: 100% !important;
      }
      .el-select {
        width: 100% !important;
      }
      .el-cascader {
        width: 100% !important;
      }
    }
    .el-form-item:last-child {
      margin-bottom: 20px !important;
    }
  }
  .dialog-footer {
    text-align: right;
  }
}
</style>
