<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="90px"
          label-suffix=":"
        >
          <el-form-item label="开始日期">
            <el-date-picker
              v-model="query.createTime"
              type="daterange"
              value-format="yyyy-MM-dd"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              @input="onInput()"
              @change="crud.toQuery"
            />
          </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="日计划编号">
            <el-input
              v-model="query.plan_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.status"
              clearable
              size="mini"
              placeholder="状态"
              style="width: 200px"
              class="filter-item"
              @change="MyQuery"
            >
              <el-option
                v-for="item in dict.planstatus"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </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.workorder_type"
              clearable
              size="mini"
              placeholder="计划类型"
              style="width: 200px"
              class="filter-item"
              @change="MyQuery"
            >
              <el-option
                v-for="item in dict.workorder_type2"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
            <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>
        <el-button
          slot="right"
          class="filter-item"
          type="primary"
          icon="el-icon-position"
          size="mini"
          @click="downdtl()"
        >
          导出excel
        </el-button>
        <el-button
          slot="right"
          class="filter-item"
          type="primary"
          icon="el-icon-position"
          size="mini"
          @click="createWork()"
        >
          生成工令
        </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
          v-permission="['admin','workorder:del','workorder:edit']"
          min-width="130"
          label="操作"
          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-column type="index" label="序号" width="55" align="center" />
        <el-table-column :selectable="checkboxT" type="selection" width="55" />
        <el-table-column prop="plan_code" label="日计划编码" width="120" />
        <el-table-column :formatter="seriesFormat" min-width="80" prop="product_series_id" label="系列产线" />
        <el-table-column prop="device_name" label="关键设备" width="100" />
        <el-table-column prop="planstart_date" label="计划开始日期" width="100" />
        <el-table-column prop="plan_org_name" label="申报单位" width="100" />
        <el-table-column prop="workorder_type" label="计划类型" :formatter="workorder_typeFormat" min-width="120" />
        <el-table-column prop="material_code" label="物料编码" min-width="120"/>
        <el-table-column prop="old_mark" label="牌号" />
        <el-table-column :formatter="seriesFormat" min-width="80" prop="mater_product_series" label="产品系列" />
        <el-table-column prop="product_weight" label="生产重量(kg)" :formatter="crud.formatNum3" min-width="120" />
        <el-table-column prop="product_num" label="批数" width="80" />
        <el-table-column prop="planend_date" label="计划结束日期" width="100" />
        <el-table-column prop="plan_finish_date" label="交货日期" width="100" />
        <el-table-column prop="day_num" label="提前天数" width="90" :formatter="crud.formatNum0" />
        <el-table-column  :formatter="stateFormat" min-width="80" prop="status" label="状态" />
        <el-table-column prop="task_code" label="生产任务号" width="120" />
        <el-table-column prop="create_time" label="创建时间" width="140px" />
        <el-table-column prop="create_name" label="创建人"  width="100" />
        <el-table-column prop="remark" label="备注" />
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
    <AddDialog @AddChanged="querytable" />
    <StructIvt4 :dialog-show.sync="structshow" :rowmst="crud.query" @StructIvtClosed="querytable" />
  </div>
</template>

<script>
import producetask from '@/api/wms/pdm/producetask'
import dailyplan from '@/api/wms/pdm/dailyplan'
import AddDialog from '@/views/wms/pdm/produce/dailyplan/AddDialog'
import StructIvt4 from '@/views/wms/pdm/produce/dailyplan/StructIvt4'
import CRUD, { presenter, header, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
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 { download } from '@/api/data'
import { downloadFile } from '@/utils'

export default {
  name: 'dailyplan',
  dicts: ['product_mode', 'workorder_type2', 'planstatus'],
  components: { crudOperation, rrOperation, udOperation, AddDialog, pagination, StructIvt4 },
  mixins: [presenter(), header(), crud()],
  cruds() {
    return CRUD({
      title: '日计划管理',
      url: 'api/dailyplan',
      idField: 'dailyplan_id',
      sort: '',
      query: { device_id: '' },
      crudMethod: { ...dailyplan },
      optShow: {
        add: true,
        edit: false,
        del: false,
        download: false,
        reset: false
      }})
  },
  data() {
    return {
      headers: { 'Authorization': getToken() },
      permission: {
        add: ['admin', 'workorder:add'],
        edit: ['admin', 'workorder:edit'],
        del: ['admin', 'workorder:del']
      },
      dialogVisible: false,
      structshow: false,
      save_flag: true,
      sub_flag: true,
      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]() {
      return true
    },
    checkboxT(row) {
      return row.status === '01'
    },
    canUd(row) {
      return row.status !== '01'
    },
    hand(value) {
      this.crud.toQuery()
    },
    stateFormat(row) {
      return this.dict.label.planstatus[row.status]
    },
    workorder_typeFormat(row) {
      return this.dict.label.workorder_type2[row.workorder_type]
    },
    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
        }
      }
    },
    save() {
      if (this.crud.query.device_id === '' || this.crud.query.device_id === undefined) {
        this.crud.notify('请先选择关键设备！')
        return false
      }
      if (this.crud.query.status !== '01' || this.crud.query.status === undefined) {
        this.crud.notify('重排只能选择生成状态记录！')
        return false
      }
      this.structshow = true
    },
    downdtl() {
      crud.downloadLoading = true
      download('/api/dailyplan/download', this.crud.query).then(result => {
        downloadFile(result, '日计划', 'xlsx')
        crud.downloadLoading = false
      }).catch(() => {
        crud.downloadLoading = false
      })
    },
    createWork() {
      this.checkrows = this.$refs.table.selection
      if (this.checkrows.length === 0) {
        this.crud.notify('请勾选需要操作的记录！')
        return false
      }
      dailyplan.submit({ query: this.crud.query, rows: this.checkrows }).then(res => {
        this.crud.notify('操作成功！')
        this.querytable()
      })
    },
    querytable() {
      this.crud.toQuery()
    }
  }
}
</script>

<style scoped>

</style>
