<template>
  <div>
    <div class="d-flex flex-between">
      <div>
        <el-button type="primary" size="medium" @click="addData">
          <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="state_value">
          <el-select class="mr10" v-model="type_value" placeholder="请选择类别">
            <el-option
              v-for="item in type_list"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="keyword">
          <el-input
            placeholder="请输入搜索关键字"
            v-model="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 type="selection" width="55"> </el-table-column>
      <el-table-column label="序号" align="center" type="index" width="50">
      </el-table-column>
      <el-table-column
        label="标题"
        show-overflow-tooltip
        prop="jxt_name"
        width="auto"
      ></el-table-column>
      <el-table-column
        label="创建人"
        width="150"
        prop="jxt_cjr"
      ></el-table-column>
      <el-table-column
        label="日期"
        width="150"
        prop="jxt_cjsj"
      ></el-table-column>
      <el-table-column label="已提交分数" prop="jxt_ytjfs" width="150">
      </el-table-column>
      <el-table-column label="状态" prop="jxt_cyzt" width="150">
        <template slot-scope="scope">
          <span v-if="scope.row.jxt_cyzt_option_k == 1">未参与</span>
          <span v-if="scope.row.jxt_cyzt_option_k == 2">已参与</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button type="text" @click="editData(scope.row)">编辑 </el-button>
          <el-button type="text" @click="detailData(scope.row)"
            >预览
          </el-button>
          <el-button type="text" @click="submitData(scope.row)"
            >发布
          </el-button>
          <el-button type="text" @click="delData(scope.row.jxt_id)"
            >删除
          </el-button>
          <!-- <el-dropdown class="ml10 hand">
            <span class="el-dropdown-link">
              其他<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>查看</el-dropdown-item>
              <el-dropdown-item>关闭</el-dropdown-item>
              <el-dropdown-item>结果统计</el-dropdown-item>
              <el-dropdown-item>查看投票记录</el-dropdown-item>
              <el-dropdown-item>删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown> -->
        </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="title"
      :visible.sync="editDialogFormVisible"
      width="750px"
      destory-on-close
    >
      <el-form :model="dialogForm" label-width="90px">
        <el-form-item label="标题" prop="bt">
          <el-input v-model="dialogForm.bt"></el-input>
        </el-form-item>

        <el-form-item label="日期" prop="rq">
          <el-date-picker
            v-model="dialogForm.rq"
            type="date"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item label="已提交分数" prop="ytjfs">
          <el-input v-model="dialogForm.ytjfs"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="formCancel">取 消</el-button>
        <el-button type="primary" @click="formSubmit">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="750px">
      <div class="pop-content">
        <div class="mt15 d-flex">
          <div class="font-size-16">标题:</div>
          <div class="flex-1 font-size-16">{{ detailInfo.jxt_name }}</div>
        </div>
        <div class="mt15 d-flex">
          <div class="font-size-16">创建人：</div>
          <div class="flex-1 font-size-16">{{ detailInfo.jxt_cjr }}</div>
        </div>
        <div class="mt15 d-flex">
          <div class="font-size-16">日期:</div>
          <div class="flex-1 font-size-16">{{ detailInfo.jxt_cjsj }}</div>
        </div>
        <div class="mt15 d-flex">
          <div class="font-size-16">已提交分数:</div>
          <div class="flex-1 font-size-16">{{ detailInfo.jxt_ytjfs }}</div>
        </div>
      </div>
      <div class="mt15 d-flex">
        <div class="font-size-16">状态:</div>
        <div class="flex-1 font-size-16">{{ detailInfo.jxt_cyzt }}</div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { useUserStore } from "@/stores/user";
const { real_name } = useUserStore();
import {
  wfqd_list,
  wfqd_add,
  wfqd_edit,
  wfqd_del,
  wfqd_detail,
  wfqd_verify,
} from "./api";
export default {
  name: "index",
  data() {
    return {
      keyword: "",
      type_value: "",
      state_list: [
        {
          label: "全部",
          value: "0",
        },
        {
          label: "待审核",
          value: "1",
        },
        {
          label: "已通过",
          value: "2",
        },
        {
          label: "未通过",
          value: "3",
        },
      ],
      type_list: [
        {
          label: "全部",
          value: "0",
        },
        {
          label: "未参与",
          value: "1",
        },
        {
          label: "已参与",
          value: "2",
        },
      ],
      tableData: [],
      limit: 10,
      total: 12,
      cur_page: 1,
      detailInfo: {},
      dialogFormVisible: false,
      editDialogFormVisible: false,
      dialogForm: {
        bt: "",
        cjr: "",
        rq: "",
        ytjfs: "",
        zt: "",
      },
      title: "",
      isEdit: false,
      id: "",
      loading: false,
    };
  },
  methods: {
    getListData() {
      this.loading = true;
      let data = {
        page: this.cur_page,
        limit: this.limit,
        keyword: this.keyword,
      };

      wfqd_list(data).then((res) => {
        this.loading = false;
        this.tableData = res.data.page_data;
        this.cur_page = Number(res.data.page_now);
        this.total = Number(res.data.total_rows);
      });
    },
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    addData() {
      this.title = "新建调查";
      this.dialogForm = {
        bt: "",
        cjr: "",
        rq: "",
        ytjfs: "",
        zt: "",
      };
      this.isEdit = false;
      this.editDialogFormVisible = true;
    },
    editData(item) {
      this.title = "编辑调查";
      this.isEdit = true;
      this.editDialogFormVisible = true;
      this.id = item.jxt_id;
      this.dialogForm.bt = item.jxt_name;
      this.dialogForm.cjr = item.jxt_cjr;
      this.dialogForm.rq = item.jxt_cjsj;
      this.dialogForm.ytjfs = item.jxt_ytjfs;
      // this.dialogForm.zt = item.jxt_zxzt;
      this.dialogForm.zt = item.jxt_cyzt;
    },
    formSubmit() {
      let data = {
        jxt_name: this.dialogForm.bt,
        jxt_cjr: real_name,
        jxt_cjsj: this.dialogForm.rq,
        jxt_ytjfs: this.dialogForm.ytjfs,
        // jxt_zxzt: this.dialogForm.zt,
        jxt_cyzt: this.dialogForm.zt,
      };
      if (!this.isEdit) {
        wfqd_add(data).then((res) => {
          if (res.code == 1) {
            this.$message({
              message: "调查添加成功！",
              type: "success",
            });

            this.dialogFormVisible = false;
            this.getListData();
          } else {
            this.$message({
              message: res.msg,
              type: "error",
            });
          }
        });
      } else {
        data.jxt_id = this.id;
        wfqd_edit(data).then((res) => {
          if (res.code == 1) {
            this.$message({
              message: "调查编辑成功！",
              type: "success",
            });

            this.dialogFormVisible = false;
            this.getListData();
          } else {
            this.$message({
              message: res.msg,
              type: "error",
            });
          }
        });
      }
    },
    formCancel() {
      this.editDialogFormVisible = false;
    },
    verify(type, item) {
      let data = {
        jxt_cyzt: type,
        jxt_id: item.jxt_id,
      };
      wfqd_verify(data).then((res) => {
        if (res.code == 1) {
          this.$message({
            message: "审核成功！",
            type: "success",
          });
          this.getListData();
        }
      });
    },
    submitData(item) {
      let data = {
        jxt_zt: 2,
        jxt_id: item.jxt_id,
      };
      wfqd_verify(data).then((res) => {
        if (res.code == 1) {
          this.$message({
            message: "发布成功！",
            type: "success",
          });
          this.getListData();
        }
      });
    },
    delData(id) {
      let data = {
        jxt_id: id,
      };

      this.$confirm("确认删除该调查?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          wfqd_del(data).then((res) => {
            if (res.code == 1) {
              this.$message({
                message: "调查删除成功！",
                type: "success",
              });
              this.getListData();
            } else {
              this.$message({
                message: res.msg,
                type: "error",
              });
            }
          });
        })
        .catch(() => {});
    },
  },
  mounted() {
    this.getListData();
  },
};
</script>

<style lang="scss" scoped>
</style>