<template>

  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="任务名称">
              <j-input placeholder="请输入任务名称" v-model="queryParam.taskName"></j-input>
              <j-input style="display: none" placeholder="请输入任务名称" v-model="queryParam.type = 3"></j-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="任务状态">
              <j-dict-select-tag placeholder="请选择任务状态" v-model="queryParam.taskState" dictCode="sms_check_task_state"/>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="10" :lg="11" :md="12" :sm="24">
              <a-form-item label="开始时间">
                <j-date :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择开始时间"
                        class="query-group-cust" v-model="queryParam.taskStartTime_begin"></j-date>
                <span class="query-group-split-cust"></span>
                <j-date :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择结束时间"
                        class="query-group-cust" v-model="queryParam.taskStartTime_end"></j-date>
              </a-form-item>
            </a-col>
            <a-col :xl="10" :lg="11" :md="12" :sm="24">
              <a-form-item label="结束时间">
                <j-date :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择开始时间"
                        class="query-group-cust" v-model="queryParam.taskEndTime_begin"></j-date>
                <span class="query-group-split-cust"></span>
                <j-date :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择结束时间"
                        class="query-group-cust" v-model="queryParam.taskEndTime_end"></j-date>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <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>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" v-has="'task:list:add'" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" v-has="'task:list:export'" icon="download" @click="handleExportXls('稽核任务')">导出任务
      </a-button>
      <!-- 高级查询区域 -->
      <!--      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>-->
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel" v-has="'task:list:delete'">
            <a-icon type="delete"/>
            删除
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作
          <a-icon type="down"/>
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域 begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a
        style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <span slot="action" slot-scope="text, record">
          <span v-has="'task:list:edit'">
            <a @click="handleEdit(record)">编辑</a>
            <a-divider type="vertical"/>
          </span>
          <span v-has="'task:list:detail'">
            <a @click="handleDetail(record)">详情</a>
            <a-divider type="vertical"/>
          </span>
          <span v-has="'task:list:detail'">
            <a @click="showCheckList(record)">稽核详情</a>
            <a-divider type="vertical"/>
          </span>

          <a-dropdown v-has="'task:list:more'">
            <a class="ant-dropdown-link">更多<a-icon type="down"/></a>
            <a-menu slot="overlay">

              <a-menu-item v-has="'task:list:start'" v-if="record.taskState == 0">
                <a-popconfirm title="确定开始稽核吗?" @confirm="() => checkStart(record)">
                  <a>开始稽核</a>
                </a-popconfirm>
              </a-menu-item>

              <a-menu-item v-has="'task:list:end'" v-if="record.taskState == 1">
                <a-popconfirm title="确定结束稽核吗?" @confirm="() => checkEnd(record)">
                  <a>结束稽核</a>
                </a-popconfirm>
              </a-menu-item>

              <a-menu-item v-has="'task:list:delete'">
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>

            </a-menu>
          </a-dropdown>
        </span>

        <!-- 任务名称可点击 -->
        <template v-slot:taskName="text, record">
          <a @click="showCheckList(record)">{{ text }}</a>
        </template>
      </a-table>
    </div>
    <!-- table区域 end -->

    <!-- 任务添加 -->
    <task-add ref="modalForm" @ok="modalFormOk"></task-add>
    <task-detail ref="modalDetail" @ok="modalFormOk"></task-detail>
  </a-card>


</template>

<script>

import {putAction} from '@api/manage'
import '@/assets/less/TableExpand.less'
import {JeecgListMixin} from '@/mixins/JeecgListMixin'
import TaskAdd from './TaskAdd'
import TaskDetail from '../../smscheck/modules/TaskDetail'


export default {
  name: 'TaskList',
  mixins: [JeecgListMixin],
  inject: ['closeCurrent'],
  components: {
    TaskAdd,
    TaskDetail,
  },
  data() {
    return {
      description: '中继线稽核任务列表',
      disableSubmit: false,
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: "center",
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          }
        },
        {
          title: '任务名称',
          align: "center",
          sorter: true,
          dataIndex: 'taskName',
          scopedSlots: {customRender: 'taskName'}
        },
        {
          title: '任务状态',
          align: "center",
          sorter: true,
          dataIndex: 'taskState_dictText'
        },
        {
          title: '任务开始时间',
          align: "center",
          sorter: true,
          dataIndex: 'taskStartTime'
        },
        {
          title: '任务结束时间',
          align: "center",
          sorter: true,
          dataIndex: 'taskEndTime'
        },
        // {
        //   title:'描述',
        //   align:"center",
        //   dataIndex: 'description'
        // },
        {
          title: '客户总数',
          align: "center",
          dataIndex: 'customerCount'
        },
        // {
        //   title:'待稽核数',
        //   align:"center",
        //   dataIndex: 'uncheckCount'
        // },
        {
          title: '操作',
          dataIndex: 'action',
          align: "center",
          fixed: "right",
          width: 147,
          scopedSlots: {customRender: 'action'}
        }
      ],
      url: {
        list: "/smsCheck/task/list",
        delete: "/smsCheck/task/delete",
        edit: "/smsCheck/task/edit",
        deleteBatch: "/smsCheck/task/deleteBatch",
        exportXlsUrl: "/smsCheck/task/exportXls",
        importExcelUrl: "smsCheck/task/importExcel",
      },
      dictOptions: {},
      superFieldList: [],
      currentTaskState: 0,
      autoSearch: false,
    }
  },
  created() {
    this.queryParam.taskState = this.$route.query.state;
    //直接加载url.list接口数据
    let queryParam = {
      "type":3,
      "column": "createTime",
      'order': "desc"
    }
    this.loadData(null,queryParam);
    this.getSuperFieldList();
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
    },
  },
  methods: {
    // 显示添加页面
    showAdd() {
      this.$emit('goAdd');
    },
    // 显示导入页面
    showImport() {
      // 选择一条
      if (this.selectedRowKeys.length !== 1) {
        this.$message.warning('请选择一条记录！');
        return;
      }
      let taskInfo = this.buildTaskInfo(this.selectionRows[0]);
      this.$router.push({path: '/specialApply/task/import', query: {id: taskInfo.id}});
    },
    // 显示导入页面
    showCheckList(record) {
      this.$router.push({path: '/specialApply/task/check', query: {id: record.id}});
    },
    /**
     * 构建 任务信息对象
     * @param record 任务信息
     * @returns {*} 任务信息对象
     */
    buildTaskInfo(record) {
      return {
        id: record.id,
        name: record.taskName,
        startTime: record.taskStartTime,
        endTime: record.taskEndTime,
        state: record.taskState,
        stateText: record.taskState_dictText,
        description: record.description,
        count: record.customerCount,
        uncheckCount: record.uncheckCount,
        checkCount: record.checkCount,
      };
    },
    initDictConfig() {
    },
    getSuperFieldList() {
      let fieldList = [];
      fieldList.push({type: 'string', value: 'taskName', text: '任务名称', dictCode: ''})
      fieldList.push({type: 'string', value: 'taskState', text: '任务状态', dictCode: 'sms_check_task_state'})
      fieldList.push({type: 'datetime', value: 'taskStartTime', text: '任务开始时间'})
      fieldList.push({type: 'datetime', value: 'taskEndTime', text: '任务结束时间'})
      this.superFieldList = fieldList
    },
    handleDetail(record) {
      this.$refs.modalDetail.detail(record);
      this.$refs.modalDetail.title = "详情";
    },
    checkStart(record) {
      record.taskState = 1;
      let that = this;
      let httpUrl = this.url.edit;
      let param = record;

      // 发送请求
      putAction(httpUrl, param)
        .then((res) => {
          if (res.success) {
            that.$message.success("稽核任务已开始！");
          } else {
            that.$message.warning(res.message);
          }
        }).finally(() => {
        that.loadData();
      })
    },
    checkEnd(record) {
      record.taskState = 2;
      let that = this;
      let httpUrl = this.url.edit;
      let param = record;

      // 发送请求
      putAction(httpUrl, param)
        .then((res) => {
          if (res.success) {
            that.$message.success("稽核任务已结束！");
          } else {
            that.$message.warning(res.message);
          }
        }).finally(() => {
        that.loadData();
      })
    },
  }
}
</script>

<style scoped>

</style>