<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="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 {
  getList,
  getInfo,
  addEmploymentSurveyManage,
  deleteEmploymentSurveyManage,
  publishEmploymentSurveyManage
} from '@/api/manage/survey'
import Detail from './sub/detail.vue'

const initPages = {
  pageSize: 10,
  currentPage: 1,
  total: 100,
}
const classEnum = {
  "1": '一般投诉',
  "2": '较难投诉',
  "3": '困难投诉',
  "4": '特殊投诉',
}
const classIsDeleted = {
  0: '否',
  1: '是',
}
const classStatus = {
  0: '未发布',
  1: '已发布',
  // 2: '关闭'
}
const classEnumList = Object.keys(classEnum).map((item) => ({
  label: classEnum[item], value: item
}))
const classEnumDeleted = Object.keys(classIsDeleted).map((item) => ({
  label: classIsDeleted[item], value: Number(item)
}))
const classEnumStatus = Object.keys(classStatus).map((item) => ({
  label: classStatus[item], value: Number(item)
}))
const columnList = [
  {
    label: "机构名称",
    prop: "companyName",
    search: true,
    addDisplay: false,
  },
  {
    label: "调研题目",
    prop: "surveyTitle",
    span: 24,
    search: true,
    rules: [
      {
        required: true,
        message: "输入调研题目",
        trigger: "change",
      },
    ]
  },
  {
    label: "调研内容",
    prop: "surveyContent",
    span: 24,
    search: true,
    rules: [
      {
        required: true,
        message: "输入调研内容",
        trigger: "change",
      },
    ]
  },
  {
    label: "创建部门",
    prop: "createDept",
    addDisplay: false,
    hide: true,
  },
  {
    label: "调研时间",
    prop: "surveyTime",
    addDisplay: false,
    hide: true,
  },
  {
    label: "创建时间",
    prop: "createTime",
    addDisplay: false,
    type: 'datetime',
    searchSpan: 8,
    searchRange: true,
  },
  {
    label: "是否发布",
    prop: "status",
    type: "select",
    addDisplay: false,
    dicData: classEnumStatus,
    search: true,
    slot: true
  },
]
const rules = {
  name: [
    {required: true, message: '请输入内容标题', trigger: 'blur'}
  ],
  type: [
    {required: true, message: '请选择分类', trigger: 'blur'}
  ],
  content: [
    {required: true, message: '请输入内容', trigger: 'blur'}
  ],
}
export default {
  name: "index",
  components: {
    Tinymce,
    Detail
  },
  created() {
  },
  data() {
    return {
      applTime: '',
      loading: false,
      viewDrawer: false,
      drawerTitle: '新增内容',
      formOption: {},
      query: {},
      rules: Object.assign({}, rules),
      data: [],
      lookData: {},
      classEnum,
      page: Object.assign({}, initPages),
    }
  },
  computed: {
    option() {
      return {
        height: "auto",
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        indexLabel: "序号",
        selectPushServiceData: null,
        selection: false,
        viewBtn: true,
        addBtn: true,
        editBtn: false,
        viewBtnText: "查看调研",
        delBtn: true,
        columnBtn: false,
        menuWidth: 300,
        labelWidth: 151,
        dialogClickModal: false,
        dialogType: "drawer",
        dialogFullscreen: true,
        column: columnList,
      }
    }
  },
  methods: {
    dateFormat,
    look(row) {
      this.selectPushServiceData = row
      this.viewDrawer = true
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.viewDrawer = false
    },
    searchReset() {
      this.query = {};
      this.onLoad();
    },
    searchChange(params, done) {
      console.log(params);
      if (params.applTime) {
        var val = JSON.parse(JSON.stringify(params.applTime))
        params.applTimeStart = val[0] + " 00:00:00";
        params.applTimeEnd = val[1] + " 23:59:59";
        delete params.applTime
      }
      this.query = params;
      console.log(this.query);
      this.page.currentPage = 1;
      this.onLoad();
      done();
    },
    async changeStatus(row) {
      const status = row.status ? 0 : 1
      let params = {id: row.id, status}
      console.log(params)
      let resData = await publishEmploymentSurveyManage(params)
      if (resData.data.code === 200) {
        this.$message.success('状态修改成功');
        this.onLoad()
      }
    },
    currentChange(val) {
      this.page.currentPage = val
    },
    sizeChange(val) {
      this.page.currentPage = 1
      this.page.pageSize = val
    },
    refreshChange() {
      this.page.currentPage = 1;
      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 deleteEmploymentSurveyManage({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,
      };
      addEmploymentSurveyManage(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 getList(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()
      })
    },
    seeDetail(row) {
      return new Promise(async (resolve, reject) => {
        let params = {
          id: row.id
        }
        let resData = await getInfo(params)
        if (resData.data.code === 200) {
          this.drawerTitle = '查看'
          this.lookData = JSON.parse(JSON.stringify(resData.data.data))
          this.formOption = JSON.parse(JSON.stringify(resData.data.data))
          this.viewDrawer = true
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
