<template>
  <div class="tableBox">
    <el-form
      class="searchBox"
      ref="form"
      :model="form"
      :inline="true"
      label-width="140px"
    >
      <div class="block-title">
        查询条件
      </div>
      <el-form-item
        label="昵称"
        prop="loginName">
        <el-input v-model="params.loginName"></el-input>
      </el-form-item>
      <el-form-item
        label="手机号"
        prop="phone">
        <el-input v-model="params.phone"></el-input>
      </el-form-item>

      <div class="btn-wrap">
        <el-button
          @click="reset"
        >
          重置
        </el-button>
        <el-button
          type="primary"
          @click="getAccountList"
        >
          搜索
        </el-button>
      </div>
    </el-form>
    <div
      class="btn-wrap-left"
      style="padding:10px 24px">
      <el-button
        type="primary"
        size="mini"
        @click="showDialog('add')"
      >
        新增账号
      </el-button>
    </div>
    <div class="block-title">
      账号列表
    </div>
    <el-table
      border
      stripe
      v-loading="loading"
      :data="tableData"
    >
      <el-table-column
        prop="id"
        label="ID"
      >
      </el-table-column>
      <el-table-column
        prop="loginName"
        label="账号名"
      >
      </el-table-column>
      <el-table-column
        prop="nick_name"
        label="昵称"
      >
      </el-table-column>
      <el-table-column
        prop="roles"
        label="角色"
      >
        <template slot-scope="scope">
          <el-tag
            v-for="(item,index) in scope.row.roleList"
            :key="index"
          >
            {{ item.name }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="creator"
        label="创建人">
      </el-table-column>
      <el-table-column
        label="上次登录时间"
        width="140"
      >
        <template slot-scope="scope">
          {{ scope.row.lastLoginTime | formatTime }}
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        width="140"
      >
        <template slot-scope="scope">
          {{ scope.row.createTime | formatTime }}
        </template>
      </el-table-column>
      <el-table-column
        label="过期时间"
        width="140"
      >
        <template slot-scope="scope">
          {{ scope.row.use_end_time | formatTime }}
        </template>
      </el-table-column>
      <el-table-column
        prop="phone"
        label="电话"
      >
      </el-table-column>
      <!-- 雷锋纪念馆定制 -->
      <el-table-column
        width="140"
        prop="work_status"
        label="工作状态"
        v-if="projectName === 'leifengjinianguan'"
      >
        <template slot-scope="scope">
          <el-switch
            @change="handleChange(scope.row)"
            v-model="scope.row.work_status"
            active-text="值班"
            inactive-text="休息"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        width="140"
        prop="on_duty"
        label="当前值日生"
        v-if="projectName === 'leifengjinianguan'"
      >
        <template slot-scope="scope">
          <el-switch
            @change="handleChange2(scope.row)"
            v-model="scope.row.on_duty"
            active-text="是"
            inactive-text="否"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
          >
          </el-switch>
        </template>
      </el-table-column>
      <!-- <el-table-column
        width="120"
        label="状态">
        <template slot-scope="scope">
          <span>{{ scope.row.is_use ? '已激活' : '已禁用' }}</span>
          <el-button
            type="text"
            size="small"
            @click="offSale(scope.row)"
            class="offSale-btn"
            v-if="scope.row.is_use"
          >
            <i class="iconfont icon-offSale"></i>禁用
          </el-button>
          <el-button
            type="text"
            size="small"
            @click="onSale(scope.row)"
            class="onSale-btn"
            v-else
          >
            <i class="iconfont icon-onSale"></i>激活
          </el-button>
        </template>
      </el-table-column> -->
      <el-table-column
        fixed="right"
        label="操作"
        width="120"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="showDialog('edit',scope.row)"
          >
            编辑
          </el-button>
          <el-button
            type="text"
            size="small"
            @click="deleteAccount(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <ElDialog
      :title="dialogType === 'add' ? '新增账户' : '编辑账户'"
      width="660px"
      v-model="dialogVisible">
      <EditDialog
        :scenic-list="scenicList"
        :dialog-type="dialogType"
        :account-item="curentItem"
        @updateList="handleUpdate"
      ></EditDialog>
    </ElDialog>
    <el-pagination
      background
      :page-size="10"
      @current-change="handlePage"
      layout="prev, pager, next"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
import { getScenic } from '@/api/ticketType'
import { getAccountList, deleteAccount, updateAccount } from '@/api/systemSetting/account'
import ElDialog from '@/components/Dialog'
import EditDialog from './account/EditDialog'
import moment from 'moment'
export default {
  components: {
    ElDialog,
    EditDialog
  },
  created () {
    this.getAccountList()
    this.getScenicList()
  },
  data () {
    return {
      projectName: process.env.VUE_APP_PROJECT,
      loading: false,
      dialogType: 'add',
      dialogVisible: false,
      tableData: [],
      scenicList: [],
      total: 0,
      form: {
        loginName: '',
        phone: ''
      },
      params: {
        export: false,
        pageNum: 1,
        pageSize: 10,
        loginName: '',
        name: '',
        phone: ''
      },
      // 当前账号对象
      curentItem: {}
    }
  },
  methods: {
    reset () {
      this.params.loginName = ''
      this.params.phone = ''
    },
    showDialog (type, item) {
      this.dialogType = type
      this.curentItem = item
      this.dialogVisible = true
    },
    getScenicList () {
      getScenic().then(res => {
        this.scenicList = res?.data.children.map(item => item.data)
      })
    },
    getAccountList () {
      this.loading = true
      getAccountList(this.params).then(res => {
        this.tableData = res?.data?.records || []
        this.total = res?.data?.total || 0
      }).finally(() => {
        this.loading = false
      })
    },
    deleteAccount (id) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteAccount(id).then(res => {
          this.$message.success('删除成功')
          this.getAccountList()
        })
      })
    },
    handlePage (pageNum) {
      this.params.pageNum = pageNum
      this.getAccountList()
    },
    offSale (data) {
      let form = Object.assign({}, data)
      form.is_use = 0
      updateAccount(form).then(res => {
        this.$message.success('操作成功')
        this.getAccountList()
      })
    },
    onSale (data) {
      if (moment().isAfter(data.use_end_time)) {
        this.$alert('请先修改账号过期时间', '账号已过期', {
          confirmButtonText: '知道了'
        })
      } else {
        let form = Object.assign({}, data)
        form.is_use = 1
        updateAccount(form).then(res => {
          this.$message.success('操作成功')
          this.getAccountList()
        })
      }
    },
    handleChange (data) {
      let form = Object.assign({}, data)
      if (data.work_status) {
        form.work_status = 1
      } else {
        form.work_status = 0
      }
      updateAccount(form).then(res => {
        this.$message.success('操作成功')
        this.getAccountList()
      })
    },
    handleChange2 (data) {
      let form = Object.assign({}, data)
      if (data.on_duty) {
        form.on_duty = 1
      } else {
        form.on_duty = 0
      }
      updateAccount(form).then(res => {
        this.$message.success('操作成功')
        this.getAccountList()
      })
    },
    handleUpdate () {
      this.$store.dispatch('getAccount')
      this.getAccountList()
    }
  }
}
</script>

<style scoped>
.offSale-btn{
  color: #F56C6C
}
.onSale-btn{
  color: #67C23A
}
</style>
