<template>
  <div>
    <Row>
      <!--查询-->
      <Col span="24" />
      <Form
        v-show="searchOpen"
        inline
        onsubmit="return false"
        :label-width="100"
        class="search-form"
      >
        <label class="label-sign" />
        <Form-item class="search-item" label="名称:">
          <Input v-model="formObj.name" placeholder="名称" @on-enter="_formSearch" />
        </Form-item>
        <Form-item class="search-btn">
          <Button type="primary" @click="_formSearch">
            搜索
          </Button>
        </Form-item>
      </Form>
      </Col>
      <!--按钮-->
      <Col span="24" />
      <!--      <btn-list :msg="btn" :open="searchOpen" show-search-btn="true" @on-result-change="_btnClick" />-->
<!--      <el-button type="success" style="margin-bottom: 5px;margin-left: 5px;" @click="_add">-->
<!--        添加-->
<!--      </el-button>-->
<!--      <el-button type="success" style="margin-bottom: 5px;margin-left: 5px;" @click="_delete">-->
<!--        删除-->
<!--      </el-button>-->
<!--      <el-button type="success" style="margin-bottom: 5px;margin-left: 5px;" @click="_submit">-->
<!--        提交-->
<!--      </el-button>-->
      </Col>
      <!--table-->
      <Col span="24" />
      <element-table
        ref="pageTable"
        :page-columns="pageColumns"
        :table-height="tableHeight"
        :get-page="getPage"
        :opt-col-width="80"
        show-check-box
        select-data
        @on-result-change="_tableResultChange"
      >
        <el-table-column
          v-for="item in pageColumns"
          :key="item.key"
          show-overflow-tooltip
          sortable
          :prop="item.key"
          :label="item.title"
          :width="item.width"
          :min-width="200"
          :fixed="item.fixed?item.fixed:undefined"
        >
          <template slot-scope="scope">
            <div v-if="item.dateTime">
              {{ scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd HH:MM:ss'):'' }}
            </div>
            <div v-if="item.key==='status'">
              <div v-if="scope.row.status===10" class="green-color">
                草稿
              </div>
              <div v-else-if="scope.row.status===20" class="green-color">
                实施中
              </div>
              <div v-else-if="scope.row.status===25" class="green-color">
                待审批人审批
              </div>
              <div v-else-if="scope.row.status===26" class="green-color">
                审批通过
              </div>
              <div v-else-if="scope.row.status===27" class="green-color">
                审批驳回
              </div>
              <div v-else-if="scope.row.status===30" class="green-color">
                完成
              </div>
            </div>
            <div v-else-if="item.date">
              {{ scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd HH:MM'):'' }}
            </div>
            <span v-else-if="item.type" class="blue-color">
              {{ scope.row[item.key] === undefined ? '': scope.row[item.key].display }}
            </span>
            <div v-else>
              {{ scope.row[item.key] }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          slot="col"
          label="操作"
          align="center"
          :width="200"
          fixed="right"
        >
          <template slot-scope="scope">
            <icon-list
              :msg="scope.row.status===10?iconMsg:scope.row.status===26?iconMsg2:iconMsg1"
              :row-data="scope.row"
              @on-result-change="_iconClick"
            />
          </template>
        </el-table-column>
      </element-table>
      </Col>
    </Row>
    <!--添加编辑-->
    <typeEdit ref="editModal" @on-result-change="_page" />

    <UncertaintyPlanList ref="planListModal" @on-result-change="_page" />
  </div>
</template>
<script>
import { qcMainPlan } from '../../../api'
import typeEdit from './InnerQcRecordEdit'
import UncertaintyPlanList from './InnerQcRecordList'
export default {
  components: { typeEdit, UncertaintyPlanList },
  data() {
    return {
      formObj: {
        name: ''
      },
      searchOpen: true,
      btn: [],
      selectData: [],
      pageColumns: [
        { title: '状态', key: 'status', status: true },
        { title: '计划名称', key: 'name', detail: true },
        { title: '编制人', key: 'maker' },
        { title: '编制日期', key: 'makeDate', width: 120, date: true },
        { title: '批准人', key: 'approver' },
        { title: '批准日期', key: 'approveDate', width: 120, date: true },
        { title: '备注', key: 'remark' }
      ],
      getPage: {},
      iconMsg: [
        { type: 'pt-a-edit', id: '', name: '编辑' },
        { type: 'pt-a-trash', id: '', name: '删除' },
        {
          type: 'pt-a-edit',
          id: '',
          name: '计划详情',
          disabled: true
        }
      ],
      iconMsg1: [
        { type: 'pt-a-edit', id: '', name: '编辑', disabled: true },
        { type: 'pt-a-trash', id: '', name: '删除', disabled: true },
        { type: 'pt-a-edit', id: '', name: '计划详情' }
      ],
      iconMsg2: [
        { type: 'pt-a-edit', id: '', name: '编辑', disabled: true },
        { type: 'pt-a-trash', id: '', name: '删除', disabled: true },
        {
          type: 'pt-a-edit',
          id: '',
          name: '计划详情'
        }
      ],
      iconMsgDis: [
        {
          type: 'pt-a-edit',
          id: '',
          name: '编辑',
          disabled: true
        },
        { type: 'pt-a-submit', id: '', name: '提交', disabled: true },
        {
          type: 'pt-a-trash',
          id: '',
          name: '删除',
          disabled: true
        },
        { type: 'pt-a-man', id: '', name: '人员管理' },
        { type: 'pt-a-cloud', id: '', name: '查看附件' }
      ]
    }
  },
  computed: {
    tableHeight: function() {
      if (this.searchOpen) {
        return this.$tableHeight('search')
      } else {
        return this.$tableHeight('noSearch')
      }
    }
  },
  mounted() {
    this._page()
  },
  methods: {
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _dateChange(data) {
      this.formObj.stime = data[0]
      this.formObj.etime = data[1]
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await qcMainPlan.pageRecord(
        this.$serializeForm(this.formObj)
      )
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _searchParams(formId) {
      return this.$refs.pageTable._searchParams(formId)
    },

    _add() {
      this.$refs.editModal._open()
    },
    _btnClick(msg, data) {
      switch (msg) {
        case '添加':
          this.$refs.editModal._open()
          break
        case 'search':
          this.searchOpen = !this.searchOpen
          break
      }
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData)
          break
        case 'selectData':
          this.selectData = data
          break
      }
    },
    _iconClick(res, data) {
      switch (res) {
        case '编辑':
          this._editModal(data.id)
          break
        case '计划详情':
          this._planListModal(data.id)
          break
        case '提交':
          this._submit(data.id)
          break
        case '删除':
          this._deleteById(data.id)
          break
      }
    },
    _planListModal(id) {
      this.$refs.planListModal._open(id)
    },
    _submit() {
      if (this.selectData.length === 0) {
        this.$Message.success('请选择要提交的数据!')
        return false
      }
      const data = this.selectData
      for (let i = 0; i < data.length; i++) {
        if (data[i].status !== 10) {
          this.$Message.success('请选择草稿状态的数据!')
          return false
        }
      }
      const ids = []
      for (let i = 0; i < data.length; i++) {
        ids.push(data[i].id)
      }
      this.$Modal.confirm({
        title: '提示',
        content: '确定提交该信息？',
        onOk: () => {
          this._submitOk(ids)
        }
      })
    },
    _submitOk: async function(id) {
      const result = await qcMainPlan.submitById(id)
      if (result) {
        this.$Message.success('提交成功')
        this._page()
      }
    },
    // 删除
    _deleteById(id) {
      this.$Modal.confirm({
        title: '提示',
        content: '确定删除该信息？',
        onOk: () => {
          this._delOk(id)
        }
      })
    },
    _delOk: async function(id) {
      const result = await qcMainPlan.deleteById(id)
      if (result) {
        this.$Message.success('删除成功')
        this._page()
      }
    },
    _editModal: async function(id) {
      const result = await qcMainPlan.getById(id)
      if (result) {
        this.$refs.editModal._open(result)
      }
    }
  }
}
</script>
