<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="接入标志" prop="connectFlag">
        <el-input
          v-model="queryParams.connectFlag"
          placeholder="请输入接入标志"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['zcustom:project:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['zcustom:project:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="projectList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="50">
        <template slot-scope="scope">
          {{(scope.$index + 1) + ((queryParams.pageNum - 1) * queryParams.pageSize)}}
        </template>
      </el-table-column>
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="项目分类" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.subdivision <= 10">{{ typeListA[scope.row.subdivision - 1].name }}</span>
          <span v-if="scope.row.subdivision > 10">{{ typeListB[scope.row.subdivision - 1].name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['zcustom:project:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['zcustom:project: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 :title="title" :visible.sync="open" :close-on-click-modal="false" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="form.projectName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="所属单位" prop="unitCode">
          <el-cascader :options="treeList" placeholder="请选择所属单位" v-model="form.appOrg" :props="props" style="width: 400px">
            <template slot-scope="{ node, data }">
              <span>{{ data.appName }}</span>
            </template>
          </el-cascader>
        </el-form-item>
        <el-form-item label="项目类型" prop="type">
          <el-radio v-model="flag" label="1">在建</el-radio>
          <el-radio v-model="flag" label="2">施工</el-radio>
        </el-form-item>
        <el-form-item label="项目小类" prop="unitCode">
          <el-select v-model="form.subdivision" placeholder="请选择">
            <el-option
              v-if="flag === '1'"
              v-for="item in typeListA"
              :key="item.key"
              :label="item.name"
              :value="item.key"/>
            <el-option
              v-if="flag === '2'"
              v-for="item in typeListB"
              :key="item.key"
              :label="item.name"
              :value="item.key"/>
          </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 { listProject, getProject, delProject, addProject, updateProject } from "@/api/zcustom/project";
import { getListTree, listPlatAppOrg } from '@/api/zcustom/platAppOrg'

export default {
  name: "Project",
  data() {
    return {
      props:{
        value: 'appOrg',
        label: 'appName',
        checkStrictly: true,
        emitPath: false
      },
      treeList:[],
      typeListA:[
        {
          key: 1,
          name:'可研评审'
        },
        {
          key: 2,
          name:'初设评审'
        },
        {
          key: 3,
          name:'政府立项备案'
        },
        {
          key: 4,
          name:'土地手续'
        },
        {
          key: 5,
          name:'用地规划'
        },
        {
          key: 6,
          name:'工程规划'
        },
        {
          key: 7,
          name:'设计招标'
        },
        {
          key: 8,
          name:'施工招标'
        },
        {
          key: 9,
          name:'监理招标'
        },
        {
          key: 10,
          name:'施工许可'
        },
      ],
      typeListB:[
        {
          key: 11,
          name:'基础施工'
        },
        {
          key: 12,
          name:'主体工程'
        },
        {
          key: 13,
          name:'室内外装修'
        },
        {
          key: 14,
          name:'室外工程'
        }
      ],
      flag: '1',

      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工程项目表格数据
      projectList: [],
      platAppOrgList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectName: null,
        unitCode: null,
        connectFlag: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "项目id不能为空", trigger: "blur" }
        ],
        projectName: [
          { required: true, message: "项目名称不能为空", trigger: "blur" }
        ],
        appOrg: [
          { required: true, message: "所属单位id不能为空", trigger: "blur" }
        ],
        connectFlag: [
          { required: true, message: "接入标志不能为空", trigger: "blur" }
        ],
        delFlag: [
          { required: true, message: "删除标志不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getUnitList()
    this.getListTree();
  },
  methods: {
    /** 查询工程项目列表 */
    getList() {
      this.loading = true;
      listProject(this.queryParams).then(response => {
        this.projectList =  response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    getListTree(){
      getListTree().then(res => {
        this.treeList.push(res.data)
      })
    },
    getUnitList() {
      listPlatAppOrg().then(response => {
        this.platAppOrgList =  response.data.records;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        projectName: null,
        unitCode: null,
        connectFlag: null,
        delFlag: 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();
      this.flag = Number(row.subdivision) < 10 ? '1' : '2'
      const id = row.id || this.ids
      getProject(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改工程项目";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProject(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProject(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除？').then(function() {
        return delProject(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
