<template>
  <a-card :bordered="false" :style="{height:windowHeight+'px'}">
    <a-row :gutter="24">
      <!-- 左侧字典列表 -->
      <a-col :span="6">
        <a-row :gutter="24">
          <!-- <a-button style="margin-right: 10px;" type="primary" icon="plus" @click="addCode">添加字典</a-button> -->
          <a-button id="dataDictionary-operate-btn" style="margin-right: 10px;" type="primary" icon="plus" @click="editCode">编辑字典</a-button>
          <!-- <a-select style="width: 110px" placeholder="更多操作" allowClear >
               <a-select-option @click="editCode" value="编辑字典">编辑字典</a-select-option>
               <a-select-option @click="delectCode" value="删除字典">删除字典</a-select-option>
             </a-select> -->
        </a-row>
        <a-row :gutter="24" class="textCount">
          <a-icon type="info-circle" style="color: #1890FF;margin-right: 10px;" />
          <span>当前选择：<span style="color: #40a9ff;font-weight: 600;">{{ selectedMenu }}</span><span v-if="selectedKey.length" class="imgSize" @click="cancelSel">取消选择</span></span>
        </a-row>
        <a-row :gutter="24" >
          <a-input-search id="dataDictionary-search" v-model="code" placeholder="输入编码类别/名称 搜索字典" style="width: 100%" @search="getCodeList" />
        </a-row>
        <a-row :gutter="24" >
          <a-menu class="menu-box" style="border-right: 0;max-height: 660px;overflow: auto;" v-model="selectedKey" >
            <a-menu-item id="dataDictionary-menu" style="margin-bottom: 0px;height: 30px; line-height: 30px;" v-for="(item,index) in codeList" :key="index" :value="item">{{ item.name }}</a-menu-item>
          </a-menu>
          <span v-if="noData" style="line-height: 50px; font-weight: 600;">暂无数据</span>
        </a-row>
      </a-col>
      <!-- 右侧数据列表 -->
      <a-col :span="18" >
        <div style="padding-left: 20px;">
          <div class="table-page-search-wrapper">
            <a-form layout="inline" id="dataDictionary-form">
              <a-row :gutter="48">
                <a-col :md="6" :sm="24">
                  <a-form-item label="数据名称">
                    <a-input id="dataDictionary-dispName" allowClear v-model.trim="queryParam.dispName" placeholder="请输入数据名称"/>
                  </a-form-item>
                </a-col>

                <a-col :md="6" :sm="24">
                  <a-form-item label="状态">
                    <a-select
                      allowClear
                      id="dataDictionary-isEnable"
                      v-model="queryParam.isEnable"
                      placeholder="请选择"
                    >
                      <a-select-option :value="1" id="dataDictionary-isEnable1">
                        是
                      </a-select-option>
                      <a-select-option :value="0" id="dataDictionary-isEnable0">
                        否
                      </a-select-option>
                    </a-select>
                    <!-- <v-select code="FLAG" v-model="queryParam.enableFlag" allowClear placeholder="是否启用"></v-select> -->
                  </a-form-item>
                </a-col>
                <a-col :md="2" :sm="24">
                  <a-button id="dataDictionary-refresh" type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                </a-col>
                <!-- <a-col :md="2" :sm="24">
                  <a-button type="primary" @click="queryParam=''">重置</a-button>
                </a-col> -->
              </a-row>
            </a-form>
          </div>
          <!-- <a-divider/> 操作按钮-->
          <div class="table-operator">
            <a-button type="primary" id="dataDictionary-add" icon="plus" @click="openCodeItemModal">添加数据</a-button>
            <!-- <a-button type="" icon="delete" @click="delectAll">批量删除</a-button> -->
            <a-button type="" id="dataDictionary-renew" icon="reload" @click="$refs.table.refresh(true)">刷新数据</a-button>
          </div>
          <!-- <div  class="textCount">
            <a-icon type="info-circle" style="color: #1890FF;margin-right: 10px;" />
            <span>已选择<span style="color: #40a9ff;font-weight: 600;margin-left: 10px;">{{selectedNum}}项</span><span class="imgSize">清空</span></span>
          </div> -->
          <!-- 列表 -->
          <s-table
            ref="table"
            size="small"
            rowKey="id"
            :columns="columns"
            :data="loadData"
            :defaultLoadData="false"
            bordered
          >
            <span slot="action" slot-scope="text, record">
              <template>
                <a class="action-button" :id="'dataDictionary-edit-btn'+record.id" @click="handleEdit(record)">修改</a>
                <a v-if="!record.isEnable" :id="'dataDictionary-del-btn'+record.id" class="action-button red-text" @click="delect(record)">删除</a>
              </template>
            </span>
          </s-table>
        </div>
      </a-col>
    </a-row>
    <!-- 添加/编辑数据字典弹窗 -->
    <codeModal :visible="showCodeModal" :data="codeData" @refresh="getCodeList" @close="showCodeModal = false"></codeModal>
    <!-- 添加数据弹窗 -->
    <codeItemModal :visible="showItemCodeModal" :data="itemCodeData" @refresh="$refs.table.refresh(true)" @close="showItemCodeModal = false"></codeItemModal>
  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { STable, VSelect } from '@/components'
import codeModal from './codeModal.vue'
import codeItemModal from './codeItemModal.vue'
// 接口
import { listLookUp, delLookUp, getLookUpItem, delLookUpItem } from '@/api/lookup.js'
export default {
  name: 'PowerDD',
  mixins: [commonMixin],
  components: {
    STable, VSelect, codeModal, codeItemModal
  },
  props: {},
  data () {
    // const rowSelection = {
    //    onChange: (selectedRowKeys, selectedRows) => {
    //      console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows)
    //    },
    //    onSelect: (record, selected, selectedRows) => {
    //      this.selectedNum = selectedRows.length
    //      console.log(record,'record')
    //      console.log(selectedRows,'selectedRows')
    //    },
    //    onSelectAll: (selected, selectedRows, changeRows) => {
    //      this.selectedNum = selectedRows.length
    //      console.log(selected, selectedRows, changeRows)
    //    }
    //  }
    return {
      // 查询参数
      queryParam: {
        dispName: '', // 名称
        isEnable: undefined, // 是否启用
        lookupCode: ''
      },
      noData: false, // 没有数据 为空图片显示
      code: '', // 根据输入编码类别/名称搜索字典
      selectedNum: '', // 已选择数据条数
      selectedKey: [], // 选中的字典编码项
      selectedMenu: '', // 选中的菜单名称
      formLayout: 'horizontal',
      // 提交参数
      form: this.$form.createForm(this, { name: 'miniForm' }),
      codeList: [], // 字典列表数据
      showCodeModal: false, // 添加/编辑数据字典弹窗
      showItemCodeModal: false, // 添加数据弹窗
      codeData: {}, // 编辑字典行数据
      itemCodeData: {}, // 当前行数据
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: 'no',
          width: '40',
          align: 'center'
        },
        {
          title: '名称',
          dataIndex: 'dispName',
          width: '100',
          align: 'center'
        },
        {
          title: '项编码',
          dataIndex: 'code',
          width: '100',
          align: 'center'
        },
        {
          title: '排序值',
          dataIndex: 'sort',
          width: '100',
          align: 'center'
        },
        {
          title: '是否启用',
          width: '100',
          align: 'center',
          dataIndex: 'isEnable',
          customRender: function (text) {
            if (text == 1) {
              return '启用'
            } else {
              return '禁用'
            }
          }
        },
        {
          title: '创建时间',
          dataIndex: 'createDate',
          width: '120',
          align: 'center'
        },
        {
          title: '备注',
          dataIndex: 'remarks',
          width: '100',
          align: 'center'
        },
        {
          title: '操作',
          width: '100',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        console.log(parameter, 'parameter')
        // 获取列表数据  有分页
        return getLookUpItem(Object.assign(parameter, this.queryParam)).then(res => {
          // 计算表格序号
          const no = (res.data.pageNo - 1) * res.data.pageSize
          for (let i = 0; i < res.data.list.length; i++) {
            const _item = res.data.list[i]
            _item.no = no + i + 1
            _item.isEnable = _item.isEnable + '' === '1'
          }
          return res.data
        })
      },
      optionAlertShow: false,
      windowHeight: 0// 页面高度
    }
  },
  methods: {

    // 获取字典列表
    getCodeList () {
      console.log({ code: this.code })
      listLookUp({ code: this.code }).then(res => {
        if (res.status == 200) {
          this.codeList = res.data
          this.noData = false
          if (!res.data.length) {
            this.noData = true
          }
        }
      })
    },
    // 添加字典
    addCode () {
      this.showCodeModal = true
      this.codeData = {}
    },
    // 编辑字典
    editCode () {
      if (!this.selectedKey.length) {
        return this.$message.warning('您还未选择要编辑的字典')
      }
      this.showCodeModal = true
      this.codeData = this.codeList[this.selectedKey]
    },
    // 删除字典
    delectCode () {
      if (!this.selectedKey.length) {
        return this.$message.warning('您还未选择要删除的字典')
      }
      const _this = this
      const id = this.codeList[this.selectedKey].id
      this.$confirm({
        title: '警告',
        content: `确定要删除 ${this.codeList[this.selectedKey].name} 吗?`,
        okText: '确定',
        cancelText: '取消',
        onOk () {
          delLookUp({
            id: id
          }).then(res => {
            if (res.status == 200) {
              _this.$message.success('删除成功')
              _this.getCodeList()
            }
          }).catch(() => {
            _this.$message.success('删除失败, 请稍后重试')
          })
        }
      })
    },
    // 字典 取消选择
    cancelSel () {
      this.selectedKey = []
      this.selectedMenu = ''
      this.$refs.table.clearTable()
    },

    // 新增数据
    openCodeItemModal () {
      if (!this.selectedKey.length) {
        return this.$message.warning('请先选择一个字典类别')
      }
      this.showItemCodeModal = true
      this.itemCodeData = { lookupCode: this.codeList[this.selectedKey].code }
    },

    // 批量删除数据
    delectAll () {
      if (!this.selectedNum) {
        return this.$message.warning('您还未选择要删除的数据')
      }
      const _this = this
      this.$confirm({
        title: '警告',
        content: `确定要删除您选择的这${this.selectedNum}条数据吗?`,
        okText: '确定',
        cancelText: '取消',
        onOk () {
          // delectUserPower({
          //   id: row.id
          // }).then(res => {
          //   if (res.status == 200) {
          //     _this.$message.success('删除成功')
          //     _this.$refs.table.refresh()
          //   }
          // }).catch(() => {
          //   _this.$message.success('删除失败, 请稍后重试')
          // })
        }
      })
    },
    // 删除
    delect (row) {
      const _this = this
      this.$confirm({
        title: '警告',
        content: '确定要删除吗?',
        okText: '确定',
        cancelText: '取消',
        onOk () {
          delLookUpItem({
            id: row.id
          }).then(res => {
            console.log(res, 'res1111')
            if (res.status == 200) {
              _this.$message.success('删除成功')
              _this.$refs.table.refresh()
            }
          }).catch(() => {
            _this.$message.success('删除失败, 请稍后重试')
          })
        }
      })
    },
    // 编辑打开添加数据弹窗
    handleEdit (row) {
      console.log(row, 'row')
      this.showItemCodeModal = true
      this.itemCodeData = row
    },
    // 计算表格高度
    setTableH () {
      this.windowHeight = window.innerHeight - 90
    }
  },
  beforeCreate () {
    this.form = this.$form.createForm(this, { name: 'miniForm' })
  },
  // beforeRouteEnter (to, from, next) {
  //   next(vm => {
  //     vm.getCodeList()
  //   })
  // },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.getCodeList()
      this.cancelSel()
      this.setTableH()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.getCodeList()
      this.cancelSel()
      this.setTableH()
    }
  },
  watch: {
    // 重组菜单列表数据
    selectedKey (newValue, oldValue) {
      console.log(newValue, 'newValue')
      if (newValue.length) {
        this.selectedMenu = this.codeList[this.selectedKey].name + ' ' + '(' + this.codeList[this.selectedKey].code + ')'
        this.queryParam.lookupCode = this.codeList[this.selectedKey].code
        this.$refs.table.refresh(true)
      }
    }
  }
}
</script>

<style scoped>
  .menu-box::-webkit-scrollbar{
    width: 6px;
    height: 6px ;
  }

  .menu-box::-webkit-scrollbar-thumb{
    width: 6px;
    height: 6px ;
    border-radius: 4px;
    -webkit-box-shadow: inset 0 0 2px #d1d1d1;
    background: #e4e4e4;
  }

  .textCount{
    border: 1px solid #91d5ff;
    background-color: #e6f7ff;
    border-radius: 5px;
    padding: 5px 10px 5px 20px;
    margin: 10px 0;
    color: rgba(0, 0, 0);
  }
  .imgSize{
    color: #2d8cf0;
    margin-left: 10px;
    font-size: 14px;
  }
  .action-button{
    line-height: 40px;
    white-space: nowrap;
    padding: 5px 10px;
    background-color: #1890ff;
    border-radius: 4px;
    color: #fff;
    margin-right: 5px;
  }
  .red-text{
    background-color: red;
  }
</style>
