<!--
 * @desc 用户管理-回访
 * @author linyuanjie
 * @date 2023/11/14
-->

<template>
  <el-dialog
    title="回访"
    :visible.sync="showDialog"
    width="900px"
    center
    :modal-append-to-body='false'
    :before-close="handleClose">
    <div class="revisit-content">
      <avue-crud
        ref="crud"
        :option="tableOption"
        :table-loading="tableLoading"
        :data="tableData"
        @refresh-change="refreshChange">
        <template slot="menuLeft">
          <el-button
            v-if="permissions['user_record_add']"
            class="filter-item"
            type="primary"
            size="small"
            icon="el-icon-edit"
            @click="addFn">添加
          </el-button>
        </template>
        <template
          slot="menu"
          slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="downloadFn(scope.row)">下载
          </el-button>
          <el-button
            v-if="permissions['user_record_del']"
            type="text"
            size="small"
            @click="deleteFn(scope.row)">删除
          </el-button>
        </template>
      </avue-crud>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">关 闭</el-button>
    </span>
    <addRevisitDialog :showDialog.sync="showAddRevisitDialog" :userId="userId" @getList="refreshChange" />
  </el-dialog>
</template>

<script>
import {revisitTableOption} from "@/const/crud/admin/user";
import {delRevisitRecord, getRevisitRecordPage} from "@/api/admin/user";
import {mapGetters} from "vuex";
import addRevisitDialog from "@/views/admin/user/components/addRevisitDialog";
import request from "@/router/axios";

export default {
  name: "revisitDialog",
  components: { addRevisitDialog },
  props:{
    showDialog: {
      type: Boolean,
      required: true,
      default:  false
    },
    userId: {
      type: Number,
      default: null
    }
  },
  data () {
    return {
      tableLoading: false,
      tableOption: revisitTableOption,
      tableData: [],
      showAddRevisitDialog: false
    }
  },
  computed: {
    ...mapGetters(['permissions'])
  },
  methods: {
    handleClose () {
      this.$emit('update:showDialog', false)
    },
    getList() {
      this.tableLoading = true
      getRevisitRecordPage({ userId: this.userId }).then(response => {
        this.tableData = response.data.data
        this.tableLoading = false
      })
    },
    refreshChange() {
      this.getList()
    },
    addFn () {
      this.showAddRevisitDialog = true
    },
    downloadFn (row) {
      request({
        url: row.msg,
        method: 'get',
        responseType: 'blob'
      }).then((response) => { // 处理返回的文件流
        const blob = response.data
        const link = document.createElement('a')
        link.href = URL.createObjectURL(blob)
        link.download = row.msg
        document.body.appendChild(link)
        link.click()
        window.setTimeout(function() {
          URL.revokeObjectURL(blob)
          document.body.removeChild(link)
        }, 0)
      })
    },
    deleteFn (row) {
      this.$confirm('确定删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await delRevisitRecord({ id: row.id })
        if (res.data.code === 0) {
          this.$message.success( '删除成功')
          this.refreshChange()
        }
      }).catch(() => {});
    }
  },
  watch: {
    showDialog (newVal) {
      if (newVal) {
        this.getList(this.page, { userId: this.userId })
      }
    }
  }
}
</script>
