<template>
  <div class="card-search">
    <a-form ref="formRef" :model="searchParams" class="ant-advanced-search-form" name="advanced_search">
      <a-row :gutter="24">
        <a-col :span="6">
          <a-form-item :label-col="{span:6}" label="任务名称" name="name">
            <a-input v-model:value="searchParams.name" placeholder=""/>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item :label-col="{span:6}" label="下达时间" name="orderDate">
            <a-range-picker v-model:value="orderDate" :placeholder="['开始日期', '结束日期']" format="YYYY-MM-DD"
                            @change="onRangeChange"/>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item :label-col="{span:6}" label="完成时间" name="finishDate">
            <a-range-picker v-model:value="finishDate" :placeholder="['开始日期', '结束日期']" format="YYYY-MM-DD"
                            @change="onFinishChange"/>
          </a-form-item>
        </a-col>
        <a-col :span="6" style="text-align: left">
          <a-button html-type="submit" type="primary" @click="onSearch">查询</a-button>
          <a-button style="margin: 0 8px" @click="formRef.resetFields();onReset();">重置</a-button>
          <a style="font-size: 12px" @click="expand = !expand">
            <template v-if="expand">
              <UpOutlined/>
            </template>
            <template v-else>
              <DownOutlined/>
            </template>
            {{ expand ? '收缩' : '展开' }}
          </a>
        </a-col>
      </a-row>
      <a-row v-show="expand" :gutter="24">
        <a-col :span="6">
          <a-form-item :label-col="{span:6}" label="所属县区" name="regionCode">
            <a-select
              ref="select"
              v-model:value="searchParams.regionCode"
              :allow-clear="true"
              :field-names="{ label: 'name', value: 'code' }"
              :options="regionList"
              @change="changeCity"
            >
            </a-select>
          </a-form-item>
        </a-col>
        <!--        <a-col :span="6">-->
        <!--          <a-form-item :label-col="{span:6}" label="所属街道" name="streetCode">-->
        <!--            <a-select-->
        <!--              ref="select"-->
        <!--              v-model:value="searchParams.streetCode"-->
        <!--              :allow-clear="true"-->
        <!--              :field-names="{ label: 'name', value: 'code' }"-->
        <!--              :options="streetList"-->
        <!--              @change="loadData"-->
        <!--            >-->
        <!--            </a-select>-->
        <!--          </a-form-item>-->
        <!--        </a-col>-->
        <a-col :span="6">
          <a-form-item :label-col="{span:6}" label="任务类型" name="workTypeId">
            <a-select
              ref="select"
              v-model:value="searchParams.workTypeId"
              :allow-clear="true"
              :field-names="{ label: 'name', value: 'value' }"
              :options="taskTypeList"
              @change="loadData"
            >
            </a-select>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row class="edit-operation">
        <a-col :span="24" style="text-align: right">
          <a-button functioncode="T01040102" html-type="submit" type="primary" @click='onAdd'>新增</a-button>
          <BExportExcel :filename="'任务信息'"
                        :params="{...searchParams, isExport: true, rows:10000,doTaskIDList:formState.selectedRowKeys.join(',')}"
                        :title="'导出'"
                        :url="'taskAndLog/doTask/export'"></BExportExcel>
        </a-col>
      </a-row>
    </a-form>
    <div class="search-result-list">
      <a-table :columns="columns" :data-source="dataList" :loading="formState.loading" :pagination="pagination"
               :row-key="(record)=>record.doTaskID"
               :row-selection="{ selectedRowKeys: formState.selectedRowKeys, onChange: onSelectChange}"
               :scroll="{ x:'100%', y: 500 }"
               bordered
               @change="handleTableChange">
        <template #bodyCell="{ column, text, record }">
          <template v-if="column.key === 'operation'">
            <div class="table-operation">
              <a-button functioncode="T01040101" size="small" type="link" @click='onDetail(record)'>查看</a-button>
              <a-button functioncode="T01040103" size="small" type="link" @click='onEdit(record)'>编辑</a-button>
              <a-button functioncode="T01040104" size="small" type="link" @click="onDel(record)">删除</a-button>
            </div>
          </template>
        </template>
      </a-table>
    </div>
  </div>
</template>

<script lang="ts">
import {get} from '@/api/common';
import {computed, createVNode, defineComponent, reactive, ref} from 'vue';
import {DownOutlined, ExclamationCircleOutlined, UpOutlined} from '@ant-design/icons-vue';
import type {FormInstance, SelectProps, TableColumnsType, TableProps} from 'ant-design-vue';
import {message, Modal} from "ant-design-vue";
import {del, getList} from '@/api/taskAndLog/dotask';
import BExportExcel from "@/components/basic/excel/exportExcel/exportExcel.vue";
import {getPaginationTotalTitle} from "@/utils/common";
import dayjs from 'dayjs';
import {useTabsViewStore} from "@/store/modules/tabsView";


export default defineComponent({
  name: 'doTaskList',
  components: {DownOutlined, UpOutlined, BExportExcel},
  setup() {
    const formRef = ref<FormInstance>();
    const searchParams = reactive({
      pageIndex: 1,
      pageSize: 20,
      recordStatus: 1,
      startTime: '',
      endTime: '',
      finishStartTime: '',
      finishEndTime: '',
      workTypeId: '',
      regionCode: '',
      streetCode: ''
    });
    const formState = reactive({
      total: 0,
      selectedRowKeys: [],
      loading: false
    });
    const columns: TableColumnsType = [
      {
        title: '序号',
        align: "center",
        key: 'institutionID',
        customRender: item => `${searchParams.pageSize * (searchParams.pageIndex - 1) + item.index + 1}`
      },
      {title: '任务名称', dataIndex: 'doTaskName', key: 'doTaskName', align: "center"},
      {title: '任务类型', dataIndex: 'workTypeName', key: 'workTypeName', width: 120, align: "center"},
      {
        title: '任务完成时间', dataIndex: 'finishTime', key: 'finishTime', align: "center", customRender: (item) => {
          return item.record.finishTime == null ? "" : (dayjs(item.record.finishTime).format('YYYY-MM-DD'))
        }
      },
      {title: '所属县区', dataIndex: 'regionName', key: 'regionName', align: "center"},
      // {title: '所属街道', dataIndex: 'streetName', key: 'streetName', align: "center"},
      {title: '执行用户数', dataIndex: 'userCount', key: 'userCount', align: "center"},
      {
        title: '下达时间', dataIndex: 'createTime', key: 'createTime', align: "center", customRender: (item) => {
          return item.record.createTime == null ? "" : (dayjs(item.record.createTime).format('YYYY-MM-DD'))
        }
      },
      {title: '下达人', dataIndex: 'createName', key: 'createName', align: "center"},
      {title: '操作', key: 'operation', fixed: 'right', width: 150, align: "center"},
    ];
    const pagination = computed(() => ({
      total: formState.total,
      current: searchParams.pageIndex,
      pageSize: searchParams.pageSize,
      showSizeChanger: true,
      showTotal: total => getPaginationTotalTitle(total)
    }));

    const tabsViewStore = useTabsViewStore();
    const regionList = ref<SelectProps['options']>();
    const streetList = ref<SelectProps['options']>();
    const taskTypeList = ref<SelectProps['options']>();
    const orderDate = ref([]);
    const finishDate = ref([]);
    const dataList = ref([]);
    const expand = ref(false);

    const onAdd = () => {
      tabsViewStore.addTabByPath('/taskAndLog/dotask/add', null);
    };
    const onEdit = (item: any) => {
      tabsViewStore.addTabByPath('/taskAndLog/dotask/edit', {id: item.doTaskID});
    };
    const onDetail = (item: any) => {
      tabsViewStore.addTabByPath('/taskAndLog/dotask/detail', {id: item.doTaskID});
    };

    get('system/area/getCityList', {}).then(data => {
      // console.log(data);
      regionList.value = data;
      // console.log(regionList.value);
    });

    get('system/dictionary/getDictionaryItemByCodeList', {code: 'TaskType'}).then(data => {
      // console.log(data);
      taskTypeList.value = data;
    });

    const changeCity = () => {
      if (searchParams.regionCode) {
        get('system/area/getAreaList', {code: searchParams.regionCode}).then(data => {
          streetList.value = data;
        })
      }
      loadData();
    }

    const loadData = async function () {
      formState.loading = true;
      const result: any = await getList(searchParams);
      dataList.value = result.list;
      // console.log(dataList.value);
      formState.total = result.total;
      formState.loading = false;
    }

    const handleTableChange: TableProps['onChange'] = (pag: { pageSize: number; current: number },) => {
      searchParams.pageIndex = pag.current;
      searchParams.pageSize = pag.pageSize;
      loadData();
    };

    const onSelectChange = (selectedRowKeys: any) => {
      formState.selectedRowKeys = selectedRowKeys;
    };

    const onRangeChange = (dateString) => {
      orderDate.value = dateString;
      searchParams.startTime = dateString ? dateString[0].format("YYYY-MM-DD") : '';
      searchParams.endTime = dateString ? dateString[1].format("YYYY-MM-DD") : '';
      loadData();
    }

    const onFinishChange = (dateString) => {
      finishDate.value = dateString;
      searchParams.finishStartTime = dateString ? dateString[0].format("YYYY-MM-DD") : '';
      searchParams.finishEndTime = dateString ? dateString[1].format("YYYY-MM-DD") : '';
      loadData();
    }

    const onReset = () => {
      orderDate.value = [];
      finishDate.value = [];
      searchParams.startTime = '';
      searchParams.endTime = '';
      searchParams.finishStartTime = '';
      searchParams.finishEndTime = '';
      loadData();
    };


    const onSearch = () => {
      loadData();
    }

    const onDel = (item: any) => {
      if (item) {
        formState.selectedRowKeys.push(item.doTaskID as never)
      }

      if (formState.selectedRowKeys.length <= 0) {
        message.warning('请选择需要删除的数据！');
        return false;
      }
      Modal.confirm({
        title: '确认删除选中的数据?',
        icon: createVNode(ExclamationCircleOutlined),
        content: '',
        okText: '确认删除',
        okType: 'danger',
        okButtonProps: {},
        cancelText: '取消',
        onOk() {
          del(formState.selectedRowKeys).then(() => {
            loadData();
          });
        },
        onCancel() {

        },
      });

    };

    return {
      expand,
      orderDate,
      finishDate,
      regionList,
      streetList,
      taskTypeList,
      formRef,
      searchParams,
      formState,
      columns,
      pagination,
      dataList,
      handleTableChange,
      onDel,
      onReset,
      onSearch,
      onSelectChange,
      onRangeChange,
      onFinishChange,
      changeCity,
      loadData,
      onAdd,
      onEdit,
      dayjs,
      onDetail
    };
  },
  created() {
    this.loadData();
  },
  activated() {
    if (history.state.params?.reload)
      this.loadData();
  }
});
</script>
<style lang="less" scoped></style>
