<template>
  <div>
    <PageTable ref="sysNoticeTable" action="GetSysNoticeList" :columnsFn="generateColumns">
      <Form :model="formSearch" ref="sysNotice" slot="search-bar" class="ivu-form-no-margin-bottom" inline>
        <Form-item prop="noticeTitle" :label="$t('module.systemNotice.noticeTitle')">
          <Input type="text" v-model="formSearch.noticeTitle" placeholder="" maxlength="30" size="small" />
        </Form-item>
        <Form-item prop="noticeType" :label="$t('module.systemNotice.noticeType')">
          <Select v-model="formSearch.noticeType" size="small" clearable style="width:120px;">
            <Option v-for="item in noticeTypeList" :title="item.vsiValue" :value="item.vsiKey" :key="item.vsiKey">{{ item.vsiValue }}</Option>
          </Select>
        </Form-item>
        <Form-item prop="noticeSource" :label="$t('module.systemNotice.noticeSource ')">
          <Input type="text" v-model="formSearch.noticeSource" placeholder="" maxlength="20" size="small" />
        </Form-item> 
        <Form-item prop="noticeTop" :label="$t('module.systemNotice.noticeTop')">
          <Select v-model="formSearch.noticeTop" size="small" clearable>
            <Option v-for="item in noticeTopList" :title="item.vsiValue" :value="item.vsiKey" :key="item.vsiKey">{{ item.vsiValue }}</Option>
          </Select>
        </Form-item>
        <Form-item prop="noticeStatus" :label="$t('common.status')">
          <Select v-model="formSearch.noticeStatus" size="small" clearable>
            <Option v-for="item in noticeStatusList" :title="item.vsiValue" :value="item.vsiKey" :key="item.vsiKey">{{ item.vsiValue }}</Option>
          </Select>
        </Form-item>
        <Form-item prop="createUserName" :label="$t('module.systemNotice.createUserName')">
          <Input type="text" v-model="formSearch.createUserName" placeholder="" maxlength="30" size="small" />
        </Form-item>
       <!-- <Form-item prop="noticeOrg" :label="$t('common.affiliation')">
            <Input v-model="formSearch.noticeOrg" @click.native="getOrgTree()" disabled></Input>
            <Icon class="getList-icon"  @click.native="getOrgTree()"></Icon>              
        </Form-item> -->
      </Form>
      <div slot="handle-bar">
        <Button size="small" @click.native="search" type="warning" icon="ios-search">{{$t("common.search")}}</Button>
        <Button size="small" @click.native="formReset" type="info" icon="md-sync">{{$t("common.reset")}}</Button>
      </div>
      <div slot="table-bar">
        <Button type="ghost" icon="plus-round" size="small" @click="addOrEdit()">{{$t("common.create")}}</Button>
      </div>
    </PageTable>
  </div>
</template>
<script>
// import GetOrgTree from '../valueSet/GetOrgTree.vue';
import SysNoticeItem  from './SysNoticeItem.vue';
export default {
  name: 'sys-notice',
  components: {
  },
  data() {
    return {
      noticeTypeList: [],
      noticeTopList: [],
      noticeStatusList: [],
      formSearch: {
        noticeTitle: "",
        noticeType: "",
        noticeTop: "",
        noticeStatus: "",
        effectEnd: "",
        noticeOrg: "",
        noticeSource: "",
        createUserName : "",
        orgId: "",
      },
      selectList: [],
    };
  },
  computed: {
    
  },
  created() {
      //类型
      $store.dispatch("GetValueSetByCode", {code: 'NOTICE_TYPE'}).then(response => {
          this.noticeTypeList = response.dataResult;
      });
      //是否置顶
      $store.dispatch("GetValueSetByCode", {code: 'YES_NO'}).then(response => {
          this.noticeTopList = response.dataResult;
      });
      //状态
      $store.dispatch("GetValueSetByCode", {code: 'NOTICE_STATUS'}).then(response => {
          this.noticeStatusList = response.dataResult;
      });
      //刷新页面
      $channel.$on("refreshPage", () => {
        this.$refs["sysNoticeTable"].load(this.formSearch);
      })
      
  },
  mounted() {

  },

  methods: {
    // getOrgTree(){
    //     new this.$pageModal(
    //         GetOrgTree,
    //         {

    //         },(data) => {
    //             if(!!data){
    //                 this.formSearch.noticeOrg = data.orgDesc;
    //                 this.formSearch.orgId = data.orgId;
    //             }else{
    //               this.formSearch.noticeOrg = "";
    //               this.formSearch.orgId = "";
    //             }
    //         }
    //     )
    // },
    generateColumns() {
      return [
        {
          title: this.$t('common.operating'),
          align: "center",
          width: 130,
          fixed: 'left',
          render: (h, params) => {
            return h("RowTools", {
              props: {
                data:[
                    {
                        type: "text",
                        // icon: "md-create",
                        text: this.$t("common.edit"),
                        handle: () => {
                            this.addOrEdit(params.row);
                        }
                    },
                    {
                        type: "text",
                        // icon: "md-menu",
                        text: params.row.noticeStatus == "PUBLISHED" ? this.$t("module.systemNotice.withdraw") : this.$t("module.systemNotice.release"),
                        handle: () => {
                            this.publish(params.row);
                        }
                    },
                    {
                        type: "text",
                        // icon: "md-menu",
                        text: this.$t("common.delete") ,
                        handle: () => {
                            this.deleteNotice(params.row);
                        }
                    },
                    ]
              }
            });
          }
        },
        {
          title: `${this.$t('module.systemNotice.noticeTitle')}`,
          align: "center",
          width: 200,
          key: "noticeTitle",
        },
        {
          title: this.$t('module.systemNotice.noticeType'),
          width: 120,
          align: "center",
          key: "noticeTypeName",
        },
        {
          title: this.$t('module.systemNotice.createUserName'),
          width: 130,
          key: "createUserName",
          align: "center",
        },
        {
          title: this.$t('module.systemNotice.noticeSource '),
          width: 120,
          align: "center",
          key: "noticeSource"
        },
        {
          title: this.$t('common.status'),
          width: 120,
          align: "center",
          key: "noticeStatusName"
        },
        {
          title: this.$t('module.systemNotice.noticeTop'),
          width: 120,
          align: "center",
          key: "noticeTop",
          render:(h, params) => {
            return h(
              "tag",
              // params.row.noticeTop == "YES" ? "是" : "否"
              this.getNoticeTop(params.row.noticeTop)
            )
          }
        },
       
        {
          title: this.$t('module.systemNotice.effectEnd'),
          key: "effectEnd",
          width: 120,
          align: "center",
          render: (h, params) => {
            return h('format', {
              props: {
                type: 'date',
                value: params.row.effectEnd,
                param: 'yyyy-MM-dd'
              }
            })
          }
        }
      ]
    },
    search() {
      // let params = {
      //   noticeTitle: this.formSearch.noticeTitle,
      //   noticeType: this.formSearch.noticeType,
      //   noticeTop: this.formSearch.noticeTop,
      //   noticeStatus: this.formSearch.noticeStatus,
      //   orgId: this.formSearch.orgId,
      //   noticeSource : this.formSearch.noticeSource,
      //   createUserName : this.formSearch.createUserName,
      // };
      this.$refs["sysNoticeTable"].load(this.formSearch);
    },
    formReset() {
      this.$refs["sysNotice"].resetFields();
      this.search();
    },
    addOrEdit(obj) {
      new this.$pageModal(
        SysNoticeItem,
        {
          props: {
           obj
          }
        },
        () => {
        //点击保存按钮就刷新了父页面列表
        }
      );
    },
    //发布、撤销公告
    publish(obj){
        let status = "";
        if(obj.noticeStatus == "PUBLISHED"){
            status = "RECALLED";
        }else{
            status = "PUBLISHED";
        }
        $store.dispatch("UpdateStatus", {version: obj.version, noticeId: obj.noticeId, noticeStatus: status}).then(response => {
            this.search();
        })
    },
    deleteNotice(param){
       new $confirm(`${this.$t("common.confirmDeletion")}`, this).then(() => {
                 $store.dispatch("DeleteNotice", param).then(response => {
                    if(response.statusCode == "0"){
                        // if(formSearch.noticeId != ""){
                        this.$Message.success({
                            content: this.$t("common.deleteSuccess"),
                            duration: 3
                        });
                        // }
                    //刷新
                    this.search();  
                    }else{
                        this.$Message.warning({
                            content: response.msg,
                            duration: 3
                        })
                    }
                })
            })
        },
        getNoticeTop(isTop){
          if(isTop == "YES"){
            return "是"
          }else if(isTop == "NO"){
            return "否"
          }else{
            return ""
          }
        }
    }
}
</script>
<style lang="less">
.modal-group-person {
  .group-person-item {
    padding: 5px 10px;
    margin-bottom: 1px;
  }
  .group-person-item.active {
    background-color: #f8f8f9 !important;
  }
}
.transfer-panel-empty {
  display: block;
  color: #b4bccc;
}
.group-person-item + .transfer-panel-empty {
  display: none;
}
.group-person-select + .transfer-panel-empty {
  display: none;
}
.group-person-select {
  width: 100%;
  padding: 5px 10px;
  margin-bottom: 1px;
}
</style>
