<style lang="scss">
.preview {
  .el-form-item {
    margin-bottom: 15px;
  }
  label,
  p {
    line-height: 25px !important;
  }
}
</style>
<template>
  <section>
    <p><span>信息管理></span>问卷模板</p>
    <div class="content">
      <div class="filter">
        <el-form
          size="small"
          label-width="70px"
          :inline="true"
          label-position="left"
        >
          <el-form-item label="">
            <el-input
              clearable
              placeholder="请输入您要搜索的内容"
              v-model="form.name"
              @clear="search"
              @keyup.enter.native="search"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="search">搜索</el-button>
          </el-form-item>
          <el-form-item style="float: right">
            <el-button
              @click="(dialogVisible = true), (dialogTitle = '新增模板')"
              type="primary"
              >新增模板</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <el-table
        class="table"
        :data="list"
        height="50vh"
        border
        v-loading="loading"
        default-expand-all
        row-key="id"
        style="width: 100%"
      >
        <el-table-column prop="name" label="模板名称"> </el-table-column>
        <el-table-column prop="cname" label="创建人"> </el-table-column>
        <el-table-column prop="ctime" label="创建时间"> </el-table-column>
        <el-table-column prop="zip" width="250" label="操作">
          <template slot-scope="scope">
            <el-button
              class="edit"
              type="text"
              @click="edit(scope.row)"
              v-if="scope.row.edit"
              >编辑</el-button
            >
            <el-tooltip
              v-else
              class="item"
              effect="dark"
              content="该模版使用的问卷正在被填报，无法修改，可新增模版-选择复用历史模版使用该模版或删除正在使用该模版的问卷!"
              placement="top-start"
            >
              <el-button
                class="edit"
                type="text"
                style="color: #ccc; cursor: not-allowed"
                >编辑</el-button
              >
            </el-tooltip>
            <el-button
              class="del"
              @click="del(scope.row.id)"
              type="text"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <Page
        ref="pageButton"
        :current="form.page"
        :page_size="form.page_size"
        :total="total"
        @pageChange="gopage"
      />
    </div>

    <el-dialog
      :title="dialogTitle"
      :close-on-click-modal="false"
      :visible.sync="dialogVisible"
    >
      <el-form
        ref="form1"
        size="small"
        class="preview"
        :inline="false"
        label-width="100px"
        :rules="rules"
        :model="form1"
      >
        <el-form-item label="复用历史模板" v-if="!form1.id">
          <el-select
            v-model="form1.copyid"
            filterable
            @change="changeTemplate"
            placeholder="请选择历史模板"
          >
            <el-option
              v-for="(item, index) in allTemplate"
              :key="index"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="模板名称" prop="name">
          <el-input
            v-model="form1.name"
            placeholder="请输入模板名称"
            maxlength="50"
            show-word-limit
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" type="primary" @click="addTtitle"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </section>
</template>
<script>
import Page from "../../components/Page";
export default {
  components: {
    Page,
  },
  data() {
    return {
      //   edit:0,
      allTemplate: [],
      form: { name: "", page: 1, page_size: 20 },
      form1: {name:""},
      total: 1,
      list: [{ name: "2333" }],
      loading: false,
      input: "",
      dialogVisible: false,
      dialogTitle: "",
      defaultProps: {},
      data: [],
      rules: {
        name: [{ required: true, message: "请输入标题", trigger: "blur" }],
      },
    };
  },
  methods: {
    switchStatus(row) {
      this.$api.editTemplateStatus({id:row.id}).then((res)=>{
        row.status = res.data.data
      })
    },
    changeTemplate(val) {
      let selVal = this.allTemplate.filter(function (x) {
        return x.id == val;
      });
      this.form1.name = selVal[0].name;
      // this.form1.name = "111"
    },
    addTtitle() {
      this.$refs["form1"].validate((valid) => {
        if (valid) {
          let query = { title: this.form1.name, id: this.form1.id };
          if (this.form1.copyid) {
            query.id = this.form1.copyid;
            query.copyid = this.form1.copyid;
          }
          this.$router.push({
            path: "/message/template/add",
            query: query,
          });
        }
      });
    },
    search() {
      let parm = this.form;
      this.getData();
    },
    getData() {
      var parm = this.form;
      this.loading = true;
      this.$api.getTemplateList(parm).then((res) => {
        this.list = res.data.data.list;
        this.total = res.data.data.total;
        this.loading = false;
      });
      this.$api.getAllTemplateList().then((res) => {
        this.allTemplate = res.data.data;
      });
    },
    edit(row) {
      this.$api.getTemplate({ id: row.id }).then((res) => {
        // this.form1 = row;
        this.form1 = res.data.data;
        this.dialogVisible = true;
      });
    },
    del(id) {
      this.$confirm(
        "删除模板与模板相关联的消息会被一起删除，确定删除吗？",
        "提示",
        {
          type: "warning",
        }
      ).then(() => {
        this.$api.delTemplate({ id: id }).then((res) => {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.getData();
        });
      });
    },
    gopage(size) {
      if (size) {
        this.form.page_size = size;
      }
      this.form.page = this.$refs.pageButton.page;
      this.getData();
    },
  },
  created() {
    this.getData();
  },
};
</script>
