<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-form
          :inline="true"
          class="demo-form-inline"
          label-position="right"
          label-width="11 0px"
          label-suffix=":"
        >
          <el-form-item label="工艺路线">
            <el-input
              v-model="query.processroute_code"
              clearable
              size="mini"
              placeholder="工路线编码或名称"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item label="工艺路线状态">
            <el-select
              v-model="query.processroute_status"
              clearable
              size="mini"
              placeholder="单据状态"
              class="filter-item"
              @change="hand"
            >
              <el-option
                v-for="item in procStatusList"
                :label="item.label"
                :value="item.code"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="生产车间">
            <el-select
              v-model="query.product_area"
              clearable
              filterable
              size="mini"
              placeholder="区域类型"
              class="filter-item"
              @change="crud.toQuery()"
            >
              <el-option
                v-for="item in dict.product_area"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <rrOperation/>
        </el-form>
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
        <el-button
          slot="right"
          :disabled="crud.selections.length !== 1"
          class="filter-item"
          type="success"
          icon="el-icon-position"
          size="mini"
          @click="submit"
        >
          提交
        </el-button>
        <el-button
          slot="right"
          :disabled="crud.selections.length !== 1"
          class="filter-item"
          type="success"
          icon="el-icon-position"
          size="mini"
          @click="CancelSubmit"
        >
          取消提交
        </el-button>
      </crudOperation>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="mini" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55"/>
        <el-table-column v-if="false" prop="processroute_id" label="工艺路线标识"/>
        <el-table-column prop="processroute_code" label="工艺路线编码" min-width="100" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-link type="warning" @click="crud.toView(scope.row)">{{ scope.row.processroute_code }}</el-link>
          </template>
        </el-table-column>
        <el-table-column prop="processroute_name" label="工艺路线名称" min-width="100" show-overflow-tooltip/>
        <el-table-column prop="processroute_status" label="状态" :formatter="formatStatus"/>
        <el-table-column prop="detail_count" label="工序数"/>
        <el-table-column prop="remark" label="备注"/>
        <el-table-column prop="create_name" label="创建人"/>
        <el-table-column prop="create_time" label="创建时间" width="150px"/>
        <el-table-column prop="audit_optname" label="审核人"/>
        <el-table-column prop="audit_time" label="审核时间" width="150px"/>
        <el-table-column v-permission="[]" label="操作" width="120px" align="center" fixed="right">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
              :disabled-edit="canUd(scope.row)"
              :disabled-dle="canUd(scope.row)"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination/>
    </div>
    <AddDialog/>
  </div>
</template>

<script>
import crudProcessroute from '@/api/wms/pdm/processroute'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import AddDialog from '@/views/wms/pdm/base/routing/AddDialog'
import CRUD, { presenter, header, form, crud } from '@crud/crud'

export default {
  name: 'Processroute',
  components: { AddDialog, pagination, crudOperation, rrOperation, udOperation },
  dicts: ['product_area'],
  mixins: [presenter(), header(), form(), crud()],
  cruds() {
    return CRUD({
      title: '工艺路线',
      url: 'api/processroute',
      idField: 'processroute_id',
      sort: 'processroute_id,desc',
      crudMethod: { ...crudProcessroute },
      optShow: {
        add: true,
        edit: false,
        del: false,
        download: false,
        reset: true
      }
    })
  },
  data() {
    return {
      procStatusList: [
        { 'label': '生成', 'code': '10' },
        { 'label': '提交', 'code': '20' }
      ],
      permission: {}
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    hand(value) {
      this.crud.toQuery()
    },
    formatStatus(row, column) {
      if (row.processroute_status === '10') {
        return '生成'
      } else if (row.processroute_status === '20') {
        return '提交'
      }
    },
    canUd(row) {
      return row.processroute_status !== '10'
    },
    submit() {
      const _selectData = this.$refs.table.selection
      const data = _selectData[0]
      if (data.processroute_status !== '10') {
        this.crud.notify('只能对生成状态的单据提交', CRUD.NOTIFICATION_TYPE.INFO)
        return false
      }
      console.log(data)
      crudProcessroute.submit(data).then(res => {
        this.crud.toQuery()
        this.crud.notify('提交成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
      })
    },
    CancelSubmit() {
      const _selectData = this.$refs.table.selection
      const data = _selectData[0]
      if (data.processroute_status !== '20') {
        this.crud.notify('只能对提交状态的单据取消', CRUD.NOTIFICATION_TYPE.INFO)
        return false
      }
      crudProcessroute.CancelSubmit(data).then(res => {
        this.crud.toQuery()
        this.crud.notify('取消成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
      })
    }
  }
}
</script>

<style scoped>

</style>
