<template>
  <div class="app-container">
    <div class="filter-container">

      <el-input v-model="search.keyWord" placeholder="关键字" style="width: 200px;" class="filter-item" />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="searchData">
        搜索
      </el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-plus"
        @click="handleCreateAdd">
        增加工艺档案
      </el-button>
    </div>
    <div class="table-con">
      <el-table
        :data="tableData"
        v-loading="listLoading"
        style="width: 100%;margin-bottom: 20px;"
        border
      >
        <el-table-column
            label="#"
            align="center"
            type="index"
            :index="indexMethod">
        </el-table-column>
        <el-table-column label="操作" align="left" width="180" class-name="small-padding fixed-width">
          <template slot-scope="{row}">
            <el-button @click="handleUpdate(row.id)" type="primary" size="mini">
              编辑
            </el-button>
            <el-button @click="editStatus(row.id,'1')" type="danger" v-if="row.voidFlag==0" size="mini">
              禁用
            </el-button>
            <el-button @click="editStatus(row.id,'0')" v-else-if="row.voidFlag==1" size="mini">
              启用
            </el-button>
          </template>
        </el-table-column>
        <el-table-column align="center" label="状态" class-name="status-col" width="100">
          <template slot-scope="{row}">
            <el-tag v-if="row.voidFlag==0" type="success">
              启用中
            </el-tag>
            <el-tag v-else-if="row.voidFlag==1" type="danger">
              禁用中
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="craftName"
          label="工序名称">
        </el-table-column>
        <el-table-column
          label="工序">
          <template slot-scope="{row}">
            <span v-if="row.craftType==0" type="success">
              未设置
            </span>
            <span v-else-if="row.craftType==1" type="danger">
              模切
            </span>
            <span v-else-if="row.craftType==2" type="danger">
              覆膜
            </span>
            <span v-else-if="row.craftType==3" type="danger">
              成型
            </span>
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.createTime">{{scope.row.createTime | getTime}}</span>
          </template>
        </el-table-column>

      </el-table>
      <pagination v-show="listData.total>0" :total="listData.total" :page.sync="listData.page" :limit.sync="listData.pageSize" @pagination="getTableInfo" />
    </div>

    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="600px">
      <el-form label-width="130px" label-position="right">
        <el-form-item label="工序名称">
          <el-input  v-model="form.craftName"></el-input>
        </el-form-item>
        <el-form-item label="工艺类型">
          <el-select v-model="form.craftType" filterable placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.craftType"
              :value="item.craftType+','+item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="反馈流程">
          <el-checkbox-group v-model="checkList">
            <el-checkbox label="验收"></el-checkbox>
            <el-checkbox label="开始"></el-checkbox>
            <el-checkbox label="下机"></el-checkbox>
            <el-checkbox label="已流转下级"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>

        <el-form-item label="排序">
          <el-input type="number" v-model="form.sortIndex"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogStatus==='1'?addBtn():editBtn()">确 定</el-button>
      </div>
    </el-dialog>
    <cancledialog @tableRefresh="getTableInfo" ref="confirmDialog"></cancledialog>
  </div>
</template>

<script>
import {
  getInfo,
  getInfoById,
  addInfo,
  updateInfo
} from '@/api/baseInfo/craftInfoList/index.js'
import Cancledialog from '@/components/Dialog/Cancledialog.vue'
import Pagination from "@/components/Pagination/index";
export default {
  name: "list",
  components: {
    Cancledialog,Pagination
  },
  data() {
    return {
      checkList: [],
      options: [{
        craftType: '未设置',
        id: 0
      }, {
        craftType: '模切',
        id: 1
      }, {
        craftType: '覆膜',
        id: 2
      }, {
        craftType: '成型',
        id: 3
      }],
      dialogStatus: 1,//1增加，2修改
      form:{},
      confirmdialog:false,
      dialogVisible:false,
      dialogTitle:"",
      listLoading: false,
      tableData: [],
      search: {
        keyWord:''
      },
      listData:{
        total:0,
        page: 1,
        pageSize:10
      }
    }
  },
  mounted() {
    this.getTableInfo();
  },
  methods: {
    //启用禁用
    editStatus(id,status){
      var _that = this;
      var data = {
        id:id,
        voidFlag:status
      }
      var params = {
        "craftInfo": JSON.stringify(data),
      }

      var url = '/outassistDoc/voidCraftInfo';
      _that.$refs.confirmDialog.updataClick(params, url);
    },
    handleUpdate(id) {

      const _that = this;
      _that.checkList=[];
      _that.dialogTitle = '修改工艺档案信息'
      _that.dialogVisible = true
      _that.dialogStatus = '2'
      _that.byId = id
      var data = {
        idCraftInfo: id
      }
      getInfoById(data).then(res => {
        //获取详情
        var datas = res.data;
        console.log(datas);
        if(datas.craftType==0){
          var craftTypeName = "未设置";
        }else if(datas.craftType==1){
          var craftTypeName = "模切";
        }else if(datas.craftType==2){
          var craftTypeName = "模切";
        }else if(datas.craftType==3){
          var craftTypeName = "模切";
        }
        var feedbackProcessArray = datas.feedbackProcess.split("");
        // console.log(feedbackProcessArray);
        if(feedbackProcessArray[0]==1){
          _that.checkList.push("验收")
        }
        if(feedbackProcessArray[1]==1){
          _that.checkList.push("开始")
        }
        if(feedbackProcessArray[2]==1){
          _that.checkList.push("下机")
        }
        if(feedbackProcessArray[3]==1){
          _that.checkList.push("已流转下级")
        }
        _that.form = {
          id: id,
          craftName: datas.craftName,
          craftType: craftTypeName,
          craftTypeNum: datas.craftType,
          sortIndex: datas.sortIndex
        }
      }).catch(err => {
        _that.$message.error(err)
      })
    },
    editBtn(){
      //修改
      const _that = this;
      if (_that.form.craftType.split(',').length > 1) {
        var craftTypeNum = _that.form.craftType.split(',')[1]
      } else {
        var craftTypeNum = _that.form.craftTypeNum
      }
      var processNum = "";
      var processText = "";
      if(_that.checkList.indexOf("验收")!=-1){
        processNum+="1";
        processText+="验收,"
      }else {
        processNum+="0"
      }
      if(_that.checkList.indexOf("开始")!=-1){
        processNum+="1"
        processText+="开始,"
      }else {
        processNum+="0"
      }
      if(_that.checkList.indexOf("下机")!=-1){
        processNum+="1"
        processText+="下机,"
      }else {
        processNum+="0"
      }
      if(_that.checkList.indexOf("已流转下级")!=-1){
        processNum+="1"
        processText+="已流转下级,"
      }else {
        processNum+="0"
      }
      var dataInfo = {
        id: _that.form.id,
        craftName: _that.form.craftName,
        craftType: craftTypeNum,
        feedbackProcess: processNum,
        sortIndex: _that.form.sortIndex
      }
      console.log(dataInfo);
      var info =JSON.stringify(dataInfo)
      var data = {
        "craftInfo": info
      }
      // console.log(data);

      updateInfo(data).then(res => {
        console.log(res);
        if(res.httpCode == 200){
          _that.dialogVisible = false
          _that.$message({
            message: '修改成功',
            type: 'success'
          });
          _that.getTableInfo();
        }else{
          _that.$message.error(res.msg)
        }
      }).catch(err => {
        _that.$message.error(err)
        _that.dialogVisible = true
      })
    },
    addBtn(){
      //增加
      const _that = this;
      if(!_that.form.craftName){
        _that.$message.error("工序名称不能为空")
        return
      }
      if(!_that.form.craftType){
        _that.$message.error("工艺类型不能为空")
        return
      }
      if(!_that.checkList){
        _that.$message.error("反馈流程不能为空")
        return
      }
      var processNum = "";
      var processText = "";
      if(_that.checkList.indexOf("验收")!=-1){
        processNum+="1";
        processText+="验收,"
      }else {
        processNum+="0"
      }
      if(_that.checkList.indexOf("开始")!=-1){
        processNum+="1"
        processText+="开始,"
      }else {
        processNum+="0"
      }
      if(_that.checkList.indexOf("下机")!=-1){
        processNum+="1"
        processText+="下机,"
      }else {
        processNum+="0"
      }
      if(_that.checkList.indexOf("已流转下级")!=-1){
        processNum+="1"
        processText+="已流转下级,"
      }else {
        processNum+="0"
      }
      var craftInfo = {
        craftName: _that.form.craftName,
        craftType: _that.form.craftType.split(',')[1],
        feedbackProcess: processNum,
        sortIndex: _that.form.sortIndex
      }
      var info =JSON.stringify(craftInfo)
      var data = {
        "craftInfo": info
      }
      // console.log("clientLevel",data);
      addInfo(data).then(res => {
        console.log(res);
        _that.dialogVisible = false
        if(res.httpCode == 200){

          _that.$message({
            message: '增加成功',
            type: 'success'
          });
          _that.getTableInfo();
        }else{
          _that.$message.error(res.msg)
        }
      }).catch(err => {
        _that.$message.error(err)
        _that.dialogVisible = true
      })
    },
    handleCreateAdd(){
      //增加工艺档案来源
      const _that = this
      _that.form={
        craftName: "",
        craftType: "",
        feedbackProcess: "",
        sortIndex: null
      }
      _that.checkList=[];
      _that.dialogTitle = '增加工艺档案'
      _that.dialogStatus = '1';
      _that.dialogVisible = true;

    },
    //获取表格数据
    getTableInfo(){
      const _that = this
      _that.listLoading = true
      var params = _that.search;
      // var page = {
      //   pageIndex:_that.listData.page,
      //   pageSize: _that.listData.pageSize,
      // }
      var data = {
        "params": {}
      }
      // console.log(data);
      getInfo(data).then(response => {
        console.log("tableData",response)
        const data = response.data
        // //  组合表单数据
        _that.tableData = data
        _that.listData.total = response.total
        // console.log(_that.tableData)
        _that.listLoading = false
      }).catch(err => {
        _that.listLoading = false
      })
    },
    //搜索
    searchData(){
      this.listData.page = 1;
      this.getTableInfo()
    },
    //序号
    indexMethod(index) {
      return index+1;
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background-color: #f0f2f5;
  padding: 30px;
  min-height: calc(100vh - 84px);
}
.filter-container {
  margin-bottom: 15px;
}
</style>
