<template>
  <div class="app-container">
    <el-card class="box-card">
     <div slot="header" class="clearfix">
    <span>生产任务列表</span>
  
  </div>
        
         <el-table
        :data="subproductionplanList.results"
        fit
        style="width: 100%"
        height="100"
          stripe
          border
          v-el-height-adaptive-table="{bottomOffset: 50}"
      >
        <el-table-column type="index" width="50" />
        
          
           <el-table-column label="产品名称">
          <template slot-scope="scope">{{ scope.row.product_.name }}</template>
        </el-table-column>
           <el-table-column label="产品型号">
          <template slot-scope="scope">{{ scope.row.product_.specification }}</template>
        </el-table-column>
         <el-table-column label="名称">
          <template slot-scope="scope">{{ scope.row.subproduction_.name }}</template>
        </el-table-column>
        
        
         <el-table-column label="工序名">
          <template slot-scope="scope">{{ scope.row.process_.name }}</template>
        </el-table-column>
         <el-table-column label="工序编号">
          <template slot-scope="scope">{{ scope.row.process_.number }}</template>
        </el-table-column>
         <el-table-column label="子工序" width="160">
           <template slot-scope="scope" v-if="scope.row.steps">
            <el-tag v-for="item in scope.row.steps"
                    :key="item.number"
                    :label="item.name"
                    :value="item.number">{{item.name}}</el-tag>


          </template>
        </el-table-column>
         <el-table-column label="生产车间">
          <template slot-scope="scope">{{ scope.row.workshop_.name }}</template>
        </el-table-column>
          <el-table-column label="生产数量">
          <template slot-scope="scope">{{ scope.row.main_count }}</template>
        </el-table-column>
          <el-table-column label="开工时间">
          <template slot-scope="scope">{{ scope.row.start_date }}</template>
        </el-table-column>
         <el-table-column label="完工时间">
          <template slot-scope="scope">{{ scope.row.end_date }}</template>
        </el-table-column>
         <el-table-column label="下达状态">
          <template slot-scope="scope">{{ state_[scope.row.state] }}</template>
        </el-table-column>
          
        
  <el-table-column label="创建时间">
          <template slot-scope="scope">{{ scope.row.create_time }}</template>
        </el-table-column>
        <el-table-column
          align="center"
          label="操作"
          width="100px"
    
        >
          <template slot-scope="scope">
            <el-link type="primary"
              v-if="checkPermission(['warehouse_update'])"
              @click="handleclick(scope)"
              >修改日期</el-link
            >
            <el-link type="primary"
              v-if="scope.row.state==0"
              @click="handleissuedclick(scope)"
              >下达</el-link
            >
            
             <el-link type="primary"
              v-if="checkPermission(['warehouse_update'])"
              @click="handleselectclick(scope)"
              >查看详情</el-link
            >
           
          </template>
        </el-table-column>
      </el-table>
    <pagination
        v-show="subproductionplanList.count > 0"
        :total="subproductionplanList.count"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.page_size"
        @pagination="getspList"
      />
    </el-card>
    <el-dialog
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
    >
      <el-form
        ref="Form"
        :model="subproductionplan"
        label-width="180px"
        label-position="right"
        :rules="rule1"
      >
        <el-form-item label="子计划开工时间" prop="start_date">
          <el-date-picker
            v-model="subproductionplan.start_date"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
            style="width:100%"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="子计划完工时间" prop="end_date">
           <el-date-picker
            v-model="subproductionplan.end_date"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
            style="width:100%"
          >
          </el-date-picker>
        </el-form-item>
        
         
      </el-form>
      <div style="text-align: right">
        <el-button type="danger" @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirm('Form')">确认</el-button>
      </div>
    </el-dialog>
     <el-dialog
      :visible.sync="dialogVisibles"
      :close-on-click-modal="false"
    >
     
          <el-table
        :data="progressList"
       border
        fit
        stripe
        style="width: 100%"
        max-height="400"
      >
        <el-table-column type="index" width="50" />
        
      
        
         <el-table-column label="预计产出/消耗">
          <template slot-scope="scope">{{ scope.row.count }}</template>
        </el-table-column>
          <el-table-column label="实际产出/消耗">
          <template slot-scope="scope">{{ scope.row.count_real }}</template>
        </el-table-column>
        <el-table-column label="生产计划编号">
          <template slot-scope="scope">{{ scope.row.subproduction_plan }}</template>
        </el-table-column>
           <el-table-column label="物料名称">
          <template slot-scope="scope">{{ scope.row.material_.name }}</template>
        </el-table-column>
          <el-table-column label="物料编号">
          <template slot-scope="scope">{{ scope.row.material_.number }}</template>
        </el-table-column>
         <el-table-column label="物料型号">
          <template slot-scope="scope">{{ scope.row.material_.specification }}</template>
        </el-table-column>
          <el-table-column label="物料单位">
          <template slot-scope="scope">{{ scope.row.material_.unit }}</template>
        </el-table-column>
        
      </el-table>
    
      <div style="text-align: right">
        <el-button type="danger" @click="dialogVisibles = false">取消</el-button>
       
      </div>
    </el-dialog>
  </div>
</template>
  <script>
  import { getsubproductionplanList,updatesubproductionplan,getProgress,issuesubplan } from "@/api/pm";
import checkPermission from "@/utils/permission";


import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
const defaultesubproductionplan = {
};
export default {
  components: { Pagination },
  data() {
    return {
       
     subproductionplan: defaultesubproductionplan,
      subproductionplanList: {
        count: 0,
      },
      listQuery: {
        page: 1,
        page_size: 20,
      },
       
      progressList:[],
      listLoading: true,
      dialogVisible: false,
       dialogVisibles: false,
      dialogType: "new",
      rule1: {
        number: [{ required: true, message: "请输入", trigger: "blur" }],
       
      },
       state_:{
        0:'制定中',
        1:'已下达',
        2:'已接受',
        3:'生产中',
        4:'已完成'}
    };
  },
  computed: {},
  watch: {},
  created() {
     this.id=this.$route.params.id;
    this.getspList();
  },
  methods: {
    checkPermission,
  //订单列表
    getspList() {
      this.listLoading = true;
      this.listQuery.production_plan =  this.id;
     console.log(this.listQuery)
      getsubproductionplanList(this.listQuery).then((response) => {
        if (response.data) {
          this.subproductionplanList = response.data;
        }
        this.listLoading = false;
      });
    },
     handleclick(scope)
     {
       this.subproductionplan = Object.assign({}, scope.row); // copy obj
      this.dialogVisible = true;
      
      this.$nextTick(() => {
        this.$refs["Form"].clearValidate();
      });
      
     },

     handleselectclick(scope)
     {
       this.dialogVisibles = true;
         getProgress(scope.row.id).then((res) => {
              if (res.code >= 200) {
                 this.progressList = res.data;
              }
            });
     },
     confirm()
     {
        updatesubproductionplan(this.subproductionplan.id,this.subproductionplan).then((res) => {
              if (res.code >= 200) {
                this.getspList();
                this.dialogVisible = false;
                this.$message.success("成功");
              }
            });
     },

     handleissuedclick(scope)
     {
          this.$confirm("确定下达子计划?", "提醒", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await  issuesubplan(scope.row.id).then((res) => {
              if (res.code >= 200) {
                this.getspList();
                this.$message.success("子计划已下达！");
              }
            });
             })
        .catch((err) => {
          console.error(err);
        });
     }
  },
};
    
  </script>