<template>
  <!-- 项目流程管理 -->
  <div class="xmlcglContent">
    <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="lc_jd">
          <el-select v-model="searchForm.lc_jd" placeholder="请选择" v-if="selectObj.lc_jd" @change="getListData">
            <el-option label="全部" value=""></el-option>
            <el-option v-for="item in selectObj.lc_jd.option" :key="item.v" :label="item.n" :value="item.v"></el-option>
          </el-select>
        </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" prop="order" 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="editData(scope.row)">编辑 </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="600px" @close="handleClose">
      <add-and-edit ref="addAndEdit" :currentId="currentId" :selectObj="selectObj" @close="handleClose"></add-and-edit>
      <div slot="footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="formSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { xmgl_xmlcList, xmgl_xmlcDelete } from "./api";
import AddAndEdit from "./components/AddAndEdit.vue";
export default {
  data() {
    return {
      searchForm: {
        lc_jd: "",
        keyword: "",
      },
      loading: false,
      columnData: [
        {
          name: "项目阶段",
          key: "lc_jd"
        },
        {
          name: "项目事项",
          key: "lc_sx"
        },
        {
          name: "事项权值",
          key: "lc_sxqz"
        },
        {
          name: "上传附件标注",
          key: "lc_fj_bz"
        },
        {
          name: "必填项",
          key: "lc_bt_name"
        },
        {
          name: "创建人",
          key: "lc_create_name"
        },
        {
          name: "创建时间",
          key: "create_dateline"
        }
      ],
      tableData: [],
      limit: 10,
      total: 0,
      cur_page: 1,
      dialogTitle: "",
      dialogFormVisible: false,
      currentId: "",
      selectObj: [],
    }
  },
  components: { AddAndEdit },
  methods: {
    async getListData(flag) {
      this.loading = true;
      let obj = {
        page: this.cur_page,
        limit: this.limit,
        lc_jd: this.searchForm.lc_jd,
        keyword: this.searchForm.keyword,
      }
      let data = await xmgl_xmlcList(obj);
      if (data) {
        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 = "新建项目事项";
    },
    editData(data) {
      this.dialogFormVisible = true;
      this.dialogTitle = "编辑项目事项";
      this.currentId = data.lc_id;
    },
    delData(data) {
      this.$confirm("是否确定删除当前流程？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let res = await xmgl_xmlcDelete({ lc_id: data.lc_id });
          if (res) {
            this.$message.success("删除成功！");
            this.getListData();
          }
        })
        .catch(() => { });
    },
    handleClose(type) {
      this.dialogFormVisible = false;
      this.$refs.addAndEdit && this.$refs.addAndEdit.resetForm();
      this.currentId = "";
      if (type == 1) {
        this.getListData();
      }
    },
    formSubmit() {
      this.$refs.addAndEdit.commitData();
    }
  },
  created() {
    this.getListData(1);
  }
}
</script>

<style>
</style>