<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-form
          :inline="true"
          class="demo-form-inline"
          label-position="right"
          label-suffix=":"
        >
          <el-form-item :label="$t('opc.placeholder.name_code_path')">
            <el-input
              v-model="query.blurry"
              clearable
              size="small"
              :placeholder="$t('opc.placeholder.enter_name_code_path')"
              style="width: 250px;"
              class="filter-item"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <rrOperation />
        </el-form>
      </div>
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="1070px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="125px">
          <el-row>
            <el-col :span="12">
              <div class="grid-content bg-purple" />
              <el-form-item :label="$t('opc.table_title.opc_code')" prop="opc_code">
                <el-input v-model="form.opc_code" style="width: 370px;" />
              </el-form-item>
              <el-form-item :label="$t('opc.table_title.opc_name')" prop="opc_name">
                <el-input v-model="form.opc_name" style="width: 370px;" />
              </el-form-item>
              <el-form-item :label="$t('opc.table_title.opc_address')">
                <el-input v-model="form.opc_host" style="width: 370px;" />
              </el-form-item>
              <el-form-item :label="$t('auto.common.username')">
                <el-input v-model="form.user" style="width: 370px;" />
              </el-form-item>
              <el-form-item :label="$t('auto.common.password')" prop="password">
                <el-input v-model="form.password" style="width: 370px;" type="password" />
                <el-button :loading="crud.cu === 2" type="primary" @click="getDetail()">{{ $t('opc.dialog.detail') }}</el-button>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <div class="grid-content bg-purple-light" />
              <el-form-item :label="$t('opc.table_title.program_id')">
                <el-input v-model="form.prog_id" style="width: 370px;" />
              </el-form-item>
              <el-form-item :label="$t('opc.table_title.registry_id')">
                <el-input v-model="form.cls_id" style="width: 370px;" />
              </el-form-item>
              <el-form-item label="domain">
                <el-input v-model="form.domain" style="width: 370px;" />
              </el-form-item>
              <el-form-item :label="$t('opc.table_title.remark')">
                <el-input v-model="form.remark" style="width: 370px;" type="textarea" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">{{ $t('auto.common.Cancel') }}</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">{{ $t('auto.common.Confirm') }}</el-button>
        </div>
      </el-dialog>

      <el-dialog title="PLC管理" :visible.sync="dialogPlcFormVisible" width="550px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-input v-model="form.opc_server_id" type="hidden" prop="opc_server_id" />
          <el-form-item :label="$t('opc.table_title.plc_code')" prop="plc_code">
            <el-input v-model="form.plc_code" style="width: 370px;" />
          </el-form-item>
          <el-form-item :label="$t('opc.table_title.opc_name')" prop="plc_opc_name">
            <el-input v-model="form.plc_opc_name" style="width: 370px;" disabled />
          </el-form-item>
          <el-form-item :label="$t('opc.table_title.plc_name')" prop="plc_name">
            <el-input v-model="form.plc_name" style="width: 370px;" />
          </el-form-item>
          <el-form-item :label="$t('opc.table_title.plc_address')" prop="plc_host">
            <el-input v-model="form.plc_host" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogPlcFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addPlc()">确 定</el-button>
        </div>
      </el-dialog>

      <!--表格渲染-->
      <el-table
        v-loading="crud.loading"
        :data="crud.data"
        highlight-current-row
        style="width: 100%"
        row-key="opc_id"
        border
        default-expand-all
        :tree-props="{children: 'children', has_children: 'has_children'}"
        @select="crud.selectChange"
        @select-all="crud.selectAllChange"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="opc_code" :label="$t('opc.table_title.opc_code')" width="130px" />
        <el-table-column prop="opc_name" :label="$t('opc.table_title.opc_name')" :min-width="flexWidth('opc_name',crud.data,$t('opc.table_title.opc_name'))" />
        <el-table-column prop="opc_host" :label="$t('opc.table_title.opc_address')" :min-width="flexWidth('opc_host',crud.data,$t('opc.table_title.opc_address'))" />
        <el-table-column prop="type" :label="$t('opc.table_title.type')" :min-width="flexWidth('type',crud.data,$t('opc.table_title.type'))" />
        <el-table-column prop="user" :label="$t('auto.common.username')" :min-width="flexWidth('user',crud.data,$t('opc.table_title.username'))" />
        <el-table-column prop="password" :label="$t('auto.common.password')" :min-width="flexWidth('password',crud.data,$t('opc.table_title.password'))" />
        <el-table-column prop="prog_id" :label="$t('opc.table_title.program_id')" :min-width="flexWidth('prog_id',crud.data,$t('opc.table_title.program_id'))" />
        <el-table-column prop="cls_id" :label="$t('opc.table_title.registry_id')" :min-width="flexWidth('cls_id',crud.data,$t('opc.table_title.registry_id'))" />
        <el-table-column prop="domain" :label="$t('opc.table_title.domain')" :min-width="flexWidth('domain',crud.data,$t('opc.table_title.domain'))" />
        <el-table-column prop="remark" :label="$t('opc.table_title.remark')" :min-width="flexWidth('remark',crud.data,$t('opc.table_title.remark'))" />
        <el-table-column prop="create_time" :label="$t('auto.common.create_time')" :min-width="flexWidth('create_time',crud.data,$t('opc.table_title.create_time'))" />
        <el-table-column :label="$t('auto.common.Operate')" width="200px" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-edit" size="small" @click="handleClick(scope.row,'1')">{{ $t('auto.common.Update') }}</el-button>
            <el-button type="text" icon="el-icon-circle-close" size="small" @click="handleClick(scope.row,'2')">{{ $t('auto.common.Delete') }}</el-button>
            <el-button v-if="scope.row.opc_flag === '1'" type="text" icon="el-icon-folder-add" size="small" @click="handleClick(scope.row,'0')">
              {{ $t('opc.table_title.plc_add') }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudOpc from '@/api/acs/device/opc'
import CRUD, { crud, form, header, presenter } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import rrOperation from '@crud/RR.operation'
import i18n from '@/i18n'

const defaultForm = {
  type: null,
  opc_id: null,
  opc_code: null,
  opc_name: null,
  opc_host: null,
  user: null,
  password: null,
  prog_id: null,
  cls_id: null,
  domain: null,
  remark: null,
  is_active: null,
  is_delete: null,
  create_by: null,
  create_time: null,
  update_by: null,
  update_time: null,
  plc_code: null,
  plc_name: null,
  plc_host: null,
  opc_server_id: null,
  plc_opc_name: null
}
export default {
  name: 'Opc',
  components: { pagination, crudOperation, rrOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: i18n.t('opc.title'), url: 'api/opc', idField: 'opc_id', sort: 'opc_id,desc', crudMethod: { ...crudOpc },
      optShow: {
        add: true,
        edit: false,
        del: false,
        download: false
      }
    })
  },
  data() {
    return {
      dialogPlcFormVisible: false,
      operateFlag: null,
      permission: {
        add: ['admin', 'opc:add'],
        edit: ['admin', 'opc:edit'],
        del: ['admin', 'opc:del']
      },
      rules: {
        opc_id: [
          { required: true, message: 'OPC标识不能为空', trigger: 'blur' }
        ],
        opc_code: [
          { required: true, message: 'OPC编码不能为空', trigger: 'blur' }
        ],
        opc_name: [
          { required: true, message: 'OPC名称不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ],
        is_active: [
          { required: true, message: '是否启用不能为空', trigger: 'blur' }
        ],
        is_delete: [
          { required: true, message: '是否删除不能为空', trigger: 'blur' }
        ],
        create_by: [
          { required: true, message: '创建者不能为空', trigger: 'blur' }
        ],
        create_time: [
          { required: true, message: '创建时间不能为空', trigger: 'blur' }
        ]
      }
    }
  },

  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    handleClick(row, flag) {
      // flag为0是新增，flag为1是编辑，flag为2是删除
      // 如果是父类操作
      if (row.opc_flag === '1') {
        if (flag === '1') {
          this.crud.toEdit(row)
        }
        if (flag === '2') {
          this.crud.doDelete(row)
        }
        if (flag === '0') {
          this.dialogPlcFormVisible = true
          this.form.opc_server_id = row.opc_id
          this.form.plc_opc_name = row.opc_name
          this.operateFlag = '0'
        }
      } else {
        if (flag === '1') {
          const jo = JSON.parse(JSON.stringify(row))
          const new_jo = {
            plc_id: jo.opc_id,
            plc_code: jo.opc_code,
            plc_name: jo.opc_name,
            plc_host: jo.opc_host,
            opc_server_id: jo.opc_server_id,
            is_active: jo.is_active,
            is_delete: jo.is_delete,
            create_by: jo.create_by,
            create_time: jo.create_time,
            update_by: jo.update_time,
            update_time: jo.update_time,
            plc_opc_name: jo.plc_opc_name
          }
          this.crud.resetForm(JSON.parse(JSON.stringify(new_jo)))
          this.dialogPlcFormVisible = true
          this.operateFlag = '1'
        }
        if (flag === '2') {
          crudOpc.delPLC(row).then(res => {
            this.crud.resetForm()
            this.crud.toQuery()
            this.crud.notify(status + '成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
          }).catch(err => {
            console.log(err.response.data.message)
          })
        }
      }
    },
    getDetail() {
      const _this = this
      this.crud.crudMethod.getmeteal(this.form).then(function(response) {
        _this.form.cls_id = response.classId
        _this.form.prog_id = response.progId
        _this.form.remark = response.description
        _this.crud.notify('操作成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
      }).catch(err => {
        console.log(err.response.data.message)
      })
    },
    // 改变状态
    addPlc() {
      if (this.operateFlag === '0') {
        crudOpc.addPLC(this.form).then(res => {
          this.crud.resetForm()
          this.dialogPlcFormVisible = false
          this.crud.toQuery()
          this.crud.notify(status + '成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        }).catch(err => {
          console.log(err.response.data.message)
        })
      }
      if (this.operateFlag === '1') {
        crudOpc.editPLC(this.form).then(res => {
          this.crud.resetForm()
          this.dialogPlcFormVisible = false
          this.crud.toQuery()
          this.crud.notify(status + '成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        }).catch(err => {
          console.log(err.response.data.message)
        })
      }
    }
  }
}
</script>
