<template>
  <div>
    <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="请选择回顾/截止日期">
        </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%;">
          <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%;">
          <el-option
            v-for="dict in agendaStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </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">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['plant:agenda:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['plant:agenda:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['plant:agenda:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table :data="agendaList" :height="clientHeight"
              border>
      <el-table-column :label="contentTitle" align="center" prop="agendaContent">
        <template slot-scope="scope">
          <span v-if="agendaType==5">{{ scope.row.planType }}{{ scope.row.agendaContent }}</span>
          <span v-else>{{ scope.row.agendaContent }}</span>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="workType" width="100">
      </el-table-column>
      <el-table-column label="跟踪人" align="center" prop="responsible" width="130">
      </el-table-column>
      <el-table-column label="回顾/截止时间" align="center" prop="deadlineTime" width="130">
        <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" width="130">
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['plant:agenda:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['plant:agenda:remove']"
          >删除
          </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 :close-on-click-modal="false" :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="auto">
        <el-form-item label="计划类型" prop="planType" v-if="agendaType==5">
          <el-select v-model="form.planType" style="width: 100%;">
            <el-option value="THF：" label="THF"/>
            <el-option value="PTHF：" label="PTHF"/>
            <el-option value="精馏：" label="精馏"/>
            <el-option value="BDO：" label="BDO"/>
          </el-select>
        </el-form-item>
        <el-form-item label="议程内容" prop="agendaContent">
          <el-input type="textarea" v-model="form.agendaContent" placeholder="请输入议程内容"/>
        </el-form-item>
        <el-form-item label="类型" prop="workType">
          <el-select v-model="form.workType" placeholder="请选择类型" @change="selectWorkType" clearable
                     size="small" style="width: 100%;">
            <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="responsible" v-if="isI">
          <el-input v-model="form.responsible" placeholder="请输入跟踪人"/>
        </el-form-item>
        <el-form-item label="回顾/截止时间" prop="deadlineTime" v-if="isI">
          <el-date-picker clearable size="small" style="width: 100%;"
                          v-model="form.deadlineTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择回顾/截止时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" prop="status" v-if="isI">
          <el-select v-model="form.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>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addAgenda, delAgenda, getAgenda, listAgenda, updateAgenda} from "@/api/plant/agenda";
import {MessageBox} from "element-ui";

export default {
  props: ['contentTitle', 'agendaType', 'meetingId'],
  name: "Agenda",
  data() {
    return {
      isI: false,
      // 页面高度
      clientHeight: 300,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 装置会议议程表格数据
      agendaList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        meetingId: this.meetingId,
        agendaType: this.agendaType,
        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: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        agendaContent: [
          {required: true, message: '议程内容不能为空', trigger: "blur"}
        ],
        responsible: [
          {required: true, message:'跟踪人不能为空', trigger: "blur"}
        ],
        deadlineTime: [
          {required: true, message: '回顾/截止日期不能为空', trigger: "blur"}
        ],
        status: [
          {required: true, message: '状态不能为空', trigger: "blur"}
        ],
        workType: [
          {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: {
    selectWorkType(val) {
      this.isI = val !== 'I';
      if (val==='A')
      this.form.status='Open'
      else if (val==='P')
        this.form.status='On Going'
    },
    /** 查询装置会议议程列表 */
    getList() {
      if (!this.meetingId) {
        this.queryParams.isHis = 0;
      }
      this.loading = true;
      listAgenda(this.queryParams).then(response => {
        this.agendaList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        meetingId: this.meetingId,
        agendaType: this.agendaType,
        agendaContent: null,
        workType: null,
        planType: null,
        responsible: null,
        status: null,
        delFlag: null,
        createrCode: null,
        createdate: null,
        updaterCode: null,
        updatedate: null,
        deptId: null,
        remarks: null,
        presided: null,
        isHis: null,
        deadlineTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      console.log(this.queryParams)
      this.$emit('queryParams', this.queryParams)
      this.queryParams.pageNum = 1;
      this.getList();

    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加装置会议议程";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAgenda(id).then(response => {
        this.form = response.data;
        this.isI = response.data.workType !== 'I';
        this.open = true;
        this.title = "修改装置会议议程";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if (this.meetingId == null) {
        this.form.isHis = 0;
      } else {
        this.form.isHis = 1;
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAgenda(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAgenda(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      MessageBox.confirm('是否确认删除数据项？').then(function () {
        return delAgenda(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('plant/agenda/export', {
        ...this.queryParams
      }, `agenda_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
