<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="30">
          
          <a-col :md="6" :sm="10" >
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button @click="handleAdd" type="primary" v-has="'cont:btn'" icon="plus">新增</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- table区域-begin -->
    <div style="height:900px;overflow-y:auto;">
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys,onChange: onSelectChange}"
        @change="handleTableChange">
<!--        :locale="myLocale"-->

        <!-- 字符串超长截取省略号显示-->
        <span slot="name" slot-scope="text">
          <j-ellipsis :value="text" :length="20" />
        </span>
        <span slot="describe" slot-scope="text">
          <j-ellipsis :value="text" :length="20" />
        </span>
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)" v-has="'cont:btn'">修改</a>
          <a-divider type="vertical" v-has="'cont:btn'" />
          <a @click="handleDistribute(record.id)" v-has="'cont:btn'">下发任务</a>
          <a-divider type="vertical" v-has="'cont:btn'" />
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <taskModal ref="modalForm" @ok="modalFormOk"></taskModal>
  </a-card>
</template>

<script>
  import taskModal from './modules/taskModal'
  import { shipModelPageList,
    shipModeldeleteById } from '@/api/ship'
  import { taskCreate,
    taskUpdateById,
    taskQueryById,
    taskPageList,
    taskDistributeTask,
    taskDeleteById } from '@/api/task'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import JEllipsis from "@/components/jeecg/JEllipsis";

  export default {
    name: "tasklist",
    mixins:[JeecgListMixin],
    components: {
      taskModal,
      JEllipsis,
      VNodes: {
        functional: true,
        render: (h, ctx) => ctx.props.vnodes,
      },
    },
    data () {
      return {
        description: '任务规划管理',
        dataSources: [],
        queryParam: {
          pageNum :1,
          pageSize:20
        },
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'id',
            width:60,
            align:"id",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title: '任务名称',
            align:"name",
            dataIndex: 'name',
          },
          {
            title: '创建时间',
            align:"createTime",
            dataIndex: 'createTime'
          },
          {
            title: '开始时间',
            align:"startTime",
            dataIndex: 'startTime'
          },
          {
            title: '结束时间',
            align:"endTime",
            dataIndex: 'endTime',
          },
          {
            title: '下发时间',
            align:"issuingTime",
            dataIndex: 'issuingTime',
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            width:180,
            scopedSlots: { customRender: 'action' },
          }
        ]
      }
    },
    computed: {
    },
    created () {
      this.loadData();
    },
    methods: {
      //筛选需要重写handleTableChange
      handleTableChange(pagination, filters, sorter) {
        //分页、排序、筛选变化时触发
        //TODO 筛选
        if (Object.keys(sorter).length > 0) {
          this.isorter.column = sorter.field;
          this.isorter.order = "ascend" == sorter.order ? "asc" : "desc"
        }
        this.ipagination = pagination;
        this.loadData();
      },
      loadData() {
        taskPageList(this.queryParam).then((res) => {
            if (res.success) {
                this.dataSource = res.result.rows||res.result;
                if(res.result.total)
                {
                  this.ipagination.total = res.result.total;
                }
            } else {
                this.$message.warning(res.message);
            }
        });
      },
      handleDelete: function (id) {
        var that = this;
        taskDeleteById({id: id}).then((res) => {
          if (res.success) {
            that.$message.success(res.message);
            that.loadData();
          } else {
            that.$message.warning(res.message);
          }
        });
      },
      handleEdit: function (record) {
        this.$refs.modalForm.edit(record);
        this.$refs.modalForm.title = "编辑";
      },
      handleDistribute:function (id) {
        var that = this;
        taskDistributeTask({id: id}).then((res) => {
          if (res.success) {
            that.$message.success(res.message);
            that.loadData();
          } else {
            that.$message.warning(res.message);
          }
        });
      },
      handleAdd() {
        this.$refs.modalForm.add();
        this.$refs.modalForm.title = "新增";
      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>