<template>
  <div class="app-container">
    <el-form v-model="pagerDto" inline>
      <el-form-item label="规则名称">
        <el-input v-model="pagerDto._Eq_Name" clearable />
      </el-form-item>
      <el-button icon="el-icon-search" type="primary" @click="searchData">搜索</el-button>
      <el-button
        icon="el-icon-circle-plus-outline"
        type="success"
        @click="showAddPanel"
      >新增</el-button>
    </el-form>
    <div>
      <el-table v-loading="loading" :data="sourceData" border>
        <el-table-column prop="Name" label="申报规则名称" />
        <el-table-column prop="EnabledText" label="是否启用" />
        <el-table-column prop="HandleTypeText" label="违规时处理方式">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.HandleType == 0" type="danger">提示错误</el-tag>
            <el-tag v-else type="warning">自动修改</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="CreateUserName" label="创建人" />
        <el-table-column prop="CreateTime" label="创建时间" />
        <el-table-column label="操作" width="400">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="showEffectivedChannels(scope.row.Id)"
            >查看作用渠道</el-button>
            <el-button size="mini" type="success">
              <router-link
                :to="{ name: 'ChannelDeclareRuleDetail', query: { Id: scope.row.Id } }"
              >管理规则明细</router-link>
            </el-button>
            <el-button
              size="mini"
              type="primary"
              @click="edit(scope.row)"
            >编辑</el-button>
            <el-button
              type="danger"
              size="mini"
              plain
              @click="remove(scope.row)"
            >移除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div>
      <el-pagination
        :page-sizes="[100, 200, 300, 400]"
        :current-page="pagerDto.PageIndex"
        :page-size="pagerDto.PageSize"
        background
        layout="prev, pager, next ,total ,jumper,sizes,slot "
        :total="pagerDto.total"
        @current-change="pagerSerach"
      />
    </div>
    <el-dialog :visible.sync="showChannelPannel" title="作用渠道">
      <div v-loading="loadingChannelNames">
        <el-tag v-for="name in ChannelNames" :key="name" style="margin: 5px">{{
          name
        }}</el-tag>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="showEditPannel" @open="loadChannels">
      <el-form
        ref="editForm"
        v-loading="submiting"
        label-width="8em"
        :model="editItem"
        :rules="rules"
      >
        <el-form-item label="渠道规则名称" prop="Name">
          <el-input v-model="editItem.Name" />
        </el-form-item>

        <el-form-item label="作用渠道" prop="Channels">
          <el-select v-model="editItem.Channels" multiple filterable>
            <el-option
              v-for="item in channels"
              :key="item.Id"
              :label="item.Name"
              :value="item.Id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="违规时处理方式" prop="HandleType">
          <el-select v-model="editItem.HandleType">
            <el-option
              v-for="item in handleTypes"
              :key="item.Id"
              :label="item.Name"
              :value="item.Id"
            />
          </el-select>

        </el-form-item>
        <el-form-item label="是否启用" prop="Enabled">
          <el-switch v-model="editItem.Enabled" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="editItem.Remark" />
        </el-form-item>
        <el-form-item style="text-align: center">
          <el-button size="large" type="success" @click="validtAndSubmit">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import request from '@/utils/request'
import { _getDeliverChannel } from '@/api/baseInfo'

export default {
  name: 'ChannelDeclareRule',
  data() {
    return {
      loading: false,
      loadingChannelNames: false,
      submiting: false,
      showEditPannel: false,
      showChannelPannel: false,
      editMode: '',
      channels: [],
      ChannelNames: [],
      handleTypes: [{ Id: 0, Name: '提示错误' }, { Id: 1, Name: '自动修改' }],
      editItem: {
        Channels: [],
        HandleType: 0,
        Name: '',
        Enabled: '',
        Remark: null
      },
      rules: {
        Name: { required: true, message: '请填写规则名称', trigger: 'blur' }
      },

      sourceData: [],
      pagerDto: {
        _Eq_Name: null,
        PageIndex: 1,
        PageSize: 100,
        total: 0
      }
    }
  },
  created() {
    _getDeliverChannel().then((resp) => {
      this.channels = resp
    })
    this.pagerDto.PageIndex = 1
    this.pagerDto._Eq_ChannelId = ''
    this.searchData()
  },
  methods: {
    showEffectivedChannels(id) {
      this.showChannelPannel = true
      this.loadingChannelNames = true
      request({
        url: '/ChannelDeclareRule/GetEffectivedChannelNames',
        params: { id: id },
        method: 'post'
      })
        .then((resp) => {
          this.ChannelNames = resp
        })
        .finally(() => {
          this.loadingChannelNames = false
        })
    },
    loadChannels() {
      this.submiting = true
      request({
        url: '/ChannelDeclareRule/GetEffectivedChannels',
        params: { id: this.editItem.Id },
        method: 'post'
      })
        .then((resp) => {
          this.editItem.Channels = resp
        })
        .finally(() => {
          this.submiting = false
        })
    },
    searchData() {
      this.loading = true
      request({
        url: '/ChannelDeclareRule/query',
        method: 'post',
        data: this.pagerDto
      })
        .then((resp) => {
          this.loading = false
          this.pagerDto.total = resp.TotalCount
          this.pagerDto.PageIndex = resp.PageIndex

          this.sourceData = resp.Result
        })
        .finally(() => {
          this.loading = false
        })
    },

    edit(item) {
      this.initEditItem()
      this.editMode = 'edit'
      this.editItem.Id = item.Id
      this.editItem.Name = item.Name
      this.editItem.HandleType = item.HandleType
      this.editItem.Enabled = item.Enabled
      this.editItem.Remark = item.Remark
      this.showEditPannel = true
    },
    remove(item) {
      this.$confirm('是否删除该申报价值规则?', '正在删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        request({
          url: '/ChannelDeclareRule/remove',
          params: { id: item.Id },
          method: 'post'
        })
          .then((resp) => {
            if (resp === true) {
              const idx = this.sourceData.findIndex((i) => i.Id === item.Id)
              this.sourceData.splice(idx, 1)
            } else {
              const msg = resp.message
              this.$message(msg)
            }
          })
          .catch(() => {
            this.$message('删除出错')
          })
          .finally(() => {
            this.loading = false
          })
      })
    },
    pagerSerach(p) {
      this.pagerDto.PageIndex = p
      this.searchData()
    },
    initEditItem() {
      this.editItem.Id = 0
      this.editItem.Name = ''
      this.editItem.HandleType = 0
      this.editItem.Channels = []
      this.editItem.Enabled = true
      this.editItem.Remark = ''
    },
    showAddPanel() {
      this.editMode = 'add'
      this.initEditItem()
      this.showEditPannel = true
    },
    submit() {
      let url = '/ChannelDeclareRule/create'
      let op = '新增'
      if (this.editMode === 'edit') {
        url = '/ChannelDeclareRule/modify'
        op = '修改'
      }
      this.submiting = true
      request({
        url: url,
        data: this.editItem,
        method: 'post'
      })
        .then((resp) => {
          if (resp.success === true) {
            this.showEditPannel = false
            this.initEditItem()
            this.$alert(`${op}成功`, `${op}成功`, {
              confirmButtonText: '确定' })
            this.searchData()
          } else {
            const msg = resp.message
            this.$message(msg)
          }
        })
        .finally(() => {
          this.submiting = false
        })
    },
    validtAndSubmit() {
      this.$refs['editForm'].validate((valid) => {
        if (valid) {
          this.submit()
        } else {
          return false
        }
      })
    }
  }
}
</script>
<style scoped></style>
