<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      ref="crud"
      @row-save="rowSave"
      @row-del="rowDel"
      @search-change="searchChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @search-reset="searchReset"
      @on-load="onLoad"
    >
      <!--      <template slot="search" slot-scope="{row,size}">-->
      <!--        <div style="width: 500px;">-->
      <!--          <el-date-picker-->
      <!--            v-model="row.applTime"-->
      <!--            type="daterange"-->
      <!--            range-separator="至"-->
      <!--            start-placeholder="开始日期"-->
      <!--            end-placeholder="结束日期"-->
      <!--            format="yyyy 年 MM 月 dd 日"-->
      <!--            value-format="yyyy-MM-dd"-->
      <!--            @change="dateChange">-->
      <!--          </el-date-picker>-->
      <!--        </div>-->
      <!--      </template>-->
      <template slot="status" slot-scope="{ row }">
        <el-switch
          :value="row.status"
          :active-value="1"
          :inactive-value="0"
          active-color="#13ce66"
          inactive-color="#ff4949"
          @change="changeStatus(row)"
        >
        </el-switch>
      </template>
      <template slot="menu" slot-scope="{row}">
        <el-button size="small" type="text" @click="look(row)">
          <i class="el-icon-view"></i>
          查看回答
        </el-button>
      </template>
    </avue-crud>
    <Detail :visible.sync="viewDrawer" :rowData="selectPushServiceData"></Detail>

  </basic-container>
</template>

<script>
import {dateFormat} from '@/util/date'
import Tinymce from "@/components/Tinymce";
import {
  getApplContentList,
  addApplContentList,
  changePublish,
  deleteQuestionnaireSurveyInfo
} from '@/api/help/applicationsRecord/applicationsRecord'
import Detail from './sub/detail.vue'

const initPages = {
  pageSize: 10,
  currentPage: 1,
  total: 100,
}
const classStatus = {
  0: '未发布',
  1: '发布',
  2: '关闭'
}
const classIsRead = {
  0: '未阅读',
  1: '已阅读',
}
const classEnumStatus = Object.keys(classStatus).map((item) => ({
  label: classStatus[item], value: Number(item)
}))
const classEnumIsRead = Object.keys(classIsRead).map((item) => ({
  label: classIsRead[item], value: Number(item)
}))
export default {
  name: "index",
  components: {
    Tinymce,
    Detail
  },
  mounted() {
  },
  data() {
    return {
      applTime: '',
      selectPushServiceData: null,
      loading: false,
      viewDrawer: false,
      query: {},
      data: [],
      page: Object.assign({}, initPages),
    }
  },
  computed: {
    option() {
      return {
        height: "auto",
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        indexLabel: "序号",
        selection: false,
        viewBtn: true,
        viewBtnText: "查看问卷",
        addBtn: true,
        editBtn: false,
        delBtn: true,
        columnBtn: false,
        menuWidth: 300,
        labelWidth: 151,
        dialogClickModal: false,
        dialogType: "drawer",
        dialogFullscreen: true,
        align: 'center',
        column: [
          {
            label: "机构名称",
            prop: "companyName",
            addDisplay: false,
            span: 24,
          },
          {
            label: "问卷题目",
            prop: "surveyTitle",
            span: 24,
            search: true,
            rules: [
              {
                required: true,
                message: "输入问卷题目",
                trigger: "change",
              },
            ]
          },
          {
            label: "问卷内容",
            prop: "surveyContent",
            span: 24,
            search: false,
            rules: [
              {
                required: true,
                message: "输入问卷内容",
                trigger: "change",
              },
            ]
          },
          {
            label: "创建人",
            prop: "createUser",
            addDisplay: false,
          },
          {
            label: "创建时间",
            prop: "createTime",
            type: 'datetime',
            addDisplay: false,
            searchSpan: 8,
            searchRange: true,
            search: false,
            format: "yyyy/MM/dd",
          },
          {
            label: '发布状态',
            prop: 'status',
            type: 'select',
            addDisplay: false,
            dicData: [
              {label: '未发布', value: 0},
              {label: '已发布', value: 1},
            ],
            slot: true,
            width: 100
          },
        ]
      }
    }
  },
  methods: {
    dateFormat,

    searchReset() {
      this.query = {};
      this.page.currentPage = 1
      this.onLoad();
    },
    searchChange(params, done) {
      console.log(params);
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad();
      done();
    },
    currentChange(val) {
      this.page.currentPage = val
      this.onLoad()
    },
    sizeChange(val) {
      this.page.currentPage = 1
      this.page.pageSize = val
      this.onLoad()
    },
    refreshChange() {
      this.page.currentPage = 1;
      this.onLoad()
    },
    look(row) {
      this.selectPushServiceData = row
      this.viewDrawer = true
    },
    async changeStatus(row) {
      const status = row.status ? 0 : 1
      let params = {id: row.id, status}
      console.log(params)
      let resData = await changePublish(params)
      if (resData.data.code === 200) {
        this.$message.success('状态修改成功');
        this.onLoad()
      }
    },
    rowDel(row) {
      const h = this.$createElement;
      this.$confirm(
        h("div", null, [
          h("p", {style: "font-size: 16px"}, "您确定要删除此问卷吗? "),
          h("p", {style: "color: red"}, "一旦删除则无法找回"),
        ]),
        {
          type: "warning",
          showClose: false,
          showCancelButton: true,
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          beforeClose: async (action, instance, done) => {
            if (action === "confirm") {
              let resData = await deleteQuestionnaireSurveyInfo({id: row.id})
              if (resData.data.code === 200) {
                this.onLoad()
                this.$message({
                  type: "success",
                  message: "操作成功!",
                });
              }
              done()
            } else {
              done();
            }
          },
        }
      )
    },
    rowSave(row, done, loading) {
      const params = {
        surveyTitle: row.surveyTitle,
        surveyContent: row.surveyContent,
      };
      addApplContentList(params).then(
        () => {
          this.page.currentPage = 1
          this.onLoad();
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          window.console.log(error);
          loading();
        })
    },
    /* 表格方法 */
    onLoad() {
      return new Promise(async (resolve, reject) => {
        this.loading = true;
        let params = {
          current: this.page.currentPage,
          size: this.page.pageSize,
          ...this.query,
        }
        let resData = await getApplContentList(params)
        if (resData.data.code === 200) {
          const {current, records, total, size} = resData.data.data
          this.data = records;
          this.page.total = total;
          this.loading = false;
        }
        resolve()
      })
    },
  }
}
</script>

<style scoped>

</style>
