<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.seatSerialNumber"/>
            <CusFormColumn
                v-if="$store.getters['app/isZBGL']"
                label="单位:"
                link="select"
                v-model:param="queryForm.unitId"
                :options="$store.state.dictionary.deptList"/>
            <CusFormColumn
                label="提交人员:"
                v-model:param="queryForm.submitter"/>
            <CusFormColumn
                label="周报日期:"
                link="select"
                static
                v-model:param="queryForm.weeklyDate"
                labelKey="yearWeekSelectValue"
                valueKey="id"
                :options="$store.getters['app/yearWeeks'].list"/>
            <CusFormColumn
                label="提交时间:"
                link="datetime"
                type="datetimerange"
                v-model:param="queryForm.submitDate"/>
            <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 #weeklyDate-column-value="{ scope }">
              {{$store.getters['app/yearWeeks'].map.get(`${scope.row.year}_${scope.row.weekTime}`)?.yearWeekSelectValue || `${scope.row.year}_${scope.row.weekTime}`}}
            </template>
            <template #do-column-value="{ scope }">
              <div class="__cus-table_do">
                <div class="__cus-table_do-green __hover" @click="onView(scope.row)">
                  <SvgIcon name="view" size="16"/>查看
                </div>
<!--                <div class="__cus-table_do-blue __hover" @click="onEdit(scope.row)">-->
<!--                  <SvgIcon name="edit" size="16"/>编辑-->
<!--                </div>-->
<!--                <div class="__cus-table_do-red __hover" @click="onDel(scope.row)">-->
<!--                  <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: "seatSerialNumber", label: "席位编号", show: true},
        {value: "unitName", label: "单位", show: true},
        {value: "submitter", label: "提交人员", show: true},
        {value: "weeklyDate", label: "周报日期", show: true, width: 320},
        {value: "submitTime", label: "提交时间", show: true, width: 180},
        {value: "do", label: "操作", show: true, popover: true, width: 260},
      ],
      showDetail: false,
      transfer: {}
    });
    const ref_cusTable = ref()
    //  获取字典
    const initDictionary = () => {
      store.dispatch('dictionary/LOAD_DEPT')
    }
    //  查询分页参数改变处理方法
    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 === 'unitId') {
            queryParams['unitName'] = store.state.dictionary.deptMap.get(v).organizationName
          } else if (k === 'weeklyDate') {
            queryParams['year'] = store.getters['app/yearWeeks'].map.get(v).year
            queryParams['weekTime'] = store.getters['app/yearWeeks'].map.get(v).week
          } else if (k === 'submitDate') {
            queryParams['beginTime'] = v[0]
            queryParams['endTime'] = v[1]
          } else {
            queryParams[k] = v
          }
        }
      }
      state.loading = true
      that.$api.getWeekReportList(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.delWeekReportDelete({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(() => {})
    }
    onMounted(() => {
      state.back_queryForm = JSON.parse(JSON.stringify(state.queryForm))
      initDictionary()
      refreshSearch()
    })
    return {
      ref_cusTable,
      ...toRefs(state),
      handleSearch,
      refreshSearch,
      handlePage,
      handleReset,
      onSearch,
      onAdd,
      onEdit,
      onView,
      onDel
    }
  },
})
</script>

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