<template>
  <div class="view-table-content">
    <div style="width: 100%">
      <div class="view-dataType-title">
        <div class="view-dataType-title-btn">
          <el-button type="success" @click="backPage()">返回</el-button>
          <el-button type="warning" @click="remove(tableCheckItems)" :disabled="tableCheckItems.length == 0">删除</el-button>
        </div>
        <div class="view-dataType-title-search">
          <el-input placeholder="请输入名称" v-model="keyWordData" class="input1">
            <el-button slot="append" icon="el-icon-search" @click="searchClick"></el-button>
          </el-input>
        </div>
      </div>
      <div class="view-dataType-table">
        <LTable ref="table" @selection-change="selection" :defaultFetch="false" :fetch="fetch" :columns="columns" :dataSource="tableData" :options="options" :pagination="tableRequset"></LTable>
      </div>
      <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="1000px" :before-close="handleClose">
        <el-form ref="form" :inline="true" :model="form" label-width="80px">
          <el-form-item label="架次号" prop="sortieNo">
            <el-input v-model="form.sortieNo" placeholder="请输入架次号" />
          </el-form-item>
          <el-form-item label="编目" prop="aircraftId">
            <el-input v-model="form.aircraftId" placeholder="请输入编目" />
          </el-form-item>
          <el-form-item label="模型类型" prop="modelType">
            <el-input v-model="form.modelType" placeholder="请输入模型类型" />
          </el-form-item>
          <el-form-item label="模型名称" prop="modelName">
            <el-input v-model="form.modelName" placeholder="请输入模型名称" />
          </el-form-item>
          <el-form-item label="参数" prop="dataParams">
            <el-input v-model="form.dataParams" placeholder="请输入参数" />
          </el-form-item>
        </el-form>
        <div style="height: 200px">算法结果</div>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getModel, removeModel } from '@/api/als/model'
import { deepClone, debounce } from '@/utils/index'
export default {
  name: 'PreResult',
  components: {},
  data() {
    // 这里存放数据
    return {
      dialogTitle: '新增',
      dialogVisible: false,
      keyWordData: '',
      aircaftModelIdList: [],
      searchValue: '',
      columns: [
        {
          prop: 'sortieNo',
          label: '架次号'
        },
        {
          prop: 'aircraftId',
          label: '编目'
        },
        {
          prop: 'modelType',
          label: '模型类型'
        },
        {
          prop: 'modelName',
          label: '模型名称'
        },
        {
          prop: 'dataParams',
          label: '参数'
        },
        {
          button: true,
          label: '操作',
          width: '240px',
          group: [
            {
              name: '查看结果',
              type: 'text',
              round: false,
              plain: false,
              onClick: (row, index, scope) => {
                this.checkResult(row)
              }
            },
            {
              name: '执行',
              type: 'text',
              round: false,
              plain: false,
              onClick: (row, index, scope) => {
                this.executeAglo(row)
              }
            },
            {
              name: '删除',
              type: 'text',
              round: false,
              plain: false,
              onClick: (row, index, scope) => {
                this.remove([row])
              }
            }
          ]
        }
      ],
      options: {
        stripe: true, // 斑马纹
        mutiSelect: true, // 多选框
        index: false, // 显示序号， 多选则 mutiSelect
        loading: false, // 表格动画
        initTable: false, // 是否一挂载就加载数据
        border: true,
        height: 'calc(100vh - 300px)'
      },
      tableCheckItems: [],
      tableData: [
        {
          id: '11',
          aircraftId: '34345345',
          sortieNo: '001',
          modelType: '去噪',
          modelName: '11',
          dataParams: '参数1、参数2...'
        }
      ],
      tableRequset: {
        total: 0,
        pageIndex: 1,
        pageSize: 10,
        searchValue: ''
      },
      form: {
        id: '',
        sortieNo: '',
        aircraftId: '',
        modelType: '',
        modelName: '',
        paramsList: ''
      },
      debounceFn: debounce(this.fetch, 500),
      agloForm: []
    }
  },
  watch: {
    keyWord() {
      this.tableRequset.pageIndex = 1
      this.debounceFn()
    }
  },
  mounted() {
    // this.getModelAPI()
    this.agloForm = this.$route.params
  },
  methods: {
    async removeModelAPI(params) {
      try {
        const { code } = await removeModel(params)
        if (code === 200) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          await this.getModelAPI()
          this.handleClose()
        }
      } catch (error) {}
    },

    async getModelAPI(params) {
      if (this.$refs.table) this.$refs.table.clearSelection()
      const { keyWord } = this
      const { pageSize, pageIndex } = this.tableRequset
      const {
        data: { list, total }
      } = await getModel({ pageSize, pageIndex, keyWord, ...params })
      this.tableData = list
      this.tableRequset.total = total
    },

    fetch() {
      this.getModelAPI()
    },

    async searchClick() {
      if (this.keyWordData) {
        const {
          data: { list, total }
        } = await getModel({ name: this.keyWordData })
        this.tableData = list
        this.tableRequset.total = total
      } else {
        this.getModelAPI()
      }
    },
    checkResult(row) {
      this.dialogTitle = '结果'
      this.form = deepClone(row)
      this.dialogVisible = true
    },

    handleClose() {
      this.dialogVisible = false
      this.agloForm = {
        id: '',
        sortieNo: '',
        aircraftId: '',
        modelType: '',
        modelName: '',
        paramsList: ''
      }
    },

    selection(val) {
      this.tableCheckItems = val
    },

    remove(row) {
      this.$confirm('是否删除该模型', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.removeModelAPI(row.map((e) => e.id))
        })
        .catch(() => {})
    },
    backPage() {
      this.$router.go(-1)
    },
    executeAglo(row) {}
  }
}
</script>

<style lang="scss" scoped>
@import '../index.scss';
</style>
