<template>
  <div class="SignatureManagement">
    <div class="header">
      <el-form :inline="true">
        <el-button class="btn" type="primary" @click="onEdit()">修改</el-button>
        <el-form-item>
          <el-select v-model="page.parentId" placeholder="单位名称选择">
            <el-option label="全部单位名称" value="" />
            <el-option label="短信服务中心" value="66" />
            <el-option label="市场部" value="96" />
            <el-option label="博物馆" value="99" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="page.smsSigHandle" placeholder="全部签名方式">
            <el-option label="全部签名方式" value="" />
            <el-option label="不做任何处理，直接转发" value="1" />
            <el-option label="追加签名（前置）" value="2" />
            <el-option label="在前或者在末尾校验签名" value="3" />
            <el-option label="在前或者在末尾校验签名，且如在末，强制转到前" value="4" />
            <el-option label="在前或者在末尾校验签名，且如在前，强制转到末" value="5" />
            <el-option label="在前或者在末尾校验签名，在追加签名（前置）" value="6" />
          </el-select>
        </el-form-item>
        <el-button type="primary" @click="onSearch()">查询</el-button>
      </el-form>
    </div>
    <Table v-show="flag" :data="tableData" :list="tableList" :total="total" :loading="loading" @sizeChange="onSizeChange" @currentChange="onCurrentChange" @selectionChange="onSelectionChange">
      <template #type="data">
        <div v-show="data.item = 'smsSigHandle'">
          <span v-show="data.scope.smsSigHandle === 1">不做任何处理，直接转发</span>
          <span v-show="data.scope.smsSigHandle === 2">追加签名（前置）</span>
          <span v-show="data.scope.smsSigHandle === 3">在前或者在末尾校验签名</span>
          <span v-show="data.scope.smsSigHandle === 4">在前或者在末尾校验签名，且如在末，强制转换到前</span>
          <span v-show="data.scope.smsSigHandle === 5">在前或者在末尾校验签名，且如在前，强制转到末</span>
          <span v-show="data.scope.smsSigHandle === 6">在前或者在末尾校验签名，在追加签名（前置）</span>
        </div>
      </template>
    </Table>
    <el-dialog
      title="修改"
      :visible.sync="dialogVisible"
      :before-close="handleClose"
      @open="onOpen"
    >
      <el-form>
        <el-form-item label="单位">
          <el-input v-model="edit.unitName" :disabled="true" />
        </el-form-item>
        <el-form-item label="短信账户">
          <el-input v-model="edit.apiName" :disabled="true" />
        </el-form-item>

        <div class="interfaceType">
          <label class="tag tagFormItem">签名处理</label>
          <el-form-item class="formItem" prop="apitype">
            <el-select v-model="edit.smsSigHandle">
              <el-option v-for="item in smsSigHandleList" :key="item.id" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <label v-show="edit.smsSigHandle !== 1" class="tag">{{ edit.smsSigHandle === 2 ? '追加签名' : '校验签名' }}</label>
          <el-form-item v-show="edit.smsSigHandle === 2" class="formItem">
            <el-input v-model="edit.smsAddSign" />
          </el-form-item>
          <el-form-item v-show="edit.smsSigHandle > 2" class="formItem">
            <el-input v-model="edit.smssigncontent" />
          </el-form-item>
        </div>
        <el-form-item v-show="edit.smsSigHandle === 6" label="追加签名">
          <el-input v-model="edit.smsAddSign" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onChangeEdit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { custCustApiListAPI, custCustApiUpdateAPI } from '@/api/Account/SignatureManagement'
import Table from '@/components/Table/index.vue'

export default {
  name: 'SignatureManagement',

  components: {
    Table
  },

  data() {
    return {
      page: {
        limit: 10,
        offset: 0,
        smsSigHandle: '',
        parentId: ''
      },
      total: 0,

      edit: {},

      tableData: [],
      tableList: [
        {
          id: 1,
          label: '序号',
          prop: 'uid',
          flag: false
        },
        {
          id: 2,
          label: '单位',
          prop: 'unitName',
          flag: false
        },
        {
          id: 3,
          label: '单位',
          prop: 'apiName',
          flag: false
        },
        {
          id: 4,
          label: '签名方式',
          prop: 'smsSigHandle',
          flag: true
        },
        {
          id: 5,
          label: '校验签名',
          prop: '',
          flag: false
        },
        {
          id: 6,
          label: '追加签名',
          prop: '',
          flag: false
        },
        {
          id: 7,
          label: '创建时间',
          prop: 'createtime',
          flag: false
        }
      ],
      tableItem: [],
      smsSigHandleList: [
        {
          id: 1,
          label: '不做任何处理，直接转发',
          value: 1
        },
        {
          id: 2,
          label: '追加签名（前置）',
          value: 2
        },
        {
          id: 3,
          label: '在前或者在末尾校验签名',
          value: 3
        },
        {
          id: 4,
          label: '在前或者在末尾校验签名，且如在末，强制转到前',
          value: 4
        },
        {
          id: 5,
          label: '在前或者在末尾校验签名，且如在前，强制转到末',
          value: 5
        },
        {
          id: 6,
          label: '在前或者在末尾校验签名，在追加签名（前置）',
          value: 6
        }
      ],

      flag: false,
      loading: false,
      dialogVisible: false
    }
  },

  async created() {
    this.getCustCustApiList()
    this.flag = true
  },

  methods: {
    async getCustCustApiList() {
      this.loading = true
      const res = await custCustApiListAPI(this.page)
      this.tableData = res.rows.map((item, index) => {
        return { ...item, uid: index + 1 }
      })
      this.total = res.total
      this.loading = false
    },
    onSearch() {
      this.getCustCustApiList(this.page)
    },
    handleClose() {
      this.dialogVisible = false
    },
    onSelectionChange(value) {
      this.tableItem = value
    },
    onEdit() {
      if (this.tableItem.length < 1 || this.tableItem.length > 1) {
        return this.$message({
          message: '只能选中一项哦',
          type: 'warning'
        })
      }
      this.dialogVisible = true
    },
    onOpen() {
      this.edit = this.tableItem[0]
      console.log(this.edit)
    },
    async onChangeEdit() {
      await custCustApiUpdateAPI(this.edit)
      this.dialogVisible = false
      this.getCustCustApiList()
    },
    onCurrentChange(value) {
      this.page.offset = value
      this.getCustCustApiList()
    },
    onSizeChange(value) {
      this.page.limit = value
      this.getCustCustApiList()
    }
  }
}
</script>

<style lang="scss" scoped>
.tag {
  display: inline-block;
  width: 100px;
  text-align: center;
}

::v-deep .formItem {
  display: inline-block;
  margin-right: 30px;
}
::v-deep .btn {
  margin-right: 20px;
}
</style>
