<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="search.keyWord" placeholder="关键字" style="width: 200px;" class="filter-item" />
        <el-select v-model="search.businessModule" filterable placeholder="请选择">
          <el-option
            v-for="item in searchModuleOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="searchData">
        搜索
      </el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-plus"
        @click="handleCreateAdd">
        增加业务脚本
      </el-button>
      <div class="top-right-btn">
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button size="mini" circle icon="el-icon-refresh" @click="getScriptInfo" />
        </el-tooltip>
      </div>
    </div>
    <el-table
      :data="tableData"
      v-loading="listLoading"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      default-expand-all>
      <el-table-column
        prop="businessName"
        label="业务名称">
      </el-table-column>
     <el-table-column
        label="业务代码"
        prop="businessCode"
      >
       <template slot-scope="{row}">
         <el-link type="primary" @click="$router.push({path:'/baseInfo/scriptCode',query:{businessCode:row.businessCode,codeId:row.id}})">{{row.businessCode}}</el-link>
       </template>
      </el-table-column>
      <el-table-column
        label="脚本模块"
        prop="businessModule"
      >
        <template slot-scope="{row}">
         <span>{{getModuleName(row.businessModule)}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="状态" class-name="status-col" width="100">
        <template slot-scope="{row}">
          <el-tag v-if="row.voidFlag==0" type="success">
            启用
          </el-tag>
          <el-tag v-else="row.voidFlag==1" type="danger">
            禁用
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="left" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button @click="handleUpdate(row)" type="primary" size="mini">
            编辑
          </el-button>
          <el-button @click="editStatus(row,'1')" type="danger" v-if="row.voidFlag==0" size="mini">
            禁用
          </el-button>
          <el-button @click="editStatus(row,'0')" v-else="row.voidFlag==1" size="mini">
            启用
          </el-button>

        </template>
      </el-table-column>

    </el-table>
    <pagination v-show="listData.total>0" :total="listData.total" :page.sync="listData.page" :limit.sync="listData.pageSize" @pagination="getScriptInfo" />

    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="500px">
      <el-form label-width="100px" label-position="right">
        <el-form-item label="业务名称">
          <el-input v-model="form.businessName"></el-input>
        </el-form-item>
        <el-form-item label="模块选择">
          <el-select v-model="form.businessModule" filterable placeholder="请选择">
            <el-option
              v-for="item in moduleOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="业务代码">
          <el-input :disabled="dialogStatus!=1" v-model="form.businessCode"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogStatus==='1'?addBtn():editBtn()">确 定</el-button>
      </div>
    </el-dialog>
<!--    <servicedialog @tableRefresh="tableRefresh" ref="menuDialog" :dialogStatus="dialogStatus" :menuId="menuId" :titleDialog="titleDialog" :dialogVisible="dialogVisible"/>-->
    <cancledialog @tableRefresh="tableRefresh" ref="confirmDialog"></cancledialog>
  </div>
</template>

<script>
import { addBusinessScript, getScriptList, getBusinessScriptInfoById, updateBusinessScript, getScriptModuleList } from '@/api/baseInfo/businessScript/businessScript.js'
import scriptCode from './scriptCode'
import Cancledialog from '@/components/Dialog/Cancledialog.vue'

const form = {
  businessName: '',
  businessCode: '',
  businessModule: ''
}
export default {
  name: 'BusinessScriptList',
  components: {
    Cancledialog, scriptCode
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        1: 'success',
        0: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      dialogStatus: 1, // 1增加，2修改
      dialogTitle: '',
      dialogVisible: false,
      search: {
        keyWord: '',
        businessModule: -1
      },
      form: Object.assign({}, form),
      listData: {
        total: 0,
        page: 1,
        pageSize: 10
      },
      options: [],
      moduleOptions: [],
      searchModuleOptions: [],

      confirmdialog: false,
      listLoading: false,
      tableData: [],
      menuId: ''
    }
  },
  mounted() {
    this.getModuleList()
    this.getScriptInfo()
  },
  methods: {
    // 获取列表
    getScriptInfo() {
      const _that = this
      _that.listLoading = true
      var params = _that.search
      var page = {
        pageIndex: _that.listData.page,
        pageSize: _that.listData.pageSize
      }
      var data = {
        'params': JSON.stringify(params),
        'page': JSON.stringify(page)
      }
      // console.log(data);
      getScriptList(data).then(response => {
        console.log(response)
        const data = response.data
        // //  组合表单数据
        _that.tableData = data

        _that.listData.total = response.total
        // console.log(_that.tableData)
        _that.listLoading = false
      }).catch(err => {
        _that.listLoading = false
      })
    },

    // 获取脚本模块列表
    getModuleList() {
      const _that = this
      var data = {
      }
      // console.log(data);
      getScriptModuleList(data).then(response => {
        console.log(response)
        const data = response.data
        _that.moduleOptions = data
        var options = JSON.parse(JSON.stringify(data))
        var all = {
          name: '全部',
          id: -1
        }
        options.unshift(all)
        _that.searchModuleOptions = options
      }).catch(err => {
        console.log(err);
      })
    },

    // 获取模块名称
    getModuleName(id) {
      const _that = this
      var obj = _that.moduleOptions.find(item => item.id == id)
      // console.log(obj);
      return obj.name
    },

    // 增加业务脚本
    handleCreateAdd() {
      // 增加系统目录
      const _that = this
      _that.form = Object.assign({}, form)
      _that.dialogTitle = '增加业务脚本'
      _that.dialogStatus = '1'
      _that.dialogVisible = true
      _that.getModuleList()
    },
    // 刷新变革
    tableRefresh() {
      // 刷新变革
      this.getScriptInfo()
    },
    // 增加业务脚本
    addBtn() {
      const _that = this
      var infoType = JSON.stringify(_that.form)
      var data = {
        'script': infoType
      }
      // console.log(data);
      // return

      addBusinessScript(data).then(res => {
        // console.log(res);
        if (res.httpCode == 200) {
          _that.dialogVisible = false
          _that.$message({
            message: '增加成功',
            type: 'success'
          })
          _that.tableRefresh()
        } else {
          _that.$message.error(res.msg)
        }
      }).catch(err => {
        _that.$message.error(err)
        _that.dialogVisible = true
      })
    },

    // 唤起修改
    handleUpdate(row) {
      const _that = this
      _that.form = Object.assign({}, form)

      _that.getModuleList()
      _that.dialogTitle = '修改业务脚本'
      _that.dialogVisible = true
      _that.dialogStatus = '2'

      var idPrint = row.businessCode
      var data = {
        businessCode: idPrint
      }

      getBusinessScriptInfoById(data).then(res => {
        // console.log(res);
        const data = res.data

        _that.form = {
          id: data.id,
          businessName: data.businessName,
          businessCode: data.businessCode,
          businessModule: data.businessModule
        }
      }).catch(err => {
        _that.$message.error(err)
      })
    },
    // 修改业务脚本
    editBtn() {
      const _that = this
      var infoType = JSON.stringify(_that.form)
      var data = {
        'script': infoType
      }

      updateBusinessScript(data).then(res => {
        // console.log(res);
        if (res.httpCode == 200) {
          _that.dialogVisible = false
          _that.$message({
            message: '修改成功',
            type: 'success'
          })
          _that.tableRefresh()
        } else {
          _that.$message.error(res.msg)
        }
      }).catch(err => {
        _that.$message.error(err)
        _that.dialogVisible = true
      })
    },

    // 启用禁用
    editStatus(row, status) {
      var _that = this
      var printerTypeInfo = {
        id: row.id,
        voidFlag: status,
        businessCode: row.businessCode
      }
      var params = {
        'script': JSON.stringify(printerTypeInfo)
      }

      var url = '/doc/voidBusinessScript'
      _that.$refs.confirmDialog.updataClick(params, url)
    },

    // 搜索
    searchData() {
      this.listData.page = 1
      this.getScriptInfo()
    }

  }
}
</script>

<style lang="scss" scoped>
  .filter-container {
    margin-bottom: 15px;
  }
</style>
