<template>
  <div>
    <!-- 算法 -->
    <!-- <a-divider /> -->
    <p-card title="策略算法" :bordered="false" style="width: 100%">
      <div slot="extra"><p-button type="primary" @click="AddStrategy()">添加</p-button></div>
      <!-- <p-grid-form :span="8" :label-col="{ span: 9 }" :items="tablecolumns.grid.items" @submit="hanlderSubmit" /> -->
      <p-table
        :remote-url="tablecolumns.MainTable.RemoteUrl"
        bordered
        :ref="tablecolumns.MainTable.ref"
        :row-key="tablecolumns.MainTable.rowkey"
        :columns="tablecolumns.MainTable.columns"
        :data-source.sync="data"
        :rowSelectType="false"
      >
        <span slot="operation" slot-scope="text, record">
          <a @click.stop="AddStrategy(text)">编辑</a>
          <a-divider type="vertical" />
          <a @click.stop="details(text)">详情</a>
          <a-divider type="vertical" />
          <a @click.stop="del(text)">删除</a>
        </span>
      </p-table>
    </p-card>
    <edit-model ref="model" :visible="visible" @hidemodel="hidemodel" :items="items"></edit-model>
    <a-modal title="算法" width="80%" :visible="visibles" @ok="visibles = false" @cancel="visibles = false">
      <a-descriptions title="规则因子详情" bordered>
        <!-- v-for="item of columns.filter(t => !t.hidden && t.key != 'operation' && t.dataIndex != '_row_count')"
          :key="item.key"
          :label="item.title" -->
        <a-descriptions-item
          v-for="item of tablecolumns.MainTable.columns.filter(
            t => !t.hidden && t.key != 'operation' && t.dataIndex != '_row_count'
          )"
          :key="item.key"
          :label="item.title"
        >
          {{ detailsdata[item.key] }}
        </a-descriptions-item>
      </a-descriptions>
    </a-modal>
  </div>
</template>
<script>
import tablecolumns from './config/tableconfjg'
import model from './config/model'
import { delItem } from './api'
import EditModel from './model.vue'
export default {
  data() {
    return {
      detailsdata: {},
      visibles: false,
      tablecolumns,
      model,
      data: [],
      //   items: [],
      visible: false,
      hide: true
    }
  },
  components: {
    EditModel
  },
  computed: {
    items() {
      return this.model.strategy.items
    }
  },
  mounted() {
  },
  methods: {
    hanlderSubmit(e) {
      e.preventDefault()
    },
    details(text) {
      //   this.$nextTick(() => {
      this.detailsdata = text
      this.visibles = true
      //   })
    },
    hidemodel(data) {
      this.visible = false
      if (!data) {
        this.reload()
      }
    },

    reload() {
      this.$refs[this.tablecolumns.MainTable.ref].reload()
    },
    SetModel(data) {
      this.$refs.model.setvalue(data)
    },
    AddStrategy(text) {
      if (text && text.remark == null) {
        text.remark = ''
      }
      this.visible = true
      if (text && text.language == 'java') {
        //   this.model.strategy.items
        this.$refs.model.sss()
      } else {
        this.$refs.model.sass()
      }

      if (text) {
        this.SetModel(text)
      } else {
        this.$refs.model.types()
      }
    },
    back(value) {
      this.hide = value
    },
    del(data) {
      let that = this
      this.$confirm({
        title: '是否删除?',
        okText: '确认',
        cancelText: '取消',
        async onOk() {
          let res = await delItem({ id: data.id })
          if (res.success) {
            that.$message.success(`删除成功！${res.message}`)
            that.reload()
          } else {
            that.$exception(res)
          }
        }
      })
    }
  }
}
</script>
