<template>
  <div class="app-container">
    <TextSearch/>
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="回顾/截止日期" prop="deadlineTime">
        <el-date-picker clearable size="small" style="width: 200px"
                        v-model="queryParams.deadlineTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择回顾/截止日期" @change="handleQuery">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="类型" prop="workType" label-width="50px">
        <el-select v-model="queryParams.workType" placeholder="请选择类型" clearable size="small"
                   style="width: 100%;" @change="handleQuery">
          <el-option
            v-for="dict in workTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status" label-width="50px">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small"
                   style="width: 100%;" @change="handleQuery">
          <el-option
            v-for="dict in agendaStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="跟踪人" prop="responsible" label-width="70px">
        <el-input v-model="queryParams.responsible" placeholder="请输入跟踪人"/>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table :data="agendaList" :height="clientHeight" border>
      <el-table-column label="主题" align="center" prop="subject"/>
      <el-table-column label="会议编号" align="center" prop="meetingNo"/>
      <el-table-column label="会议时间" align="center" prop="meetingDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.meetingDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="内容" align="center" prop="agendaContent">
        <template slot-scope="scope">
          <el-input type="textarea" v-model="scope.row.agendaContent" placeholder="请输入行动项内容"
                    v-if="scope.row.isEdit&&scope.row.workType==='P'"/>
          <span v-else>{{ scope.row.agendaContent }}</span>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="workType"/>
      <el-table-column label="跟踪人" align="center" prop="responsible"/>
      <el-table-column label="回顾/截止时间" align="center" prop="deadlineTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deadlineTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-select v-if="scope.row.isEdit" v-model="scope.row.status" placeholder="请选择状态"
                     clearable
                     size="small" style="width: 100%;">
            <el-option
              v-for="dict in agendaStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
          <span v-else>{{ scope.row.status }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="!scope.row.isEdit&&scope.row.workType!=='I'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdateRecord(scope.row)"
            v-hasPermi="['plant:agenda:edit']"
          >修改
          </el-button>
          <el-button v-if="scope.row.isEdit"
                     size="mini"
                     type="text"
                     icon="el-icon-check"
                     @click="save(scope.row)"
          >保存
          </el-button>
          <el-button v-if="scope.row.isEdit"
                     size="mini"
                     type="text"
                     icon="el-icon-close"
                     @click="cancelRecord(scope.row,scope.$index)"
          >取消
          </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"
    />
  </div>
</template>

<script>
import {listAgenda2, updateAgenda} from "@/api/plant/agenda";

export default {
  name: "hisAgenda",
  data() {
    return {
      // 页面高度
      clientHeight: 300,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 装置会议议程表格数据
      agendaList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        meetingId: null,
        agendaType: null,
        agendaContent: null,
        workType: null,
        planType: null,
        responsible: null,
        status: null,
        createrCode: null,
        createdate: null,
        updaterCode: null,
        updatedate: null,
        deptId: null,
        remarks: null,
        presided: null,
        isHis: 1,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        agendaContent: [
          {required: true, message: '议程内容不能为空', trigger: "blur"}
        ],
      },
      workTypeOptions: [],
      agendaStatusOptions: [],
    };
  },
  created() {
    this.getList();
    //设置表格高度对应屏幕高度
    this.$nextTick(() => {
      this.clientHeight = (document.body.clientHeight - 130) * 0.8
    });
    this.getDicts("agenda_type").then(res => {
      this.workTypeOptions = res.data;
    });
    this.getDicts("agenda_status").then(res => {
      this.agendaStatusOptions = res.data;
    });
  },
  methods: {
    save(row) {
      let data = {
        id: row.id,
        status: row.status
      }
      updateAgenda(data).then(res => {
        this.$modal.msgSuccess("修改成功");
        row.isEdit = false;
        this.getList();
      })
    },
    handleUpdateRecord(row) {
      // 备份原始数据
      row['oldRow'] = JSON.parse(JSON.stringify(row));
      this.$nextTick(() => {
        row.isEdit = true;
      })
    },
    cancelRecord(row, index) {
      // 如果是新增的数据
      if (row.isAdd) {
        this.record.dataList.splice(index, 1)
      } else {
        // 不是新增的数据  还原数据
        for (const i in row.oldRow) {
          row[i] = row.oldRow[i]
        }
        row.isEdit = false
      }
    },
    /** 查询装置会议议程列表 */
    getList() {
      this.loading = true;
      listAgenda2(this.queryParams).then(response => {
        this.total = response.total;
        this.loading = false;
        response.rows.forEach(item => {
          item["isEdit"] = false;
        })
        this.agendaList = response.rows;
      });
    }, /** 搜索按钮操作 */
    handleQuery() {
      console.log(this.queryParams)
      this.$emit('queryParams', this.queryParams)
      this.queryParams.pageNum = 1;
      this.getList();

    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  }
}
</script>

<style scoped>

</style>
