<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="80px"
          label-suffix=":"
        >
          <el-form-item label="计划月份">
            <label slot="label">计划月份:</label>
            <el-date-picker
              v-model="query.plan_month"
              type="month"
              style="width: 200px"
              size="mini"
              class="filter-item"
              value-format="yyyyMM"
              placeholder="选择年月"
            />
          </el-form-item>
          <el-form-item label="关键设备">
            <label slot="label">关键设备:</label>
            <el-select
              v-model="query.device_id"
              clearable
              size="mini"
              placeholder="关键设备"
              class="filter-item"
              style="width: 200px"
              @change="hand"
            >
              <el-option
                v-for="item in Devices"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="申报单位">
            <label slot="label">申报单位:</label>
            <el-select
              v-model="query.plan_org_code"
              clearable
              size="mini"
              placeholder="申报单位"
              class="filter-item"
              style="width: 200px"
              @change="hand"
            >
              <el-option
                v-for="item in Depts"
                :key="item.code"
                :label="item.name"
                :value="item.code"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="产品">
            <el-input
              v-model="query.material_code"
              clearable
              size="mini"
              placeholder="请输入物料编码"
              style="width: 200px"
              class="filter-item"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item label="状态">
            <el-select
              v-model="query.is_proc"
              clearable
              size="mini"
              placeholder="状态"
              style="width: 200px"
              class="filter-item"
              @change="MyQuery"
            >
              <el-option
                v-for="item in dict.is_proc2"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

            <el-form-item label="任务号">
              <el-input
                v-model="query.task_code"
                clearable
                size="mini"
                placeholder="请输入任务号"
                style="width: 200px"
                class="filter-item"
                @keyup.enter.native="crud.toQuery"
              />
            </el-form-item>

            <el-form-item label="排产模板">
              <label slot="label">排产模板:</label>
              <el-select
                v-model="query.captemplate_id"
                clearable
                size="mini"
                placeholder="排产模板"
                class="filter-item"
                style="width: 200px"
              >
                <el-option
                  v-for="item in Capacitytes"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>

            <el-form-item label="初始日期">
              <label slot="label">初始日期:</label>
              <el-date-picker
                v-model="query.nowstart_date"
                type="date"
                style="width: 200px"
                value-format="yyyy-MM-dd"
                placeholder="选择日期">
              </el-date-picker>
              <el-checkbox v-model="query.checked">启用</el-checkbox>
            <rrOperation />
          </el-form-item>
        </el-form>
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation>
        <el-button
          slot="right"
          class="filter-item"
          type="primary"
          icon="el-icon-position"
          size="mini"
          @click="save()"
        >
          预排
        </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
          min-width="125"
          label="操作"
          align="center"
          fixed="right"
        >
          <template scope="scope">
            <el-button  :disabled="scope.row.is_proc==='2'" type="primary" class="filter-item" size="mini" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)"></el-button>
          </template>
        </el-table-column>
        <el-table-column type="index" label="序号" width="55" align="center" />
        <el-table-column :selectable="checkboxT" type="selection" width="55" />
        <el-table-column prop="plan_month" label="计划月份" />
        <el-table-column prop="task_code" label="任务号" min-width="130"/>
        <el-table-column prop="plan_org_name" label="申报单位" width="100" />
        <el-table-column prop="device_name" label="关键设备" width="100" />
        <el-table-column prop="plan_finish_date" label="交货日期" width="90" />
        <el-table-column prop="old_mark" min-width="100" label="牌号" />
        <el-table-column prop="material_code" label="物料编码" min-width="150"/>
        <el-table-column :formatter="seriesFormat" min-width="80" prop="product_series" label="系列" />
        <el-table-column prop="fact_weight" label="生产重量Kg" :formatter="crud.formatNum0" min-width="95" />
        <el-table-column prop="standard_weight" label="每批重量Kg" width="95" :formatter="crud.formatNum0"/>
        <el-table-column prop="pcsn_num" label="批数" :formatter="pcsn_num_format"/>
        <el-table-column  :formatter="stateFormat" min-width="80" prop="is_proc" label="状态" />
        <el-table-column prop="update_time" label="提交时间" width="140px" />
        <el-table-column prop="update_optname" label="提交人" />
        <el-table-column prop="remark" label="备注" />
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
    <AddDialog :dialog-show.sync="changeShow" :rowmst="mstrow" @AddChanged="querytable" />
  </div>
</template>

<script>
import producetask from '@/api/wms/pdm/producetask'
import CRUD, { presenter, header, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import AddDialog from '@/views/wms/pdm/produce/mouthtask/AddDialog'
import pagination from '@crud/Pagination'
import { getToken } from '@/utils/auth'
import { mapGetters } from 'vuex'
import workorder from '@/api/wms/pdm/workorder'
import crudseriesProcessRoute from '@/api/wms/pdm/seriesProcessRoute'
import Date from '@/utils/datetime'

export default {
  name: 'Mouthtask',
  dicts: ['product_mode', 'is_proc2'],
  components: { pagination, crudOperation, rrOperation, AddDialog },
  mixins: [presenter(), header(), crud()],
  cruds() {
    return CRUD({
      title: '月计划管理',
      url: 'api/producetask/mouthtask',
      permission: {
        add: ['admin', 'producetask:add'],
        edit: ['admin', 'producetask:edit'],
        del: ['admin', 'producetask:del']
      },
      idField: 'plan_id',
      sort: '',
      query:{ nowstart_date: new Date(), captemplate_id: '',is_proc: '1'},
      optShow: {
        add: false,
        edit: false,
        del: false,
        download: false,
        reset: false
      }})
  },
  data() {
    return {
      headers: { 'Authorization': getToken() },
      dialogVisible: false,
      save_flag: true,
      sub_flag: true,
      changeShow: false,
      mstrow: {},
      Depts: [],
      Devices: [],
      Capacitytes: [],
      XLList: [],
      fileList: [],
      checkrows: [],
      rules: {
      }}
  },
  computed: {
    ...mapGetters([
      'baseApi',
      'fileUploadApi'
    ])
  },
  created() {
    workorder.getDepts().then(res => {
      this.Depts = res
    })

    producetask.getDevices().then(res => {
      this.Devices = res
    })
    crudseriesProcessRoute.getXLlist2().then(res => {
      this.XLList = res
    })
    producetask.getCapacitytes().then(res => {
      this.Capacitytes = res
    })
  },
  methods: {
    [CRUD.HOOK.beforeRefresh]() {
      if (this.crud.query.is_proc === '0') {
        this.save_flag = false
      } else {
        this.save_flag = true
      }
      return true
    },
    canUd(row) {
      return row.is_proc !== '2'
    },
    checkboxT(row) {
      return row.is_proc === '1'
    },
    hand(value) {
      this.crud.toQuery()
    },
    stateFormat(row) {
      return this.dict.label.is_proc2[row.is_proc]
    },
    pcsn_num_format(row) {
      return Math.ceil(row.fact_weight / row.standard_weight)
    },
    onInput() {
      this.$forceUpdate()
    },
    MyQuery(value) {
      if (value === '1') {
        this.save_flag = false
      } else {
        this.save_flag = true
      }
      this.crud.toQuery()
    },
    seriesFormat(row) {
      for (const item of this.XLList) {
        if (item.id === row.product_series) {
          return item.name
        }
      }
    },
    handleEdit(index, row) {
      this.changeShow = true
      this.mstrow = row
    },
    save() {
      if(this.crud.query.captemplate_id === '' || this.crud.query.captemplate_id === undefined ){
        this.crud.notify('请先选择排产模板！')
        return false
      }
      this.checkrows = this.$refs.table.selection
      if(this.checkrows.length === 0 ){
        this.crud.notify('请勾选需要提交的记录！')
        return false
      }
      this.crud.loading = true
      producetask.submit2({ query: this.crud.query, rows: this.checkrows}).then(res => {
        this.crud.notify('操作成功！')
        this.crud.loading = false
        this.querytable()
      }).catch(() => {
        this.crud.loading = false
      })
    },
    querytable() {
      this.crud.toQuery()
    }
  }
}
</script>

<style scoped>

</style>
