<template>
  <!-- 项目管控台 -->
  <div class="xmgktContent">
    <div class="d-flex flex-between">
      <div>
        <el-button type="success" size="medium" @click="createNew">
          <span class="d-in-block v-mid el-icon-plus font-size-16"></span>
          <span class="d-in-block v-mid font-size-16">新建项目</span>
        </el-button>
      </div>
      <el-form label-width="100px" :inline="true">
        <el-form-item prop="xm_start_time">
          <el-date-picker v-model="searchForm.xm_start_time" type="date" placeholder="选择日期" value-format="yyyy-MM-dd" @change="getListData"></el-date-picker>
        </el-form-item>
        <el-form-item prop="keyword">
          <el-input placeholder="请输入搜索关键字" v-model="searchForm.keyword" clearable @clear="getListData"></el-input>
        </el-form-item>
        <el-button type="primary" class="ml5" @click="getListData">搜索</el-button>
      </el-form>
    </div>

    <el-table :data="tableData" tooltip-effect="dark" v-loading="loading" style="width: 100%">
      <el-table-column label="序号" align="center" type="index" width="50">
      </el-table-column>
      <el-table-column v-for="(item, index) in columnData" :key="index" :label="item.name" show-overflow-tooltip :prop="item.key" width="auto"></el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button type="text" @click="handleManage(scope.row)">管理</el-button>
          <el-button type="text" @click="editData(scope.row, 2)">详情</el-button>
          <!-- <el-button type="text" @click="editData(scope.row, 1)">编辑 </el-button>
          <el-button type="text" @click="delData(scope.row)">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <footer class="flex-item-none" style="display: flex; justify-content: flex-end; margin-top: 30px">
      <el-pagination background layout="total,prev, pager, next" :page-size="limit" :total="total" :current-page.sync="cur_page" @current-change="handleCurrentChange"></el-pagination>
    </footer>

    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible" :width="dialogType==1?'600px':'80%'" @close="handleClose">
      <add-and-edit v-if="dialogType==1" ref="addAndEdit" :currentId="currentId" :selectObj="selectObj" @close="handleClose"></add-and-edit>
      <detail v-if="dialogType==2" :currentId="currentId" />
      <div slot="footer" v-if="dialogType==1">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="formSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { xmgl_xmzlkList, xmgl_xmzlkDelete } from "./api";
import AddAndEdit from "./AddAndEdit.vue";
import Detail from "./Detail.vue";
export default {
  data() {
    return {
      searchForm: {
        xm_start_time: "",
        keyword: "",
      },
      loading: false,
      columnData: [
        {
          name: "项目名称",
          key: "xm_name"
        },
        {
          name: "项目类别",
          key: "xm_type"
        },
        {
          name: "项目预算(万元)",
          key: "xm_ys"
        },
        {
          name: "中标金额(万元)",
          key: "xm_zbje"
        },
        {
          name: "实际支付(万元)",
          key: "xm_sjzf"
        },
        {
          name: "经办科室",
          key: "department_name"
        },
        {
          name: "经办人",
          key: "xm_zrr"
        },
        {
          name: "项目开始时间",
          key: "xm_start_time"
        },
        {
          name: "项目阶段",
          key: "xm_jd"
        },
        {
          name: "当前事项",
          key: "xm_dqsx"
        },
        {
          name: "项目进度",
          key: "per"
        }
      ],
      tableData: [],
      limit: 10,
      total: 0,
      cur_page: 1,
      dialogTitle: "",
      dialogType: "",
      dialogFormVisible: false,
      currentId: "",
      selectObj: [],
    }
  },
  components: { AddAndEdit, Detail },
  methods: {
    async getListData(flag) {
      this.loading = true;
      let obj = {
        page: this.cur_page,
        limit: this.limit,
        xm_start_time: this.searchForm.xm_start_time,
        keyword: this.searchForm.keyword,
      }
      let data = await xmgl_xmzlkList(obj);
      if (data) {
        data.page_data.map(item => {
          item.xm_start_time = item.xm_start_time.substring(0, 10);
          item.per = item.xm_dqjd != "" ? item.xm_dqjd + "%" : "0%";
        })
        this.tableData = data.page_data;
        this.cur_page = Number(data.page_now);
        this.total = Number(data.total_rows);
        this.loading = false;

        if (flag == 1) {
          for (let key in data.table_structure.field) {
            if (data.table_structure.field[key].type == "radio") {
              this.selectObj[key] = data.table_structure.field[key];
            }
          }
        }
      }
    },
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    createNew() {
      this.dialogFormVisible = true;
      this.dialogTitle = "新建项目";
      this.dialogType = 1;
    },
    editData(data, type) {
      this.dialogFormVisible = true;
      this.currentId = data.xm_id;
      this.dialogType = type;
      this.dialogTitle = type == 1 ? "编辑项目" : "详情";
    },
    delData(data) {
      this.$confirm("是否确定删除当前数据？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let res = await xmgl_xmzlkDelete({ xm_id: data.xm_id });
          if (res) {
            this.$message.success("删除成功！");
            this.getListData();
          }
        })
        .catch(() => { });
    },
    handleClose(type) {
      this.dialogFormVisible = false;
      this.dialogType = "";
      this.$refs.addAndEdit && this.$refs.addAndEdit.resetForm();
      this.currentId = "";
      if (type == 1) {
        this.getListData();
      }
    },
    formSubmit() {
      this.$refs.addAndEdit.commitData();
    },
    handleManage(data) {
      this.$router.push({ name: "xmgl-xmgkt-manage", params: { id: data.xm_id } })
    }
  },
  created() {
    this.getListData(1);
  }
}
</script>

<style>
</style>