<template>
  <a-card :bordered="false" class="task-list">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="24">
          <a-col :md="6" :sm="8">
            <a-form-item label="任务名称">
              <a-input placeholder="请输入任务名称" v-model="queryParam.taskName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="APP包名">
              <a-input placeholder="请输入APP包名" v-model="queryParam.appPackage"></a-input>
            </a-form-item>
          </a-col>
          <template>
            <a-col :md="6" :sm="8">
              <a-form-item label="APP界面名">
                <a-input placeholder="请输入APP界面名" v-model="queryParam.appActivity"></a-input>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
    </div>

    <!-- table区域-begin -->
    <div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        @change="handleTableChange"
        @expand="expand"
      >
        <a-table
          slot="expandedRowRender"
          slot-scope="text"
          :columns="innerColumns"
          :dataSource="text.innerData"
          :pagination="false"
        >
          <span slot="action" slot-scope="text, record" class="table-operation">
            <a href="javascript:;" @click="lookDetail(record)">查看</a>
          </span>
        </a-table>
      </a-table>
    </div>
    <!-- table区域-end -->
    <a-modal title="执行设备" :visible="visible" @ok="handleOk" @cancel="handleCancel">
      <!-- dataList -->
      <a-select
        showSearch
        style="width:100%"
        placeholder="请选择设备"
        optionFilterProp="children"
        @change="handleChange"
        :filterOption="filterOption"
        v-model="value"
      >
        <a-select-option :value="item.id" v-for="(item, index) of dataList" :key="index"
          >{{ item.ip }}:{{ item.port }}</a-select-option
        >
      </a-select>
    </a-modal>
    <!-- 表单区域 -->
    <appiumTask-modal ref="modalForm" @ok="modalFormOk"></appiumTask-modal>
    <appiumTask-item-modal ref="modalItemForm" @ok="modalFormOk"></appiumTask-item-modal>
  </a-card>
</template>

<script>
import AppiumTaskModal from './modules/AppiumTaskModal'
import AppiumTaskItemModal from './modules/AppiumTaskItemModal'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { getAction, postAction } from '@/api/manage'
import { transformTozTreeFormat } from '@/utils/util.js'
const innerColumns = [
  {
    title: '名称',
    align: 'center',
    dataIndex: 'name'
  },
  {
    title: '操作',
    dataIndex: 'action',
    align: 'center',
    scopedSlots: { customRender: 'action' },
    width: '210px'
  }
]
export default {
  name: 'AppiumTaskList',
  mixins: [JeecgListMixin],
  components: {
    AppiumTaskModal,
    AppiumTaskItemModal
  },
  data() {
    return {
      description: '任务表管理页面',
      // 表头
      innerColumns,
      visible: false,
      detail: null,
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '任务名称',
          align: 'center',
          dataIndex: 'taskName'
        },
        {
          title: 'APP包名',
          align: 'center',
          dataIndex: 'appPackage'
        },
        {
          title: 'APP界面名',
          align: 'center',
          dataIndex: 'appActivity'
        }
        // {
        //   title: '操作',
        //   dataIndex: 'action',
        //   align: 'center',
        //   scopedSlots: { customRender: 'action' }
        // }
      ],
      url: {
        list: '/appium/appiumTask/list',
        listTwo: '/appium/appiumTaskItem/list',
        delete: '/appium/appiumTask/delete',
        deleteBatch: '/appium/appiumTask/deleteBatch'
      },
      run: '/appium/appiumTask/run',
      appiumDevice: '/appium/appiumDevice/list',
      dataList: [],
      value: null
    }
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    lookDetail(item) {
      this.$refs.modalItemForm.edit(item)
      this.$refs.modalItemForm.title = '编辑'
      this.$refs.modalItemForm.disableSubmit = false
    },
    handleOk() {
      this.visible = false
      var params = {}
      params.deviceId = this.value
      params.taskId = this.detail.id
      if (this.value) {
        postAction(this.run, params).then(res => {
          console.log(res.result.records)
          if (res.code == 0) {
            this.handleCancel()
          }
        })
      } else {
        this.visible = true
        this.$message.error('请选择设备')
        return
      }
    },
    handleCancel() {
      this.value = null
      this.detail = null
      this.visible = false
    },
    start(item) {
      this.visible = true
      this.detail = item
      this.value = null
      var param = {}
      param.pageNo = 1
      param.pageSize = 100

      getAction(this.appiumDevice, param).then(res => {
        console.log(res.result.records)
        if (res.code == 0) {
          this.dataList = res.result.records.filter(item => {
            return item.status == 1
          })
        }
      })
    },
    handleChange(value) {
      //   this.value = value
    },
    filterOption(input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    }
  },
  mounted() {
    this.$nextTick(() => {})
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
<style>
.task-list .ant-table-middle tr.ant-table-expanded-row td > .ant-table-wrapper {
  margin: 0;
}
.task-list tr.ant-table-expanded-row td > .ant-table-wrapper {
  margin: 0;
}
</style>
