<template>
  <div>
    <j-modal
      :title="title"
      width="70%"
      :visible="visible"
      :mask-closable="false"
      cancel-text="关闭"
      @close="close"
    >
      <!-- 操作按钮区域 -->
      <div class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="searchQuery">
          <a-form-item>
            <j-dict-select-tag
              v-model="queryParam.toolType"
              style="width: 150px"
              placeholder="选择工具名称"
              dict-code="tool_type"
            />
          </a-form-item>
          <a-form-item>
            <a-tree-select
              v-model="queryParam.deptId"
              style="width: 150px"
              :show-search="true"
              allow-clear
              placeholder="选择车间"
              :tree-data="treeData"
              tree-node-filter-prop="label"
              :replace-fields="{children:'children', title:'label', key:'id', value: 'id' }"
            />
          </a-form-item>
          <a-form-item>
            <a-input
              v-model="queryParam.storePlace"
              style="width: 150px"
              placeholder="输入存放处"
            />
          </a-form-item>
          <a-form-item>
            <a-date-picker
              v-model="queryParam.lastTestDate"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              placeholder="上次检验日期"
            />
          </a-form-item>
          <a-form-item>
            <a-input
              v-model="queryParam.toolCode"
              style="width: 150px"
              placeholder="输入编号"
            />
          </a-form-item>
          <a-form-item>
            <a-button type="primary" @click="searchQuery">查询</a-button>
            <a-button style="margin-left: 8px" @click="searchReset">重置</a-button>
          </a-form-item>
        </a-form>
      </div>

      <!-- table区域-begin -->
      <div style="margin-top: 10px">
        <a-table
          ref="table"
          size="middle"
          bordered
          row-key="toolId"
          :columns="columns"
          :data-source="dataSource"
          :pagination="ipagination"
          :loading="loading"
          @change="handleTableChange"
        >
          <template slot="isok" slot-scope="text, record">
            <a-switch
              :checked="record.isok==='1'?true:false"
              checked-children="合格"
              un-checked-children="不合格"
              @change="changeStatus({$event,record})"
            />
          </template>
          <span slot="action" slot-scope="text, record">
            <a-button v-if="record.isok!=='1'" size="small" type="primary" @click="handleEdit1(record)">
              更换工具
            </a-button>
          </span>
        </a-table>
      </div>
    <!-- table区域-end -->
    <!-- 表单区域 -->
    </j-modal>
    <check-and-edit-model ref="changeModel" @ok="modalFormOk" />
  </div>
</template>
<script>
import { listMixin } from '@/mixin/listMixin'
import { getAction, httpAction } from '@/api/request'
import JModal from '@/components/JModal'
import columns from './syColumns'
import CheckAndEditModel from '../../CheckAndEditModel'

export default {
  name: 'SyList',
  components: {
    CheckAndEditModel,
    JModal
  },
  mixins: [listMixin],
  data() {
    return {
      title: '完成试验',
      visible: false,
      queryParam: {},
      // 表头
      isNotCreateLoad: true,
      columns: columns(this),
      dataSource: [],
      treeData: [],
      id: '',
      url: {
        list: '/business/safetool/base/safety/tool/listExperimentTool',
        tree: '/system/dept/treeSelect'
      }

    }
  },
  created() {
  },
  methods: {
    handleEdit1: function(record) {
      this.$refs.changeModel.edit(record)
      this.$refs.changeModel.title = '更换安全工具'
      this.$refs.changeModel.disableSubmit = false
    },
    edit(record) {
      this.visible = true
      debugger
      this.id = record.id
      this.loadData()
    },
    changeStatus({ $event, record }) {
      console.log($event, record)
      record.isok = $event ? '1' : '0'
      httpAction('/business/safetool/sec/experiment/setToolNQ', record, 'post').then((res) => {
        if (res.code === 200) {
          this.$message.success(res.msg)
        } else {
          console.log(res)
        }
      })
    },
    loadData() {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      var params = this.getQueryParams()// 查询条件
      this.queryParam = Object.assign({}, params, { id: this.id })
      this.loading = true
      getAction(this.url.list, this.queryParam).then((res) => {
        if (res.code === 200) {
          this.dataSource = res.rows || res.data
          if (res.total) {
            this.ipagination.total = res.total
          } else {
            this.ipagination.total = 0
          }
        } else {
          this.$message.warning(res.msg)
        }
      }).finally(() => {
        this.loading = false
      })
    },
    loadTree() {
      this.treeData = []
      getAction(this.url.tree).then((res) => {
        if (res.code === 200) {
          this.treeData = res.data
        }
      })
    },
    close() {
      this.visible = false
    }
  }
}
</script>
