<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="工程名称" prop="proName">
        <el-input
          v-model="queryParams.proName"
          placeholder="请输入工程名称"
          maxlength="60"
          show-word-limit
          clearable v-no-whitespace
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label-width="100px" label="工程负责人" prop="proLeader">
        <el-input
          v-model="queryParams.proLeader"
          placeholder="请输入工程负责人姓名"
          maxlength="20"
          show-word-limit
          clearable v-no-whitespace
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工程类型" prop="proType">
        <el-select v-model="queryParams.proType" placeholder="工程类型" clearable>
          <el-option
            v-for="dict in proTypeDict"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建人" prop="createPerson">
        <el-input
          v-model="queryParams.createPerson"
          placeholder="请输入创建人姓名"
          maxlength="20"
          show-word-limit
          clearable v-no-whitespace
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label-width="100px" label="工程创建时间">
        <el-date-picker v-model="createTime" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
                        :editable="false" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item style="display: flex; justify-content: flex-end;">
        <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">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:proList:add']"
        >新建工程</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:proList:export']"
        >导出数据</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="proList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" type="index" width="55" align="center" :index="indexContinuous(queryParams.pageNum, queryParams.pageSize)"/>
      <el-table-column label="工程名称" show-overflow-tooltip align="center" prop="proName" />
      <el-table-column label="工程负责人" show-overflow-tooltip align="center" prop="proLeader" />
      <el-table-column label="负责人联系方式" show-overflow-tooltip align="center" >
        <template slot-scope="scope">
          <!-- 检查是否存在身份证号 -->
          <span v-if="scope.row.proLeaderPhone">{{ hideSensitiveInfo(scope.row.proLeaderPhone) }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="工程类型" show-overflow-tooltip align="center" prop="proType" />
      <el-table-column label="计划开工日期" show-overflow-tooltip align="center" prop="startDate" />
      <el-table-column label="计划竣工日期" show-overflow-tooltip align="center" prop="endDate" />
      <el-table-column label="工程状态" show-overflow-tooltip align="center" prop="proStatus" />
      <el-table-column label="创建人" show-overflow-tooltip align="center" prop="createPerson" />
      <el-table-column label="创建时间" show-overflow-tooltip align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:proList:edit']"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handlePreparation(scope.row)"
            v-hasPermi="['system:proList:preparation']"
          >筹备</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:proList:del']"
          >删除</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 { listPost, delPost, preparationPost} from "@/api/pro/proList";
import { dictTableOption} from "@/api/tool/select";
import { hideSensitiveInfo, indexContinuous } from '@/utils/bonus'

export default {
  name: "Post",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工程表格数据
      proList: [],
      //工程类型下拉选
      proTypeDict: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      createTime: undefined,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        proName: undefined,
        proLeader: undefined,
        proType: undefined,
        createPerson: undefined,
        startDate: undefined,
        endDate: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
    this.getProType();
  },
  methods: {
    hideSensitiveInfo,
    indexContinuous,
    //判断是否选择工程创建时间
    judgeTime(){
      if (this.createTime !== undefined){
        this.queryParams.startDate = this.createTime[0];
        this.queryParams.endDate = this.createTime[1];
      }
    },
    /** 查询工程列表 */
    getList() {
      this.loading = true;
      this.judgeTime();
      listPost(this.queryParams).then(response => {
        this.proList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //获取工程类型下拉选
    getProType() {
      const params = {
        dictType: 'sys_pro_type',
        dictValue: ''
      }
      dictTableOption(params).then(response => {
        this.proTypeDict = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        proId: undefined,
        proName: undefined,
        proLeader: undefined,
        proType: undefined,
        proStatus: undefined,
        startDate: undefined,
        endDate: undefined,
        postSort: 0,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.judgeTime();
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.startDate = undefined;
      this.queryParams.endDate = undefined;
      this.createTime = undefined;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.proId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    // handleAdd() {
    //   this.reset();
    //   const proId = "0";
    //   this.$router.push("/system/pro-auth/pro/" + proId);
    // },
    handleAdd() {
      this.reset();
      const proId = "0";
      this.$router.push("/project/pro-auth/projectManagement/proList/addPro/" + proId);
    },
    /** 新增按钮操作 */
    handleUpdate(row) {
      this.reset();
      const proId = row.proId;
      this.$router.push("/project/pro-auth/projectManagement/proList/editPro/"+proId);
    },
    /** 筹备按钮操作 */
    handlePreparation(row) {
      const proIds = row.proId;
      const proName = row.proName;
      let text = '<span style="font-size: 14px;font-weight: bold">确认将工程：<span style="color:red;">'+proName+'</span>进入筹备状态么？</span>' +
        '<br/><span style="font-size: 12px">进入筹备状态后，该工程部分信息不可修改（如工程负责人），系统开始通知各方人员上传工程资料！</span>';
      this.$modal.confirm(text).then(function() {
        return preparationPost(proIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("筹备成功");
      }).catch(() => {});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const proIds = row.proId;
      const proName = row.proName;
      this.$modal.confirm('是否确认删除工程名称为<span style="color:red;">'+proName+'</span>的数据项？').then(function() {
        return delPost(proIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.derive('project/pro/export', {
        ...this.queryParams
      }, `工程信息.xlsx`)
    }
  }
};
</script>
