<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu"
        :title="crud.status.title"
        width="800px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
          <el-form-item :label="$t('config.table.device_execution')" prop="device_code">
            <el-select
              v-model="form.device_code"
              filterable
              :placeholder="$t('config.table.please_select')"
              style="width: 300px;"
              :disabled="crud.status.edit == 1"
            >
              <el-option
                v-for="item in deviceList"
                :key="item.device_code"
                :label="item.device_code"
                :value="item.device_code"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('config.table.end_of_instruction')" prop="inst_nextDevice_code">
            <el-select
              v-model="form.inst_nextdevice_code"
              multiple
              filterable
              :placeholder="$t('config.table.please_select')"
              style="width: 300px;"
              @change="getValue"
            >
              <el-option
                v-for="item in deviceList"
                :key="item.device_code"
                :label="item.device_code"
                :value="item.device_code"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('config.table.end_of_task')" prop="task_nextDevice_code">
            <el-select
              v-model="form.task_nextdevice_code"
              filterable
              multiple
              :placeholder="$t('config.table.please_select')"
              style="width: 300px;"
            >
              <el-option
                v-for="item in deviceList"
                :key="item.device_code"
                :label="item.device_code"
                :value="item.device_code"
              />
            </el-select>
          </el-form-item>
          <el-table
            v-if="form.tableData.length>0"
            :data="form.tableData"
            style="width: 100%"
          >
            <el-table-column
              prop="device"
              :label="$t('config.table.end_device')"
              width="180"
            />

            <el-table-column prop="limit" :label="$t('config.table.max')">
              <template slot-scope="scope">
                <el-input v-model="form.tableData[scope.$index].limit" size="mini" class="edit-input" />
              </template>
            </el-table-column>
            <el-table-column prop="scale" :label="$t('config.table.allocation_ratio')">
              <template slot-scope="scope">
                <el-input v-model="form.tableData[scope.$index].scale" size="mini" class="edit-input" />
              </template>
            </el-table-column>
          </el-table>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">{{ $t('config.table.cancel') }}</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">{{ $t('config.table.confirm') }}</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <!--        <el-table-column type="selection" width="55" />-->
        <el-table-column type="selection" width="55" />
        <el-table-column v-if="false" prop="assigned_id" label="任务标识" />
        <el-table-column prop="device_code" :label="$t('config.table.device_code')" />
        <el-table-column prop="task_nextdevice_code" :label="$t('config.table.destination_device')" />
        <el-table-column prop="inst_nextdevice_code" :label="$t('config.table.inst_nextdevice_code')" />
        <el-table-column prop="param" :label="$t('config.table.destination_configuration')" :formatter="paramFormate" />
        <el-table-column
          v-permission="['admin','AcsDeviceAssigned:edit','AcsDeviceAssigned:del']"
          :label="$t('config.table.operation')"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudAcsDeviceAssigned from '@/api/acs/config/deviceAssigned'
import CRUD, { crud, form, header, presenter } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import deviceCrud from '@/api/acs/device/device'
import i18n from '@/i18n'

const defaultForm = {
  assigned_id: null,
  device_code: null,
  task_nextdevice_code: null,
  inst_nextdevice_code: null,
  param: null,
  tableData: []
}
export default {
  name: 'AcsDeviceAssigned',
  components: { pagination, crudOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: i18n.t('config.table.mapping_relationship'), url: 'api/deviceAssigned', idField: 'assigned_id', sort: 'assignned_id', optShow: {
        add: true,
        edit: true,
        del: true
      }, crudMethod: { ...crudAcsDeviceAssigned }
    })
  },
  data() {
    return {
      showList: true,
      permission: {
        add: ['admin', 'AcsDeviceAssigned:add'],
        edit: ['admin', 'AcsDeviceAssigned:edit'],
        del: ['admin', 'AcsDeviceAssigned:del']
      },
      tableData: [{
        name: ''
      }],
      deviceList: [],
      rules: {
        device_code: [
          { required: true, message: this.$t('config.table.device_is_not_null'), trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.$nextTick(() => {
      deviceCrud.selectDeviceList().then(data => {
        this.deviceList = data
      })
    })
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    paramFormate(row, index) {
      return JSON.stringify(row.param)
    },
    getStatus() {
      if (this.crud.status.edit === 0) {
        this.showList = false
      } else {
        this.showList = true
      }
    },
    getValue: function(value) {
      console.log(value)
      const tableData = []
      value.forEach(item => {
        tableData.push({
          device: item
        })
      })
      this.form.tableData = tableData
    },
    changeNextDevice(value) {
      debugger
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.edit-input {
  .el-input__inner {
    border: 1px solid #e5e6e7;
  }
}
</style>
<style scoped>

</style>
