<!--
  @description: 短信配置
  @Modifier: 苏志刚
  @routerPath: /systemManagement/sms
  @date: 2020/8/18 13:45
-->
<template>
  <div>
    <!--标题-->
    <div class="title title_border p0-16">
      <i class="el-icon-s-home" />短信配置
    </div>
    <section class="p0-16">
      <!--查询条件-->
      <div class="crud_menu_title clearfix">
        <span class="icon-line fl">查询条件</span>
        <span class="fr pointer" @click="handleToggleSearch">
          <el-link
            :underline="false"
            :class="toggleSearchStatus?'arrow-is-active':''"
            type="primary"
            class="toggleSearchStatus icon el-icon-arrow-down"
          />
          {{ toggleSearchStatus?'收起高级查询' :'展开高级查询' }}
        </span>
      </div>
      <section :class="{ SearchContent_show: toggleSearchStatus }" class="clearfix SearchContent">
        <div class="SearchContent_leftInput fl">
          <el-form ref="searchForm" :model="searchForm" inline>
            <!-- 资源编号 -->
            <el-form-item ref="smsCode" label="资源编号" prop="smsCode">
              <el-input v-model="searchForm.smsCode" placeholder="请输入资源编号" />
            </el-form-item>
            <!--分类-->
            <el-form-item ref="category" label="分类" prop="category">
              <el-select v-model="searchForm.category" placeholder="请选择">
                <el-option
                  v-for="item in categoryList"
                  :key="item.typecode"
                  :label="item.typename"
                  :value="item.typecode"
                />
              </el-select>
            </el-form-item>
            <!--使用状态-->
            <el-form-item ref="useStatus" label="使用状态" prop="useStatus">
              <el-select v-model="searchForm.useStatus" placeholder="请选择">
                <el-option
                  v-for="item in useStatusList"
                  :key="item.typecode"
                  :label="item.typename"
                  :value="item.typecode"
                />
              </el-select>
            </el-form-item>
          </el-form>
        </div>
        <div class="fr">
          <el-button type="primary" size="small" @click="onSearch">
            搜 索
          </el-button>
          <el-button plain type="info" size="small" @click="resetForm('searchForm')">
            重 置
          </el-button>
        </div>
      </section>
      <!--查询结果-->
      <div class="crud_menu_title">
        <span class="icon-line">查询结果</span>
      </div>
      <div class="mb10">
        <el-button type="primary" plain @click="add">
          新 增
        </el-button>
      </div>
      <section>
        <el-table ref="smallTable" v-loading="loading" :data="tableData">
          <el-table-column type="index" label="序号" width="60px" align="center" />
          <!-- 分类 -->
          <el-table-column
            header-align="center"
            label="分类"
            prop="category"
            align="center"
            min-width="200"
          >
            <template slot-scope="scope">
              <span
                v-for="(item, index) in categoryList"
                v-if="scope.row.category === item.typecode"
                :key="index"
              >{{ item.typename }}</span>
            </template>
          </el-table-column>
          <!-- 资源编号 template-->
          <el-table-column prop="smsCode" header-align="center" align="left" label="资源编号" width="300" />
          <!-- 资源地址-->
          <el-table-column prop="resourceAddress" header-align="center" align="left" label="资源地址" width="300" />
          <!-- 短信签名-->
          <el-table-column prop="signName" header-align="center" align="left" label="短信签名" width="200" />
          <!-- 使用状态 -->
          <el-table-column
            header-align="center"
            prop="useStatus"
            label="使用状态"
            align="center"
            min-width="150"
          >
            <template slot-scope="scope">
              <span
                v-for="(item, index) in useStatusList"
                v-if="scope.row.useStatus === item.typecode"
                :key="index"
              >{{ item.typename }}</span>
            </template>
          </el-table-column>
          <el-table-column
            header-align="center"
            label="操作"
            align="center"
            min-width="200"
          >
            <template slot-scope="scope">
              <el-link type="primary" @click.stop="edit(scope.row)">
                [编辑]
              </el-link>
              <el-link type="primary" @click.stop="view(scope.row)">
                [查看]
              </el-link>
              <el-link type="primary" @click.stop="del(scope.row)">
                [删除]
              </el-link>
              <el-link type="primary" @click.stop="handleFreeze(scope.row)">
                {{ scope.row.useStatus==='00'?'[启用]':'[停用]' }}
              </el-link>
            </template>
          </el-table-column>
        </el-table>
        <!--分页组件-->
        <pagination :pagination-data="paginationData" @change="loadData" />
        <!-- 新增短信配置/编辑短信配置弹框 -->
        <smsAdd
          v-if="dialogDataAdd.dialog"
          :dialog-data="dialogDataAdd"
          @watchChild="watchChild"
        />
        <!-- 查看短信配置详情弹框 -->
        <smsDetail v-if="dialogDataDetail.dialog" :dialog-data="dialogDataDetail" />
      </section>
    </section>
  </div>
</template>
<script>
/* 接口*/
import {
  querySmsConfigPage,
  deleteSmsConfig,
  updateSmsConfigUserStatusById
} from '@/api/procurement/procurementCommonTools.js' // 短信配置模块接口文件
/* 公用方法*/
import { filterObj } from '@/assets/js/utils' // 常用方法1
/* 混合开发方法-参数字段*/
import { ListMixin } from '@/mixins/ListMixin'
import { procurementSmsMaintainGrounpMixins } from '../mixins/procurementSmsMaintainGrounpMixins'
/* 组件*/
import pagination from '@comp/common/pagination/pagination' // 分页
import smsAdd from './procurementSmsMaintainDeployAdd-dialog' // 新增短信配置/编辑短信配置弹框
import smsDetail from './procurementSmsMaintainDeployDetail-dialog' // 查看短信配置详情弹框
export default {
  name: 'SmsList',
  components: {
    pagination, // 分页
    smsAdd, // 新增短信配置/编辑短信配置
    smsDetail // 短信配置详情
  },
  mixins: [ListMixin, procurementSmsMaintainGrounpMixins],
  data() {
    return {
      loading: true,
      // 搜索
      searchForm: {
        useStatus: '', // 使用状态 00停用；01启用
        smsCode: '', // 资源编号
        category: '' // 分类 配置字典表 00启瑞云 01阿里云 02七牛云 03腾讯云 04云片 （目前只对接了启瑞云）
      },
      tableData: [],
      dialogDataAdd: {
        dialog: false, // 新增短信配置/编辑短信配置弹框
        type: '00' // 00新增；01编辑
      },
      dialogDataDetail: {
        dialog: false // 查看短信配置详情弹框
      }
    }
  },
  created() {
    // 获取列表信息
    this.loadData()
  },
  methods: {
    // 获取数据
    loadData() {
      const params = this.getQueryParams() // 查询条件
      querySmsConfigPage(params)
        .then(response => {
          this.loading = false
          this.tableData = response.data.records
          this.paginationData.total = response.data.total
        })
        .catch(() => {})
    },
    // 查询条件
    getQueryParams() {
      const param = Object.assign({}, this.searchForm)
      param.pageNo = this.paginationData.pageNo
      param.pageSize = this.paginationData.pageSize
      return filterObj(param)
    },
    // 编辑
    edit(row) {
      this.dialogDataAdd = {
        dialog: true,
        type: '01',
        id: row.id
      }
    },
    // 查看
    view(row) {
      this.dialogDataDetail.dialog = true
      this.dialogDataDetail.detailObj = row.id
    },
    // 搜索
    onSearch() {
      this.paginationData.pageNo = 1
      this.paginationData.pageSize = 10
      // 获取列表信息
      this.loadData()
    },
    // 重置
    resetForm(formName) {
      this.paginationData.pageNo = 1
      this.paginationData.pageSize = 10
      this.$refs[formName].resetFields()
      // 获取列表信息
      this.loadData()
    },
    // 新增
    add() {
      this.dialogDataAdd = {
        dialog: true,
        type: '00'
      }
    },
    // 删除
    del(row) {
      this.$confirm('确定将选择数据删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn',
        type: 'warning'
      }).then(() => {
        deleteSmsConfig(row.id).then(res => {
          if (res.header.ret === '0') {
            this.$message({
              type: 'success',
              message: '删除成功'
            })
            this.loadData()
          }
        })
      })
    },
    // 停用启用事件
    handleFreeze(row) {
      if (row.useStatus === '00') {
        this.$confirm('是否确定启用该条配置？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          confirmButtonClass: 'confirmBtn',
          cancelButtonClass: 'cancelBtn',
          type: 'warning'
        }).then(() => {
          updateSmsConfigUserStatusById({ id: row.id, userStatus: '01' }).then(res => {
            if (res.header.ret === '0') {
              this.$message({
                type: 'success',
                message: '启用成功'
              })
              this.loadData()
            }
          })
        })
      }
      if (row.useStatus === '01') {
        this.$confirm('是否确定停用该条配置？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          confirmButtonClass: 'confirmBtn',
          cancelButtonClass: 'cancelBtn',
          type: 'warning'
        }).then(() => {
          updateSmsConfigUserStatusById({ id: row.id, userStatus: '00' }).then(res => {
            if (res.header.ret === '0') {
              this.$message({
                type: 'success',
                message: '停用成功'
              })
              this.loadData()
            }
          })
        })
      }
    },
    // 监听子组件
    watchChild(val) {
      if (val) {
        this.loadData()
      }
    }
  }
}
</script>
<style lang="less" scoped>
.SearchContent_leftInput {
  width: calc(100% - 300px) !important;
}
</style>
