<template>
  <div class="app-container">
    <div v-if="isShowUpdate">
      <div class="head">
        编辑设备
        <el-button
          type="primary"
          size="mini"
          style="margin-left: 20px"
          @click="back"
        >返回</el-button>
      </div>
      <edit-device
        :obj="updateData"
        style="margin-left: 16px"
        @finish="back"
        @back="back"
      />
    </div>
    <div v-show="!isShowUpdate">
      <div class="search-layout" style="margin-bottom: 0px; margin-top: -10px">
        <el-select
          v-model="form.deviceTemplateId"
          class="filter-item"
          placeholder="模板"
          clearable
          filterable
        >
          <el-option
            v-for="(item, index) in templateDataList"
            :key="index"
            :label="item.templateName"
            :value="item.id"
          />
        </el-select>
        <el-select
          v-model="form.applicationTypeId"
          class="filter-item"
          placeholder="应用类型"
          clearable
          filterable
        >
          <el-option
            v-for="(item, index) in applicationList"
            :key="index"
            :label="item.applicationType"
            :value="item.applicationTypeId"
          />
        </el-select>
        <el-select
          v-model="form.deviceModelHardwareId"
          class="filter-item"
          placeholder="设备型号(硬件)"
          clearable
          filterable
        >
          <el-option
            v-for="(item, index) in devicelList"
            :key="index"
            :label="item.deviceModel"
            :value="item.deviceModelId"
          />
        </el-select>
        <el-select
          v-model="form.deviceModelEnvironmentId"
          class="filter-item"
          placeholder="设备型号(环境)"
          clearable
          filterable
        >
          <el-option
            v-for="(item, index) in devicelList"
            :key="index"
            :label="item.deviceModel"
            :value="item.deviceModelId"
          />
        </el-select>
        <el-select
          v-model="form.systemTypeCode"
          class="filter-item"
          placeholder="系统类型"
          clearable
          filterable
        >
          <el-option
            v-for="(item, index) in systemType"
            :key="index"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </div>
      <div class="search-layout">
        <el-button
          class="filter-item"
          style="margin-right: 0"
          type="primary"
          icon="el-icon-search"
          @click="query(1)"
        >
          搜索
        </el-button>
        <el-button
          class="filter-item"
          style="margin-right: 0"
          type="primary"
          icon="el-icon-plus"
          @click="handleCreate"
        >
          添加
        </el-button>
      </div>
      <el-table
        v-loading="tableLoading"
        :data="tableData"
        element-loading-text="加载中..."
        border
        fit
        highlight-current-row
      >
        <el-table-column align="center" label="序号" width="95">
          <template slot-scope="scope">
            {{
              (pagination.page - 1) * pagination.pageSize + (scope.$index + 1)
            }}
          </template>
        </el-table-column>
        <el-table-column label="模板名称">
          <template slot-scope="scope">
            {{ scope.row.templateName }}
          </template>
        </el-table-column>
        <el-table-column label="应用类型">
          <template slot-scope="scope">
            {{ scope.row.applicationType }}
          </template>
        </el-table-column>
        <el-table-column label="设备型号(硬件)">
          <template slot-scope="scope">
            {{ scope.row.deviceModelHardware }}
          </template>
        </el-table-column>
        <el-table-column label="设备型号(环境)">
          <template slot-scope="scope">
            {{ scope.row.deviceModelEnvironment }}
          </template>
        </el-table-column>
        <el-table-column label="系统类型">
          <template slot-scope="scope">
            {{ scope.row.systemType }}
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="250"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="{ row, $index }">
            <el-button type="primary" size="mini" @click="handleUpdate(row)">
              编辑
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(row, $index)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        :total="pagination.total"
        :page.sync="pagination.page"
        :limit.sync="pagination.pageSize"
        :page-sizes="pagination.pageSizeOptions"
        :layout="pagination.layout"
        @pagination="handleSizeChange"
      />
    </div>
  </div>
</template>

<script>
import EditDevice from './components/edit'
import tableMixins from '@/mixins/tableMixins'
import { getAllList as getMerchantList } from '@/api/merchant'
import { getList as getSceneList } from '@/api/scene'
import { getList as getUserlibList } from '@/api/userlib'
import Pagination from '@/components/Pagination'
import {
  applicationTypeList,
  deviceModelList,
  purchaserList
} from '@/api/configuration'
import {
  templatePage,
  templateDel,
  templateList
} from '@/api/FKJ'

export default {
  components: { Pagination, EditDevice },
  filters: {},
  mixins: [tableMixins],
  data() {
    return {
      form: {},
      isShowUpdate: false,
      updateData: {},
      pagination: {
        page: 1,
        current: 1
      },
      applicationList: [],
      devicelList: [],
      systemType: [
        { id: 1, name: '蚂蚁OS' },
        { id: 2, name: '非蚂蚁OS' }
      ],
      templateDataList: []
    }
  },
  methods: {
    initData() {
      this.getTableList()
      this.applicationTypeList()
      this.deviceModelList()
      this.templateList()
    },
    // 获取表格数据
    async getTableList() {
      this.pagination.current = this.pagination.page
      this.tableLoading = true
      try {
        const result = await templatePage(this.getFilterParams())
        this.setDataList(result.data)
      } catch (e) {
        console.log(e)
      } finally {
        this.tableLoading = false
      }
    },
    // 获取筛选信息
    query(type) {
      if (type) {
        this.pagination.page = 1
        this.pagination.current = 1
      }
      this.clearPageParams()
      this.getTableList()
      this.templateList()
    },
    // 增加筛选条件
    transformFilterForm() {
      return {
        applicationTypeId: this.form.applicationTypeId || null,
        deviceModelHardwareId: this.form.deviceModelHardwareId || null,
        deviceModelEnvironmentId: this.form.deviceModelEnvironmentId || null,
        systemTypeCode: this.form.systemTypeCode || null,
        sn: this.form.sn || null,
        mode: this.form.mode || null,
        deviceTemplateId: this.form.deviceTemplateId || null
      }
    },
    handleCreate() {
      this.$router.push({ path: '/FKJAddTemplate/add' })
    },
    handleUpdate(row) {
      this.isShowUpdate = true
      this.updateData = JSON.parse(JSON.stringify(row))
    },
    back() {
      this.isShowUpdate = false
      this.query()
    },
    templateList() {
      templateList().then((res) => {
        this.templateDataList = res.data
      })
    },
    handleDelete(row, index) {
      const that = this
      this.$confirm('确定删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        showClose: false,
        type: 'warning',
        beforeClose: async(action, instance, done) => {
          if (action === 'confirm') {
            instance.confirmButtonLoading = true
            instance.cancelButtonLoading = true
            instance.confirmButtonText = '提交中...'
            try {
              await templateDel(row.id)
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              that.getTableList()
              that.templateList()
            } catch (e) {
              // this.$message({
              //   type: 'error',
              //   message: '删除失败!'
              // })
            } finally {
              done()
              setTimeout(() => {
                instance.confirmButtonLoading = false
                instance.cancelButtonLoading = false
              }, 300)
            }
          } else {
            done()
          }
        }
      })
        .then(() => {})
        .catch(() => {})
    },
    // 获取应用类型列表
    applicationTypeList() {
      applicationTypeList().then((res) => {
        this.applicationList = res.data
      })
    },
    // 设备型号列表
    deviceModelList() {
      deviceModelList().then((res) => {
        this.devicelList = res.data
      })
    }
  }
}
</script>

<style scoped>
.search-layout {
  margin-bottom: 20px;
}
.search-layout .filter-item {
  margin-right: 20px;
  margin-top: 10px;
}
.search-layout .filter-item:last-of-type {
  margin-right: 0;
}
.head {
  margin-left: 16px;
  font-weight: 600;
  font-size: 18px;
  color: #333333;
}
.input {
  width: 200px;
  margin-right: 15px;
}
.m-r {
  margin-right: 15px;
}
</style>
