<template>
<!-- 评论管理 -->
  <div>
    
    <div class="gva-table-box">
      <div class="gva-btn-list">
        <!-- <el-button size="mini" type="primary" icon="el-icon-plus" @click="openDialog">新增</el-button> -->
        <el-popover v-model:visible="deleteVisible" placement="top" width="160">
          <p>确定要删除吗？</p>
          <div style="text-align: right; margin-top: 8px;">
            <el-button size="mini" type="text" @click="deleteVisible = false">取消</el-button>
            <el-button size="mini" type="primary" @click="onDelete">确定</el-button>
          </div>
          <template #reference>
            <el-button icon="el-icon-delete" size="mini" :disabled="!apis.length" style="margin-left: 10px;">删除</el-button>
          </template>
        </el-popover>
      </div>
      <el-table
        ref="multipleTable"
        :data="List"
        style="width: 100%"
        tooltip-effect="dark"
        row-key="ID"
         @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column align="left" label="类型" prop="type" width="180"/>
        <el-table-column align="left" label="被评论的内容" prop="content" />
        <el-table-column align="left" label="评论人" prop="commentator" />
        <el-table-column align="left" label="评论内容" prop="comments" />
        <el-table-column align="left" label="时间" prop="time" />
        <el-table-column label="状态" prop="state" center>
                <template #default="scope"> 
                    <div>
                        <el-switch style="display: block" v-model="scope.row.state" active-color="#13ce66" inactive-color="#ff4949" active-text="启用" inactive-text="禁用"></el-switch>
                        <!-- <el-switch :active-value="1" :inactive-value="2" v-model="scope.row.state" disabled /> -->
                    </div>
                </template>
            </el-table-column>
        <el-table-column align="left" label="编辑">
          <template #default="scope">
            <!-- <el-button size="small" type="text" icon="el-icon-edit" @click="updateCustomer(scope.row)">变更</el-button> -->
            <el-popover :visible="scope.row.visible" placement="top" width="160">
              <p>确定要删除吗？</p>
              <div style="text-align: right; margin-top: 8px;">
                <el-button size="mini" type="text" @click="scope.row.visible = false">取消</el-button>
                <el-button type="primary" size="mini" @click="deleteCustomer(scope.row)">确定</el-button>
              </div>
              <template #reference>
                <el-button type="text" icon="el-icon-delete" size="mini">删除</el-button>
              </template>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
      <div class="gva-pagination">
        <el-pagination
          :current-page="page"
          :page-size="pageSize"
          :page-sizes="[10, 30, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </div>
    </div>
    <el-dialog v-model="dialogFormVisible" :before-close="closeDialog" title="客户">
      <el-form :inline="true" :model="form" label-width="80px">
        <el-form-item label="客户名">
          <el-input v-model="form.customerName" autocomplete="off" />
        </el-form-item>
        <el-form-item label="客户电话">
          <el-input v-model="form.customerPhoneData" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button size="small" @click="closeDialog">取 消</el-button>
          <el-button size="small" type="primary" @click="enterDialog">确 定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  createExaCustomer,
  updateExaCustomer,
  deleteExaCustomer,
  getExaCustomer,
  getExaCustomerList
} from '@/api/customer'
import infoList from '@/mixins/infoList'
import warningBar from '@/components/warningBar/warningBar.vue'

export default {
  name: 'Customer',
  components: { warningBar },
  mixins: [infoList],
  data() {
    return {
      deleteVisible: false,
      apis:[],
      List:[
        {
          // 数据Id
          id:1,
          // 类型
          type:'文档类',
          // 被评论的内容
          content:'2021年12月7日机械伤害车辆伤害检查亮点展示',
          // 评论人
          commentator:'毛现国',
          // 评论内容
          comments:'	赞👍👍👍👍👍👍👍👍',
          // 时间
          time:'2021-12-07 20:32:51',
          // 状态
          state:true,

        },
        {
          // 数据Id
          id:2,
          // 类型
          type:'问题答案评论',
          // 被评论的内容
          content:'11.23',
          // 评论人
          commentator:'付洪宝',
          // 评论内容
          comments:'	👌',
          // 时间
          time:'2021-12-04 11:50:01',
          // 状态
          state:true,

        },
      ],
      listApi: getExaCustomerList,
      dialogFormVisible: false,
      type: '',
      form: {
        customerName: '',
        customerPhoneData: ''
      }
    }
  },
  created() {
    this.getTableData()
  },
  methods: {
    //  选中api
    handleSelectionChange(val) {
      this.apis = val
    },
    // 多删除
    async onDelete() {
      console.log(this.apis)
      const ids = this.apis.forEach(item => item.id)
      console.log(ids)
      // const res = await deleteApisByIds({ ids })
      // if (res.code === 0) {
      //   this.$message({
      //     type: 'success',
      //     message: res.msg
      //   })
      //   if (this.tableData.length === ids.length && this.page > 1) {
      //     this.page--
      //   }
      //   this.deleteVisible = false
      //   this.getTableData()
      // }
    },
    async updateCustomer(row) {
      const res = await getExaCustomer({ ID: row.ID })
      this.type = 'update'
      if (res.code === 0) {
        this.form = res.data.customer
        this.dialogFormVisible = true
      }
    },
    closeDialog() {
      this.dialogFormVisible = false
      this.form = {
        customerName: '',
        customerPhoneData: ''
      }
    },
    async deleteCustomer(row) {
      row.visible = false
      const res = await deleteExaCustomer({ ID: row.ID })
      if (res.code === 0) {
        this.$message({
          type: 'success',
          message: '删除成功'
        })
        if (this.tableData.length === 1 && this.page > 1) {
          this.page--
        }
        this.getTableData()
      }
    },
    async enterDialog() {
      let res
      switch (this.type) {
        case 'create':
          res = await createExaCustomer(this.form)
          break
        case 'update':
          res = await updateExaCustomer(this.form)
          break
        default:
          res = await createExaCustomer(this.form)
          break
      }

      if (res.code === 0) {
        this.closeDialog()
        this.getTableData()
      }
    },
    openDialog() {
      this.type = 'create'
      this.dialogFormVisible = true
    }
  }
}
</script>

<style></style>
