<script setup>
import {onBeforeMount, onMounted, reactive, ref, unref} from 'vue';
import {cinema_list, cinema_search} from "@/apis/update.js";


// 表单部分
const cinemaSearch = reactive({
  ip: '',
  version: '',
})

const cinemaSearchRef = ref()

// 定义表格数据
const tableData = ref([])

let refresh_disable = ref(false)
let refresh_loading = ref(false)

//  数据校验部分
const ip_validate = (rule, value, callback) => {
  const regex = new RegExp('((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}');
  if (value === '') {
    callback()
  }
  if (!regex.test(value)) {
    callback(new Error('请输入正确的IP地址'))
  } else {
    callback()
  }
}

const ipRule = reactive({
  ip: [{validator: ip_validate, trigger: 'change'}]
})

// 数据提交
const submitSearch = async (formEl) => {
  console.log('至少执行了')
  console.log(cinemaSearch.ip)
  const {ip, version} = unref(cinemaSearch)
  tableData.value = []
  await cinema_search({ip, version}).then(
      res => {
        if (Array.isArray(res)) {
          tableData.value = [...res]
          console.log(tableData)
        } else {
          console.log('else')
        }
      }
  ).catch(
      err => {
        console.log('err')
        console.log(err)
      }
  )

  // console.log(formEl)
  console.log(ip, version)
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      console.log('提交数据')
    } else {
      console.log('提交失败')
      return false
    }
  })
}

const resetSearch = (formEl) => {
  console.log(formEl)
  if (!formEl) return
  console.log("resetSearch")
  formEl.resetFields()
}

const refresh = (formEL) => {
  formEL.resetFields()
  refresh_disable.value = true;
  refresh_loading.value = true;
  get_table_data()
  setTimeout(()=>{
    refresh_disable.value = false;
    refresh_loading.value = false;
  }, 5000)
}

// 表格部分


async function get_table_data() {
  tableData.value = []
  await cinema_list().then(res => {
    if (Array.isArray(res)) {
      tableData.value = [...res]
      console.log(tableData)
    } else {
      console.log('else')
    }
  }).catch(err => {
        console.log('err')
        console.log(err)
      }
  )
}

onMounted(
    async () => {
      await get_table_data();
      console.log(tableData)
    }
)

</script>

<template>
  <el-form
      ref="cinemaSearchRef"
      :model="cinemaSearch"
      status-icon
      :rules="ipRule"
      class="cinema-search"
      :inline="true"
      style="width: 100%"
  >
    <el-row>
      <el-col :span="8">
        <el-form-item label="影院IP" prop="ip" label-width="60">
          <el-input v-model="cinemaSearch.ip" placeholder="请输入完整的影院IP" style="width: 180px;"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="系统版本" prop="version">
          <el-input v-model="cinemaSearch.version" placeholder="支持模糊查询" style="width: 160px;"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item>
          <el-button type="primary" @click="submitSearch(cinemaSearchRef)">搜索</el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="resetSearch(cinemaSearchRef)">重置</el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-bind:disabled="refresh_disable" @click="refresh(cinemaSearchRef)" v-bind:loading="refresh_loading">刷新</el-button>
        </el-form-item>

      </el-col>
    </el-row>
  </el-form>
  <el-table :data="tableData" style="width: 100%">
    <el-table-column prop="update_time" label="最后查询时间" min-width="200"></el-table-column>
    <el-table-column prop="name" label="影院名称" min-width="200"></el-table-column>
    <el-table-column prop="ip" label="影院IP" min-width="120"></el-table-column>
    <el-table-column prop="zz_num" label="专资编码" min-width="120"></el-table-column>
    <el-table-column prop="inner_id" label="鼎新编码" min-width="100"></el-table-column>
    <el-table-column prop="sys_ver" label="系统版本" min-width="200"></el-table-column>
    <el-table-column prop="client_ver" label="客户端版本" min-width="200"></el-table-column>
    <el-table-column prop="db_user" label="数据库账号" min-width="100"></el-table-column>
    <el-table-column prop="db_pwd" label="数据库密码" min-width="120"></el-table-column>
  </el-table>
</template>

<style scoped>

</style>
