<template>
  <div style="padding:10px">
    <q-card inline class="fit shadow-6">
      <q-card-main style="height:80%">
        <q-table
          ref="table"
          color="primary"
          :data="serverData"
          :columns="columns"
          separator="cell"
          selection="multiple"
          :selected.sync="selected"
          row-key="id"
          :pagination.sync="serverPagination"
          @request="request"
          :loading="loading"
          :rows-per-page-label="$t('Rows per page')"
          :rows-per-page-options="[5,10,20,30,40,50,60,200,500]"
          :no-data-label="$t('No data')"
        >
          <template slot="top-left" slot-scope="props">
            <q-input v-model="filter.name" type="text" prefix="标题：" />&nbsp;&nbsp;
             <q-datetime v-model="filter.datetime" type="date" prefix="日期：" clearable/>&nbsp;&nbsp;
            <q-btn push dense color="primary" icon="search" @click="search">{{$t('Search')}}</q-btn>&nbsp;&nbsp;
            <q-btn push dense color="primary" icon="add" @click="addsaveHoliday">{{$t('Add')}}</q-btn>&nbsp;&nbsp;
          </template>
          <template slot="top-right" slot-scope="props">
            <q-btn
              flat
              round
              dense
              :icon="props.inFullscreen ? 'fullscreen_exit' : 'fullscreen'"
              @click="props.toggleFullscreen"
            />
          </template>
          <template slot="top-selection" slot-scope="props">
            <q-btn icon="create" color="positive" label="启用" @click="updateDHolidaysStatuslist (1)" />&nbsp;&nbsp;
            <q-btn icon="create" color="negative" label="停用" @click="updateDHolidaysStatuslist (0)" />
          </template>
          <q-td slot="body-cell-id" slot-scope="props" :props="props" style="width:200px">
           <q-btn
              small
              round
              push
              glossy
              dense
              icon="edit"
              color="primary"
              @click="editHoliday(props.value,1)"
            ></q-btn>
            <q-btn
              small
              round
              push
              glossy
              dense
              icon="delete"
              color="red"
              @click="delHoliday(props.value)"
            ></q-btn>
            <!--<q-btn-dropdown color="secondary" label="操作" no-wrap>
                <q-list link>
                  <q-item v-close-overlay  @click.native="editHoliday(props.value,1)">
                    <q-item-main>
                      <q-item-tile sublabel>修改</q-item-tile>
                    </q-item-main>
                  </q-item>
                  <q-item v-close-overlay @click.native="delHoliday(props.value)" >
                    <q-item-main>
                      <q-item-tile sublabel >删除</q-item-tile>
                    </q-item-main>
                  </q-item>
                  <q-item v-close-overlay>
                    <q-item-main>
                    <q-item-tile sublabel>关闭弹窗</q-item-tile>
                    </q-item-main>
                  </q-item>
                </q-list>
            </q-btn-dropdown>-->
          </q-td>
        </q-table>
      </q-card-main>
    </q-card>
    <!-- 新增修改页面 -->
    <q-modal v-model="editModal" maximized>
      <q-modal-layout>
        <q-toolbar slot="header">
          <q-btn flat round dense @click="editModal = false" icon="reply" />
          <q-toolbar-title v-if="subtype=='edit'">{{$t('Edit')}}</q-toolbar-title>
          <q-toolbar-title v-if="subtype=='add'">{{$t('Add')}}</q-toolbar-title>
        </q-toolbar>
        <q-toolbar slot="footer">
          <q-toolbar-title></q-toolbar-title>
          <q-btn round color="red"  @click="saveUpdateHoliday" v-if="subtype=='edit'">{{$t('Save')}}</q-btn>
          <q-btn round color="red"  @click="saveHoliday" v-if="subtype=='add'">{{$t('Add')}}</q-btn>
          <q-btn round @click="editModal = false">{{$t('Cancel')}}</q-btn>
        </q-toolbar>
        <div class="layout-padding">
          <q-field  class="liststype">
            <q-input v-model="tempHoliday.title" float-label="标题" />
          </q-field>
           <q-field  class="liststype"  >
             <q-item  style="    width: 100%;    padding: 0;">
               <q-datetime v-model="tempHoliday.datetime" type="date" float-label="日期：" style="    width: 100%;    padding: 0;"/>
             </q-item>
          </q-field>
           <q-field  class="liststype">
            <q-input v-model="tempHoliday.holiday" float-label="节日名称" />
          </q-field>
          <q-field class="liststype">
            <q-select v-model="tempHoliday.status" :options="yeornoOptions" float-label="启用" />
          </q-field>
             <q-field  class="liststype">
            <q-input v-model="tempHoliday.memo" float-label="描述" />
          </q-field>
        </div>
      </q-modal-layout>
    </q-modal>
  </div>
</template>

<script>
import {
  getHolidayPagedList,
  delHoliday,
  updateHolidayStatus,
  saveHoliday,
  editHoliday,
  loadHoliday,
  updateHolidaysStatus,
  getSysHolidayListByTypeAndNum
} from "@/service/holiday/holiday";
import tool from "@/service/other/tool";
export default {
  name: "holiday",
  data() {
    return {
      serverData: [],
      menuModal: false,
      tempFunction: {
        id: 0,
        name: "",
        moduleId: 0,
        module: "",
        code: "",
        description: ""
      },
      serverPagination: {
        page: 1,
        rowsNumber: 0, // specifying this determines pagination is server-side
        rowsPerPage: 10 // current rows per page being displayed
      },
      nextserverPagination: {
        page: 1,
        rowsNumber: 0, // specifying this determines pagination is server-side
        rowsPerPage: 100 // current rows per page being displayed
      },
      selectOptions: [
        {
          label: "启用",
          value: "1",
          selected:true
        },
        {
          label: "停用",
          value: "0"
        }
      ],
      yeornoOptions: [
        {
          label: "是",
          value: "1",
          selected:true
        },
        {
          label: "否",
          value: "0"
        }
      ],
      columns: [
        {
          name: "title",
          required: true,
          label: "标题",
          align: "left",
          field: "title",
         sortable: false
        },
        
        {
          name: "datetime",
          label: "日期",
          field: "datetime",
         sortable: false,
          align: "center"
        },
        {
          name: "holiday",
          label: "节日名称",
          field: "holiday",
         sortable: false,
          align: "center"
        },{
          name: "statusname",
          label: "启用状态",
          field: "statusname",
         sortable: false,
          align: "center"
        },
        {
          name: "memo",
          label: "描述",
          field: "memo",
         sortable: false,
          align: "left"
        },
        {
          name: "id",
          required: true,
          label: this.$t("ID"),
          align: "center",
          field: "id"
        }
      ],
      filter: {
        name: "",
        pid: "",
        datetime:""
      },
      selected: [],
      loading: false,
      editModal: false,
      subtype: "add",
      tempHoliday: {
        id: "",
        title: "",
        status: "",
        datetime: "",
        holiday: "",
        memo: ""
      }
    };
  },
  methods: {
    async request(props) {
      this.loading = true;
      this.serverPagination = props.pagination;
      let table = this.$refs.table,
        { page, rowsPerPage, sortBy, descending } = props.pagination;
      let query = {
        pageNum: page,
        pageSize: rowsPerPage,
        sortBy: sortBy,
        descending: descending,
        title: this.filter.name,
        datetime:
              this.filter.datetime != undefined &&
              this.filter.datetime != null
                ? tool.GMTToStr(this.filter.datetime)
                : ""
        // pid: this.pid
        // account: this.filter.account,
        // phoneNumber: this.filter.phoneNumber
      };

      let dataRes = await getHolidayPagedList(query);
      let data = dataRes.data.data;
      this.serverPagination.rowsNumber = data.total;
      this.serverData = data.list;
          // this.getSysHolidayListByTypeAndNum();
      setTimeout(() => {
        this.loading = false;
      }, 500);
    },
    async delHoliday(id) {
      try {
          await this.$q.dialog({
            title: "删除操作",
            message: this.$t("Comfirm do it"),
            position: "right",
            ok: {
              push: true,
              label: "确认"
            },
            cancel: {
              push: true,
              color: "negative",
              label: "取消"
            }
          });
          await delHoliday({ id: id });
          this.$q.notify({
            type: "positive",
            message: this.$t("Successfully"),
            position: "bottom-right"
          });
          this.search();
        } catch (e) {}
    },
    /**
     * 批量更新状态
     */
    async updateDHolidaysStatuslist(type) {
      try {
        await this.$q.dialog({
          title: this.$t("Batch do it"),
          message: this.$t("Comfirm do it"),
          ok: {
            push: true,
            label: "提交"
          },
          cancel: {
            push: true,
            color: "negative",
            label: "取消"
          }
        });
        let idlist = this.selected.map(s => {
          return s.id;
        });
        await updateHolidaysStatus(idlist.join(","), type);
        this.$q.notify({
          type: "positive",
          message: this.$t("Successfully"),
          position: "bottom-right"
        });
        this.selected = [];
        this.search();
      } catch (e) {}
    },
    async getSysHolidayListByTypeAndNum(){
      
     let data=await getSysHolidayListByTypeAndNum("2", "1");
    },
    /**用户修改 */
    editHoliday(id, type) {
      this.subtype = "edit";
      let editHoliday = this.serverData.filter(s => {
        return s.id === id;
      });

      if (editHoliday && editHoliday.length > 0) {
        this.tempHoliday = JSON.parse(JSON.stringify(editHoliday[0]));
      } else {
        return true;
      }
      this.editModal = true;
    },
    async saveUpdateHoliday() {
        if (this.tempHoliday.title == null ||
        this.tempHoliday.datetime == null ||
        this.tempHoliday.holiday == null ||
        this.tempHoliday.title == "" ||
        this.tempHoliday.datetime == "" ||
        this.tempHoliday.holiday == "" 
        
      ) {
        this.$q.notify({
          message: "请完善信息，除描述外其他项不能为空",
          type: "negative",
          icon: "error_outline",
          timeout: 3000,
          position: "center"
        });
        return false;
      }
       this.tempHoliday.datetime =
        this.tempHoliday.datetime != undefined && this.tempHoliday.datetime != null
          ? tool.GMTToStr(this.tempHoliday.datetime)
          : "";
      await editHoliday(this.tempHoliday);
      this.$q.notify({
        type: "positive",
        message: this.$t("Saved successfully"),
        position: "bottom-right"
      });
      this.editModal = false;
        this.search();
    },
    search() {
  
      this.request({
        pagination: this.serverPagination,
        filter: this.filter
      });
    },
    /**
     * 更新用户
     */
    async saveHoliday() {
     
       if (this.tempHoliday.title == null ||
        this.tempHoliday.datetime == null ||
        this.tempHoliday.holiday == null ||
        this.tempHoliday.title == "" ||
        this.tempHoliday.datetime == "" ||
        this.tempHoliday.holiday == "" 
        
      ) {
        this.$q.notify({
          message: "请完善信息，除描述外其他项不能为空",
          type: "negative",
          icon: "error_outline",
          timeout: 3000,
          position: "center"
        });
        return false;
      }
       this.tempHoliday.pid = this.pid;
        this.tempHoliday.datetime =
        this.tempHoliday.datetime != undefined && this.tempHoliday.datetime != null
          ? tool.GMTToStr(this.tempHoliday.datetime)
          : "";
        await saveHoliday(this.tempHoliday);
        this.$q.notify({
          type: "positive",
          message: this.$t("Saved successfully"),
          position: "bottom-right"
        });
        this.editModal = false;
          this.search();
    },
    search() {
      this.request({
        pagination: this.serverPagination,
        filter: this.filter
      });
    },
    addsaveHoliday() {
      this.pid = "1";
      this.tempHoliday = {};
      this.tempHoliday.status="1";
      this.subtype = "add";
      this.editModal = true;
    },
      },

  mounted() {
    this.request({
      pagination: this.serverPagination,
      filter: this.filter
    });
  }
};
</script>
<style scoped>
.liststype {
  width: 98%;
  /* padding-left: 4%; */
  /* float: left; */
}
</style>
