<template>
  <basic-container>
    <avue-crud :option="option"
               :data="data"
               :page.sync="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.post_delete"
                   @click="handleDelete">删 除
        </el-button>
        <el-button type="success"
                   size="small"
                   plain
                   icon="el-icon-upload2"
                   @click="handleImport">导入
        </el-button>
        <el-button type="warning"
                   size="small"
                   plain
                   icon="el-icon-download"
                   @click="handleExport">导出
        </el-button>
      </template>
      <template slot-scope="{row}"
                slot="category">
        <el-tag>{{row.categoryName}}</el-tag>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import {getList, getDetail, add, update, remove} from "@/api/system/post";
import {mapGetters} from "vuex";

export default {
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: 'auto',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        viewBtn: true,
        selection: true,
        dialogType: "drawer",
        dialogClickModal: false,
        column: [
          {
            "type": 'year',
            "label": '年度',
            "span": 12,
            "display": true,
            "format": 'yyyy',
            "valueFormat": 'yyyy',
            "prop": 'niandu',
            "required": true,
            "rules": [
              {
                "required": true,
                "message": "年度必须选择"
              }
            ]
          },
          {
            "label": "姓名",
            "prop": "xingming",
            "type": "input",
            "span": 12,
            "display": true,
            "labelWidth": 100,
            "required": true,
            "rules": [
              {
                "required": true,
                "message": "姓名必须填写"
              }
            ]
          },
          {
            "label": "性别",
            "prop": "xingbie",
            "type": "select",
            "span": 12,
            "dicData": [
              {
                "label": "男",
                "value": "0"
              },
              {
                "label": "女",
                "value": "1"
              }
            ],
            "display": true,
            "props": {
              "label": "label",
              "value": "value",
              "desc": "desc"
            },
            "labelWidth": 100,
            "required": true,
            "rules": [
              {
                "required": true,
                "message": "性别必须选择"
              }
            ]
          },
          {
            "type":'date',
            "label":'出生年月',
            "span":12,
            "display":true,
            "format":'yyyy-MM-dd',
            "valueFormat":'yyyy-MM-dd',
            "prop":'chushengnianyue',
            "labelWidth":100,
            "required": true,
            "width":85,
            "rules": [
              {
                "required": true,
                "message": "性别必须选择"
              }
            ]
          },
          {
            "label": "政治面貌",
            "prop": "zhengzhimianmao",
            "type": "select",
            "span": 12,
            "dicData": [
              {
                "label": "中共党员",
                "value": "0"
              },
              {
                "label": "九三学社",
                "value": "1"
              }
            ],
            "display": true,
            "props": {
              "label": "label",
              "value": "value",
              "desc": "desc"
            },
            "labelWidth": 100,
          },
          {
            "label": "身份证号",
            "prop": "shenfenzhenghao",
            "type": "input",
            "span": 12,
            "display": true,
            "labelWidth": 100,
            "width":150,
            "required": true,
            "rules": [
              {
                "required": true,
                "message": "身份证号必须填写"
              }
            ]
          },
          {
            "label": "工作单位",
            "prop": "gongzuodanwei",
            "type": "select",
            "span": 12,
            "dicData": [
              {
                "label": "信息技术处",
                "value": "0"
              },
              {
                "label": "政治部",
                "value": "1"
              },
              {
                "label": "人事警务处",
                "value": "2"
              }
            ],
            "display": true,
            "props": {
              "label": "label",
              "value": "value",
              "desc": "desc"
            },
            "labelWidth": 100,
            "required": true,
            "rules": [
              {
                "required": true,
                "message": "单位部门必须填写"
              }
            ]
          },
          {
            "label": "职务",
            "prop": "zhiwu",
            "type": "select",
            "span": 12,
            "dicData": [
              {
                "label": "处长",
                "value": "0"
              },
              {
                "label": "副处长",
                "value": "1"
              },
              {
                "label": "员工",
                "value": "2"
              }
            ],
            "display": true,
            "props": {
              "label": "label",
              "value": "value",
              "desc": "desc"
            },
            "labelWidth": 100,
            "required": true,
            "rules": [
              {
                "required": true,
                "message": "职务必须填写"
              }
            ]
          },
          {
            "label": "级别",
            "prop": "jibie",
            "type": "select",
            "span": 12,
            "width":90,
            "dicData": [
              {
                "label": "县处级副职",
                "value": "0"
              },
              {
                "label": "县处级正职",
                "value": "1"
              }
            ],
            "display": true,
            "props": {
              "label": "label",
              "value": "value",
              "desc": "desc"
            },
            "labelWidth": 100,
            "required": true,
            "rules": [
              {
                "required": true,
                "message": "职务必须填写"
              }
            ]
          },
          {
            "label": "条线/行业",
            "prop": "tiaoxianhangye",
            "type": "select",
            "span": 12,
            "dicData": [
              {
                "label": "机关",
                "value": "0"
              },
              {
                "label": "监狱",
                "value": "1"
              },
              {
                "label": "戒毒",
                "value": "2"
              }
            ],
            "display": true,
            "props": {
              "label": "label",
              "value": "value",
              "desc": "desc"
            },
            "labelWidth": 100,
            "required": true,
            "rules": [
              {
                "required": true,
                "message": "条线/行业必须选择"
              }
            ]
          },
          {
            "label": "推荐单位",
            "prop": "tuijiandanwei",
            "type": "input",
            "span": 12,
            "display": true,
            "labelWidth": 100,
            "width":100,
            "required": true,
            "rules": [
              {
                "required": true,
                "message": "推荐单位必须填写"
              }
            ]
          },
          {
            "label": "所获荣誉",
            "prop": "suohuorongyu",
            "type": "select",
            "width":180,
            "span": 12,
            "dicData": [
              {
                "label": "全国评比达标表彰保留项目",
                "value": "0"
              },
              {
                "label": "上海市评比达标表彰保留项目",
                "value": "1"
              },
              {
                "label": "公务员（事业单位人员）奖励种类",
                "value": "2"
              }
            ],
            "display": true,
            "props": {
              "label": "label",
              "value": "value",
              "desc": "desc"
            },
            "labelWidth": 100,
            "required": true,
            "rules": [
              {
                "required": true,
                "message": "单位部门必须填写"
              }
            ]
          },
          {
            "label": "发文单位",
            "prop": "fawendanwei",
            "type": "input",
            "span": 12,
            "display": true,
            "labelWidth": 100,
            "required": true,
            "rules": [
              {
                "required": true,
                "message": "发文单位必须填写"
              }
            ]
          },
          {
            "label": "发文号",
            "prop": "fawenhao",
            "type": "input",
            "span": 12,
            "display": true,
            "labelWidth": 100,
            "width":120,
            "required": true,
            "rules": [
              {
                "required": true,
                "message": "发文号必须填写"
              }
            ]
          },
          {
            "label": "文件名",
            "prop": "wenjianming",
            "type": "input",
            "span": 12,
            "display": true,
            "labelWidth": 100,
            "width":160,

            "required": true,
            "rules": [
              {
                "required": true,
                "message": "文件名必须填写"
              }
            ]
          },

          {
            "label": "发文日期",
            "prop": "fawenriqi",
            "type": "date",
            "span": 12,
            "format": "yyyy-MM-dd",
            "valueFormat": "yyyy-MM-dd",
            "display": true,
            "labelWidth": 100,
            "width":85,
            "required": true,
            "rules": [
              {
                "required": true,
                "message": "发文日期必须填写"
              }
            ]
          },
          {
            "label": "荣誉层级",
            "prop": "rongyucengji",
            "type": "select",
            "span": 12,
            "dicData": [
              {
                "label": "全国性",
                "value": "0"
              },
              {
                "label": "省部级",
                "value": "1"
              },
              {
                "label": "市级机关",
                "value": "2"
              }
            ],
            "display": true,
            "props": {
              "label": "label",
              "value": "value",
              "desc": "desc"
            },
            "labelWidth": 100,
            "required": true,
            "rules": [
              {
                "required": true,
                "message": "单位部门必须填写"
              }
            ]
          },
          {
            "label": "荣誉类别",
            "prop": "rongyuleibie",
            "type": "select",
            "span": 12,
            "dicData": [
              {
                "label": "评比表彰",
                "value": "0"
              },
              {
                "label": "公务员奖励",
                "value": "1"
              },
              {
                "label": "事业单位人员奖励",
                "value": "2"
              }
            ],
            "display": true,
            "props": {
              "label": "label",
              "value": "value",
              "desc": "desc"
            },
            "labelWidth": 100,
            "required": true,
            "rules": [
              {
                "required": true,
                "message": "单位部门必须填写"
              }
            ]
          },


        ]
      },
      data: [
        {
          niandu:'2022',
          xingming:'王老师',
          xingbie:'男',
          chushengnianyue:'2022-03-31',
          zhengzhimianmao:'中共党员',
          shenfenzhenghao:'410103197703312453',
          gongzuodanwei:'政治部',
          zhiwu:'员工',
          jibie:'县处级副职',
          tiaoxianhangye:'机关',
          tuijiandanwei:'上海市司法局',
          suohuorongyu:'全国评比达标表彰保留项目',
          fawendanwei:'司法部',
          fawenhao:'司备字20220331',
          wenjianming:'关于全国评比结果的通知',
          fawenriqi:'2022-03-31',
          rongyucengji:'全国性',
          rongyuleibie:'评比表彰',
        },
        {
          niandu:'2022',
          xingming:'李老师',
          xingbie:'男',
          chushengnianyue:'2022-03-31',
          zhengzhimianmao:'中共党员',
          shenfenzhenghao:'410103197703312453',
          gongzuodanwei:'政治部',
          zhiwu:'员工',
          jibie:'县处级副职',
          tiaoxianhangye:'机关',
          tuijiandanwei:'上海市司法局',
          suohuorongyu:'全国评比达标表彰保留项目',
          fawendanwei:'司法部',
          fawenhao:'司备字20220331',
          wenjianming:'关于全国评比结果的通知',
          fawenriqi:'2022-03-31',
          rongyucengji:'全国性',
          rongyuleibie:'评比表彰',
        },
        {
          niandu:'2022',
          xingming:'赵老师',
          xingbie:'男',
          chushengnianyue:'2022-03-31',
          zhengzhimianmao:'中共党员',
          shenfenzhenghao:'410103197703312453',
          gongzuodanwei:'政治部',
          zhiwu:'员工',
          jibie:'县处级副职',
          tiaoxianhangye:'机关',
          tuijiandanwei:'上海市司法局',
          suohuorongyu:'全国评比达标表彰保留项目',
          fawendanwei:'司法部',
          fawenhao:'司备字20220331',
          wenjianming:'关于全国评比结果的通知',
          fawenriqi:'2022-03-31',
          rongyucengji:'全国性',
          rongyuleibie:'评比表彰',
        }
      ]
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.post_add, false),
        viewBtn: this.vaildData(this.permission.post_view, false),
        delBtn: this.vaildData(this.permission.post_delete, false),
        editBtn: this.vaildData(this.permission.post_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  methods: {
    rowSave(row, done, loading) {
      add(row).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        done();
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        done();
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then(res => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    }
  }
};
</script>

<style>
</style>
