<template>
  <div class="app-container">
    <div v-if="isShowUpdate">
      <div class="head">
        编辑配置模板
        <el-button
          type="primary"
          size="mini"
          style="margin-left: 10px"
          @click="back"
        >返回</el-button>
      </div>
      <edit-device-module
        :key="`device_tem_${updateData.deviceTemplateId}`"
        :is-dis="true"
        :obj="updateData"
        :application-list="applicationList"
        :device-list="deviceList"
        :brand-list="brandList"
        :reader-brand-list="readerBrandList"
        :qr-brand-list="qrBrandList"
        :camera-type-list="cameraTypeList"
        :push-failed-record-type-list="pushFailedRecordTypeList"
        :success-show-personal-info-type-list="successShowPersonalInfoTypeList"
        :set-personal-type-list="setPersonalTypeList"
        :live-type-list="liveTypeList"
        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.deviceTemplateId"
          />
        </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 deviceList"
            :key="index"
            :label="item.deviceModel"
            :value="item.deviceModelId"
          />
        </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="enumLoading || 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="操作"
          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 tableMixins from '@/mixins/tableMixins'
import Pagination from '@/components/Pagination'
import EditDeviceModule from './components/EditDeviceModule'
import {
  applicationTypeList,
  deviceModelList
} from '@/api/configuration'
import {
  templatePage,
  templateDel,
  templateList,
  getCameraTypeEnum,
  getLiveTypeEnum,
  getPushFailedRecordTypeEnum,
  getSuccessShowPersonalInfoListEnum,
  getSetPersonalListEnum
} from '@/api/HHLocalDevice'
import { getIdCardReaderBrand, getQrCodeReaderBrandEnum, getReaderBrand } from '@/api/device'

export default {
  components: { Pagination, EditDeviceModule },
  filters: {
  },
  mixins: [tableMixins],
  data() {
    return {
      enumLoading: false, // 是否加载中
      statusLists: [
        {
          key: false,
          value: '未授权'
        },
        {
          key: true,
          value: '已授权'
        }
      ],
      form: {},
      isShowUpdate: false,
      updateData: {},
      pagination: {
        page: 1,
        current: 1
      },
      applicationList: [], // 设备型号下拉
      deviceList: [], // 设备型号下拉
      brandList: [], // 身份证阅读器品牌下拉
      readerBrandList: [], // 阅读器品牌下拉
      qrBrandList: [], // 二维码读头品牌下拉
      cameraTypeList: [], // 摄像头类型下拉
      liveTypeList: [], // 活体检测模式下拉
      pushFailedRecordTypeList: [], // 记录类型下拉
      successShowPersonalInfoTypeList: [], // 成功显示人员信息下拉
      setPersonalTypeList: [], // 设置——人员列表下拉
      systemType: [
        { id: 1, name: '蚂蚁OS' },
        { id: 2, name: '非蚂蚁OS' }
      ],
      templateDataList: []
    }
  },
  created() {
    this.selectInit()
  },
  methods: {
    async selectInit() {
      this.enumLoading = true
      const applicationTypeAxios = await applicationTypeList() // 获取应用类型列表
      const deviceModelAxios = await deviceModelList() // 设备型号列表
      const templateAxios = await templateList() // 模板列表
      const qrBrandAxios = await getQrCodeReaderBrandEnum() // 获取二维码读头品牌
      const brandAxios = await getIdCardReaderBrand() // 身份证阅读器品牌下拉
      const readerBrandAxios = await getReaderBrand() // 阅读器品牌下拉
      const cameraTypeAxios = await getCameraTypeEnum() // 摄像头类型下拉
      const liveTypeAxios = await getLiveTypeEnum() // 活体检测模式下拉
      const pushFailedRecordTypeAxios = await getPushFailedRecordTypeEnum() // 记录类型下拉
      const successShowPersonalInfoTypeAxios = await getSuccessShowPersonalInfoListEnum() // 成功显示人员信息下拉
      const setPersonalTypeAxios = await getSetPersonalListEnum() // 设置——人员列表下拉
      Promise.all([
        applicationTypeAxios,
        deviceModelAxios,
        templateAxios,
        qrBrandAxios,
        brandAxios,
        readerBrandAxios,
        cameraTypeAxios,
        liveTypeAxios,
        pushFailedRecordTypeAxios,
        successShowPersonalInfoTypeAxios,
        setPersonalTypeAxios
      ]).then(res => {
        const applicationTypeAxiosRes = res[0]
        this.applicationList = applicationTypeAxiosRes.data
        const deviceModelAxiosRes = res[1]
        this.deviceList = deviceModelAxiosRes.data
        const templateAxiosRes = res[2]
        this.templateDataList = templateAxiosRes.data
        const qrBrandAxiosRes = res[3]
        this.qrBrandList = qrBrandAxiosRes.data
        const brandAxiosRes = res[4]
        this.brandList = brandAxiosRes.data
        const readerBrandAxiosRes = res[5]
        this.readerBrandList = readerBrandAxiosRes.data
        const cameraTypeAxiosRes = res[6]
        this.cameraTypeList = cameraTypeAxiosRes.data
        const liveTypeAxiosRes = res[7]
        this.liveTypeList = liveTypeAxiosRes.data
        const pushFailedRecordTypeAxiosRes = res[8]
        this.pushFailedRecordTypeList = pushFailedRecordTypeAxiosRes.data
        const successShowPersonalInfoTypeAxiosRes = res[9]
        this.successShowPersonalInfoTypeList = successShowPersonalInfoTypeAxiosRes.data
        const setPersonalTypeAxiosRes = res[10]
        this.setPersonalTypeList = setPersonalTypeAxiosRes.data
      }).finally(() => {
        this.enumLoading = false
      })
    },
    initData() {
      this.getTableList()
    },
    // 获取表格数据
    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,
        sn: this.form.sn || null,
        deviceTemplateId: this.form.deviceTemplateId || null
      }
    },
    handleCreate() {
      this.$router.push({ path: '/HHLocalDevice/addTemplate' })
    },
    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.deviceTemplateId)
              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(() => {})
    }
  }
}
</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>
