<template>
  <div class="app-container prj-design-index">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目名称" prop="projectId">
        <el-select v-model="queryParams.projectId" filterable placeholder="请选择项目">
          <el-option v-for="(item, index) in projectOptions" :key="index" :label="item.projectName" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="开始日期" prop="startDate">
        <el-date-picker clearable v-model="queryParams.startDate" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择开始日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束日期" prop="endDate">
        <el-date-picker clearable v-model="queryParams.endDate" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择结束日期">
        </el-date-picker>
      </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-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5" v-if="1 == 2">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['project:projectDesign:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="projectDesignList" border class="tb_data"
      @selection-change="handleSelectionChange">
      <el-table-column prop="nodeText" label="任务内容" width="200">
        <template slot-scope="scope">
          <div :class="'node-text-' + scope.row.nodeLvl.length">{{ scope.row.nodeText }}</div>
        </template>
      </el-table-column>
      <el-table-column label="责任人" align="center" prop="ownerName"></el-table-column>
      <el-table-column label="数据日期" align="center" prop="dataDate" width="80">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dataDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开始日期" align="center" prop="startDate" width="80">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划开始日期" align="center" prop="planStartDate" width="90">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planStartDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束日期" align="center" prop="endDate" width="80">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划结束日期" align="center" prop="planEndDate" width="90">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planEndDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="工期" align="center" prop="duration" width="60">
        <template slot-scope="scope">
          <span :class="scope.row.duration < 0 ? 'sp-red' : ''"> {{ scope.row.duration }}</span>
          <span v-if="scope.row.duration">天</span>
        </template>
      </el-table-column>
      <el-table-column label="剩余天数" align="center" prop="remainingDays" width="80">
        <template slot-scope="scope">
          <span :class="scope.row.remainingDays < 0 ? 'sp-red' : ''">{{ scope.row.remainingDays }}</span>
          <span v-if="scope.row.remainingDays">天</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="80">
        <template slot-scope="scope">
          <span class="sp-orange" v-if="scope.row.status == '进行中'">进行中</span>
          <span class="sp-green" v-if="scope.row.status == '完成'">完成</span>
          <span class="sp-red" v-if="scope.row.status == '延迟'">延迟</span>
        </template>
      </el-table-column>
      <el-table-column label="本周完成工作" align="center" prop="work">
        <template slot-scope="scope">         
          <el-popover placement="top-start" width="400" trigger="hover" :content="scope.row.work">
            <div slot="reference" class="div-txt-2">
              {{ scope.row.work }}
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="下周计划" align="center" prop="nextWeekWork">
        <template slot-scope="scope">          
          <el-popover placement="top-start" width="400" trigger="hover" :content="scope.row.nextWeekWork">
            <div slot="reference" class="div-txt-2">
              {{ scope.row.nextWeekWork }}
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="存在问题" align="center" prop="problem">
        <template slot-scope="scope">
          <el-popover placement="top-start" width="400" trigger="hover" :content="scope.row.problem">
            <div slot="reference" class="div-txt-2">
              {{ scope.row.problem }}
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="修改记录" align="center" width="80">
        <template slot-scope="scope">
          <el-popover placement="top-start" width="400" trigger="hover">
            <pre style="font-size:12px;">{{ scope.row.updateInfo }}</pre>
            <div slot="reference" style="cursor: pointer;color: cornflowerblue;">
              修改记录
            </div>
          </el-popover>
        </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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="${comment}" prop="id">
          <el-input v-model="form.id" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="节点编号" prop="nodeId">
          <el-input v-model="form.nodeId" placeholder="请输入节点编号" />
        </el-form-item>
        <el-form-item label="项目ID" prop="projectId">
          <el-input v-model="form.projectId" placeholder="请输入项目ID" />
        </el-form-item>
        <el-form-item label="责任人" prop="ownerName">
          <el-input v-model="form.ownerName" placeholder="请输入责任人" />
        </el-form-item>
        <el-form-item label="责任人电话" prop="ownerPhone">
          <el-input v-model="form.ownerPhone" placeholder="请输入责任人电话" />
        </el-form-item>
        <el-form-item label="数据日期，同一周一套数据" prop="dataDate">
          <el-date-picker clearable v-model="form.dataDate" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择数据日期，同一周一套数据">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="开始日期" prop="startDate">
          <el-date-picker clearable v-model="form.startDate" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="计划开始日期" prop="planStartDate">
          <el-date-picker clearable v-model="form.planStartDate" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择计划开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束日期" prop="endDate">
          <el-date-picker clearable v-model="form.endDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="计划结束日期" prop="planEndDate">
          <el-date-picker clearable v-model="form.planEndDate" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择计划结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="工期" prop="duration">
          <el-input v-model="form.duration" placeholder="请输入工期" />
        </el-form-item>
        <el-form-item label="剩余天数" prop="remainingDays">
          <el-input v-model="form.remainingDays" placeholder="请输入剩余天数" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="文件" prop="files">
          <el-input v-model="form.files" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="本周完成工作" prop="work">
          <el-input v-model="form.work" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="下周计划" prop="nextWeekWork">
          <el-input v-model="form.nextWeekWork" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="存在问题" prop="problem">
          <el-input v-model="form.problem" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="删除状态" prop="isDel">
          <el-input v-model="form.isDel" placeholder="请输入删除状态" />
        </el-form-item>
        <el-form-item label="修改记录" prop="updateInfo">
          <el-input v-model="form.updateInfo" type="textarea" placeholder="请输入内容" />
        </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 { listProjectDesign, getProjectDesign, delProjectDesign, addProjectDesign, updateProjectDesign } from "@/api/project/projectDesign";

export default {
  name: "ProjectDesign",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设计管理表格数据
      projectDesignList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      projectOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 19,
        nodeId: null,
        projectId: null,
        ownerName: null,
        ownerPhone: null,
        dataDate: null,
        startDate: null,
        planStartDate: null,
        endDate: null,
        planEndDate: null,
        duration: null,
        remainingDays: null,
        status: null,
        files: null,
        work: null,
        nextWeekWork: null,
        problem: null,
        isDel: null,
        updateInfo: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "$comment不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.$api.publics.getMyProjectList({}).then((response) => {
      this.projectOptions = response.rows;
    });
    this.loading = false;
    //this.getList();
  },
  methods: {
    /** 查询设计管理列表 */
    getList() {
      if (!this.queryParams.projectId) {
        this.projectDesignList = [];
        this.total = 0;
        this.loading = false;
        return;
      }
      this.loading = true;
      listProjectDesign(this.queryParams).then(response => {
        this.projectDesignList = (response.rows || []).map(it => {
          if (it.ownerName) {
            it.ownerName += (it.ownerPhone ? "-" + it.ownerPhone : "");
          } else {
            it.ownerName = "";
          }
          it.nodeText = it.baseBuildNode?.nodeText || "";
          it.nodeLvl = it.baseBuildNode?.nodeLvl || "";
          it.showOwner = it.nodeLvl != '0301' && it.nodeLvl != '0302';
          return it;
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        nodeId: null,
        projectId: null,
        ownerName: null,
        ownerPhone: null,
        dataDate: null,
        startDate: null,
        planStartDate: null,
        endDate: null,
        planEndDate: null,
        duration: null,
        remainingDays: null,
        status: null,
        remark: null,
        files: null,
        work: null,
        nextWeekWork: null,
        problem: null,
        isDel: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        updateInfo: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      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
      getProjectDesign(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设计管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id) {
            updateProjectDesign(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProjectDesign(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除设计管理编号为"' + ids + '"的数据项？').then(function () {
        return delProjectDesign(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('project/projectDesign/export', {
        ...this.queryParams
      }, `projectDesign_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss">
.prj-design-index {
  font-size: 12px;

  .tb_data {
    .el-table__cell {
      padding: 2px;

      .cell {
        padding: 0px 2px;
        font-size: 12px;

        .node-text-4 {
          font-weight: bold;
        }

        .node-text-6 {
          margin-left: 20px;
        }

        .sp-red {
          color: red;
        }

        .sp-green {
          color: green;
        }

        .sp-orange {
          color: orange;
        }
        
      }
    }
  }
}
</style>
