<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="60px" size="mini">
      <el-form-item v-hasPermi="['system:task:send']" label="员工" prop="userId">
        <el-select v-model="queryParams.userId" placeholder="请选择" size="mini">
          <el-option v-for="dict in userOptions" :key="dict.id" :label="dict.name" :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="summaryType">
        <el-select v-model="queryParams.summaryType" placeholder="请选择" size="mini">
          <el-option label="月总结" :value="1" />
          <el-option label="日总结" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="月份" prop="month">
        <el-select v-model="queryParams.month" placeholder="请选择" size="mini">
          <el-option v-for="dict in 12" :key="dict" :label="dict" :value="dict" />
        </el-select>
      </el-form-item>
      <el-form-item label="日期" prop="day">
        <el-date-picker v-model="queryParams.day" value-format="yyyy-MM-dd" format="yyyy-MM-dd" type="date" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button v-hasPermi="['system:summary:add']" type="primary" icon="el-icon-plus" size="mini" @click="handleAdd()">新增</el-button>
      </el-form-item>
    </el-form>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="taskList" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50" align="center" />
      <el-table-column label="姓名" align="center" prop="employeeName" width="100" />
      <el-table-column label="任务类型" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.summaryType == 1">月总结</span>
          <span v-else>日总结</span>
        </template>
      </el-table-column>
      <el-table-column label="月份" align="center" prop="month" />
      <el-table-column label="日期" align="center" prop="day" width="100" />
      <el-table-column label="内容" align="center" min-width="100">
        <template slot-scope="scope">
          <div v-html="scope.row.content" />
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermi="['system:summary:edit']" size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(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="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <!-- <el-col :span="24">
            <el-form-item label="员工" prop="userId">
              <el-select v-model="form.userId" placeholder="请选择" size="small" :disabled="dayEdit">
                <el-option v-for="dict in userOptions" :key="dict.id" :label="dict.name" :value="dict.id" />
              </el-select>
            </el-form-item>
          </el-col> -->
          <el-col :span="24">
            <el-form-item label="总结类型" prop="summaryType">
              <el-radio-group v-model="form.summaryType">
                <el-radio :label="1">月总结</el-radio>
                <el-radio :label="2">日总结</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="月份" prop="month">
              <el-select v-model="form.month" placeholder="请选择" size="mini" :disabled="dayEdit">
                <el-option v-for="dict in 12" :key="dict" :label="dict" :value="dict" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col v-if="form.summaryType === 2" :span="24">
            <el-form-item label="日期" prop="day">
              <el-date-picker v-model="form.day" value-format="yyyy-MM-dd" format="yyyy-MM-dd" type="date" :disabled="dayEdit" />
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="任务内容" prop="content">
              <editor v-model="form.content" />
              <!-- <el-input v-model="form.content" type="textarea" :rows="13" /> -->
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" style="padding-top:20px">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { saveTask, getList } from '@/api/system/summary';
import Editor from '@/components/Editor';
import empAPi from '@/api/system/employee';

export default {
  name: 'Summary',
  components: {
    Editor
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 公告表格数据
      taskList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        summaryType: 1,
        month: undefined,
        day: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [{ required: true, message: '员工不能为空', trigger: 'blur' }],
        month: [{ required: true, message: '月份不能为空', trigger: 'blur' }],
        day: [{ required: true, message: '日期不能为空', trigger: 'blur' }],
        content: [
          { required: true, message: '任务内容不能为空', trigger: 'blur' }
        ],
        summaryType: [{ required: true, message: '总结类型不能为空', trigger: 'blur' }]
      },
      taskType: 1,
      userOptions: [],
      month: undefined,
      day: undefined,
      dayEdit: false,
      userId: localStorage.getItem('userId')
    };
  },
  created() {
    this.getList();
    this.getEmployee();
    const now = new Date();
    const year = now.getFullYear(); // 得到年份
    const month = now.getMonth() + 1; // 得到月份
    const date = now.getDate(); // 得到日期
    this.month = month;
    this.day = year + '-' + month + '-' + date;
  },
  methods: {
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      getList(this.queryParams).then((response) => {
        this.taskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        summaryType: 1,
        userId: undefined,
        month: this.month,
        day: undefined,
        content: undefined
      };
      this.resetForm('form');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.$set(this.form, 'userId', this.userId);
      this.$set(this.form, 'day', this.day);
      this.dayEdit = false;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();

      this.form = Object.assign({}, row);
      this.taskType = this.form.taskType;
      if (this.form.taskType == 1) {
        this.title = this.month + '月-总结';
      } else {
        this.title = this.month + '月-总结';
      }
      this.open = true;
      this.dayEdit = true;
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.summaryType === 1) {
            this.form.day = undefined;
          }
          saveTask(this.form).then((response) => {
            if (response.code === 200) {
              this.$message.success('保存成功');
              this.open = false;
              this.getList();
            }
          });
        }
      });
    },
    // 查询员工
    getEmployee() {
      empAPi.getEmployee({ coach: false }).then((resp) => {
        if (resp.code == 200) {
          // this.userOptions = resp.data;
          if (resp.data && resp.data.length > 0) {
            resp.data.forEach((item) => {
              this.userOptions.push(item);
            });
          }
        }
      });
    }
  }
};
</script>
