<template>
  <div>
    <el-drawer
      title="反馈"
      :visible.sync="drawer"
      direction="rtl"
      size="660px">
      <div style="padding: 10px 20px;">
        <div style="padding-bottom: 10px;">
          <Button type="primary" @click="reject">调令退回</Button>
        </div>
        <xy-table ref="xyTable"
                  :action="index"
                  :req-opt="select"
                  :table-item="table"
                  :is-first-req="false"></xy-table>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { index, save } from "@/api/system/baseForm"
import { show } from "@/api/system/customFormField";
export default {
  data() {
    return {
      drawer: false,
      select: {
        table_name: 'feedbacks',
        filter: [
          {
            key: "transfer_id",
            op: "eq",
            value: ""
          }
        ]
      },
      table: [
        {
          prop: "content",
          label: "内容",
          minWidth: 200,
          align: "left"
        },
        {
          prop: "type",
          label: "反馈类型",
          width: 160,
          formatter: (t1, t2, val) => {
            return this.types.get(val)
          }
        },
        {
          prop: "feedback_department_id_departments_id_relation.name",
          label: "反馈科室",
          width: 160
        },
        {
          prop: "status",
          label: "状态",
          width: 120,
          formatter: (t1, t2, val) => {
            return val ? '正常' : '退回'
          }
        },
        {
          prop: "operate",
          label: "操作",
          width: 100,
          customFn: row => {
            return row.status === 1 ? (
              <Button size="small"
                      type="primary"
                      on={{
                        ['click']:_ => {
                          this.$confirm('确认驳回反馈, 是否继续?', '提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'warning'
                          }).then(() => {
                            save({
                              table_name: "feedbacks",
                              id: row.id,
                              status: 0
                            }).then(res => {
                              this.$message({
                                type: "success",
                                message: "操作成功"
                              })
                              this.$refs['xyTable'].getTableData()
                            })
                          })
                        }
                      }}>驳回反馈</Button>
            ) : ''
          }
        }
      ],
      types: []
    }
  },
  methods: {
    index,
    show () {
      this.drawer = true
    },
    hide () {
      this.drawer = false
    },
    setId (id) {
      this.select.filter[0].value = id
    },
    async getType() {
      const obj = (await show({ id: 23 }, false))?.select_item;
      if (obj && typeof obj === "object") {
        let keys = Object.keys(obj);
        if (keys.length > 0) {
          this.types = new Map(keys.map(key => [/^\d*$/.test(obj[key]) ? Number(obj[key]) : obj[key],key]))
        }
      }
    },

    reject () {
      save({
        table_name: "transfers",
        id: this.select.filter[0].value,
        status: 1
      },false).then(_ => (this.$message({ type: 'success', message: '操作成功'})))
    }
  },
  computed: {


  },
  watch: {
    drawer (newVal) {
      if (newVal) {
        this.$nextTick(() => {
          this.$refs['xyTable'].getTableData(true)
        })
      }
    }
  },
  created() {
    this.getType()
  }
}
</script>

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