<template>
  <div>
    <div>
      <el-button
        icon="plus"
        @click="openAddDemand"
      >
        新增需求信息
      </el-button>
    </div>
    <div style="margin-top: 20px">
      <el-table
        :data="customerData.demandList"
        height="750px"
        stripe
      >
        <el-table-column
          label="提出时间"
          align="center"
          width="150px"
          prop="proposeTime"
        />
        <el-table-column
          label="是否完成"
          align="center"
          width="150px"
        >
          <template #default="scope">
            <el-text :type="scope.row.isFinish ? 'success' : 'danger'">
              {{ scope.row.isFinish ? '已完成' : '未完成' }}
            </el-text>
          </template>
        </el-table-column>
        <el-table-column
          label="需求内容"
          align="center"
          width="300px"
        >
          <template #default="scope">
            <el-input
              v-model="scope.row.content"
              type="textarea"
              :rows="3"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
        >
          <template #default="scope">
            <el-button
              type="primary"
              text
              icon="Edit"
              @click="demandEdit(scope.row)"
            >
              编辑
            </el-button>
            <el-button
              type="primary"
              text
              icon="delete"
              @click="deleteDemand(scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
      v-model="addDemandShow"
      width="35%"
      title="新增客户需求"
    >
      <el-form label-width="100px">
        <el-form-item label="提出时间：">
          <el-date-picker
            v-model="addDemandData.proposeTime"
            type="date"
            placeholder="请选择提出时间"
            size="large"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
            clearable
            @clear="addDemandData.proposeTime = ''"
          />
        </el-form-item>
        <el-form-item label="需求内容：">
          <el-input
            v-model="addDemandData.content"
            type="textarea"
            :rows="3"
            placeholder="请输入需求内容"
          />
        </el-form-item>
        <el-form-item label="是否完成：">
          <el-select
            v-model="addDemandData.isFinish"
            size="large"
            placeholder="是否完成"
          >
            <el-option
              label="已完成"
              :value="true"
            />
            <el-option
              label="未完成"
              :value="false"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button
          type="primary"
          size="large"
          @click="addDemand"
        >确定</el-button>
      </template>
    </el-dialog>
    <el-dialog
      v-model="editDemandShow"
      width="35%"
      title="编辑客户需求"
    >
      <el-form label-width="100px">
        <el-form-item label="提出时间：">
          <el-date-picker
            v-model="editDemandData.proposeTime"
            type="date"
            placeholder="请选择提出时间"
            size="large"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
            clearable
            @clear="editDemandData.proposeTime = ''"
          />
        </el-form-item>
        <el-form-item label="需求内容：">
          <el-input
            v-model="editDemandData.content"
            type="textarea"
            :rows="3"
            placeholder="请输入需求内容"
          />
        </el-form-item>
        <el-form-item label="是否完成：">
          <el-select
            v-model="editDemandData.isFinish"
            size="large"
            placeholder="是否完成"
          >
            <el-option
              label="已完成"
              :value="true"
            />
            <el-option
              label="未完成"
              :value="false"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button
          type="primary"
          size="large"
          @click="editDemand"
        >确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useCustomer } from '@/pinia/customer/customer'
import { ElMessage, ElMessageBox } from 'element-plus'
import { postUserDemand, delUserDemand, putUserDemand } from '@/api/demand'
defineOptions({
  name: 'DemandUnit'
})
const customerData = useCustomer()
const openAddDemand = () => {
  addDemandShow.value = true
}

// 新增需求
const addDemandShow = ref(false)

const addDemandData = reactive({
  content: '',
  customerId: customerData.customerId,
  proposeTime: '',
  isFinish: false
})

const addDemand = () => {
  for (const key in addDemandData) {
    if (addDemandData[key] === '' || addDemandData[key] === 0) {
      ElMessage.error('请将信息填写完整')
      return
    }
  }
  console.log(addDemandData)
  postUserDemand(addDemandData).then(res => {
    if (res.code === 0) {
      ElMessage.success(res.msg)
      customerData.getDemandList(customerData.customerId)
      addDemandShow.value = false
    }
  })
}

// 编辑需求
const editDemandShow = ref(false)
const editDemandData = reactive({
  id: 0,
  proposeTime: '',
  content: '',
  isFinish: false,
  customerId: customerData.customerId
})
const demandEdit = (row) => {
  editDemandShow.value = true
  editDemandData.id = row.ID
  editDemandData.proposeTime = row.proposeTime
  editDemandData.isFinish = row.isFinish
  editDemandData.content = row.content
}

const editDemand = () => {
  console.log(editDemandData)
  putUserDemand(editDemandData).then(res => {
    if (res.code === 0) {
      ElMessage.success(res.msg)
      editDemandShow.value = false
      customerData.getDemandList(customerData.customerId)
    }
  })
}

// 删除需求
const deleteDemand = (row) => {
  ElMessageBox.confirm(
    '确定删除此客户进度吗?',
    '删除',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      delUserDemand(row.ID).then(res => {
        if (res.code === 0) {
          ElMessage.success(res.msg)
          customerData.getDemandList(customerData.customerId)
        }
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消删除',
      })
    })
}
</script>

<style scoped lang="scss">

</style>
