<template>
  <div class="__normal-page">
    <div class="__normal-content">
      <CusContent
          v-model:tableHead="tableHead"
          @handleReset="handleReset"
          @handleSearch="onSearch"
      >
        <template #fieldOut>
          <CusForm labelWidth="100px" @handleEnter="onSearch">
            <CusFormColumn
                label="标题:"
                v-model:param="queryForm.title"/>
            <CusFormColumn
                label="发布单位:"
                link="select"
                v-model:param="queryForm.deptId"
                :options="$store.state.dictionary.deptList"/>
            <CusFormColumn
                label="发布人:"
                v-model:param="queryForm.publisher"/>
            <CusFormColumn
                label="发布时间:"
                link="datetime"
                type="datetimerange"
                v-model:param="queryForm.publishDate"/>
            <CusFormColumn
                label="类型:"
                link="select"
                v-model:param="queryForm.type"
                :options="$store.state.dictionary.noticeTypeList"/>
            <CusFormColumn
                label="状态:"
                link="select"
                v-model:param="queryForm.status"
                :options="$store.state.dictionary.noticeStatusList"/>
            <CusSearchButtons
                @handleReset="handleReset"
                @handleSearch="onSearch"
            />
          </CusForm>
        </template>
        <template #buttons>
          <div class="__cus-button_submit __hover" @click="onAdd">
            <SvgIcon name="add" size="16"/>新增
          </div>
        </template>
        <template #table>
          <CusTable
              v-loading="loading"
              ref="ref_cusTable"
              :tableData="queryResult.tableData"
              :tableHead="tableHead"
              :total="queryResult.total"
              :page="queryPage.pageNum"
              :pageSize="queryPage.pageSize"
              @handlePage="handlePage"
          >
            <template #title-column-value="{ scope }">
              <template v-if="scope.row.status === '1'">
                <el-link class="__cus-table_column-view" @click="onView(scope.row)">{{scope.row.title}}</el-link>
              </template>
              <template v-else>{{scope.row.title}}</template>
            </template>
            <template #publishTime-column-value="{ scope }">
              {{scope.row.publishTime ? $util.YMDHms(scope.row.publishTime) : scope.row.publishTime}}
            </template>
            <template #type-column-value="{ scope }">
              {{$store.state.dictionary.noticeTypeMap.get(scope.row.type) || scope.row.type}}
            </template>
            <template #status-column-value="{ scope }">
              {{$store.state.dictionary.noticeStatusMap.get(scope.row.status) || scope.row.status}}
            </template>
            <template #do-column-value="{ scope }">
              <div class="__cus-table_do">
                <div class="__cus-table_do-green __hover" @click="onView(scope.row)" v-if="scope.row.status === '1'">
                  <SvgIcon name="view" size="16"/>查看
                </div>
                <div class="__cus-table_do-blue __hover" @click="onEdit(scope.row)" v-if="scope.row.status === '2'">
                  <SvgIcon name="edit" size="16"/>编辑
                </div>
                <div class="__cus-table_do-green __hover" @click="onPublish(scope.row)" v-if="scope.row.status === '2'">
                  <SvgIcon name="publish" size="16"/>发布
                </div>
                <div class="__cus-table_do-blue __hover" @click="onRevoke(scope.row)" v-if="scope.row.status === '1'">
                  <SvgIcon name="back" size="16"/>撤回
                </div>
                <div class="__cus-table_do-red __hover" @click="onDel(scope.row)" v-if="scope.row.status === '2'">
                  <SvgIcon name="del" size="16"/>删除
                </div>
              </div>
            </template>
          </CusTable>
        </template>
      </CusContent>
    </div>
    <DetailCom v-model:show="showDetail" :transfer="transfer" @refresh="refreshSearch"/>
  </div>
</template>

<script lang="ts">
import {
  defineComponent,
  computed,
  onMounted,
  ref,
  reactive,
  watch,
  getCurrentInstance,
  ComponentInternalInstance,
  toRefs,
  nextTick
} from 'vue'
import {useStore} from 'vuex'
import {useRouter, useRoute} from 'vue-router'
import DetailCom from './detail.vue'
import {ElMessage, ElMessageBox} from 'element-plus';

export default defineComponent({
  name: '',
  components: {
    DetailCom
  },
  setup(props, {emit}) {
    const store = useStore();
    const router = useRouter();
    const route = useRoute();
    const that = (getCurrentInstance() as ComponentInternalInstance).appContext.config.globalProperties
    const state = reactive({
      //  加载中
      loading: false,
      //  查询分页参数
      queryPage: {
        pageNum: 1,
        pageSize: 10
      },
      //  查询结果
      queryResult: {
        total: 0,
        tableData: []
      },
      //  查询表单参数
      queryForm: <any>{},
      //  查询表单参数备份
      back_queryForm: {},
      //  表格表头
      tableHead: [
        {value: "title", label: "标题", show: true},
        {value: "deptName", label: "发布单位", show: true},
        {value: "publisher", label: "发布人", show: true},
        {value: "publishTime", label: "发布时间", show: true, width: 180},
        {value: "type", label: "类型", show: true},
        {value: "status", label: "状态", show: true},
        {value: "do", label: "操作", show: true, popover: true, width: 260},
      ],
      showDetail: false,
      transfer: {}
    });
    const ref_cusTable = ref()
    //  获取字典
    const initDictionary = () => {
      store.dispatch('dictionary/LOAD_DEPT')
      store.dispatch('dictionary/LOAD_DICT_LIST', 'notice_type')
      store.dispatch('dictionary/LOAD_DICT_LIST', 'notice_status')
    }
    //  查询分页参数改变处理方法
    const handlePage = ({page, pageSize}: any) => {
      state.queryPage.pageNum = page
      state.queryPage.pageSize = pageSize
      handleSearch(page, pageSize)
    }
    //  列表刷新方法
    const refreshSearch = () => {
      state.queryPage.pageNum = 1
      handleSearch(state.queryPage.pageNum, state.queryPage.pageSize)
    }
    //  重置查询表单方法
    const handleReset = () => {
      state.queryForm = {}
      state.back_queryForm = JSON.parse(JSON.stringify(state.queryForm))
      refreshSearch()
    }
    //  查询方法
    const handleSearch = (page = 1, pageSize = 10) => {
      //  添加分页参数
      const queryParams: any = {
        pageNum: page,
        pageSize: pageSize,
      }
      //  添加表单参数
      for (const [k, v] of Object.entries(state.back_queryForm)) {
        if (that.$util.isValue(v)) {
          if (k === 'publishDate') {
            queryParams['beginTime'] = v[0]
            queryParams['endTime'] = v[1]
          } else {
            queryParams[k] = v
          }
        }
      }
      state.loading = true
      that.$api.getNoticeList(queryParams).then((res: any) => {
        if (res.code === 200) {
          state.queryResult.tableData = res.rows
          state.queryResult.total = res.total
        } else {
          ElMessage.error(res.message)
        }
        state.loading = false
      }).catch(() => {
        state.loading = false
      })
    }
    //  点击查询按钮后
    const onSearch = () => {
      ref_cusTable.value.resetFilter()
      state.back_queryForm = JSON.parse(JSON.stringify(state.queryForm))
      refreshSearch()
    }
    const onAdd = () => {
      state.transfer = {
        method: 'add',
      }
      state.showDetail = true
    }
    const onEdit = (val) => {
      state.transfer = {
        method: 'edit',
        detail: val
      }
      state.showDetail = true
    }
    const onView = (val) => {
      state.transfer = {
        method: 'view',
        detail: val
      }
      state.showDetail = true
    }
    const onDel = (val) => {
      ElMessageBox.confirm(`是否删除${val.title}?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        state.loading = true
        that.$api.delNoticeDelete({id: val.id}).then(res => {
          if (res.code === 200) {
            ElMessage.success(res.message)
            refreshSearch()
          } else {
            ElMessage.error(res.message)
            state.loading = false
          }
        }).catch(() => {
          state.loading = false
        })
      }).catch(() => {})
    }
    const onPublish = (val) => {
      ElMessageBox.confirm(`是否发布${val.title}?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        state.loading = true
        that.$api.getNoticePublish({id: val.id}).then(res => {
          if (res.code === 200) {
            ElMessage.success('发布成功！')
            refreshSearch()
          } else {
            ElMessage.error(res.message)
            state.loading = false
          }
        }).catch(() => {
          state.loading = false
        })
      }).catch(() => {})
    }
    const onRevoke = (val) => {
      ElMessageBox.confirm(`是否撤回${val.title}?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        state.loading = true
        that.$api.getNoticeRevoke({id: val.id}).then(res => {
          if (res.code === 200) {
            ElMessage.success('撤回成功！')
            refreshSearch()
          } else {
            ElMessage.error(res.message)
            state.loading = false
          }
        }).catch(() => {
          state.loading = false
        })
      }).catch(() => {})
    }
    onMounted(() => {
      state.back_queryForm = JSON.parse(JSON.stringify(state.queryForm))
      initDictionary()
      refreshSearch()
    })
    return {
      ref_cusTable,
      ...toRefs(state),
      handleSearch,
      handlePage,
      handleReset,
      onSearch,
      onAdd,
      onEdit,
      onView,
      onDel,
      onRevoke,
      onPublish,
      refreshSearch
    }
  },
})
</script>

<style scoped lang="scss">
</style>
