<template>
  <div class="app-container">
    <el-form ref="queryForm" size="small" :inline="true">
      <el-form-item label="年度：">
        <el-date-picker
          v-model="valueYear"
          type="year"
          placeholder="选择年"
          value-format="yyyy"
          :clearable="false"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="职责类别：">
        <el-select v-model="queryParams.highlightsType">
          <el-option label="全部" value=""></el-option>
          <el-option label="业务职责" :value="1"></el-option>
          <el-option label="党建及党风廉政" :value="2"></el-option>
          <el-option label="安全环保职责" :value="3"></el-option>
          <el-option label="合规保密职" :value="4"></el-option>
          <el-option label="其他" :value="5"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" type="flex" justify="end">
      <el-col :span="1.5">
        <el-button type="primary" @click="doClick(`新建`, '')">新建</el-button>
      </el-col>
    </el-row>

    <el-table border v-loading="loading" :data="tableData" height="calc(100% - 150px)">
      <el-table-column label="序号" align="center" width="80">
        <template slot-scope="scope">
          <span>{{
            scope.$index + 1 + (queryParams.pageNum - 1) * queryParams.pageSize
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="类别"
        align="center"
        width="180"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span>{{
            scope.row.highlightsType
              ? initHighlightsType(scope.row.highlightsType)
              : '--'
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="管理亮点" align="center">
        <template slot-scope="scope">
            <div class="long_title">
                <span>{{scope.row.highlights}}</span>
            </div>
        </template>
      </el-table-column>
      <el-table-column
        label="记录时间"
        align="center"
        width="180"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span>{{ scope.row.createTime ? scope.row.createTime : '--' }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="120"
      >
        <template slot-scope="scope">
          <el-button type="text" @click="doClick(`查看`, scope.row)"
            >查看</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="45%"
      :close-on-click-modal="false"
      v-if="dialogVisible"
      :before-close="handleClose"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="类别：" prop="highlightsType">
          <el-select
            v-model="ruleForm.highlightsType"
            placeholder="请选择类别"
            style="width: 80%"
            :disabled="title === `查看`"
          >
            <el-option label="业务职责" :value="1"></el-option>
            <el-option label="党建及党风廉政" :value="2"></el-option>
            <el-option label="安全环保职责" :value="3"></el-option>
            <el-option label="合规保密职" :value="4"></el-option>
            <el-option label="其他" :value="5"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="管理亮点：" prop="highlights">
          <el-input
            type="textarea"
            v-model="ruleForm.highlights"
            placeholder="请输入管理亮点"
            style="width: 80%"
            :rows="6"
            :disabled="title === `查看`"
          ></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer" v-if="title !== `查看`">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="submit">保 存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { postHighlightsList, postHighlightsAdd } from '@/api/allApi/index.js'

export default {
  name: 'PostResponsibilityMeun',

  data() {
    return {
      valueYear:"",
      queryParams: {
        highlightsType: '',
        pageNum: 1,
        pageSize: 10
      },
      tableData: [],
      total: 0,
      loading: false,
      dialogVisible: false,
      title: '',
      ruleForm: {
        highlightsType: '',
        highlights: ''
      },
      rules: {
        highlightsType: [
          { required: true, message: '请选择类别', trigger: 'change' }
        ],
        highlights: [
          { required: true, message: '请输入管理亮点', trigger: 'blur' }
        ]
      }
    }
  },

  mounted() {
    this.initYear()
    this.getList()
  },

  methods: {
    initYear() {
      let _now = new Date();
      let currentYear = _now.getFullYear();
      this.valueYear = `${currentYear}`;
    },
    /** 查询岗位列表 */
    getList() {
      this.loading = true
      const dateRange = [`${this.valueYear}-01-01`, `${this.valueYear}-12-31`];
      postHighlightsList(this.addDateRange(this.queryParams, dateRange)).then((response) => {
        this.tableData = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        highlightsType: '',
        pageNum: 1,
        pageSize: 10
      }
      this.initYear();
      this.handleQuery()
    },
    doClick(title, val) {
      this.title = title
      if (title === '查看') {
        this.ruleForm = val
      } else {
        this.ruleForm = {
          highlightsType: '',
          highlights: ''
        }
      }
      this.dialogVisible = true
    },
    submit() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          const res = await postHighlightsAdd(this.ruleForm)
          if (res.code !== 200) return this.$message.error(res.msg)
          this.$message.success('保存成功')
          this.dialogVisible = false

          this.getList()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    initHighlightsType(val) {
      switch (val) {
        case 1:
          return '业务职责'
        case 2:
          return '党建及党风廉政'
        case 3:
          return '安全环保职责'
        case 4:
          return '合规保密职'
        case 5:
          return '其他'
      }
    },
    handleClose() {
      this.ruleForm = {
        highlightsType: '',
        highlights: ''
      }
      this.$refs.ruleForm.resetFields()
      this.dialogVisible = false
    }
  }
}
</script>

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